Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
Show First 20 Lines • Show All 281 Lines • ▼ Show 20 Lines | export default function useBCH() { | ||||
console.log( | console.log( | ||||
`tokenAmountBeingSentToAddress.toString()`, | `tokenAmountBeingSentToAddress.toString()`, | ||||
tokenAmountBeingSentToAddress.toString() | tokenAmountBeingSentToAddress.toString() | ||||
); | ); | ||||
*/ | */ | ||||
let tokenUtxosBeingSpent = []; | let tokenUtxosBeingSpent = []; | ||||
for (let i = 0; i < tokenUtxos.length; i++) { | for (let i = 0; i < tokenUtxos.length; i++) { | ||||
finalTokenAmountSent = finalTokenAmountSent.plus( | finalTokenAmountSent = finalTokenAmountSent.plus( | ||||
new BigNumber(tokenUtxos[i].tokenQty).div( | new BigNumber(tokenUtxos[i].tokenQty), | ||||
Math.pow(10, tokenUtxos[i].decimals), | |||||
), | |||||
); | ); | ||||
transactionBuilder.addInput( | transactionBuilder.addInput( | ||||
tokenUtxos[i].tx_hash, | tokenUtxos[i].tx_hash, | ||||
tokenUtxos[i].tx_pos, | tokenUtxos[i].tx_pos, | ||||
); | ); | ||||
tokenUtxosBeingSpent.push(tokenUtxos[i]); | tokenUtxosBeingSpent.push(tokenUtxos[i]); | ||||
if (tokenAmountBeingSentToAddress.lte(finalTokenAmountSent)) { | if (tokenAmountBeingSentToAddress.lte(finalTokenAmountSent)) { | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
// Debug logging | |||||
console.log( | |||||
`string finalTokenAmountSent`, | |||||
finalTokenAmountSent.toString(), | |||||
); | |||||
console.log( | |||||
`string tokenAmount`, | |||||
tokenAmountBeingSentToAddress.toString(), | |||||
); | |||||
// Just in case -- make sure you are not sending more than your balance | |||||
if (finalTokenAmountSent.lt(tokenAmountBeingSentToAddress)) { | |||||
console.log(`Error: Trying to send more than your balance`); | |||||
throw new Error( | |||||
'You are attempting to send more than your balance!', | |||||
); | |||||
} | |||||
// Debug logging | |||||
if (finalTokenAmountSent.eq(tokenAmountBeingSentToAddress)) { | |||||
console.log(`Sending your full token balance`); | |||||
} | |||||
// Run a test function to mock the outputs generated by BCH.SLP.TokenType1.generateSendOpReturn below | // Run a test function to mock the outputs generated by BCH.SLP.TokenType1.generateSendOpReturn below | ||||
slpDebug( | slpDebug( | ||||
tokenUtxosBeingSpent, | tokenUtxosBeingSpent, | ||||
tokenAmountBeingSentToAddress.toString(), | tokenAmountBeingSentToAddress.toString(), | ||||
); | ); | ||||
// Generate the OP_RETURN code. | // Generate the OP_RETURN code. | ||||
console.log(`Debug output`); | console.log(`Debug output`); | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | */ | ||||
const tx = transactionBuilder.build(); | const tx = transactionBuilder.build(); | ||||
// output rawhex | // output rawhex | ||||
const hex = tx.toHex(); | const hex = tx.toHex(); | ||||
// console.log(`Transaction raw hex: `, hex); | // console.log(`Transaction raw hex: `, hex); | ||||
// END transaction construction. | // END transaction construction. | ||||
// Broadcast transaction to the network | // Broadcast transaction to the network if debugSlpa mode is false | ||||
const debugSlpa = false; | |||||
if (debugSlpa) { | |||||
console.log(`RawTx Hex`, hex); | |||||
return hex; | |||||
} else { | |||||
const txidStr = await BCH.RawTransactions.sendRawTransaction([hex]); | const txidStr = await BCH.RawTransactions.sendRawTransaction([hex]); | ||||
if (txidStr && txidStr[0]) { | if (txidStr && txidStr[0]) { | ||||
console.log(`${currency.tokenTicker} txid`, txidStr[0]); | console.log(`${currency.tokenTicker} txid`, txidStr[0]); | ||||
} | } | ||||
let link; | let link; | ||||
if (process.env.REACT_APP_NETWORK === `mainnet`) { | if (process.env.REACT_APP_NETWORK === `mainnet`) { | ||||
link = `${currency.blockExplorerUrl}/tx/${txidStr}`; | link = `${currency.blockExplorerUrl}/tx/${txidStr}`; | ||||
} else { | } else { | ||||
link = `${currency.blockExplorerUrlTestnet}/tx/${txidStr}`; | link = `${currency.blockExplorerUrlTestnet}/tx/${txidStr}`; | ||||
} | } | ||||
//console.log(`link`, link); | //console.log(`link`, link); | ||||
return link; | return link; | ||||
} | |||||
}; | }; | ||||
const slpDebug = (tokenUtxos, sendQty) => { | const slpDebug = (tokenUtxos, sendQty) => { | ||||
console.log(`slpDebug test called with`); | console.log(`slpDebug test called with`); | ||||
console.log(`tokenUtxos`, tokenUtxos); | console.log(`tokenUtxos`, tokenUtxos); | ||||
console.log(`sendQty`, sendQty); | console.log(`sendQty`, sendQty); | ||||
try { | try { | ||||
//const tokenId = tokenUtxos[0].tokenId; | //const tokenId = tokenUtxos[0].tokenId; | ||||
▲ Show 20 Lines • Show All 284 Lines • Show Last 20 Lines |