fix: update token API handling
This commit is contained in:
		
				
					committed by
					
						
						Justin Starry
					
				
			
			
				
	
			
			
			
						parent
						
							7430896c79
						
					
				
				
					commit
					2261c066f3
				
			
							
								
								
									
										25
									
								
								web3.js/module.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										25
									
								
								web3.js/module.d.ts
									
									
									
									
										vendored
									
									
								
							@@ -158,21 +158,10 @@ declare module '@solana/web3.js' {
 | 
				
			|||||||
    root: number;
 | 
					    root: number;
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  export type TokenAccountInfo = {
 | 
					  export type TokenAmount = {
 | 
				
			||||||
    mint: PublicKey;
 | 
					    uiAmount: number;
 | 
				
			||||||
    owner: PublicKey;
 | 
					    decimals: number;
 | 
				
			||||||
    amount: number;
 | 
					    amount: string;
 | 
				
			||||||
    delegate: null | PublicKey;
 | 
					 | 
				
			||||||
    delegatedAmount: number;
 | 
					 | 
				
			||||||
    isInitialized: boolean;
 | 
					 | 
				
			||||||
    isNative: boolean;
 | 
					 | 
				
			||||||
  };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  export type TokenAccount = {
 | 
					 | 
				
			||||||
    executable: boolean;
 | 
					 | 
				
			||||||
    owner: PublicKey;
 | 
					 | 
				
			||||||
    lamports: number;
 | 
					 | 
				
			||||||
    data: TokenAccountInfo;
 | 
					 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  export type AccountChangeCallback = (
 | 
					  export type AccountChangeCallback = (
 | 
				
			||||||
@@ -267,17 +256,17 @@ declare module '@solana/web3.js' {
 | 
				
			|||||||
    getTokenSupply(
 | 
					    getTokenSupply(
 | 
				
			||||||
      tokenMintAddress: PublicKey,
 | 
					      tokenMintAddress: PublicKey,
 | 
				
			||||||
      commitment?: Commitment,
 | 
					      commitment?: Commitment,
 | 
				
			||||||
    ): Promise<RpcResponseAndContext<number>>;
 | 
					    ): Promise<RpcResponseAndContext<TokenAmount>>;
 | 
				
			||||||
    getTokenAccountBalance(
 | 
					    getTokenAccountBalance(
 | 
				
			||||||
      tokenAddress: PublicKey,
 | 
					      tokenAddress: PublicKey,
 | 
				
			||||||
      commitment?: Commitment,
 | 
					      commitment?: Commitment,
 | 
				
			||||||
    ): Promise<RpcResponseAndContext<number>>;
 | 
					    ): Promise<RpcResponseAndContext<TokenAmount>>;
 | 
				
			||||||
    getTokenAccountsByOwner(
 | 
					    getTokenAccountsByOwner(
 | 
				
			||||||
      ownerAddress: PublicKey,
 | 
					      ownerAddress: PublicKey,
 | 
				
			||||||
      filter: TokenAccountsFilter,
 | 
					      filter: TokenAccountsFilter,
 | 
				
			||||||
      commitment?: Commitment,
 | 
					      commitment?: Commitment,
 | 
				
			||||||
    ): Promise<
 | 
					    ): Promise<
 | 
				
			||||||
      RpcResponseAndContext<Array<{pubkey: PublicKey; account: TokenAccount}>>
 | 
					      RpcResponseAndContext<Array<{pubkey: PublicKey; account: AccountInfo}>>
 | 
				
			||||||
    >;
 | 
					    >;
 | 
				
			||||||
    getLargestAccounts(
 | 
					    getLargestAccounts(
 | 
				
			||||||
      config?: GetLargestAccountsConfig,
 | 
					      config?: GetLargestAccountsConfig,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -174,21 +174,10 @@ declare module '@solana/web3.js' {
 | 
				
			|||||||
    root: number,
 | 
					    root: number,
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  declare export type TokenAccountInfo = {
 | 
					  declare export type TokenAmount = {
 | 
				
			||||||
    mint: PublicKey,
 | 
					    uiAmount: number,
 | 
				
			||||||
    owner: PublicKey,
 | 
					    decimals: number,
 | 
				
			||||||
    amount: number,
 | 
					    amount: string,
 | 
				
			||||||
    delegate: null | PublicKey,
 | 
					 | 
				
			||||||
    delegatedAmount: number,
 | 
					 | 
				
			||||||
    isInitialized: boolean,
 | 
					 | 
				
			||||||
    isNative: boolean,
 | 
					 | 
				
			||||||
  };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  declare export type TokenAccount = {
 | 
					 | 
				
			||||||
    executable: boolean,
 | 
					 | 
				
			||||||
    owner: PublicKey,
 | 
					 | 
				
			||||||
    lamports: number,
 | 
					 | 
				
			||||||
    data: TokenAccountInfo,
 | 
					 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  declare type AccountChangeCallback = (
 | 
					  declare type AccountChangeCallback = (
 | 
				
			||||||
@@ -283,17 +272,17 @@ declare module '@solana/web3.js' {
 | 
				
			|||||||
    getTokenSupply(
 | 
					    getTokenSupply(
 | 
				
			||||||
      tokenMintAddress: PublicKey,
 | 
					      tokenMintAddress: PublicKey,
 | 
				
			||||||
      commitment: ?Commitment,
 | 
					      commitment: ?Commitment,
 | 
				
			||||||
    ): Promise<RpcResponseAndContext<number>>;
 | 
					    ): Promise<RpcResponseAndContext<TokenAmount>>;
 | 
				
			||||||
    getTokenAccountBalance(
 | 
					    getTokenAccountBalance(
 | 
				
			||||||
      tokenAddress: PublicKey,
 | 
					      tokenAddress: PublicKey,
 | 
				
			||||||
      commitment: ?Commitment,
 | 
					      commitment: ?Commitment,
 | 
				
			||||||
    ): Promise<RpcResponseAndContext<number>>;
 | 
					    ): Promise<RpcResponseAndContext<TokenAmount>>;
 | 
				
			||||||
    getTokenAccountsByOwner(
 | 
					    getTokenAccountsByOwner(
 | 
				
			||||||
      ownerAddress: PublicKey,
 | 
					      ownerAddress: PublicKey,
 | 
				
			||||||
      filter: TokenAccountsFilter,
 | 
					      filter: TokenAccountsFilter,
 | 
				
			||||||
      commitment: ?Commitment,
 | 
					      commitment: ?Commitment,
 | 
				
			||||||
    ): Promise<
 | 
					    ): Promise<
 | 
				
			||||||
      RpcResponseAndContext<Array<{pubkey: PublicKey, account: TokenAccount}>>,
 | 
					      RpcResponseAndContext<Array<{pubkey: PublicKey, account: AccountInfo}>>,
 | 
				
			||||||
    >;
 | 
					    >;
 | 
				
			||||||
    getLargestAccounts(
 | 
					    getLargestAccounts(
 | 
				
			||||||
      config: ?GetLargestAccountsConfig,
 | 
					      config: ?GetLargestAccountsConfig,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -508,58 +508,30 @@ const GetSupplyRpcResult = jsonRpcResultAndContext(
 | 
				
			|||||||
  }),
 | 
					  }),
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					type TokenAmount = {
 | 
				
			||||||
 * Information describing a token account
 | 
					  amount: string,
 | 
				
			||||||
 */
 | 
					  decimals: 2,
 | 
				
			||||||
type TokenAccountInfo = {|
 | 
					  uiAmount: number,
 | 
				
			||||||
  mint: PublicKey,
 | 
					 | 
				
			||||||
  owner: PublicKey,
 | 
					 | 
				
			||||||
  amount: number,
 | 
					 | 
				
			||||||
  delegate: null | PublicKey,
 | 
					 | 
				
			||||||
  delegatedAmount: number,
 | 
					 | 
				
			||||||
  isInitialized: boolean,
 | 
					 | 
				
			||||||
  isNative: boolean,
 | 
					 | 
				
			||||||
|};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					 | 
				
			||||||
 * Information describing an account with token account data
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * @typedef {Object} TokenAccount
 | 
					 | 
				
			||||||
 * @property {number} lamports Number of lamports assigned to the account
 | 
					 | 
				
			||||||
 * @property {PublicKey} owner Identifier of the program that owns the account
 | 
					 | 
				
			||||||
 * @property {TokenAccountInfo} data Token account data
 | 
					 | 
				
			||||||
 * @property {boolean} executable `true` if this account's data contains a loaded program
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
type TokenAccount = {
 | 
					 | 
				
			||||||
  executable: boolean,
 | 
					 | 
				
			||||||
  owner: PublicKey,
 | 
					 | 
				
			||||||
  lamports: number,
 | 
					 | 
				
			||||||
  data: TokenAccountInfo,
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const TokenAccountResult = struct({
 | 
					/**
 | 
				
			||||||
  token: struct({
 | 
					 * Expected JSON RPC structure for token amounts
 | 
				
			||||||
    account: struct({
 | 
					 */
 | 
				
			||||||
      mint: 'string',
 | 
					const TokenAmountResult = struct({
 | 
				
			||||||
      owner: 'string',
 | 
					  amount: 'string',
 | 
				
			||||||
      amount: 'number',
 | 
					  uiAmount: 'number',
 | 
				
			||||||
      delegate: struct.union(['string', 'null']),
 | 
					  decimals: 'number',
 | 
				
			||||||
      delegatedAmount: 'number',
 | 
					 | 
				
			||||||
      isInitialized: 'boolean',
 | 
					 | 
				
			||||||
      isNative: 'boolean',
 | 
					 | 
				
			||||||
    }),
 | 
					 | 
				
			||||||
  }),
 | 
					 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Expected JSON RPC response for the "getTokenAccountBalance" message
 | 
					 * Expected JSON RPC response for the "getTokenAccountBalance" message
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
const GetTokenAccountBalance = jsonRpcResultAndContext('number');
 | 
					const GetTokenAccountBalance = jsonRpcResultAndContext(TokenAmountResult);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Expected JSON RPC response for the "getTokenSupply" message
 | 
					 * Expected JSON RPC response for the "getTokenSupply" message
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
const GetTokenSupplyRpcResult = jsonRpcResultAndContext('number');
 | 
					const GetTokenSupplyRpcResult = jsonRpcResultAndContext(TokenAmountResult);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Expected JSON RPC response for the "getTokenAccountsByOwner" message
 | 
					 * Expected JSON RPC response for the "getTokenAccountsByOwner" message
 | 
				
			||||||
@@ -572,7 +544,7 @@ const GetTokenAccountsByOwner = jsonRpcResultAndContext(
 | 
				
			|||||||
        executable: 'boolean',
 | 
					        executable: 'boolean',
 | 
				
			||||||
        owner: 'string',
 | 
					        owner: 'string',
 | 
				
			||||||
        lamports: 'number',
 | 
					        lamports: 'number',
 | 
				
			||||||
        data: TokenAccountResult,
 | 
					        data: 'any',
 | 
				
			||||||
        rentEpoch: 'number?',
 | 
					        rentEpoch: 'number?',
 | 
				
			||||||
      }),
 | 
					      }),
 | 
				
			||||||
    }),
 | 
					    }),
 | 
				
			||||||
@@ -1270,7 +1242,7 @@ export class Connection {
 | 
				
			|||||||
  async getTokenSupply(
 | 
					  async getTokenSupply(
 | 
				
			||||||
    tokenMintAddress: PublicKey,
 | 
					    tokenMintAddress: PublicKey,
 | 
				
			||||||
    commitment: ?Commitment,
 | 
					    commitment: ?Commitment,
 | 
				
			||||||
  ): Promise<RpcResponseAndContext<number>> {
 | 
					  ): Promise<RpcResponseAndContext<TokenAmount>> {
 | 
				
			||||||
    const args = this._argsWithCommitment(
 | 
					    const args = this._argsWithCommitment(
 | 
				
			||||||
      [tokenMintAddress.toBase58()],
 | 
					      [tokenMintAddress.toBase58()],
 | 
				
			||||||
      commitment,
 | 
					      commitment,
 | 
				
			||||||
@@ -1290,7 +1262,7 @@ export class Connection {
 | 
				
			|||||||
  async getTokenAccountBalance(
 | 
					  async getTokenAccountBalance(
 | 
				
			||||||
    tokenAddress: PublicKey,
 | 
					    tokenAddress: PublicKey,
 | 
				
			||||||
    commitment: ?Commitment,
 | 
					    commitment: ?Commitment,
 | 
				
			||||||
  ): Promise<RpcResponseAndContext<number>> {
 | 
					  ): Promise<RpcResponseAndContext<TokenAmount>> {
 | 
				
			||||||
    const args = this._argsWithCommitment(
 | 
					    const args = this._argsWithCommitment(
 | 
				
			||||||
      [tokenAddress.toBase58()],
 | 
					      [tokenAddress.toBase58()],
 | 
				
			||||||
      commitment,
 | 
					      commitment,
 | 
				
			||||||
@@ -1309,14 +1281,14 @@ export class Connection {
 | 
				
			|||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Fetch all the token accounts owned by the specified account
 | 
					   * Fetch all the token accounts owned by the specified account
 | 
				
			||||||
   *
 | 
					   *
 | 
				
			||||||
   * @return {Promise<RpcResponseAndContext<Array<{pubkey: PublicKey, account: TokenAccount}>>>}
 | 
					   * @return {Promise<RpcResponseAndContext<Array<{pubkey: PublicKey, account: AccountInfo}>>>}
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  async getTokenAccountsByOwner(
 | 
					  async getTokenAccountsByOwner(
 | 
				
			||||||
    ownerAddress: PublicKey,
 | 
					    ownerAddress: PublicKey,
 | 
				
			||||||
    filter: TokenAccountsFilter,
 | 
					    filter: TokenAccountsFilter,
 | 
				
			||||||
    commitment: ?Commitment,
 | 
					    commitment: ?Commitment,
 | 
				
			||||||
  ): Promise<
 | 
					  ): Promise<
 | 
				
			||||||
    RpcResponseAndContext<Array<{pubkey: PublicKey, account: TokenAccount}>>,
 | 
					    RpcResponseAndContext<Array<{pubkey: PublicKey, account: AccountInfo}>>,
 | 
				
			||||||
  > {
 | 
					  > {
 | 
				
			||||||
    let _args = [ownerAddress.toBase58()];
 | 
					    let _args = [ownerAddress.toBase58()];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1347,23 +1319,15 @@ export class Connection {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
      context,
 | 
					      context,
 | 
				
			||||||
      value: value.map(result => {
 | 
					      value: value.map(result => ({
 | 
				
			||||||
        const data = result.account.data.token.account;
 | 
					        pubkey: result.pubkey,
 | 
				
			||||||
        return {
 | 
					        account: {
 | 
				
			||||||
          pubkey: new PublicKey(result.pubkey),
 | 
					          executable: result.account.executable,
 | 
				
			||||||
          account: {
 | 
					          owner: new PublicKey(result.account.owner),
 | 
				
			||||||
            executable: result.account.executable,
 | 
					          lamports: result.account.lamports,
 | 
				
			||||||
            owner: new PublicKey(result.account.owner),
 | 
					          data: bs58.decode(result.account.data),
 | 
				
			||||||
            lamports: result.account.lamports,
 | 
					        },
 | 
				
			||||||
            data: {
 | 
					      })),
 | 
				
			||||||
              ...data,
 | 
					 | 
				
			||||||
              mint: new PublicKey(data.mint),
 | 
					 | 
				
			||||||
              owner: new PublicKey(data.owner),
 | 
					 | 
				
			||||||
              delegate: data.delegate ? new PublicKey(data.delegate) : null,
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
          },
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
      }),
 | 
					 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1286,7 +1286,7 @@ test('token methods', async () => {
 | 
				
			|||||||
    payerAccount,
 | 
					    payerAccount,
 | 
				
			||||||
    mintOwner.publicKey,
 | 
					    mintOwner.publicKey,
 | 
				
			||||||
    accountOwner.publicKey,
 | 
					    accountOwner.publicKey,
 | 
				
			||||||
    new u64(10000),
 | 
					    new u64(11111),
 | 
				
			||||||
    2,
 | 
					    2,
 | 
				
			||||||
    TOKEN_PROGRAM_ID,
 | 
					    TOKEN_PROGRAM_ID,
 | 
				
			||||||
    false,
 | 
					    false,
 | 
				
			||||||
@@ -1314,7 +1314,9 @@ test('token methods', async () => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  const supply = (await connection.getTokenSupply(token.publicKey, 'recent'))
 | 
					  const supply = (await connection.getTokenSupply(token.publicKey, 'recent'))
 | 
				
			||||||
    .value;
 | 
					    .value;
 | 
				
			||||||
  expect(supply).toEqual(10000);
 | 
					  expect(supply.uiAmount).toEqual(111.11);
 | 
				
			||||||
 | 
					  expect(supply.decimals).toEqual(2);
 | 
				
			||||||
 | 
					  expect(supply.amount).toEqual('11111');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const newAccount = new Account();
 | 
					  const newAccount = new Account();
 | 
				
			||||||
  await expect(
 | 
					  await expect(
 | 
				
			||||||
@@ -1324,7 +1326,9 @@ test('token methods', async () => {
 | 
				
			|||||||
  const balance = (
 | 
					  const balance = (
 | 
				
			||||||
    await connection.getTokenAccountBalance(tokenAccount, 'recent')
 | 
					    await connection.getTokenAccountBalance(tokenAccount, 'recent')
 | 
				
			||||||
  ).value;
 | 
					  ).value;
 | 
				
			||||||
  expect(balance).toEqual(9999);
 | 
					  expect(balance.amount).toEqual('11110');
 | 
				
			||||||
 | 
					  expect(balance.decimals).toEqual(2);
 | 
				
			||||||
 | 
					  expect(balance.uiAmount).toEqual(111.1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  await expect(
 | 
					  await expect(
 | 
				
			||||||
    connection.getTokenAccountBalance(newAccount.publicKey, 'recent'),
 | 
					    connection.getTokenAccountBalance(newAccount.publicKey, 'recent'),
 | 
				
			||||||
@@ -1338,12 +1342,6 @@ test('token methods', async () => {
 | 
				
			|||||||
    )
 | 
					    )
 | 
				
			||||||
  ).value;
 | 
					  ).value;
 | 
				
			||||||
  expect(accountsWithMintFilter.length).toEqual(2);
 | 
					  expect(accountsWithMintFilter.length).toEqual(2);
 | 
				
			||||||
  for (const {account} of accountsWithMintFilter) {
 | 
					 | 
				
			||||||
    expect(account.data.mint.toBase58()).toEqual(token.publicKey.toBase58());
 | 
					 | 
				
			||||||
    expect(account.data.owner.toBase58()).toEqual(
 | 
					 | 
				
			||||||
      accountOwner.publicKey.toBase58(),
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const accountsWithProgramFilter = (
 | 
					  const accountsWithProgramFilter = (
 | 
				
			||||||
    await connection.getTokenAccountsByOwner(
 | 
					    await connection.getTokenAccountsByOwner(
 | 
				
			||||||
@@ -1353,11 +1351,6 @@ test('token methods', async () => {
 | 
				
			|||||||
    )
 | 
					    )
 | 
				
			||||||
  ).value;
 | 
					  ).value;
 | 
				
			||||||
  expect(accountsWithProgramFilter.length).toEqual(3);
 | 
					  expect(accountsWithProgramFilter.length).toEqual(3);
 | 
				
			||||||
  for (const {account} of accountsWithProgramFilter) {
 | 
					 | 
				
			||||||
    expect(account.data.owner.toBase58()).toEqual(
 | 
					 | 
				
			||||||
      accountOwner.publicKey.toBase58(),
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const noAccounts = (
 | 
					  const noAccounts = (
 | 
				
			||||||
    await connection.getTokenAccountsByOwner(
 | 
					    await connection.getTokenAccountsByOwner(
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user