0

Hey i want to fetch the token amount how do I do that? I have reached this point but want to hold just the value

used this till here console.log("Signature: ",hash.meta.postTokenBalances);

TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

1

It's up to you what you need. Let's use SOL as an example, which as 9 decimal places, such that 1 SOL = 1_000_000_000 lamports

  • amount is the total amount of lamports, so 90_000_000_000 in your example
  • uiAmountString is the formatted amount of SOL, so 90.000000000 in your example

If you need to do calculations, you should hold on to amount to do the math, and decimals to display it. If you only need to display, then uiAmountString will be easiest.

Jon C
  • 7,019
  • 10
  • 17
  • Yes that is not the issue. My query is when I try to display the uiAmountString I am getting an error : UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'uiTokenAmount' of undefined. I did the following code console.log('Pre Token Balance: ',hash1.meta.postTokenBalances.uiTokenAmount.uiAmountString); – Varun Chodha Oct 15 '21 at 04:33
  • 1
    `postTokenBalances` is an array, so you'll need to do `hash1.meta.postTokenBalances[0].uiTokenAmount.uiAmountString`. More info at https://docs.solana.com/developing/clients/jsonrpc-api#results-68 – Jon C Oct 15 '21 at 10:21