Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
Show First 20 Lines • Show All 719 Lines • ▼ Show 20 Lines | ) => { | ||||
// Send dust transaction representing tokens being sent. | // Send dust transaction representing tokens being sent. | ||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
BCH.SLP.Address.toLegacyAddress(tokenReceiverAddress), | BCH.SLP.Address.toLegacyAddress(tokenReceiverAddress), | ||||
546, | 546, | ||||
); | ); | ||||
// Return any token change back to the sender. | // Return any token change back to the sender. | ||||
if (slpSendObj.outputs > 1) { | if (slpSendObj.outputs > 1) { | ||||
// Try to send this to Path1899 to move all utxos off legacy addresses | // Change goes back to where slp utxo came from | ||||
if (wallet.Path1899.legacyAddress) { | |||||
transactionBuilder.addOutput( | |||||
wallet.Path1899.legacyAddress, | |||||
546, | |||||
); | |||||
} else { | |||||
// If you can't, send it back from whence it came | |||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
BCH.SLP.Address.toLegacyAddress( | BCH.SLP.Address.toLegacyAddress( | ||||
tokenUtxosBeingSpent[0].address, | tokenUtxosBeingSpent[0].address, | ||||
), | ), | ||||
546, | 546, | ||||
); | ); | ||||
} | } | ||||
} | |||||
// get byte count to calculate fee. paying 1 sat | // get byte count to calculate fee. paying 1 sat | ||||
// Note: This may not be totally accurate. Just guessing on the byteCount size. | // Note: This may not be totally accurate. Just guessing on the byteCount size. | ||||
const txFee = calcFee( | const txFee = calcFee( | ||||
BCH, | BCH, | ||||
tokenUtxosBeingSpent, | tokenUtxosBeingSpent, | ||||
5, | 5, | ||||
1.1 * currency.defaultFee, | 1.1 * currency.defaultFee, | ||||
); | ); | ||||
// amount to send back to the sending address. It's the original amount - 1 sat/byte for tx size | // amount to send back to the sending address. It's the original amount - 1 sat/byte for tx size | ||||
const remainder = originalAmount - txFee - 546 * 2; | const remainder = originalAmount - txFee - 546 * 2; | ||||
if (remainder < 1) { | if (remainder < 1) { | ||||
throw new Error('Selected UTXO does not have enough satoshis'); | throw new Error('Selected UTXO does not have enough satoshis'); | ||||
} | } | ||||
// Last output: send the BCH change back to the wallet. | // Last output: send the BCH change back to the wallet. | ||||
// If Path1899, send it to Path1899 address | |||||
if (wallet.Path1899.legacyAddress) { | // Send it back from whence it came | ||||
transactionBuilder.addOutput( | |||||
wallet.Path1899.legacyAddress, | |||||
remainder, | |||||
); | |||||
} else { | |||||
// Otherwise send it back from whence it came | |||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
BCH.Address.toLegacyAddress(largestBchUtxo.address), | BCH.Address.toLegacyAddress(largestBchUtxo.address), | ||||
remainder, | remainder, | ||||
); | ); | ||||
} | |||||
// Sign the transaction with the private key for the BCH UTXO paying the fees. | // Sign the transaction with the private key for the BCH UTXO paying the fees. | ||||
let redeemScript; | let redeemScript; | ||||
transactionBuilder.sign( | transactionBuilder.sign( | ||||
0, | 0, | ||||
bchECPair, | bchECPair, | ||||
redeemScript, | redeemScript, | ||||
transactionBuilder.hashTypes.SIGHASH_ALL, | transactionBuilder.hashTypes.SIGHASH_ALL, | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | ) => { | ||||
new BigNumber( | new BigNumber( | ||||
fromSmallestDenomination(currency.dustSats).toString(), | 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 inputUtxos = []; | const inputUtxos = []; | ||||
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'); | ||||
Show All 21 Lines | ) => { | ||||
? calcFee(BCH, inputUtxos, 3, feeInSatsPerByte) | ? calcFee(BCH, inputUtxos, 3, feeInSatsPerByte) | ||||
: calcFee(BCH, inputUtxos, 2, feeInSatsPerByte); | : calcFee(BCH, inputUtxos, 2, feeInSatsPerByte); | ||||
if (originalAmount.minus(satoshisToSend).minus(txFee).gte(0)) { | if (originalAmount.minus(satoshisToSend).minus(txFee).gte(0)) { | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
// Get change address from sending utxos | |||||
// fall back to what is stored in wallet | |||||
let REMAINDER_ADDR; | |||||
// Validate address | |||||
let isValidChangeAddress; | |||||
try { | |||||
REMAINDER_ADDR = inputUtxos[0].address; | |||||
isValidChangeAddress = BCH.Address.isCashAddress( | |||||
REMAINDER_ADDR, | |||||
); | |||||
} catch (err) { | |||||
isValidChangeAddress = false; | |||||
} | |||||
if (!isValidChangeAddress) { | |||||
REMAINDER_ADDR = wallet.Path1899.cashAddress; | |||||
} | |||||
// amount to send back to the remainder address. | // amount to send back to the remainder address. | ||||
const remainder = originalAmount.minus(satoshisToSend).minus(txFee); | const remainder = originalAmount.minus(satoshisToSend).minus(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; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 100 Lines • Show Last 20 Lines |