Skip to content

Instantly share code, notes, and snippets.

@zh
Last active March 25, 2021 04:12
Show Gist options
  • Save zh/c5c64240205eb5b3980a8ca7981582d7 to your computer and use it in GitHub Desktop.
Save zh/c5c64240205eb5b3980a8ca7981582d7 to your computer and use it in GitHub Desktop.
const fromSlpAddress = 'simpleledger:q... put your wallet SLP address here ...'
const fromWIF = 'K... put your wallet private key here ...'
const childNFT = {
group: '--- put your group TXID here',
name: 'Some NFT Token Name',
ticker: 'NFT.FROG',
uri: 'Qm... put your Pinata hash here'
}
// npm install slpjs
const BITBOX = require('bitbox-sdk').BITBOX
const { GrpcClient } = require('grpc-bchrpc-node')
const slpjs = require('slpjs')
const BigNumber = require('bignumber.js')
const bchaddr = require('bchaddrjs-slp')
const bitbox = new BITBOX({ restURL: 'https://bchd.fountainhead.cash' })
const client = new GrpcClient({ url: 'bchd.fountainhead.cash' })
const validator = new slpjs.BchdValidator(client, console)
const bchdNetwork = new slpjs.BchdNetwork({
BITBOX: bitbox,
client,
validator
})
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
async function sendChildNFT (from, fromWif, to, config) {
try {
const tokenId = config.group
let balances = await bchdNetwork.getAllSlpBalancesAndUtxos(from)
let bchUtxos = balances.nonSlpUtxos
let tokenBalance = new BigNumber(balances.slpTokenBalances[tokenId])
if (bchUtxos.length === 0 || !tokenBalance.isGreaterThan(0) || !balances.slpTokenUtxos[tokenId]) {
throw Error('BCH balance in faucet wallet is 0.')
}
bchUtxos.forEach((j) => j.wif = fromWif)
let tokenUtxos = balances.slpTokenUtxos[tokenId]
tokenUtxos.forEach((j) => j.wif = fromWif)
const sendCost = bchdNetwork.slp.calculateSendCost(60, bchUtxos.length + tokenUtxos.length, 3, from) - 546
console.log('Token input quantity: ', tokenBalance.toFixed())
console.log('BCH (satoshis_available_bch): ', balances.satoshis_available_bch)
console.log('Estimated send cost (satoshis): ', sendCost)
console.log("BCH balance:", balances.satoshis_available_bch)
let inputUtxos = [...bchUtxos, ...tokenUtxos]
const burnTxHex = await bchdNetwork.txnHelpers.simpleTokenSend({
tokenId,
sendAmounts: [ new BigNumber(1) ],
inputUtxos,
tokenReceiverAddresses: [ from ],
changeReceiverAddress: from
})
const burnTxId = await bchdNetwork.sendTx(burnTxHex)
console.log(`burn tx: ${burnTxId}`)
console.log(`wait sync...`)
await sleep(3000)
let burnUtxo = null
balances = await bchdNetwork.getAllSlpBalancesAndUtxos(from)
balances.slpTokenUtxos[tokenId].forEach((txo) => {
if (!burnUtxo && txo.slpUtxoJudgementAmount.isEqualTo(1)) {
burnUtxo = txo
}
})
if (!burnUtxo) throw new Error('No token ready for burn')
const name = config.name || 'NFT Test Token'
const ticker = config.ticker || 'NFTCHLD'
const documentUri = config.uri || null
const documentHash = config.hash || null
inputUtxos = [burnUtxo, ...balances.nonSlpUtxos]
inputUtxos.forEach((j) => j.wif = fromWif)
const genesisTxHex = bchdNetwork.txnHelpers.simpleNFT1ChildGenesis({
nft1GroupId: tokenId,
tokenName: name,
tokenTicker: ticker,
documentUri,
documentHash,
tokenReceiverAddress: to,
bchChangeReceiverAddress: bchaddr.toCashAddress(from),
inputUtxos
})
const burn = {
tokenId: Buffer.from(tokenId, 'hex'),
tokenType: 129,
amount: '1',
outpointHash: Buffer.from(Buffer.from(burnUtxo.txid, 'hex').reverse()),
outpointVout: burnUtxo.vout
}
const genesisTxId = await bchdNetwork.sendTx(genesisTxHex, [burn])
console.log('NFT1 Child GENESIS txn complete: ', genesisTxId)
} catch (error) {
console.error('error in sendChildNFT: ', error)
}
}
sendChildNFT(fromSlpAddress, fromWIF, fromSlpAddress, childNFT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment