2

Hello I am building a daap and i wanted to save blockFrost requests so I wonder if it is posible to fetch all assets using the nami wallet? I was trying with the window.cardano.get balance() , after using cardano serialization lib with Value.from_bytes() I get the ada total using Value.from_bytes().coin().to_str() but for all the other methods defined on Value.from_bytes() i can not find any that allows me to get .to_str() ... so even if i think that the total assets is in the original Value, I can not read it. Any help?

1 Answer 1

4

You could try this out. Basically assets are under value.multiassets() and is a double map from policy ID => assetName => quantity. You can iterate over this map with 2 for loops:

const value = CSL.Value.from_bytes(...) // your value
const lovelace = value.coin().to_str();
if (value.multiasset()) {
const multiAssets = value.multiasset().keys();
for (let j = 0; j < multiAssets.len(); j++) {
  const policy = multiAssets.get(j);
  const policyAssets = value.multiasset().get(policy);
  const assetNames = policyAssets.keys();
  for (let k = 0; k < assetNames.len(); k++) {
    const assetPolicy = Buffer.from(policy.to_bytes()).toString("hex"); // hex encoded policy
    const assetName = Buffer.from(
      assetNames.get(k).name(),
      "hex"
    ).toString(); // utf8 encoded asset name
    const quantity = policyAssets.get(assetNames.get(k)).to_str(); // asset's quantity

    // your code here

  }
}
1
  • Can I use Buffer in the browser or is this code only running on a Server (NodeJS)?
    – eddex
    Dec 24, 2021 at 15:18

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.