Comments (12)
Hey you can call cardano.getBalance()
, which returns a Value type, which is a cbor encoded hex string. To deserialize it you can use the serialization-lib. It's kinda low level at the moment, we will build a top-level API later on.
from nami.
Yeah I noticed after posting. I updated the comment. Check it out - it will give you the policy id and asset name as hex strings for both.
from nami.
I'm also looking for something similar, from what I've seen it's still not possible due to the injected API. But analyzing the nami source code, I noticed that they look in the blockfrost API and in blockfrost there is an endpoint where it returns the assets from a reward (stake) address.
I'm developing a platform in cardano which I need to return the NFTs inside nami, I'll probably have to use the blockfrost API, or maybe, I just don't understand correctly the injected nami API!
but it's a good question to your @kraxyyy
\o/
from nami.
@alessandrokonrad what do you have planned for the structure of the top level API? Will it be compatible with other wallets? (e.g. an intermediate layer between the dApp and CIP-30 externally available and part of the dApp not the wallet or something like that) Or how will this be handled? I've noticed in other issues there were users who wanted a very simple API too for handling only the simple cases like in #43
from nami.
@alessandrokonrad what do you have planned for the structure of the top level API? Will it be compatible with other wallets? (e.g. an intermediate layer between the dApp and CIP-30 externally available and part of the dApp not the wallet or something like that) Or how will this be handled? I've noticed in other issues there were users who wanted a very simple API too for handling only the simple cases like in #43
As far as I know what @alessandrokonrad is doing is correct. The CIP30 defines the response of getBalance
to be a cbor encoded hex string. So he would need to do it differently, which is unfortunately not gonna be compatible with other wallets. Let's see what changes to the CIP30 might come and what @alessandrokonrad will come up with
from nami.
Is anyone able to provide more specific info on how to deserialize the result of cardano.getBalance()
via serialization-lib? I've not really been able to figure it out from any documentation or examples I can find
from nami.
I believe this should work @olliejm although I haven't tested it.
let rawBalance = await cardano.getBalance();
let balance = SerLib.Value.from_bytes(Buffer.from(rawBalance, 'hex'));
from nami.
Thanks, that gets me a little further, I know get an object with a value ptr
, { ptr: XXX }
where XXX
is some number (that doesn't immediately appear to be my balance in either ADA or lovelace). I'm specifically wanting to get a list of assets in the wallet though, as the original issue was asking. I'm starting to think it's unlikely whatever value ptr
is contains any information about assets, so maybe just using blockfrost is simpler
from nami.
That's how rust wasm-bindgen
works. It's a handle to wasm linear memory. You can call methods on that according to what's in Value in the ser lib. If you're just looking for the ADA-only value (not multiassets/nfts) you can do balance.coin().to_str()
to get the ADA-only value as a js string.
from nami.
I'm specifically wanting to get a list of assets in the wallet though
Ah sorry, try something like (untested):
let allAssets = balance.multiasset();
let assetTypes = allAssets.keys();
for (var i = 0; i < assetTypes.len(); i++) {
let policyId = assetTypes.get(i);
let assets = allAssets.get(polciyId);
let assetNames = assets.keys();
for (var j = 0; j < assetNames.len(); j++) {
let assetName = assetNames.get(j);
let asset = assets.get(assetName);
let policyHex = Buffer.from(policyId.to_bytes()).toString('hex');
let assetHex = Buffer.from(assetName.name()).toString('hex');
console.log(`${policyHex}:${assetHex} = ${asset.to_str()}`);
}
}
I'm just writing it in the comment box so maybe it's not entirely correct
from nami.
That makes sense now obviously about ptr
, I forgot for a second this was WASM.
Your snippet above got me most of the way so thanks so much already. One issue was I had to get the key for the asset before getting the asset, e.g.
const allAssets = balance.multiasset();
const assetTypes = allAssets.keys();
for (let i = 0; i < assetTypes.len(); i++) {
const policyId = assetTypes.get(i);
const assets = allAssets.get(policyId);
for (let j = 0; j < assets.len(); j++) {
const name = assets.keys().get(j);
const asset = assets.get(name).to_str();
console.log(asset);
}
}
I now have result logged to console, just a string "1"
. I have 1 asset from a single policy ID in my wallet, so I assume that's my balance of that specific asset.
What I need is the policy ID as a string (to filter out assets I'm not interested in), and the name of the asset (to fetch information about it from an external API).
I see policyId and name are already there now, but according to TypeScript no to_str
method is available on them. If you're able to help any further it's massively appreciated, I will keep trial and erroring with the methods I can find in the lib
from nami.
That's perfect, everything I need now and without needing to call blockfrost. Thank you so much for the help, really appreciate it
from nami.
Related Issues (20)
- help please HOT 4
- Restoring an overwritten Brave installation and a Nami wallet plugin within that HOT 3
- Can transfer out of wallet-Transaction failed HOT 1
- Assets not showing in imported wallet HOT 1
- Couldn't access my nami wallet and now I can't recover it either HOT 9
- All tx's erroring
- Blank screen in extension HOT 8
- Nami wallet
- Help delegation transaction(delegated on yoroi then looked at nami and have a delegation transaction and my balance is lower) HOT 3
- Sending ETH away from NAMI HOT 6
- :warning: ATTENTION: DO NOT CLICK ANY LINKS IN RESPONSES
- I can
- Unable to withdraw Cardano Nami Wallet staking rewards with Trezor HOT 1
- HW wallets don't need a password HOT 1
- Blank screen in Chrome Extension HOT 2
- Token does not show HOT 3
- transfer completed not showing in nami wallet please advise?? NAMI WALLET cardano ADA HOT 1
- Funds that I transferred have disappeared.. HOT 1
- How to disconnect from dAPP using javascript? HOT 1
- Issue Regarding Ada Handles Resolving To a JPG Store Contract Address HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nami.