How can i transfer ada to another address if the utxo contains multi native assets cardano serialization library

i wanted to transfer ADA to another address using Cardano serialization library the problem i am facing is the utxo also contain the native asset so when i send the transaction hash of that kind of utxo i get the error because of the assets do you guys have any solution how can i solve this

        "address": "addr_test1qz6uleapn0tzd0gdyejhanadqnw3jnxg9gvxzwfr8g3x4d220lldl3pyxdvp8nxdgd2hjmleruq6fqjyu4caztnx6scq53l9kh",
        "tx_hash": "6deb76e39fdaba2ea9610f5cd389eeb2fe6dabc2ee755b3dd2d621ce354bf936",
        "tx_index": 1,
        "output_index": 1,
        "amount": [
            {
                "unit": "lovelace",
                "quantity": "1452843"
            },
            {
                "unit": "a788fce773ab5748e3e3483f0343866a8a21208a7aad89a34f3f48ebe382a2e382bbe38383e38388",
                "quantity": "1"
            },
            {
                "unit": "aad0401c838240d6250c56ccd0109950c566739e7f30195cb6241fb84e4654",
                "quantity": "3"
            },
            {
                "unit": "aad0401c838240d6250c56ccd0109950c566739e7f30195cb6241fb86e66744e616d65",
                "quantity": "6"
            }
        ],
        "block": "e8e8c1be484f43108625c8e7b68eb5e65c0f1d9da20f17dd505ffb7511b3f6d7",
        "data_hash": null,
        "inline_datum": null,
        "reference_script_hash": null
    }

this how the utxo looks like

this is the error i am getting because the utxo also contain the assets

BlockfrostServerError: “transaction submit error ShelleyTxValidationError ShelleyBasedEraBabbage (ApplyTxError [UtxowFailure (UtxoFailure (FromAlonzoUtxoFail (ValueNotConservedUTxO (Value 1452843 (fromList [(PolicyID {policyID = ScriptHash "a788fce773ab5748e3e3483f0343866a8a21208a7aad89a34f3f48eb"},fromList [(e382a2e382bbe38383e38388,1)]),(PolicyID {policyID = ScriptHash "aad0401c838240d6250c56ccd0109950c566739e7f30195cb6241fb8"},fromList [(4e4654,3),(6e66744e616d65,6)])])) (Value 1452843 (fromList )))))])”

You have to add all the tokens to the output by using CardanoWasm.Value.new_with_assets instead of CardanoWasm.Value.new in the txBuilder.add_output call:

For that, you have to build a MultiAsset object:

is this how am should i add assets ?

const multiAsset = CardanoWasm.MultiAsset.new()

  const assets = CardanoWasm.Assets.new()
  assets.insert(
    CardanoWasm.AssetName.new(Buffer.from(assetName, "hex")), // Asset Name
    CardanoWasm.BigNum.from_str(amount) // How much to send
  );
  multiAsset.insert(
    CardanoWasm.ScriptHash.from_bytes(Buffer.from(policy, "hex")), // PolicyID
    assets
  );

in this case, i dunno how to retrieve the asset name as it is not mentioned in the utxo can i skip the asset name part

No, you can’t skip it. The token is only identified by the policy ID together with the name.

The "unit"s in your UTxO above contain both combined. The first 28 bytes (56 hexadecimal characters) are the policy ID, the rest is the asset name.

i used this thing to store the unit into parts, this one is for policy id

unit.substr(0, 56)

and this is for the asset name

unit.substr(56, unit.length)

when i run this part

assets.insert(
          CardanoWasm.AssetName.new(Buffer.from(name, "hex")),
          CardanoWasm.BigNum.from_str(quantity) // How much to send
        );
        multiAsset.insert(
          CardanoWasm.ScriptHash.from_bytes(Buffer.from(policyid, "hex")), // PolicyID
          assets
        );

i am getting this error

Deserialization failed in ScriptHash because: Invalid cbor: expected tuple ‘hash length’ of length 28 but got length Len(12).

the unit was this a788fce773ab5748e3e3483f0343866a8a21208a7aad89a34f3f48ebe382a2e382bbe38383e38388 and the substrings i got were

> a788fce773ab5748e3e3483f0343866a8a21208a7aad89a34f3f48eb // policy id
> e382a2e382bbe38383e38388 // asset name

so what i did for now is to pass the policy string it self which was "a788fce773ab5748e3e3483f0343866a8a21208a7aad89a34f3f48eb" so it worked but i am getting the error with the function you told me about to put in the add_output but i can’t seem to understand what was the big num part in it

txBuilder.add_output(
      CardanoWasm.TransactionOutput.new(
        RECEIVER_ADDRESS,
        CardanoWasm.Value.new_with_assets(multiAsset)
      )
    );

the multiasset is from the part of the script i mentioned above

That looks right if I look at: https://preview.cardanoscan.io/token/a788fce773ab5748e3e3483f0343866a8a21208a7aad89a34f3f48ebe382a2e382bbe38383e38388

Is it possible that you are mixing up the variables? …, because the asset name is exactly 12 bytes.

I am passing the policy id
this onea788fce773ab5748e3e3483f0343866a8a21208a7aad89a34f3f48eb in the script hash part from variable

multiAsset.insert(
          CardanoWasm.ScriptHash.from_bytes(Buffer.from(policyid, "hex")), // PolicyID
          assets
        );

…, but that hex string clearly corresponds to 28 bytes and your error message clearly says that it’s a wrong length of 12 bytes.

If you haven’t accidentally swapped the content of those variables, it’s a miracle to me how that can be.

just noticed the mistake i accidentally named the policy id to name thanks

1 Like
txBuilder.add_output(
      CardanoWasm.TransactionOutput.new(
        RECEIVER_ADDRESS,
        CardanoWasm.Value.new_with_assets(multiAsset)
      )
    );

this thing is still not working it requires the instance of BigNum and i don’t seem to get the second parameter that what input it requires.

i stored all the assets of different policy id using the multi-asset class

const multiAsset = CardanoWasm.MultiAsset.new();

i am calling the below code in the loop to store all the assets from the utxo

 const assets = CardanoWasm.Assets.new();
 assets.insert(
          CardanoWasm.AssetName.new(Buffer.from(name, "hex")),
          CardanoWasm.BigNum.from_str(quantity) // How much to send
        );
        multiAsset.insert(
          CardanoWasm.ScriptHash.from_bytes(Buffer.from(policyid, "hex")), // PolicyID
          assets
        );

should i call the new_with_assets() also in the loop to store each asset type or the way i am doing it now is ok?

new_with_assets(coin: BigNum, multiasset: MultiAsset) … The first argument to that function are still the ADA that you want to send as a BigNum. The MultiAsset object containing all the tokens that shall come with them is only the second argument.

1 Like

that thing is working now it says that i am not adding assets into the input should i add this thing in the txbuilder

 txBuilder.add_mint_asset(
      unit,
      CardanoWasm.AssetName.new(Buffer.from(name,"hex)),
      CardanoWasm.Int.new_i32(quantity)
    );

here in the unit i am taking the unit of the asset in the utxo, also the unit is the script hash right?

According to

the first parameter should not be a unit, but the policy script itself.

Also, they deprecated that function and now want you to use:

1 Like

the parameter in add_asset, MintWitness is it taking the witnesses added in the script?

i made i work using this thing adding assets and the amount and then used the add_input function thanks for the help!

cardano serialization lib - How to add multiasset as input to txBuilder? - Cardano Stack Exchange

1 Like