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
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
}
}
-
Can I use
Buffer
in the browser or is this code only running on a Server (NodeJS)?– eddex ♦Commented Dec 24, 2021 at 15:18