5

Hi i just recently started working with Nami Wallet to be implementes in a web app, but when im trying to get my wallet balance:

window.cardano.getBalance().then(res => console.log(res))

its returning an string.

"1a3b983883"

would love to know what that is and if i can convert that to an actual number.

Picture:

getBalance() response

4

getBalance function returns cbor encoded hex string, so you should actually decode it.

Easiest way to do it is by using Cardano Serialization Lib.

After you've downloaded the library you can decoded it by doing this:

import * as wasm from '@emurgo/cardano-serialization-lib-browser';

window.cardano.getBalance().then(res => {
   const balance = wasm.Value.from_bytes(Buffer.from(res, 'hex'));
   const lovelaces = balance.coin().to_str();

   console.log(lovelaces);
})

EDIT:

Thanks to Hernan Rajchert for explaining on how to get lovelaces from Values object.

5
  • 2
    thanks! but this code returns this object "{ptr: 1180552}", what to do with that? Oct 13 at 15:43
  • 1
    Yeah I got the same response so I'm not too sure about it. I'd recommend you rather building your transactions using TransactionBuilder, because it offers you nice way to specify change address and etc. Oct 14 at 9:50
  • 2
    @DanielDiaz, that is a pointer to the WASM object, if you need to get the lovelace value as string you can do balance.coin().to_str() Nov 3 at 20:30
  • 1
    Yes as DanielDias suggested thats the way to go I got my balance in lovelaces Nov 6 at 13:40
  • I'll update my answer and tag @HernanRajchert Nov 6 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.