Emurgo / cardano-serialization-lib

This is a library, written in Rust, for serialization & deserialization of data structures used in Cardano's Haskell implementation of Alonzo along with useful utility functions.
Other
230 stars 124 forks source link

Add inputs with assets. #666

Open bakon11 opened 5 months ago

bakon11 commented 5 months ago

How do I add assets to TX build as inputs when having them returned in JSON format not cbor hex like you do with getUTxos() from wallet extensions defined in cip30.

So for example first code snippet will add UTXOs that are returned in CBOR hex from a wallet extension in the frontend and the CBOR hex already has all the assets defined in it, and this works great for frontend applications in the web browsers where you have access to wallet extensions and CIP30 methods


 /*  ###########################################################################################
 Use this when getting UTXOS in array CBOR hex like cip30 getUtxos() method returns.
 #############################d#############################################################
  */

const utxos = [
    "828258204b82398febd68fab512a56fd1786f05cc13ae2af706ebb9dc90502b499dfff010082583901130768a0e391b8c77be560580729ec927393e688991929129e609a4bb093f044b77f2fb13ce1828c954b1858ac9f76935e74e391c4255e72821a004c4b40a2581cb812d5a466604bcc25d2313183c387cebf52302738b5a178daf146f0a6494d616e64616c6123311864494d616e64616c6123321864494d616e64616c6123331864494d616e64616c6123341864494d616e64616c6123351864494d616e64616c6123361864581cb88d9fe270b184cf02c99b19ffa5ab0181daeff00c52811c6511c12aa8494d65726b61626123301864494d65726b61626123311864494d65726b61626123321864494d65726b61626123331864494d65726b616261233418644a4d65726b616261232d3118644a4d65726b616261232d3218644a4d65726b616261232d331864",
    "82825820ad7828a65ced5b48058230c88f1b420973bafa2fd546d5161cf008ab807624760082583901130768a0e391b8c77be560580729ec927393e688991929129e609a4bb093f044b77f2fb13ce1828c954b1858ac9f76935e74e391c4255e721a002dc6c0",
    "82825820ad7828a65ced5b48058230c88f1b420973bafa2fd546d5161cf008ab807624760182583901130768a0e391b8c77be560580729ec927393e688991929129e609a4bb093f044b77f2fb13ce1828c954b1858ac9f76935e74e391c4255e721a000eedc2",
    "828258205d8e75f465e9d7fc413bb03caf7a0b0d280b62d01bb9b853afbef8a25523ae820082583901130768a0e391b8c77be560580729ec927393e688991929129e609a4bb093f044b77f2fb13ce1828c954b1858ac9f76935e74e391c4255e721a002b1d8b"
]
    // console.log("utxos", utxos);
    let txUnspentOutputs = CSLwasm.TransactionUnspentOutputs.new();
    await Promise.all(
      await utxos.map((utxo) => {
        txUnspentOutputs.add(CSLwasm.TransactionUnspentOutput.from_hex(utxo));
      })
    );

Example two is using CSL in the backend like Nodejs where you don't have access to wallet extensions but sources like Kupo or Blockfrost. Question here is how to add the assets with the inputs in the example below?

 /*
   ##########################################################################################
   Add UTXOs returned in JSON like KUPO indexer.
   #############################d############################################################
    */

const utxos = [
    {
        "transaction_index": 12,
        "transaction_id": "eeef8460d4b629b6174a285da26400cce6fb4e1bc1a240d5d5a5c90049185d6f",
        "output_index": 1,
        "address": "addr1q9shhjkju8aw2fpt4ttdnzrqcdacaegpglfezen33kq9l2wcdqua0w5yj7d8thpulynjly2yrhwxvdhtrxqjpmy60uqs4h7cyp",
        "value": {
            "coins": 10004576334,
            "assets": {
                "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303031": 5,
                "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303032": 7,
                "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303033": 11,
                "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303034": 5,
                "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303035": 7,
                "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303036": 2,
                "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303037": 4,
                "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303038": 3,
                "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303039": 6,
                "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303130": 5,
                "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303131": 6,
                "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303132": 5,
                "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303133": 7,
                "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303134": 3,
            }
        },
        "datum_hash": null,
        "script_hash": null,
        "created_at": {
            "slot_no": 116526928,
            "header_hash": "0d4df2f45d6109db00035e94030bde6f81e2f3fd7849fd7c31a9d5c92d5d8230"
        },
        "spent_at": null
    }
]

    if ( utxos.length > 0 ){
      console.log("adding UTXOs");
      await utxos.map( async( utxo ) => {
        // set utxo input map the array 
        console.log("Adding utxo: " + utxo.txix)
        await txBuilder.add_input(
          CardanoWasm.Address.from_bech32(changeAddress),
          CardanoWasm.TransactionInput.new( CardanoWasm.TransactionHash.from_bytes( Buffer.from(utxo.txid, "hex")), utxo.txIndex ),
          CardanoWasm.Value.new(CardanoWasm.BigNum.from_str(utxo.value))
        );
      });
    };

Thank you!!

lisicky commented 4 months ago

Hi @bakon11 ! the "0fe7b9c1abbf139414d8e138721a85dd8d6e24ee7dc0d895587b4f57.6a633030303030303133" has pattern "policy_id,asset_name" you can use to build current one MintAssets object