cardano-serialization-lib
cardano-serialization-lib copied to clipboard
Documentation
Can we get some real documentation please? The link "documentation here" has no technical information at all.
I am already struggling trying to get a Value to display as an actual ADA amount. Which should be simple.
If there is a documentation of the underlying methods on which the JS serialization is written/generated, please add the link to this in the readme so we can at least try to figure something out.
@xocdr does this help? https://developers.cardano.org/docs/get-started/cardano-serialization-lib/overview
It's worth noting that Value
as of the Mary hardfork isn't necessarily just a ADA value, as it can contain multiassets (e.g. NFTs) as well.
If you want to extract just the ADA value you can use Value.coin().to_str()
. Before that hard fork we used Coin
(aka BigNum
) directly.
also @xocdr all the JS code is automatically generated from the rust codebase located in rust/src/
. Most of it is in lib.rs
but Value
is in util.rs
. The method/type names are all the same when converted to JS. Just some primitives are mapped e.g. Vec<u32>
-> Array<number>
. If you look in rust/pkg/cardano_serialization_lib.js.flow
you'll find the flow types of the js code, which are auto-generated from the aforementioned rust code.
I hope this helps.
@xocdr I noticed that the javascript (flow types) actually are linked in the documentation already on the first page of the docs. Although most of the functions defined there aren't well commented since they're auto-generated directly from shelley-ma.cddl
binary spec. Later in the docs there's documentation on how to construct values, transaction-related types, addresses and metadata.
+1 for good docs
+1 even a simple example of how to submit a Plutus transaction using this library would help a lot 🙏
You cannot directly submit a transaction with the serialization lib if that's what you mean. You need a node for that.
You can also check the SpaceBudz code for examples
@rooooooooob I have already went through the doc and successfully built the transaction for sending ada, but don't know how to send the assets to another address, is there any doc about the multi-asset part?
@eric248550 If you're just interested in sending other assets along with the ADA then when you construct your Value
object you must create a MultiAsset
like:
// let's assume {value} here is your value you used for ADA-only
let value = Value.new(BigNum.from_str("2"));
// now let's make some multiassets to combine with that value
let ma = MultiAsset.new();
let policyId = PoliceId.from_bytes(/*scripthash bytes here for the policy*/);
let assets = Assets.new();
let assetName = AssetName.new(//*asset name bytes here*/);
assets.insert(assetName, BigNum.from_str("1000"));
ma.insert(policyId, assets);
value.set_multiasset(ma);
// now just use {value} how you were using it before and it should work out
There's no doc for it right now I think. I have updating the docs in my backlog though since it's a major issue and they could be expanded more. I also want to add in more doc-comments on methods/classes as well.
@rooooooooob
Thank you for your help!
It seems some problem when sending assets, I got the error from PoliceId.from_bytes
so I change to ScriptHash.from_bytes
here is my implementation to send asset but encounter the error: INPUTS_EXHAUSTED in CoinSelection.randomImprove
const S = await import('@emurgo/cardano-serialization-lib-browser/cardano_serialization_lib.js')
const _Buffer = (await import('buffer/')).Buffer
let value = S.Value.new(S.BigNum.from_str("2"));
let ma = S.MultiAsset.new();
let policyId = S.ScriptHash.from_bytes(_Buffer.from('3124bddc14e0a7f0871e013acbbe3969170006e00f0603385b6465c7','hex'));
let assets = S.Assets.new();
let assetName = S.AssetName.new(_Buffer.from('41756374696f6e416d756c65747330303138', 'hex'));
assets.insert(assetName, S.BigNum.from_str("1000"));
ma.insert(policyId, assets);
value.set_multiasset(ma);
outputs.add(
S.TransactionOutput.new(
S.Address.from_bech32(addr),
value
)
)
const selection = await CoinSelection.randomImprove(
utxos,
outputs,
20 + totalAssets,
protocolParameters.minUtxo.to_str()
);
It seems some problem when sending assets, I got the error from PoliceId.from_bytes so I change to ScriptHash.from_bytes
Ah sorry, that should be PolicyID
with a y not an e.
I'm not sure what that CoinSelection
thing is but from a quick search it looks like this is something in nami-wallet? It's not a part of cardano-serialization-lib so this probably isn't the place and also I am not familiar with it. It looks like you aren't providing enough inputs to cover your output(s). I recently implemented random improve selection for multiassets here #264 in cardano-serialization-lib and it's possible that nami-wallet's implementation also suffers from the same issue where you might need more inputs than are strictly necessary due to how random-improve works. It's only specified for ADA-only in CIP-2 so it would depend how they adapted it for multiassets.
@rooooooooob It finally works! Thank you for your great help! Besides, Is there possible to do the multi-sig for it? (send ada or assets to get minted asset(NFT) in one transaction)