<p>There is <a href="https://docs.cardano.org/native-tokens/minimum-ada-value-requirement#min-ada-valuecalculation" rel="nofollow noreferrer">documentation</a> to calculate the minimum ADA value for a tx output however it is not only out of date(e.g. <code>coinSize</code> has since increased to 2, asset names now hex-encoded instead of ASCII chars), but also difficult to decipher into working code.</p>
<p>After quite a bit of effort I implemented the particular algorithm in C# which should translate to other languages without much difficulty:</p>
<pre><code>public record struct NativeAssetValue(
    string PolicyId, string AssetNameHex, long Quantity);

public record struct TokenBundle(
    long LovelaceValue, NativeAssetValue[] NativeAssets);

public static long CalculateMinUtxoLovelace(
    TokenBundle txOutBundle,
    int lovelacePerUtxoWord = 34482, // utxoCostPerWord in protocol params (could change in the future)
    int policyIdSizeBytes = 28, // 224 bit policyID (won't change in forseeable future)
    bool hasDataHash = false) // for UTxOs with smart contract datum
{
    const int fixedUtxoPrefixWords = 6;
    const int fixedUtxoEntryWithoutValueSizeWords = 27; // The static parts of a UTxO: 6 + 7 + 14 words
    const int coinSizeWords = 2; // since updated from 0 in docs.cardano.org/native-tokens/minimum-ada-value-requirement
    const int adaOnlyUtxoSizeWords = fixedUtxoEntryWithoutValueSizeWords + coinSizeWords;
    const int fixedPerTokenCost = 12;
    const int byteRoundUpAddition = 7; 
    const int bytesPerWord = 8; // One &quot;word&quot; is 8 bytes (64-bit)
    const int fixedDataHashSizeWords = 10;

    var isAdaOnly = txOutBundle.NativeAssets.Length == 0;
    if (isAdaOnly)
        return lovelacePerUtxoWord * adaOnlyUtxoSizeWords; // 999978 lovelaces or 0.999978 ADA

    // Get distinct policyIDs and assetNames
    var policyIds = new HashSet&lt;string&gt;();
    var assetNameHexadecimals = new HashSet&lt;string&gt;();
    foreach (var customToken in txOutBundle.NativeAssets)
    {
        policyIds.Add(customToken.PolicyId);
        assetNameHexadecimals.Add(customToken.AssetNameHex);
    }

    // Calculate (prefix + (numDistinctPids * 28(policyIdSizeBytes) + numTokens * 12(fixedPerTokenCost) + tokensNameLen + 7) ~/8)
    var tokensNameLen = assetNameHexadecimals.Sum(an =&gt; an.Length) / 2; // 2 hexadecimal chars = 1 Byte
    var valueSizeWords = fixedUtxoPrefixWords + (
        (policyIds.Count * policyIdSizeBytes)
        + (txOutBundle.NativeAssets.Length * fixedPerTokenCost)
        + tokensNameLen + byteRoundUpAddition) / bytesPerWord;
    var dataHashSizeWords = hasDataHash ? fixedDataHashSizeWords : 0;

    var minUtxoLovelace = lovelacePerUtxoWord 
        * (fixedUtxoEntryWithoutValueSizeWords + valueSizeWords + dataHashSizeWords);

    return minUtxoLovelace;
}
</code></pre>
<p>Special thanks to <a href="https://github.com/ilap/ShelleyStuffs" rel="nofollow noreferrer">Pal's visualisation from his Shelley Stuffs Repository</a> which was of tremendous help <a href="https://i.sstatic.net/6CA1l.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6CA1l.png" alt="Visualisation" /></a></p>

CC BY-SA 4.0
2022-04-26T06:27:11.383Z
2022-04-26T06:27:11.383Z
keefie
<p>I think the answer is supposed to be extractable from the docs</p>
<p><a href="https://docs.cardano.org/native-tokens/minimum-ada-value-requirement" rel="nofollow noreferrer">https://docs.cardano.org/native-tokens/minimum-ada-value-requirement</a></p>
<p>but its not very clear (to me).</p>
<p>I'm also pretty suspicious</p>
<pre><code>sumAssetNameLengths B = length myAssetNameSBS + length yourAssetNameSBS + length someAssetNameSBS
</code></pre>
<p>implies that the blockchain economises when two assets share a token name (but different currency symbol). This is weird.</p>
<hr />
<p>I found your Q after looking for the same answer ... =&gt; I also don't really know yet</p>

