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 SlpWallet from 'minimal-slp-wallet'; | import SlpWallet from 'minimal-slp-wallet'; | ||||
import { toSmallestDenomination } from '@utils/cashMethods'; | import { | ||||
toSmallestDenomination, | |||||
batchArray, | |||||
flattenBatchedHydratedUtxos, | |||||
} 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, | ||||
NETWORK_ERROR: 1, | NETWORK_ERROR: 1, | ||||
INSUFFICIENT_PRIORITY: 66, // ~insufficient fee | INSUFFICIENT_PRIORITY: 66, // ~insufficient fee | ||||
DOUBLE_SPENDING: 18, | DOUBLE_SPENDING: 18, | ||||
MAX_UNCONFIRMED_TXS: 64, | MAX_UNCONFIRMED_TXS: 64, | ||||
▲ Show 20 Lines • Show All 285 Lines • ▼ Show 20 Lines | const getUtxos = async (BCH, addresses) => { | ||||
return utxosResponse.utxos; | return utxosResponse.utxos; | ||||
} catch (err) { | } catch (err) { | ||||
console.log(`Error in BCH.Electrumx.utxo(addresses):`); | console.log(`Error in BCH.Electrumx.utxo(addresses):`); | ||||
return err; | return err; | ||||
} | } | ||||
}; | }; | ||||
const getHydratedUtxoDetails = async (BCH, utxos) => { | const getHydratedUtxoDetails = async (BCH, utxos) => { | ||||
const hydrateUtxosPromises = []; | |||||
for (let i = 0; i < utxos.length; i += 1) { | |||||
let thisAddress = utxos[i].address; | |||||
let theseUtxos = utxos[i].utxos; | |||||
const batchedUtxos = batchArray( | |||||
theseUtxos, | |||||
currency.hydrateUtxoBatchSize, | |||||
); | |||||
// Iterate over each utxo in this address field | |||||
for (let j = 0; j < batchedUtxos.length; j += 1) { | |||||
const utxoSetForThisPromise = [ | |||||
{ utxos: batchedUtxos[j], address: thisAddress }, | |||||
]; | |||||
const thisPromise = BCH.SLP.Utils.hydrateUtxos( | |||||
utxoSetForThisPromise, | |||||
); | |||||
hydrateUtxosPromises.push(thisPromise); | |||||
} | |||||
} | |||||
let hydratedUtxoDetails; | let hydratedUtxoDetails; | ||||
try { | try { | ||||
hydratedUtxoDetails = await BCH.SLP.Utils.hydrateUtxos(utxos); | hydratedUtxoDetails = await Promise.all(hydrateUtxosPromises); | ||||
return hydratedUtxoDetails; | const flattenedBatchedHydratedUtxos = flattenBatchedHydratedUtxos( | ||||
} catch (err) { | hydratedUtxoDetails, | ||||
console.log( | |||||
`Error in BCH.SLP.Utils.hydrateUtxos(utxosResponse.utxos)`, | |||||
); | ); | ||||
return flattenedBatchedHydratedUtxos; | |||||
} catch (err) { | |||||
console.log(`Error in Promise.all(hydrateUtxosPromises)`); | |||||
console.log(err); | console.log(err); | ||||
return err; | return err; | ||||
} | } | ||||
}; | }; | ||||
const getSlpBalancesAndUtxos = hydratedUtxoDetails => { | const getSlpBalancesAndUtxos = hydratedUtxoDetails => { | ||||
const hydratedUtxos = []; | const hydratedUtxos = []; | ||||
for (let i = 0; i < hydratedUtxoDetails.slpUtxos.length; i += 1) { | for (let i = 0; i < hydratedUtxoDetails.slpUtxos.length; i += 1) { | ||||
▲ Show 20 Lines • Show All 506 Lines • Show Last 20 Lines |