Created
December 2, 2021 20:22
-
-
Save drewhaines/781cb14e60efdac9ac03e231cae1e5f0 to your computer and use it in GitHub Desktop.
Send multiple assets (NFTs) via cardanocli-js
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
// send multiple assets at once | |
// ************ run me once ************ | |
const CardanocliJs = require("/Users/drewhaines/sites/cardanocli-js/index") | |
const socketPath = | |
"/Users/drewhaines/Library/Application Support/Daedalus Mainnet/cardano-node.socket" | |
const testnet_socketPath = | |
"/Users/drewhaines/Library/Application Support/Daedalus Testnet/cardano-node.socket" | |
const shelleyGenesisPath = | |
"/Users/drewhaines/sites/cardano-node-1.29.0-macos/configuration/cardano/mainnet-shelley-genesis.json" | |
const cliPath = "/Users/drewhaines/sites/cardano-node-1.29.0-macos/cardano-cli" | |
const cardanocliJs = new CardanocliJs({ | |
shelleyGenesisPath, | |
cliPath, | |
socketPath, | |
}) | |
console.log(cardanocliJs.queryTip()) | |
let assets = [] | |
const mintScript = require("./mint-policy.json") | |
const POLICY_ID = cardanocliJs.transactionPolicyid(mintScript) | |
const sender = cardanocliJs.wallet("GoatTribe1") | |
let quantities = {} | |
let balance = sender.balance().value | |
let sendAmount = 1 | |
let minAda = 1.5 | |
// if you get UtxoFailure (OutputTooSmallUTxO | |
// increase the minAda value | |
// ************ run me once ************ | |
function sendAssets({ receiver, assets, amounts, minAda }) { | |
const txOut_value_sender = assets.reduce( | |
(result, asset) => { | |
const ASSET_ID = POLICY_ID + "." + asset | |
const assetAmount = amounts[asset] | |
if (assetAmount) { | |
result[ASSET_ID] = assetAmount - sendAmount | |
} | |
return result | |
}, | |
{ | |
...sender.balance().value, | |
} | |
) | |
const txOut_value_receiver = assets.reduce((result, asset) => { | |
const ASSET_ID = POLICY_ID + "." + asset | |
result[ASSET_ID] = sendAmount | |
return result | |
}, {}) | |
const MIN_ADA = minAda || 1.5 | |
const txInfo = { | |
txIn: cardanocliJs.queryUtxo(sender.paymentAddr), | |
txOut: [ | |
{ | |
address: sender.paymentAddr, | |
value: { | |
...txOut_value_sender, | |
lovelace: | |
txOut_value_sender.lovelace - cardanocliJs.toLovelace(MIN_ADA), | |
}, | |
}, | |
{ | |
address: receiver, | |
value: { | |
lovelace: cardanocliJs.toLovelace(MIN_ADA), | |
...txOut_value_receiver, | |
}, | |
}, | |
], | |
} | |
const raw = cardanocliJs.transactionBuildRaw(txInfo) | |
const fee = cardanocliJs.transactionCalculateMinFee({ | |
...txInfo, | |
txBody: raw, | |
witnessCount: 1, | |
}) | |
txInfo.txOut[0].value.lovelace -= fee | |
const tx = cardanocliJs.transactionBuildRaw({ ...txInfo, fee }) | |
const txSigned = cardanocliJs.transactionSign({ | |
txBody: tx, | |
signingKeys: [sender.payment.skey], | |
}) | |
const txHash = cardanocliJs.transactionSubmit(txSigned) | |
console.log(txHash) | |
} | |
// ************ set assets and rerun function ************ | |
assets = ["GOATtribePromo18", "GOATtribePromo23", "GOATtribePromo24"] | |
balance = sender.balance().value | |
quantities = {} | |
for (const [key, value] of Object.entries(balance)) { | |
const name = key.replace( | |
"e203ad09dada30d3c3073a4ffe08140dfecc1bca5fd73887ac4bc860.", | |
"" | |
) | |
quantities[name] = value | |
} | |
console.log(quantities) | |
minAda = 1.7 | |
sendAssets({ | |
receiver: | |
"addr1q85khjtrn255q8d9kjdysl9kn9kzumskdnerqf77evz6l5jmlcy7y7703za4cy7zh789jnjffstkgsgk5vhr5ks6vj5qejsare", | |
assets, | |
amounts: quantities, | |
minAda, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a simple script I use to send out multiple assets via cardanocli-js
I normally open a terminal, run "node", then copy and paste from the script as needed. This allows me to easily send multiple NFTs to various people from the mint wallet.
Note I run Daedalus and use it's socket to submit transactions.
Your paths and code should be slightly different, but hopefully, it's a helpful example.