CC BY-SA 4.0
2022-04-24T09:07:24.200Z
2022-04-24T09:07:24.200Z
waalge
<p>I have an application where I want to avoid using 3rd binaries like cardano-cli. I need to calculate what is minimal UTXO size for TX where N different tokens is sent.</p>
<p>Right now cardano-wallet HTTP endpoint returns error 404 with long error string that contains correct value</p>
<pre><code>
{
  &quot;code&quot;: &quot;utxo_too_small&quot;,
  &quot;message&quot;: &quot;Some outputs have ada values that are too small. There's a minimum ada value specified by the protocol that each output must satisfy. I'll handle that minimum value myself when you do not explicitly specify an ada value for an output. Otherwise, you must specify enough ada. Here are the problematic outputs:   - Expected min coin value: 1.413762     TxOut:       address: 00064225...d59deacc       coin: 1.400000       tokens:         - policy: 9e87deb6ec1e989f125652b4883728fdd308e4223a0396f45b2023c2           tokens:             - token: 766d3774657374486f736b794361736847726162               quantity: 1 &quot;
}
</code></pre>
<p>I want to be able to calculate this value myself. What I do right now is that I take &quot;base min UTXO&quot; size of 1.6ADA for lovelace and 1 token and then add 0.3ADA for each extra token, so if someone is sending 2 tokens it asks for 1.9ADA deposit.</p>
<p>That works but isn't correct and I probably use unnecessarily large deposits (it doesn't matter so much as whole deposit - tx fees is returned back to user) but I'd like to make this correct.</p>
<p>So what is the correct algorithm to calculate precise fee? Mind this is just a simple transaction of ADA and some native assets to single output address, no plutus or other complex things to consider.</p>

CC BY-SA 4.0
2022-02-08T14:17:59.243Z
Check out this Q/A: https://cardano.stackexchange.com/questions/2545/manual-calculation-of-transaction-size-and-fee

Manual calculation of a cbor is an "involved process", as Andrew puts it. I'm not sure the exact way to do it though.
zhekson
CC BY-SA 4.0
2022-02-08T14:27:08.570Z
unfortunatelly that doesn't explain how to calculate it without cardano-cli, eg. how to get TX size without it?
Petr
<transaction><fee>
What is precise calculation algorithm for native asset fees?
# What is precise calculation algorithm for native asset fees?

(Asked by: username_0 on 2022-02-08)

I have an application where I want to avoid using 3rd binaries like cardano-cli. I need to calculate what is minimal UTXO size for TX where N different tokens is sent.
Right now cardano-wallet HTTP endpoint returns error 404 with long error string that contains correct value

{
  "code": "utxo_too_small",
  "message": "Some outputs have ada values that are too small. There's a minimum ada value specified by the protocol that each output must satisfy. I'll handle that minimum value myself when you do not explicitly specify an ada value for an output. Otherwise, you must specify enough ada. Here are the problematic outputs:   - Expected min coin value: 1.413762     TxOut:       address: 00064225...d59deacc       coin: 1.400000       tokens:         - policy: 9e87deb6ec1e989f125652b4883728fdd308e4223a0396f45b2023c2           tokens:             - token: 766d3774657374486f736b794361736847726162               quantity: 1 "
}

I want to be able to calculate this value myself. What I do right now is that I take "base min UTXO" size of 1.6ADA for lovelace and 1 token and then add 0.3ADA for each extra token, so if someone is sending 2 tokens it asks for 1.9ADA deposit.
That works but isn't correct and I probably use unnecessarily large deposits (it doesn't matter so much as whole deposit - tx fees is returned back to user) but I'd like to make this correct.
So what is the correct algorithm to calculate precise fee? Mind this is just a simple transaction of ADA and some native assets to single output address, no plutus or other complex things to consider.

