2

I want to create a script (smart-contract) utxo with a specific number of tokens (it can be an output of a transaction before) in cardano-serialization-lib - javascript. I need this utxo to be part of a future output of a transaction. How can I do this?

-- Scenario to explain better --

Imagining a scenario where we have a smart contract feeded with 1000 tokens. Then, I want to send 30 tokens for a client (when he claims these tokens). If I want to have an UTXO with only these 30 tokens, I have to create an output before that transaction (for example, when the user locks an asset, the output for the smart contract will be the asset + 30 tokens), but these tokens are not an input from the client, is "from smart contract to smart contract".

1 Answer 1

1

You can follow this example on how to generate a transaction using Cardano Serialization Lib.

For your specific question, focus on the following section of the code that will help you create an output with the ada value you like:

// add output to the tx
txBuilder.add_output(
    CardanoWasm.TransactionOutput.new(
    shelleyOutputAddress,
    CardanoWasm.Value.new(CardanoWasm.BigNum.from_str('1000000'))    
    ),
);

If you also like to add assets to that output, you can try this code:

  const outputAddress = CardanoWasm.Address.from_bech32(toAddress);
  
  var txOutputBuilder = CardanoWasm.TransactionOutputBuilder.new();
  txOutputBuilder = txOutputBuilder.with_address(outputAddress);
  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();

This second example is more in line with what is being used recently to create transactions and outputs in Cardano (TransactionOutputBuilder object).

7
  • 1
    Hey! Thanks for your answer. I was wondering if I explain well my question, because I already have this code. However, I want to create an output of a smart contract (a script) that will be an UTXO (without input provided), I mean, an output to the smart contract itself. Then, I can use this UTXO with this specific number of tokens to send to a client that is interacting with the smart contract.
    – pprs
    Commented Jun 29, 2022 at 9:30
  • 1
    I added a scenario on the question to be more easier to explain what I want
    – pprs
    Commented Jun 29, 2022 at 9:33
  • But... can't you just create the UTxO with the 30 tokens + the asset when the user "locks" that asset? That way, you will have generated a new UTxO with 30 tokens + 1 locked asset + some ada, in that same smart contract, for future use at some point in time. Commented Jun 30, 2022 at 3:03
  • 1
    You don't have to spend the rest of the UTxO in the smart contract if you do that. You can just consume the UTxO with 1000 tokens and generate the one you need with 30 tokens (as I explained before). Then the remaining 970 tokens will be automatically sent to ANOTHER UTxO, in the same contract, as change for your transaction. Is this clear? Commented Jul 1, 2022 at 0:46
  • 1
    Yes @Falcon thanks. In the continuation of this question, I made a new one : cardano.stackexchange.com/q/8606/6611 if you can help me I will appreciate it.
    – pprs
    Commented Jul 7, 2022 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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