1

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?

1 Answer 1

1

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()

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.