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