I'm using cardano-serialization-lib ^9.1.2
to create transaction.
I'm using method that are now deprecated in version 10
, so is there an example of transaction with serialization-lib latest version used on nodejs?
Cardano Stack Exchange is a question and answer site for users and developers of the Cardano cryptocurrency ecosystem. It only takes a minute to sign up.
Sign up to join this communityI'm using cardano-serialization-lib ^9.1.2
to create transaction.
I'm using method that are now deprecated in version 10
, so is there an example of transaction with serialization-lib latest version used on nodejs?
You can check the changelog here for some examples and explanations: https://github.com/Emurgo/cardano-serialization-lib/releases/tag/10.0.0
A rough example could look something like this:
function createExampleTx() {
const protocolParams = await blockfrost.fetchParameters();
// Create TransactionBuilderConfig (see func below)
const txBuilderConfig = getTxBuilderConfig(protocolParams);
// Create TransactionBuilder
const txBuilder = TransactionBuilder.new(txBuilderConfig);
txBuilder.set_ttl(protocolParams.slot + 10000);
// Create and add outputs
const txOut = TransactionOutputBuilder.new()
.with_address(RECEIVER)
.next()
.with_coin(BigNum.from_str("5000000"))
.build();
txBuilder.add_output(txOut);
// Create and add Inputs
const utxos = await getUtxos(SENDER);
txBuilder.add_inputs_from(utxos, CoinSelectionStrategyCIP2.LargestFirst);
// Add change and fees
txBuilder.add_change_if_needed(SENDER);
// Build transaction
const txBody = txBuilder.build();
const unsignedTx = Transaction.new(
txBody,
TransactionWitnessSet.new()
);
// ...
}
function getTxBuilderConfig(protocolParams: ProtocolParameters): TransactionBuilderConfig {
return TransactionBuilderConfigBuilder.new()
.fee_algo(LinearFee.new(
BigNum.from_str(protocolParams.linearFee.minFeeA),
BigNum.from_str(protocolParams.linearFee.minFeeB)
))
.pool_deposit(BigNum.from_str(protocolParams.poolDeposit))
.key_deposit(BigNum.from_str(protocolParams.keyDeposit))
.max_tx_size(protocolParams.maxTxSize)
.max_value_size(parseInt(protocolParams.maxValSize!))
.coins_per_utxo_word(BigNum.from_str(protocolParams.coinsPerUtxoWord!))
.build()
}