I have followed this example 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?
I have followed this example 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?
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?
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());
}