**Tags:** transaction, fee
> **Comments:**
> **username_1**: Check out this Q/A: https://cardano.stackexchange.com/questions/2545/manual-calculation-of-transaction-size-and-fee  Manual calculation of a cbor is an "involved process", as Andrew puts it. I'm not sure the exact way to do it though.  
> **username_0**: unfortunatelly that doesn't explain how to calculate it without cardano-cli, eg. how to get TX size without it?  

---

## 2 answers

### Answer by username_2 on 2022-04-26. Score: 3

There is documentation to calculate the minimum ADA value for a tx output however it is not only out of date(e.g. coinSize has since increased to 2, asset names now hex-encoded instead of ASCII chars), but also difficult to decipher into working code.
After quite a bit of effort I implemented the particular algorithm in C# which should translate to other languages without much difficulty:
public record struct NativeAssetValue(
    string PolicyId, string AssetNameHex, long Quantity);

public record struct TokenBundle(
    long LovelaceValue, NativeAssetValue[] NativeAssets);

public static long CalculateMinUtxoLovelace(
    TokenBundle txOutBundle,
    int lovelacePerUtxoWord = 34482, // utxoCostPerWord in protocol params (could change in the future)
    int policyIdSizeBytes = 28, // 224 bit policyID (won't change in forseeable future)
    bool hasDataHash = false) // for UTxOs with smart contract datum
{
    const int fixedUtxoPrefixWords = 6;
    const int fixedUtxoEntryWithoutValueSizeWords = 27; // The static parts of a UTxO: 6 + 7 + 14 words
    const int coinSizeWords = 2; // since updated from 0 in docs.cardano.org/native-tokens/minimum-ada-value-requirement
    const int adaOnlyUtxoSizeWords = fixedUtxoEntryWithoutValueSizeWords + coinSizeWords;
    const int fixedPerTokenCost = 12;
    const int byteRoundUpAddition = 7; 
    const int bytesPerWord = 8; // One "word" is 8 bytes (64-bit)
    const int fixedDataHashSizeWords = 10;

    var isAdaOnly = txOutBundle.NativeAssets.Length == 0;
    if (isAdaOnly)
        return lovelacePerUtxoWord * adaOnlyUtxoSizeWords; // 999978 lovelaces or 0.999978 ADA

    // Get distinct policyIDs and assetNames
    var policyIds = new HashSet();
    var assetNameHexadecimals = new HashSet();
    foreach (var customToken in txOutBundle.NativeAssets)
    {
        policyIds.Add(customToken.PolicyId);
        assetNameHexadecimals.Add(customToken.AssetNameHex);
    }

    // Calculate (prefix + (numDistinctPids * 28(policyIdSizeBytes) + numTokens * 12(fixedPerTokenCost) + tokensNameLen + 7) ~/8)
    var tokensNameLen = assetNameHexadecimals.Sum(an => an.Length) / 2; // 2 hexadecimal chars = 1 Byte
    var valueSizeWords = fixedUtxoPrefixWords + (
        (policyIds.Count * policyIdSizeBytes)
        + (txOutBundle.NativeAssets.Length * fixedPerTokenCost)
        + tokensNameLen + byteRoundUpAddition) / bytesPerWord;
    var dataHashSizeWords = hasDataHash ? fixedDataHashSizeWords : 0;

    var minUtxoLovelace = lovelacePerUtxoWord 
        * (fixedUtxoEntryWithoutValueSizeWords + valueSizeWords + dataHashSizeWords);

    return minUtxoLovelace;
}

Special thanks to Pal's visualisation from his Shelley Stuffs Repository which was of tremendous help


---

### Answer by username_3 on 2022-04-24. Score: 0

I think the answer is supposed to be extractable from the docs
https://docs.cardano.org/native-tokens/minimum-ada-value-requirement
but its not very clear (to me).
I'm also pretty suspicious
sumAssetNameLengths B = length myAssetNameSBS + length yourAssetNameSBS + length someAssetNameSBS

implies that the blockchain economises when two assets share a token name (but different currency symbol). This is weird.

I found your Q after looking for the same answer ... => I also don't really know yet


---

