-
-
Save CherryDT/2dfa338664883843800d42860a016739 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CardanoWasm = require('@emurgo/cardano-serialization-lib-nodejs') | |
// This isn't the actual private key for those UTXOs | |
const privateKeyHex = '4841ccc9e76666ddfebe6b5648013a60d9989b4445366c6307b071d9cd4fea54fecbec6fdf120a58a520f7b00efbdeef283d599a375914550a2f30cedb7b419a' | |
const privateKey = CardanoWasm.PrivateKey.from_extended_bytes(Buffer.from(privateKeyHex, 'hex')) | |
const publicKey = privateKey.to_public() | |
const publicKeyHash = publicKey.hash() | |
const enterpriseAddress = CardanoWasm.EnterpriseAddress.new(CardanoWasm.NetworkInfo.mainnet().network_id(), CardanoWasm.StakeCredential.from_keyhash(publicKeyHash)) | |
const address = enterpriseAddress.to_address() | |
const bn = n => CardanoWasm.BigNum.from_str(BigInt(Number(n).toFixed(0)).toString()) | |
// These normally come from cardano-graphql via { cardano { currentEpoch { protocolParams { ... } } } } | |
const minFeeA = bn(44) | |
const minFeeB = bn(155381) | |
const minUTxOValue = bn(1000000) | |
const poolDeposit = bn(500000000) | |
const keyDeposit = bn(2000000) | |
const txBuilder = CardanoWasm.TransactionBuilder.new(CardanoWasm.LinearFee.new(minFeeA, minFeeB), minUTxOValue, poolDeposit, keyDeposit) | |
console.log('Initial min. fee:', txBuilder.min_fee().to_str()) | |
const adaTargetAddressObject = CardanoWasm.Address.from_bech32('addr1q829m7me6rwpta7kvg8m82ez59nmefhqy0gh259wwrwjv76tw3wvum3mh0ck4vhmppq0yvltqs45tgj36x5mxlnca8vs5pn8ur') | |
// Output: 5 ADA | |
txBuilder.add_output(CardanoWasm.TransactionOutput.new(adaTargetAddressObject, CardanoWasm.Value.new(bn(5000000)))) | |
console.log('Min. fee after output added:', txBuilder.min_fee().to_str()) | |
const addInput = (txHash, index, value) => { | |
const txHashObject = CardanoWasm.TransactionHash.from_bytes(Buffer.from(txHash, 'hex')) | |
txBuilder.add_key_input(publicKeyHash, CardanoWasm.TransactionInput.new(txHashObject, index), CardanoWasm.Value.new(bn(value))) | |
} | |
// First input: 1 ADA | |
addInput('016da679e8757a0ba17e4de79fd3e7677fdfb38d9853d5a6fc6106498e3c8d6b', 0, 1000000) | |
console.log('Min. fee after 1st input added:', txBuilder.min_fee().to_str()) | |
try { | |
txBuilder.add_change_if_needed(address) | |
} catch (e) { | |
if (e === 'Insufficient input in transaction') { | |
console.log('Insufficient input as expected') | |
} else { | |
throw e | |
} | |
} | |
// Second input: 2.5 ADA | |
addInput('731b9db933db3d2ca5473308873dd17dd325613d413322e6f3ac7f68ffa69659', 0, 2500000) | |
console.log('Min. fee after 2nd input added:', txBuilder.min_fee().to_str()) | |
try { | |
txBuilder.add_change_if_needed(address) | |
} catch (e) { | |
if (e === 'Insufficient input in transaction') { | |
console.log('Insufficient input as expected') | |
} else { | |
throw e | |
} | |
} | |
// Third input: 11.111 ADA (yes I know this alone would suffice as sole input, but it should still work with the other inputs too, no?) | |
addInput('36440d14605cc9c718e842353dff013a8024c93f8801dd56694908086c5a8cdf', 0, 11111000) | |
console.log('Min. fee after 3rd input added:', txBuilder.min_fee().to_str()) | |
txBuilder.add_change_if_needed(address) | |
console.log('Min. fee after change added:', txBuilder.min_fee().to_str()) | |
console.log('Final fee:', txBuilder.get_fee_if_set().to_str()) | |
// Building TX | |
const txBody = txBuilder.build() | |
const txHash = CardanoWasm.hash_transaction(txBody) | |
const witnesses = CardanoWasm.TransactionWitnessSet.new() | |
const vkeyWitnesses = CardanoWasm.Vkeywitnesses.new() | |
vkeyWitnesses.add(CardanoWasm.make_vkey_witness(txHash, privateKey)) | |
witnesses.set_vkeys(vkeyWitnesses) | |
const transaction = CardanoWasm.Transaction.new(txBody, witnesses, undefined /* transaction metadata */) | |
const hex = Buffer.from(transaction.to_bytes()).toString('hex') | |
console.log('ADA transaction hex:', hex) | |
// This time the error is MissingVKeyWitnessesUTXOW because this example doesn't include the real private key. | |
// But the fee amounts reported are the exact same as with the real private key (with which I got FeeTooSmallUTxO). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment