<p>You can follow <a href="https://github.com/Emurgo/cardano-serialization-lib/blob/master/doc/getting-started/generating-transactions.md" rel="nofollow noreferrer">this example</a> on how to generate a transaction using Cardano Serialization Lib.</p>
<p>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:</p>
<pre><code>// add output to the tx
txBuilder.add_output(
    CardanoWasm.TransactionOutput.new(
    shelleyOutputAddress,
    CardanoWasm.Value.new(CardanoWasm.BigNum.from_str('1000000'))    
    ),
);
</code></pre>
<p>If you also like to add assets to that output, you can try this code:</p>
<pre><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, &quot;hex&quot;)), // Asset Name
    CardanoWasm.BigNum.from_str(amount) // How much to send
  );
  multiAsset.insert(
    CardanoWasm.ScriptHash.from_bytes(Buffer.from(policy, &quot;hex&quot;)), // 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();
</code></pre>
<p>This second example is more in line with what is being used recently to create transactions and outputs in Cardano (<code>TransactionOutputBuilder</code> object).</p>

CC BY-SA 4.0
2022-06-29T09:30:40.937Z
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
CC BY-SA 4.0
2022-06-29T09:33:55.467Z
I added a scenario on the question to be more easier to explain what I want
pprs
CC BY-SA 4.0
2022-06-30T03:03:46.193Z
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.
Falcon Stakepool
CC BY-SA 4.0
2022-06-30T03:05:42.203Z
BTW, you can't generate an output without an input since UTxOs are static: they only get spent by a trigger. The triggers are the inputs.
Falcon Stakepool
CC BY-SA 4.0
2022-06-30T13:18:06.217Z
Yes Falcon, it is that I want (30 tokens + the asset + some ada). But this way I have to make some thing like: input -> 1000 tokens from smart contract ; output -> 30 tokens to smart contract ? but I have to spent the rest to the smart contract? how can I make this?
pprs
CC BY-SA 4.0
2022-07-01T00:46:17.457Z
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?
Falcon Stakepool
CC BY-SA 4.0
2022-07-07T17:26:04.473Z
Yes @Falcon thanks. In the continuation of this question, I made a new one : https://cardano.stackexchange.com/q/8606/6611 if you can help me I will appreciate it.
pprs
CC BY-SA 4.0
2022-06-29T04:07:29.940Z
2022-06-30T03:22:38.660Z
2022-06-30T03:22:38.660Z
Falcon Stakepool
<p>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?</p>
<p>-- Scenario to explain better --</p>
<p>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 &quot;from smart contract to smart contract&quot;.</p>

<plutus><smart-contract><cardano-serialization-lib>
How to create an unspent UTXO with specific value
# How to create an unspent UTXO with specific value

(Asked by: username_0 on 2022-06-28)

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".

**Tags:** plutus, smart-contract, cardano-serialization-lib

---

## 1 answers

### Answer by username_1 on 2022-06-29. Score: 1 | Accepted answer

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

> **Comments:**
> **username_0**: 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.  
> **username_0**: I added a scenario on the question to be more easier to explain what I want  
> **username_1**: 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.  
> **username_1**: BTW, you can't generate an output without an input since UTxOs are static: they only get spent by a trigger. The triggers are the inputs.  
> **username_0**: Yes Falcon, it is that I want (30 tokens + the asset + some ada). But this way I have to make some thing like: input -> 1000 tokens from smart contract ; output -> 30 tokens to smart contract ? but I have to spent the rest to the smart contract? how can I make this?  
> **username_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?  
> **username_0**: Yes @Falcon thanks. In the continuation of this question, I made a new one : https://cardano.stackexchange.com/q/8606/6611 if you can help me I will appreciate it.  

---

