Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
Show First 20 Lines • Show All 513 Lines • ▼ Show 20 Lines | const createToken = async (BCH, wallet, feeInSatsPerByte, configObj) => { | ||||
let transactionBuilder; | let transactionBuilder; | ||||
// instance of transaction builder | // instance of transaction builder | ||||
if (process.env.REACT_APP_NETWORK === `mainnet`) | if (process.env.REACT_APP_NETWORK === `mainnet`) | ||||
transactionBuilder = new BCH.TransactionBuilder(); | transactionBuilder = new BCH.TransactionBuilder(); | ||||
else transactionBuilder = new BCH.TransactionBuilder('testnet'); | else transactionBuilder = new BCH.TransactionBuilder('testnet'); | ||||
let originalAmount = new BigNumber(0); | let originalAmount = new BigNumber(0); | ||||
const tokenOutputDust = new BigNumber( | |||||
fromSmallestDenomination(currency.dustSats).toString(), | |||||
); | |||||
let txFee = 0; | let txFee = 0; | ||||
for (let i = 0; i < utxos.length; i++) { | for (let i = 0; i < utxos.length; i++) { | ||||
const utxo = utxos[i]; | const utxo = utxos[i]; | ||||
originalAmount = originalAmount.plus(utxo.value); | originalAmount = originalAmount.plus(new BigNumber(utxo.value)); | ||||
const vout = utxo.vout; | const vout = utxo.vout; | ||||
const txid = utxo.txid; | const txid = utxo.txid; | ||||
// add input with txid and index of vout | // add input with txid and index of vout | ||||
transactionBuilder.addInput(txid, vout); | transactionBuilder.addInput(txid, vout); | ||||
inputUtxos.push(utxo); | inputUtxos.push(utxo); | ||||
txFee = calcFee(BCH, inputUtxos, 3, feeInSatsPerByte); | txFee = calcFee(BCH, inputUtxos, 3, feeInSatsPerByte); | ||||
if (originalAmount.minus(tokenOutputDust).minus(txFee).gte(0)) { | if ( | ||||
originalAmount | |||||
.minus(new BigNumber(currency.dustSats)) | |||||
.minus(new BigNumber(txFee)) | |||||
.gte(0) | |||||
) { | |||||
break; | break; | ||||
} | } | ||||
} | } | ||||
// amount to send back to the remainder address. | // amount to send back to the remainder address. | ||||
const remainder = originalAmount | const remainder = originalAmount | ||||
.minus(tokenOutputDust) | .minus(new BigNumber(currency.dustSats)) | ||||
.minus(txFee); | .minus(new BigNumber(txFee)); | ||||
if (remainder.lt(0)) { | if (remainder.lt(0)) { | ||||
const error = new Error(`Insufficient funds`); | const error = new Error(`Insufficient funds`); | ||||
error.code = SEND_BCH_ERRORS.INSUFFICIENT_FUNDS; | error.code = SEND_BCH_ERRORS.INSUFFICIENT_FUNDS; | ||||
throw error; | throw error; | ||||
} | } | ||||
// Generate the OP_RETURN entry for an SLP GENESIS transaction. | // Generate the OP_RETURN entry for an SLP GENESIS transaction. | ||||
const script = BCH.SLP.TokenType1.generateGenesisOpReturn( | const script = BCH.SLP.TokenType1.generateGenesisOpReturn( | ||||
configObj, | configObj, | ||||
); | ); | ||||
// OP_RETURN needs to be the first output in the transaction. | // OP_RETURN needs to be the first output in the transaction. | ||||
transactionBuilder.addOutput(script, 0); | transactionBuilder.addOutput(script, 0); | ||||
// add output w/ address and amount to send | // add output w/ address and amount to send | ||||
transactionBuilder.addOutput( | transactionBuilder.addOutput(CREATION_ADDR, currency.dustSats); | ||||
CREATION_ADDR, | |||||
parseInt(toSmallestDenomination(tokenOutputDust)), | |||||
); | |||||
// Send change to own address | // Send change to own address | ||||
if (remainder.gte(new BigNumber(currency.dustSats))) { | if (remainder.gte(new BigNumber(currency.dustSats))) { | ||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
CREATION_ADDR, | CREATION_ADDR, | ||||
parseInt(remainder), | parseInt(remainder), | ||||
); | ); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 434 Lines • Show Last 20 Lines |