<p>This is how you build a multi-token transaction using <code>cardano-serialization-lib</code>:</p>
<pre><code>export async function buildMultiAssetTx(
  fromAddress: string, 
  toAddress: string, 
  policy: string, 
  assetName: string, 
  amount: string, 
  privateKey: CardanoWasm.PrivateKey
) {
  
  const params = await getParameters();

  const linearFee = CardanoWasm.LinearFee.new(
    CardanoWasm.BigNum.from_str(params.data.genesis.shelley.protocolParams.minFeeA.toString()),
    CardanoWasm.BigNum.from_str(params.data.genesis.shelley.protocolParams.minFeeB.toString())
  );
  
  const txBuilderConfig = CardanoWasm.TransactionBuilderConfigBuilder.new()
    .fee_algo(linearFee)
    .pool_deposit(CardanoWasm.BigNum.from_str(params.data.genesis.shelley.protocolParams.poolDeposit.toString()))
    .key_deposit(CardanoWasm.BigNum.from_str(params.data.genesis.shelley.protocolParams.keyDeposit.toString()))
    .max_value_size(params.data.genesis.alonzo.maxValueSize)
    .max_tx_size(params.data.genesis.shelley.protocolParams.maxTxSize)
    .coins_per_utxo_word(CardanoWasm.BigNum.from_str(params.data.genesis.alonzo.lovelacePerUTxOWord.toString()))
    .build();

  // instantiate the tx builder with the Cardano protocol parameters - these may change later on
  const txBuilder = CardanoWasm.TransactionBuilder.new(txBuilderConfig);

  const unspentOutputs = await getTxUnspentOutputs(fromAddress);
   
  // pointer address
  const shelleyChangeAddress = CardanoWasm.Address.from_bech32(fromAddress);

  const minter = CardanoWasm.Address.from_bech32(toAddress);
  
  var txOutputBuilder = CardanoWasm.TransactionOutputBuilder.new();
  txOutputBuilder = txOutputBuilder.with_address(minter);
  var txOutputAmountBuilder = txOutputBuilder.next();

  const multiAsset = CardanoWasm.MultiAsset.new()

  const assets = CardanoWasm.Assets.new()
  assets.insert(
    CardanoWasm.AssetName.new(Buffer.from(assetName, &quot;hex&quot;)), // Asset Name
    CardanoWasm.BigNum.from_str(amount) // How much to send
  );
  multiAsset.insert(
    CardanoWasm.ScriptHash.from_bytes(Buffer.from(policy, &quot;hex&quot;)), // PolicyID
    assets
  );
  
  txOutputAmountBuilder = txOutputAmountBuilder.with_asset_and_min_required_coin(multiAsset, CardanoWasm.BigNum.from_str(params.data.genesis.alonzo.lovelacePerUTxOWord.toString()))
  const txOutput = txOutputAmountBuilder.build();

  txBuilder.add_output(txOutput)

  // add a keyhash input - for ADA held in a Shelley-era normal address (Base, Enterprise, Pointer)
  txBuilder.add_inputs_from(unspentOutputs, 3); // LargestFirstMultiAsset

  // set the time to live - the absolute slot value before the tx becomes invalid
  const slot = await getSlotNo();
  txBuilder.set_ttl(slot.data.cardano.tip.slotNo + 10000);

  // calculate the min fee required and send any change to an address
  txBuilder.add_change_if_needed(shelleyChangeAddress)

  // once the transaction is ready, we build it to get the tx body without witnesses
  const txBody = txBuilder.build()
  const txHash = CardanoWasm.hash_transaction(txBody);
  const witnesses = CardanoWasm.TransactionWitnessSet.new();
  const vkeyWitnesses = CardanoWasm.Vkeywitnesses.new();
  const vkeyWitness1 = CardanoWasm.make_vkey_witness(txHash, privateKey);

  vkeyWitnesses.add(vkeyWitness1);

  witnesses.set_vkeys(vkeyWitnesses);
  witnesses.set_native_scripts(CardanoWasm.NativeScripts.new());
  
  // create the finalized transaction with witnesses
  const transaction = CardanoWasm.Transaction.new(
    txBody,
    witnesses,
    undefined, // transaction metadata
  );

  return toHexString(transaction.to_bytes());

}
</code></pre>

CC BY-SA 4.0
2023-01-27T00:06:24.603Z
2023-01-27T00:06:24.603Z
Falcon Stakepool
<p><a href="https://github.com/ozgrakkurt/cardano-mint-nft/blob/master/index.js" rel="nofollow noreferrer">https://github.com/ozgrakkurt/cardano-mint-nft/blob/master/index.js</a></p>
<p>The example above from Özgür Akkurt on GitHub demonstrates how to mint native tokens in JS, assuming you are looking for that language.</p>
<p><a href="https://github.com/Emurgo/cardano-serialization-lib/blob/master/doc/getting-started/minting-nfts.md" rel="nofollow noreferrer">https://github.com/Emurgo/cardano-serialization-lib/blob/master/doc/getting-started/minting-nfts.md</a></p>
<p>This is the next example directly from the Emurgo library is the next example in sequence from the previous example you followed.</p>
<p>Is it Native tokens you are looking to send?</p>

CC BY-SA 4.0
2022-05-31T00:14:45.497Z
Referencing guidelines: https://cardano.stackexchange.com/help/referencing
gRebel
CC BY-SA 4.0
2022-05-30T20:58:02.677Z
2022-05-31T10:59:21.737Z
2022-05-31T10:59:21.737Z
Rust_Dev
<p>I have followed <a href="https://github.com/Emurgo/cardano-serialization-lib/blob/master/doc/getting-started/generating-transactions.md" rel="nofollow noreferrer">this example</a> on how to create a transaction to send ada and I had no issues so far.</p>
<p>What I would like to do next is to add different assets to this same transaction.</p>
<p>Is there an example on how to do that?</p>

