import * as CardanoWasm from '@emurgo/cardano-serialization-lib-nodejs'; import { CARDANO_PARAMS, CertificateType, DATA_COST_PER_UTXO_BYTE, ERROR, MAX_TOKENS_PER_OUTPUT, } from '../constants'; import { CardanoDRepType, } from '../types/types'; import { CoinSelectionError } from './errors'; export const bigNumFromStr = (num) => CardanoWasm.BigNum.from_str(num); export const getProtocolMagic = (tesnet) => tesnet ? CARDANO_PARAMS.PROTOCOL_MAGICS.testnet_preview : CARDANO_PARAMS.PROTOCOL_MAGICS.mainnet; export const getNetworkId = (testnet) => testnet ? CARDANO_PARAMS.NETWORK_IDS.testnet_preview : CARDANO_PARAMS.NETWORK_IDS.mainnet; export const parseAsset = (hex) => { const policyIdSize = 56; const policyId = hex.slice(0, policyIdSize); const assetNameInHex = hex.slice(policyIdSize); return { policyId, assetNameInHex, }; }; export const buildMultiAsset = (assets) => { const multiAsset = CardanoWasm.MultiAsset.new(); const assetsGroupedByPolicy = {}; assets.forEach(assetEntry => { const { policyId, assetNameInHex } = parseAsset(assetEntry.unit); if (!assetsGroupedByPolicy[policyId]) { assetsGroupedByPolicy[policyId] = CardanoWasm.Assets.new(); } const assets = assetsGroupedByPolicy[policyId]; assets.insert(CardanoWasm.AssetName.new(Buffer.from(assetNameInHex, 'hex')), bigNumFromStr(assetEntry.quantity || '0')); }); Object.keys(assetsGroupedByPolicy).forEach(policyId => { const scriptHash = CardanoWasm.ScriptHash.from_bytes(Buffer.from(policyId, 'hex')); multiAsset.insert(scriptHash, assetsGroupedByPolicy[policyId]); }); return multiAsset; }; export const multiAssetToArray = (multiAsset) => { if (!multiAsset) return []; const assetsArray = []; const policyHashes = multiAsset.keys(); for (let i = 0; i < policyHashes.len(); i++) { const policyId = policyHashes.get(i); const assetsInPolicy = multiAsset.get(policyId); if (!assetsInPolicy) continue; const assetNames = assetsInPolicy.keys(); for (let j = 0; j < assetNames.len(); j++) { const assetName = assetNames.get(j); const amount = assetsInPolicy.get(assetName); if (!amount) continue; const policyIdHex = Buffer.from(policyId.to_bytes()).toString('hex'); const assetNameHex = Buffer.from(assetName.name()).toString('hex'); assetsArray.push({ quantity: amount.to_str(), unit: `${policyIdHex}${assetNameHex}`, }); } } return assetsArray; }; export const getAssetAmount = (obj, asset = 'lovelace') => { var _a, _b; return (_b = (_a = obj.amount.find(a => a.unit === asset)) === null || _a === void 0 ? void 0 : _a.quantity) !== null && _b !== void 0 ? _b : '0'; }; export const getUtxoQuantity = (utxos, asset = 'lovelace') => utxos.reduce((acc, utxo) => acc.checked_add(bigNumFromStr(getAssetAmount(utxo, asset))), bigNumFromStr('0')); export const getOutputQuantity = (outputs, asset = 'lovelace') => { if (asset === 'lovelace') { return outputs.reduce((acc, output) => { var _a; return acc.checked_add(bigNumFromStr((_a = output.amount) !== null && _a !== void 0 ? _a : '0')); }, bigNumFromStr('0')); } return outputs.reduce((acc, output) => { var _a, _b, _c; return acc.checked_add(bigNumFromStr((_c = (_b = (_a = output.assets) === null || _a === void 0 ? void 0 : _a.find(a => a.unit === asset)) === null || _b === void 0 ? void 0 : _b.quantity) !== null && _c !== void 0 ? _c : '0')); }, bigNumFromStr('0')); }; export const sortUtxos = (utxos, asset = 'lovelace') => { const copy = JSON.parse(JSON.stringify(utxos)); return copy.sort((u1, u2) => bigNumFromStr(getAssetAmount(u2, asset)).compare(bigNumFromStr(getAssetAmount(u1, asset)))); }; export const buildTxInput = (utxo) => { const input = CardanoWasm.TransactionInput.new(CardanoWasm.TransactionHash.from_bytes(Buffer.from(utxo.txHash, 'hex')), utxo.outputIndex); const amount = CardanoWasm.Value.new(bigNumFromStr(getAssetAmount(utxo))); const assets = utxo.amount.filter(a => a.unit !== 'lovelace'); if (assets.length > 0) { const multiAsset = buildMultiAsset(assets); amount.set_multiasset(multiAsset); } const address = CardanoWasm.Address.from_bech32(utxo.address); return { input, address, amount }; }; export const buildTxOutput = (output, dummyAddress) => { var _a; // If output.address was not defined fallback to bech32 address (useful for "precompose" tx // which doesn't have all necessary data, but we can fill in the blanks and return some info such as fee) const outputAddr = output.address && CardanoWasm.ByronAddress.is_valid(output.address) ? CardanoWasm.ByronAddress.from_base58(output.address).to_address() : CardanoWasm.Address.from_bech32((_a = output.address) !== null && _a !== void 0 ? _a : dummyAddress); // Set initial amount const outputAmount = output.amount ? bigNumFromStr(output.amount) : bigNumFromStr('0'); // Create Value including assets let outputValue = CardanoWasm.Value.new(outputAmount); const multiAsset = output.assets.length > 0 ? buildMultiAsset(output.assets) : null; if (multiAsset) { outputValue.set_multiasset(multiAsset); } // Calculate min required ADA for the output let txOutput = CardanoWasm.TransactionOutput.new(outputAddr, outputValue); const minAdaRequired = CardanoWasm.min_ada_for_output(txOutput, DATA_COST_PER_UTXO_BYTE); // If calculated min required ada is greater than current output value than adjust it if (outputAmount.compare(minAdaRequired) < 0) { outputValue = CardanoWasm.Value.new(minAdaRequired); if (multiAsset) { outputValue.set_multiasset(multiAsset); } txOutput = CardanoWasm.TransactionOutput.new(outputAddr, outputValue); } return txOutput; }; export const getOutputCost = (txBuilder, output, dummyAddress) => { const txOutput = buildTxOutput(output, dummyAddress); const outputFee = txBuilder.fee_for_output(txOutput); const minAda = CardanoWasm.min_ada_for_output(txOutput, DATA_COST_PER_UTXO_BYTE); return { output: txOutput, outputFee, minOutputAmount: minAda, // should match https://cardano-ledger.readthedocs.io/en/latest/explanations/min-utxo.html }; }; export const prepareWithdrawals = (withdrawals) => { const preparedWithdrawals = CardanoWasm.Withdrawals.new(); withdrawals.forEach(withdrawal => { const rewardAddress = CardanoWasm.RewardAddress.from_address(CardanoWasm.Address.from_bech32(withdrawal.stakeAddress)); if (rewardAddress) { preparedWithdrawals.insert(rewardAddress, bigNumFromStr(withdrawal.amount)); } }); return preparedWithdrawals; }; export const prepareCertificates = (certificates, accountKey) => { const preparedCertificates = CardanoWasm.Certificates.new(); if (certificates.length === 0) return preparedCertificates; const stakeKey = accountKey.derive(2).derive(0); const stakeCred = CardanoWasm.Credential.from_keyhash(stakeKey.to_raw_key().hash()); certificates.forEach(cert => { if (cert.type === CertificateType.STAKE_REGISTRATION) { preparedCertificates.add(CardanoWasm.Certificate.new_stake_registration(CardanoWasm.StakeRegistration.new(stakeCred))); } else if (cert.type === CertificateType.STAKE_DELEGATION) { preparedCertificates.add(CardanoWasm.Certificate.new_stake_delegation(CardanoWasm.StakeDelegation.new(stakeCred, CardanoWasm.Ed25519KeyHash.from_bytes(Buffer.from(cert.pool, 'hex'))))); } else if (cert.type === CertificateType.STAKE_DEREGISTRATION) { preparedCertificates.add(CardanoWasm.Certificate.new_stake_deregistration(CardanoWasm.StakeDeregistration.new(stakeCred))); } else if (cert.type === CertificateType.VOTE_DELEGATION) { let targetDRep; switch (cert.dRep.type) { case CardanoDRepType.ABSTAIN: targetDRep = CardanoWasm.DRep.new_always_abstain(); break; case CardanoDRepType.NO_CONFIDENCE: targetDRep = CardanoWasm.DRep.new_always_no_confidence(); break; case CardanoDRepType.KEY_HASH: targetDRep = CardanoWasm.DRep.new_key_hash(CardanoWasm.Ed25519KeyHash.from_hex(cert.dRep.keyHash)); break; case CardanoDRepType.SCRIPT_HASH: targetDRep = CardanoWasm.DRep.new_script_hash(CardanoWasm.ScriptHash.from_hex(cert.dRep.scriptHash)); break; } if (targetDRep) { preparedCertificates.add(CardanoWasm.Certificate.new_vote_delegation(CardanoWasm.VoteDelegation.new(stakeCred, targetDRep))); } } else { throw new CoinSelectionError(ERROR.UNSUPPORTED_CERTIFICATE_TYPE); } }); return preparedCertificates; }; export const calculateRequiredDeposit = (certificates) => { const CertificateDeposit = { [CertificateType.STAKE_DELEGATION]: 0, [CertificateType.VOTE_DELEGATION]: 0, [CertificateType.STAKE_POOL_REGISTRATION]: 500000000, [CertificateType.STAKE_REGISTRATION]: 2000000, [CertificateType.STAKE_DEREGISTRATION]: -2000000, }; return certificates.reduce((acc, cert) => (acc += CertificateDeposit[cert.type]), 0); }; export const setMinUtxoValueForOutputs = (txBuilder, outputs, dummyAddress) => { const preparedOutputs = outputs.map(output => { // sets minimal output ADA amount in case of multi-asset output const { minOutputAmount } = getOutputCost(txBuilder, output, dummyAddress); const outputAmount = bigNumFromStr(output.amount || '0'); let amount; if (output.assets.length > 0 && outputAmount.compare(minOutputAmount) < 0) { // output with an asset(s) adjust minimum ADA to met network requirements amount = minOutputAmount.to_str(); } else { amount = output.amount; } if (!output.setMax && output.assets.length === 0 && output.amount && outputAmount.compare(minOutputAmount) < 0) { // Case of an output without any asset, and without setMax = true // If the user entered less than min utxo val then throw an error (won't throw if there is no amount yet) // (On outputs with setMax flag we set '0' on purpose) // (On outputs with an asset we automatically adjust ADA amount if it is below required minimum) throw new CoinSelectionError(ERROR.UTXO_VALUE_TOO_SMALL); } if (output.setMax) { // if setMax is active set initial value to 0 if (output.assets.length > 0) { output.assets[0].quantity = '0'; } else { amount = '0'; } } return Object.assign(Object.assign({}, output), { // if output contains assets make sure that minUtxoValue is at least minOutputAmount (even for output where we want to setMax) amount }); }); return preparedOutputs; }; export const splitChangeOutput = (txBuilder, singleChangeOutput, changeAddress, maxTokensPerOutput = MAX_TOKENS_PER_OUTPUT) => { // TODO: https://github.com/Emurgo/cardano-serialization-lib/pull/236 const multiAsset = singleChangeOutput.output.amount().multiasset(); if (!multiAsset || (multiAsset && multiAsset.len() < maxTokensPerOutput)) { return [singleChangeOutput]; } let lovelaceAvailable = singleChangeOutput.output .amount() .coin() .checked_add(singleChangeOutput.outputFee); const allAssets = multiAssetToArray(singleChangeOutput.output.amount().multiasset()); const nAssetBundles = Math.ceil(allAssets.length / maxTokensPerOutput); const changeOutputs = []; // split change output to multiple outputs, where each bundle has maximum of maxTokensPerOutput assets for (let i = 0; i < nAssetBundles; i++) { const assetsBundle = allAssets.slice(i * maxTokensPerOutput, (i + 1) * maxTokensPerOutput); const outputValue = CardanoWasm.Value.new_from_assets(buildMultiAsset(assetsBundle)); const txOutput = CardanoWasm.TransactionOutput.new(CardanoWasm.Address.from_bech32(changeAddress), outputValue); const minAdaRequired = CardanoWasm.min_ada_for_output(txOutput, DATA_COST_PER_UTXO_BYTE); changeOutputs.push({ isChange: true, address: changeAddress, amount: minAdaRequired.to_str(), assets: assetsBundle, }); } const changeOutputsCost = changeOutputs.map((partialChange, i) => { let changeOutputCost = getOutputCost(txBuilder, partialChange, changeAddress); lovelaceAvailable = lovelaceAvailable.clamped_sub(bigNumFromStr(partialChange.amount).checked_add(changeOutputCost.outputFee)); if (i === changeOutputs.length - 1) { // add all unused ADA to the last change output let changeOutputAmount = lovelaceAvailable.checked_add(bigNumFromStr(partialChange.amount)); if (changeOutputAmount.compare(changeOutputCost.minOutputAmount) < 0) { // computed change amount would be below minUtxoValue // set change output amount to met minimum requirements for minUtxoValue changeOutputAmount = changeOutputCost.minOutputAmount; } partialChange.amount = changeOutputAmount.to_str(); changeOutputCost = getOutputCost(txBuilder, partialChange, changeAddress); } return changeOutputCost; }); return changeOutputsCost; }; export const prepareChangeOutput = (txBuilder, usedUtxos, preparedOutputs, changeAddress, utxosTotalAmount, totalOutputAmount, totalFeesAmount, pickAdditionalUtxo) => { // change output amount should be lowered by the cost of the change output (fee + minUtxoVal) // The cost will be subtracted once we calculate it. const placeholderChangeOutputAmount = utxosTotalAmount.clamped_sub(totalFeesAmount.checked_add(totalOutputAmount)); const uniqueAssets = []; usedUtxos.forEach(utxo => { const assets = utxo.amount.filter(a => a.unit !== 'lovelace'); assets.forEach(asset => { if (!uniqueAssets.includes(asset.unit)) { uniqueAssets.push(asset.unit); } }); }); const changeOutputAssets = uniqueAssets .map(assetUnit => { const assetInputAmount = getUtxoQuantity(usedUtxos, assetUnit); const assetSpentAmount = getOutputQuantity(preparedOutputs, assetUnit); return { unit: assetUnit, quantity: assetInputAmount.clamped_sub(assetSpentAmount).to_str(), }; }) .filter(asset => asset.quantity !== '0'); const changeOutputCost = getOutputCost(txBuilder, { address: changeAddress, amount: placeholderChangeOutputAmount.to_str(), assets: changeOutputAssets, }, changeAddress); // calculate change output amount as utxosTotalAmount - totalOutputAmount - totalFeesAmount - change output fee const totalSpent = totalOutputAmount .checked_add(totalFeesAmount) .checked_add(changeOutputCost.outputFee); let changeOutputAmount = utxosTotalAmount.clamped_sub(totalSpent); // Sum of all tokens in utxos must be same as sum of the tokens in external + change outputs // If computed change output doesn't contain any tokens then it makes sense to add it only if the fee + minUtxoValue is less then the amount let isChangeOutputNeeded = false; if (changeOutputAssets.length > 0 || changeOutputAmount.compare(changeOutputCost.minOutputAmount) >= 0) { isChangeOutputNeeded = true; } else if (pickAdditionalUtxo && changeOutputAmount.compare(bigNumFromStr('5000')) >= 0) { // change amount is above our constant (0.005 ADA), but still less than required minUtxoValue // try to add another utxo recalculate change again const utxo = pickAdditionalUtxo(); if (utxo) { utxo.addUtxo(); const newTotalFee = txBuilder.min_fee(); return prepareChangeOutput(txBuilder, usedUtxos, preparedOutputs, changeAddress, getUtxoQuantity(usedUtxos, 'lovelace'), totalOutputAmount, newTotalFee, pickAdditionalUtxo); } } if (isChangeOutputNeeded) { if (changeOutputAmount.compare(changeOutputCost.minOutputAmount) < 0) { // computed change amount would be below minUtxoValue // set change output amount to met minimum requirements for minUtxoValue changeOutputAmount = changeOutputCost.minOutputAmount; } // TODO: changeOutputCost.output.amount().set_coin(changeOutputAmount)? const txOutput = buildTxOutput({ amount: changeOutputAmount.to_str(), address: changeAddress, assets: changeOutputAssets, }, changeAddress); // WARNING: It returns a change output also in a case where we don't have enough utxos to cover the output cost, but the change output is needed because it contains additional assets return { outputFee: changeOutputCost.outputFee, minOutputAmount: changeOutputCost.minOutputAmount, output: txOutput, }; } // Change output not needed return null; }; export const getTxBuilder = (a = '44') => CardanoWasm.TransactionBuilder.new(CardanoWasm.TransactionBuilderConfigBuilder.new() .fee_algo(CardanoWasm.LinearFee.new(bigNumFromStr(a), bigNumFromStr('155381'))) .pool_deposit(bigNumFromStr('500000000')) .key_deposit(bigNumFromStr('2000000')) .coins_per_utxo_byte(bigNumFromStr(CARDANO_PARAMS.COINS_PER_UTXO_BYTE)) .max_value_size(CARDANO_PARAMS.MAX_VALUE_SIZE) .max_tx_size(CARDANO_PARAMS.MAX_TX_SIZE) .build()); export const getUnsatisfiedAssets = (selectedUtxos, outputs) => { const assets = []; outputs.forEach(output => { if (output.assets.length > 0) { const asset = output.assets[0]; const assetAmountInUtxos = getUtxoQuantity(selectedUtxos, asset.unit); if (assetAmountInUtxos.compare(bigNumFromStr(asset.quantity)) < 0) { assets.push(asset.unit); } } }); const lovelaceUtxo = getUtxoQuantity(selectedUtxos, 'lovelace'); if (lovelaceUtxo.compare(getOutputQuantity(outputs, 'lovelace')) < 0) { assets.push('lovelace'); } return assets; }; export const getInitialUtxoSet = (utxos, maxOutput) => { var _a, _b; // Picks all utxos containing an asset on which the user requested to set maximum value if (!maxOutput) return { used: [], remaining: utxos, }; const used = []; const remaining = []; const maxOutputAsset = (_b = (_a = maxOutput.assets[0]) === null || _a === void 0 ? void 0 : _a.unit) !== null && _b !== void 0 ? _b : 'lovelace'; // either all UTXOs will be used (send max for ADA output) or initial set of used utxos will contain all utxos containing given token utxos.forEach(u => { if (u.amount.find(a => a.unit === maxOutputAsset)) { used.push(u); } else { remaining.push(u); } }); return { used, remaining, }; }; export const setMaxOutput = (txBuilder, maxOutput, changeOutput) => { var _a, _b, _c, _d, _e; const maxOutputAsset = (_b = (_a = maxOutput.assets[0]) === null || _a === void 0 ? void 0 : _a.unit) !== null && _b !== void 0 ? _b : 'lovelace'; let newMaxAmount = bigNumFromStr('0'); const changeOutputAssets = multiAssetToArray(changeOutput === null || changeOutput === void 0 ? void 0 : changeOutput.output.amount().multiasset()); if (maxOutputAsset === 'lovelace') { // set maxOutput for ADA if (changeOutput) { // Calculate the cost of previous dummy set-max output const previousMaxOutputCost = getOutputCost(txBuilder, maxOutput, (_c = maxOutput.address) !== null && _c !== void 0 ? _c : changeOutput.output.address().to_bech32()); newMaxAmount = changeOutput.output.amount().coin(); if (changeOutputAssets.length === 0) { // Add a fee that was previously consumed by the dummy max output. // Cost calculated for the change output will be greater (due to larger coin amount // than in dummy output - which is 0) than the cost of the dummy set-max output. newMaxAmount = newMaxAmount.checked_add(previousMaxOutputCost.outputFee); changeOutput = null; } else { newMaxAmount = newMaxAmount.clamped_sub(changeOutput.minOutputAmount); const txOutput = CardanoWasm.TransactionOutput.new(changeOutput.output.address(), CardanoWasm.Value.new(newMaxAmount)); const minUtxoVal = CardanoWasm.min_ada_for_output(txOutput, DATA_COST_PER_UTXO_BYTE); if (newMaxAmount.compare(minUtxoVal) < 0) { // the amount would be less than min required ADA throw new CoinSelectionError(ERROR.UTXO_BALANCE_INSUFFICIENT); } } } maxOutput.amount = newMaxAmount.to_str(); } else { // set maxOutput for token if (changeOutput) { // max amount of the asset in output is equal to its quantity in change output newMaxAmount = bigNumFromStr((_e = (_d = changeOutputAssets.find(a => a.unit === maxOutputAsset)) === null || _d === void 0 ? void 0 : _d.quantity) !== null && _e !== void 0 ? _e : '0'); maxOutput.assets[0].quantity = newMaxAmount.to_str(); // TODO: set 0 if no change? const txOutput = CardanoWasm.TransactionOutput.new(changeOutput.output.address(), // new_from_assets does not automatically include required ADA CardanoWasm.Value.new_from_assets(buildMultiAsset(maxOutput.assets))); // adjust ADA amount to cover min ada for the asset maxOutput.amount = CardanoWasm.min_ada_for_output(txOutput, DATA_COST_PER_UTXO_BYTE).to_str(); } } return { maxOutput }; }; export const getUserOutputQuantityWithDeposit = (outputs, deposit, asset = 'lovelace') => { let amount = getOutputQuantity(outputs, asset); if (deposit > 0) { amount = amount.checked_add(bigNumFromStr(deposit.toString())); } return amount; }; export const filterUtxos = (utxos, asset) => { return utxos.filter(utxo => utxo.amount.find(a => a.unit === asset)); }; export const getRandomUtxo = (txBuilder, utxoRemaining, utxoSelected) => { const index = Math.floor(Math.random() * utxoRemaining.length); const utxo = utxoRemaining[index]; if (!utxo) return null; return { utxo, addUtxo: () => { utxoSelected.push(utxo); const { input, address, amount } = buildTxInput(utxo); txBuilder.add_regular_input(address, input, amount); utxoRemaining.splice(utxoRemaining.indexOf(utxo), 1); }, }; }; export const calculateUserOutputsFee = (txBuilder, userOutputs, changeAddress) => { // Calculate fee and minUtxoValue for all external outputs const outputsCost = userOutputs.map(output => getOutputCost(txBuilder, output, changeAddress)); const totalOutputsFee = outputsCost.reduce((acc, output) => (acc = acc.checked_add(output.outputFee)), bigNumFromStr('0')); return totalOutputsFee; }; export const orderInputs = (inputsToOrder, txBody) => { // reorder inputs to match order within tx const orderedInputs = []; for (let i = 0; i < txBody.inputs().len(); i++) { const txid = Buffer.from(txBody.inputs().get(i).transaction_id().to_bytes()).toString('hex'); const outputIndex = txBody.inputs().get(i).index(); const utxo = inputsToOrder.find(uu => uu.txHash === txid && uu.outputIndex === outputIndex); if (!utxo) { throw new Error('Failed to order the utxos to match the order of inputs in constructed tx. THIS SHOULD NOT HAPPEN'); } orderedInputs.push(utxo); } return orderedInputs; };