Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
import BigNumber from 'bignumber.js'; | import BigNumber from 'bignumber.js'; | ||||
import { currency } from '@components/Common/Ticker'; | import { currency } from '@components/Common/Ticker'; | ||||
import { | import { | ||||
toSmallestDenomination, | toSmallestDenomination, | ||||
fromSmallestDenomination, | |||||
batchArray, | batchArray, | ||||
flattenBatchedHydratedUtxos, | flattenBatchedHydratedUtxos, | ||||
isValidStoredWallet, | isValidStoredWallet, | ||||
} from '@utils/cashMethods'; | } from '@utils/cashMethods'; | ||||
export default function useBCH() { | export default function useBCH() { | ||||
const SEND_BCH_ERRORS = { | const SEND_BCH_ERRORS = { | ||||
INSUFFICIENT_FUNDS: 0, | INSUFFICIENT_FUNDS: 0, | ||||
▲ Show 20 Lines • Show All 498 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(currency.dust); | 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(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); | ||||
Show All 26 Lines | const createToken = async (BCH, wallet, feeInSatsPerByte, configObj) => { | ||||
// add output w/ address and amount to send | // add output w/ address and amount to send | ||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
CREATION_ADDR, | CREATION_ADDR, | ||||
parseInt(toSmallestDenomination(tokenOutputDust)), | parseInt(toSmallestDenomination(tokenOutputDust)), | ||||
); | ); | ||||
// Send change to own address | // Send change to own address | ||||
if ( | if (remainder.gte(new BigNumber(currency.dustSats))) { | ||||
remainder.gte( | |||||
toSmallestDenomination(new BigNumber(currency.dust)), | |||||
) | |||||
) { | |||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
CREATION_ADDR, | CREATION_ADDR, | ||||
parseInt(remainder), | parseInt(remainder), | ||||
); | ); | ||||
} | } | ||||
// Sign the transactions with the HD node. | // Sign the transactions with the HD node. | ||||
for (let i = 0; i < inputUtxos.length; i++) { | for (let i = 0; i < inputUtxos.length; i++) { | ||||
▲ Show 20 Lines • Show All 251 Lines • ▼ Show 20 Lines | ) => { | ||||
try { | try { | ||||
if (!sendAmount) { | if (!sendAmount) { | ||||
return null; | return null; | ||||
} | } | ||||
const value = new BigNumber(sendAmount); | const value = new BigNumber(sendAmount); | ||||
// If user is attempting to send less than minimum accepted by the backend | // If user is attempting to send less than minimum accepted by the backend | ||||
if (value.lt(new BigNumber(currency.dust))) { | if ( | ||||
value.lt( | |||||
new BigNumber( | |||||
fromSmallestDenomination(currency.dustSats).toString(), | |||||
), | |||||
) | |||||
) { | |||||
// Throw the same error given by the backend attempting to broadcast such a tx | // Throw the same error given by the backend attempting to broadcast such a tx | ||||
throw new Error('dust'); | throw new Error('dust'); | ||||
} | } | ||||
const REMAINDER_ADDR = wallet.Path1899.cashAddress; | const REMAINDER_ADDR = wallet.Path1899.cashAddress; | ||||
const inputUtxos = []; | const inputUtxos = []; | ||||
let transactionBuilder; | let transactionBuilder; | ||||
// instance of transaction builder | // instance of transaction builder | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | ) => { | ||||
} | } | ||||
// add output w/ address and amount to send | // add output w/ address and amount to send | ||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
BCH.Address.toCashAddress(destinationAddress), | BCH.Address.toCashAddress(destinationAddress), | ||||
parseInt(toSmallestDenomination(value)), | parseInt(toSmallestDenomination(value)), | ||||
); | ); | ||||
if ( | if (remainder.gte(new BigNumber(currency.dustSats))) { | ||||
remainder.gte( | |||||
toSmallestDenomination(new BigNumber(currency.dust)), | |||||
) | |||||
) { | |||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
REMAINDER_ADDR, | REMAINDER_ADDR, | ||||
parseInt(remainder), | parseInt(remainder), | ||||
); | ); | ||||
} | } | ||||
// Sign the transactions with the HD node. | // Sign the transactions with the HD node. | ||||
for (let i = 0; i < inputUtxos.length; i++) { | for (let i = 0; i < inputUtxos.length; i++) { | ||||
▲ Show 20 Lines • Show All 79 Lines • Show Last 20 Lines |