CC BY-SA 4.0
2022-05-30T02:47:28.347Z
If you are open to use other packages, https://github.com/StricaHQ/typhonjs is a package which you can use to build transactions, It's easy to explore this as this is written in TS.
Prashanth S.
<transaction><native-assets><cardano-serialization-lib><utxo>
How can I build a transaction to send different assets (not just ada) using Cardano Serialization Lib?
# How can I build a transaction to send different assets (not just ada) using Cardano Serialization Lib?

(Asked by: username_0 on 2022-05-30)

I have followed [this example](https://github.com/Emurgo/cardano-serialization-lib/blob/master/doc/getting-started/generating-transactions.md) on how to create a transaction to send ada and I had no issues so far.

What I would like to do next is to add different assets to this same transaction.

Is there an example on how to do that?

**Tags:** transaction, native-assets, cardano-serialization-lib, utxo
> **Comments:**
> **username_1**: If you are open to use other packages, https://github.com/StricaHQ/typhonjs is a package which you can use to build transactions, It's easy to explore this as this is written in TS.  

---

## 2 answers

### Answer by username_0 on 2023-01-27. Score: 1 | Accepted answer

This is how you build a multi-token transaction using cardano-serialization-lib:
export async function buildMultiAssetTx(
  fromAddress: string, 
  toAddress: string, 
  policy: string, 
  assetName: string, 
  amount: string, 
  privateKey: CardanoWasm.PrivateKey
) {
  
  const params = await getParameters();

  const linearFee = CardanoWasm.LinearFee.new(
    CardanoWasm.BigNum.from_str(params.data.genesis.shelley.protocolParams.minFeeA.toString()),
    CardanoWasm.BigNum.from_str(params.data.genesis.shelley.protocolParams.minFeeB.toString())
  );
  
  const txBuilderConfig = CardanoWasm.TransactionBuilderConfigBuilder.new()
    .fee_algo(linearFee)
    .pool_deposit(CardanoWasm.BigNum.from_str(params.data.genesis.shelley.protocolParams.poolDeposit.toString()))
    .key_deposit(CardanoWasm.BigNum.from_str(params.data.genesis.shelley.protocolParams.keyDeposit.toString()))
    .max_value_size(params.data.genesis.alonzo.maxValueSize)
    .max_tx_size(params.data.genesis.shelley.protocolParams.maxTxSize)
    .coins_per_utxo_word(CardanoWasm.BigNum.from_str(params.data.genesis.alonzo.lovelacePerUTxOWord.toString()))
    .build();

  // instantiate the tx builder with the Cardano protocol parameters - these may change later on
  const txBuilder = CardanoWasm.TransactionBuilder.new(txBuilderConfig);

  const unspentOutputs = await getTxUnspentOutputs(fromAddress);
   
  // pointer address
  const shelleyChangeAddress = CardanoWasm.Address.from_bech32(fromAddress);

  const minter = CardanoWasm.Address.from_bech32(toAddress);
  
  var txOutputBuilder = CardanoWasm.TransactionOutputBuilder.new();
  txOutputBuilder = txOutputBuilder.with_address(minter);
  var txOutputAmountBuilder = txOutputBuilder.next();

  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
  );
  
  txOutputAmountBuilder = txOutputAmountBuilder.with_asset_and_min_required_coin(multiAsset, CardanoWasm.BigNum.from_str(params.data.genesis.alonzo.lovelacePerUTxOWord.toString()))
  const txOutput = txOutputAmountBuilder.build();

  txBuilder.add_output(txOutput)

  // add a keyhash input - for ADA held in a Shelley-era normal address (Base, Enterprise, Pointer)
  txBuilder.add_inputs_from(unspentOutputs, 3); // LargestFirstMultiAsset

  // set the time to live - the absolute slot value before the tx becomes invalid
  const slot = await getSlotNo();
  txBuilder.set_ttl(slot.data.cardano.tip.slotNo + 10000);

  // calculate the min fee required and send any change to an address
  txBuilder.add_change_if_needed(shelleyChangeAddress)

  // once the transaction is ready, we build it to get the tx body without witnesses
  const txBody = txBuilder.build()
  const txHash = CardanoWasm.hash_transaction(txBody);
  const witnesses = CardanoWasm.TransactionWitnessSet.new();
  const vkeyWitnesses = CardanoWasm.Vkeywitnesses.new();
  const vkeyWitness1 = CardanoWasm.make_vkey_witness(txHash, privateKey);

  vkeyWitnesses.add(vkeyWitness1);

  witnesses.set_vkeys(vkeyWitnesses);
  witnesses.set_native_scripts(CardanoWasm.NativeScripts.new());
  
  // create the finalized transaction with witnesses
  const transaction = CardanoWasm.Transaction.new(
    txBody,
    witnesses,
    undefined, // transaction metadata
  );

  return toHexString(transaction.to_bytes());

}


---

### Answer by username_2 on 2022-05-30. Score: 2

<https://github.com/ozgrakkurt/cardano-mint-nft/blob/master/index.js>

The example above from Özgür Akkurt on GitHub demonstrates how to mint native tokens in JS, assuming you are looking for that language.

<https://github.com/Emurgo/cardano-serialization-lib/blob/master/doc/getting-started/minting-nfts.md>

This is the next example directly from the Emurgo library is the next example in sequence from the previous example you followed.

Is it Native tokens you are looking to send?

> **Comments:**
> **username_3**: Referencing guidelines: https://cardano.stackexchange.com/help/referencing  

---

