Search code examples
rustblockchainrust-cargosolanadaap

how to get the the amount of the given token address in anchor solana?


I'm getting two token addresses address as input for a function in my solana program. How can fetch the balance for the two token. using getTokenSupply in solana webjs i can get it in javascript how can i get that in rust.

 const data = await Connection.getTokenSupply(from_pairs);
 const data2 = await Connection.getTokenSupply(to_pairs);
 console.log(data, data2)

what is similar approach in anchor rust?


Solution

  • If you're using Anchor, that likely means that you're on-chain. To get the supply of a token mint on-chain, you'll need to pass it in to your program, deserialize the data in the account as a Token Mint, and then read the supply field.

    Here's an example of a test doing that: https://github.com/solana-labs/solana-program-library/blob/756696ee8e7b8dbe640113079ba11447ba3524c1/token/program/src/processor.rs#L4234-L4235