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(), | |||||
); | |||||
deadalnix: Remove | |||||
// 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; | |||||
deadalnixUnsubmitted Not Done Inline ActionsThis is dead code, also a typical instance of the if/return/else pattern. deadalnix: This is dead code, also a typical instance of the if/return/else pattern. | |||||
} 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 All 17 Lines | const slpDebug = (tokenUtxos, sendQty) => { | ||||
// Calculate total amount of tokens using Big Number throughout | // Calculate total amount of tokens using Big Number throughout | ||||
/* | /* | ||||
let totalTokens = new BigNumber(0); | let totalTokens = new BigNumber(0); | ||||
for (let i = 0; i < tokenUtxos.length; i++) { | for (let i = 0; i < tokenUtxos.length; i++) { | ||||
console.log(`tokenQty normal`, tokenUtxos[i].tokenQty); | console.log(`tokenQty normal`, tokenUtxos[i].tokenQty); | ||||
const thisTokenQty = new BigNumber(tokenUtxos[i].tokenQty); | const thisTokenQty = new BigNumber(tokenUtxos[i].tokenQty); | ||||
totalTokens.plus(thisTokenQty); | totalTokens.plus(thisTokenQty); | ||||
} | } | ||||
totalTokens.times(10 ** decimals); | totalTokens.times(10 ** decimals); | ||||
alcipirUnsubmitted Not Done Inline ActionsI didn't understand this commented code, what is it for? Since we have git can we get rid of it and keep track of these TODOs elsewhere? alcipir: I didn't understand this commented code, what is it for? Since we have git can we get rid of it… | |||||
*/ | */ | ||||
let totalTokens = tokenUtxos.reduce((tot, txo) => { | let totalTokens = tokenUtxos.reduce((tot, txo) => { | ||||
return tot.plus( | return tot.plus( | ||||
new BigNumber(txo.tokenQty).times(10 ** decimals), | new BigNumber(txo.tokenQty).times(10 ** decimals), | ||||
); | ); | ||||
}, new BigNumber(0)); | }, new BigNumber(0)); | ||||
console.log(`totalTokens`, totalTokens); | console.log(`totalTokens`, totalTokens); | ||||
▲ Show 20 Lines • Show All 249 Lines • Show Last 20 Lines |
Remove