Skip to main content

generating_transactions

Example code

The example below builds a transaction with all 2 of the 3 input types: key and bootstrap. Multisig (script) inputs are essentially identical to key inputs, but using the scripthash instead of the keyhash, however they are not supported for implicit fee calculation yet. Fees are automatically calculated and sent to a change address in the example.

// instantiate the tx builder with the Cardano protocol parameters - these may change later on
const txBuilder = makeTxBuilder();
const testnetId = 0;
// add a keyhash input - for ADA held in a Shelley-era normal address (Base, Enterprise, Pointer)
const prvKey = CML.PrivateKey.from_bech32("ed25519e_sk16rl5fqqf4mg27syjzjrq8h3vq44jnnv52mvyzdttldszjj7a64xtmjwgjtfy25lu0xmv40306lj9pcqpa6slry9eh3mtlqvfjz93vuq0grl80");
const inputAddr = CML.EnterpriseAddress.new(testnetId, CML.StakeCredential.new_key(prvKey.to_public().hash())).to_address();
txBuilder.add_input(CML.SingleInputBuilder.new(
CML.TransactionInput.new(
CML.TransactionHash.from_hex("8561258e210352fba2ac0488afed67b3427a27ccf1d41ec030c98a8199bc22ec"), // tx hash
0, // index
),
CML.TransactionOutput.new(
inputAddr,
CML.Value.from_coin(BigInt(6000000)),
)
);

// base address
const outputAddress = CML.Address.from_bech32("addr_test1qpu5vlrf4xkxv2qpwngf6cjhtw542ayty80v8dyr49rf5ewvxwdrt70qlcpeeagscasafhffqsxy36t90ldv06wqrk2qum8x5w");
// pointer address
const changeAddress = CML.Address.from_bech32("addr_test1gz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzerspqgpsqe70et");

// add output to the tx
txBuilder.add_output(
CML.TransactionOutputBuilder()
.with_address(outputAddress)
.next()
.with_value(CML.Value.from_coin(BigInt(1000000)))
.build()
);

// calculate the min fee required and send any change to an address
// this moves onto the next step of building the transaction: providing witnesses
const signedTxBuilder = tx_builder.build(
changeAddress,
CML.ChangeSelectionAlgo.Default
);

// sign with the key that owns the input used
signedTxBuilder.add_vkey(CML.make_vkey_witness(txHash, prvKey));

const tx = signedTxBuilder.build_checked();
// ready to submit, can be converted to CBOR via tx.to_cbor_bytes() or to_cbor_hex() for hex

A note on fees

Fees in Cardano are based directly on the size of the final encoded transaction. It is important to note that a transaction created by this library potentially can vary in size compared to one built with other tools. This is because transactions, as well as other Cardano structures, are encoded using CBOR a binary JSON-like encoding. Due to arrays and maps allowing both definite or indefinite length encoding in the encoded transaction created by the library, the size can vary. This is because definite encoding consists of a tag containing the size of the array/map which can be 1 or more bytes long depending on the number of elements the size of the encoded structure, while indefinite length encoding consists of a 1 byte starting tag and after all elements are listed, a 1 byte ending tag. These variances should should only be a couple bytes and cardano-multiplatform-lib uses definite encoding by default which is the same length or smaller for any reasonable sized transaction.