Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/utils/cashMethods.js
Show First 20 Lines • Show All 853 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
export const loadStoredWallet = walletStateFromStorage => { | export const loadStoredWallet = walletStateFromStorage => { | ||||
// Accept cached tokens array that does not save BigNumber type of BigNumbers | // Accept cached tokens array that does not save BigNumber type of BigNumbers | ||||
// Return array with BigNumbers converted | // Return array with BigNumbers converted | ||||
// See BigNumber.js api for how to create a BigNumber object from an object | // See BigNumber.js api for how to create a BigNumber object from an object | ||||
// https://mikemcl.github.io/bignumber.js/ | // https://mikemcl.github.io/bignumber.js/ | ||||
const liveWalletState = walletStateFromStorage; | const liveWalletState = walletStateFromStorage; | ||||
const { slpBalancesAndUtxos, tokens } = liveWalletState; | const { tokens } = liveWalletState; | ||||
for (let i = 0; i < tokens.length; i += 1) { | for (let i = 0; i < tokens.length; i += 1) { | ||||
const thisTokenBalance = tokens[i].balance; | const thisTokenBalance = tokens[i].balance; | ||||
thisTokenBalance._isBigNumber = true; | thisTokenBalance._isBigNumber = true; | ||||
tokens[i].balance = new BigNumber(thisTokenBalance); | tokens[i].balance = new BigNumber(thisTokenBalance); | ||||
} | } | ||||
// Also confirm balance is correct | // Also confirm balance is correct | ||||
// Necessary step in case currency.decimals changed since last startup | // Necessary step in case currency.decimals changed since last startup | ||||
let nonSlpUtxosToParseForBalance; | |||||
if (Object.keys(liveWalletState).includes('slpBalancesAndUtxos')) { | |||||
// If this wallet still includes the wallet.state.slpBalancesAndUtxos field | |||||
nonSlpUtxosToParseForBalance = | |||||
liveWalletState.slpBalancesAndUtxos.nonSlpUtxos; | |||||
} else { | |||||
nonSlpUtxosToParseForBalance = liveWalletState.nonSlpUtxos; | |||||
} | |||||
const balancesRebased = getWalletBalanceFromUtxos( | const balancesRebased = getWalletBalanceFromUtxos( | ||||
slpBalancesAndUtxos.nonSlpUtxos, | nonSlpUtxosToParseForBalance, | ||||
); | ); | ||||
liveWalletState.balances = balancesRebased; | liveWalletState.balances = balancesRebased; | ||||
return liveWalletState; | return liveWalletState; | ||||
}; | }; | ||||
export const getWalletBalanceFromUtxos = nonSlpUtxos => { | export const getWalletBalanceFromUtxos = nonSlpUtxos => { | ||||
const totalBalanceInSatoshis = nonSlpUtxos.reduce( | const totalBalanceInSatoshis = nonSlpUtxos.reduce( | ||||
(previousBalance, utxo) => | (previousBalance, utxo) => | ||||
previousBalance.plus(new BigNumber(utxo.value)), | previousBalance.plus(new BigNumber(utxo.value)), | ||||
new BigNumber(0), | new BigNumber(0), | ||||
); | ); | ||||
return { | return { | ||||
totalBalanceInSatoshis: totalBalanceInSatoshis.toString(), | totalBalanceInSatoshis: totalBalanceInSatoshis.toString(), | ||||
totalBalance: fromSatoshisToXec(totalBalanceInSatoshis).toString(), | totalBalance: fromSatoshisToXec(totalBalanceInSatoshis).toString(), | ||||
}; | }; | ||||
}; | }; | ||||
export const isValidStoredWallet = walletStateFromStorage => { | export const isValidStoredWallet = walletStateFromStorage => { | ||||
return ( | return ( | ||||
typeof walletStateFromStorage === 'object' && | typeof walletStateFromStorage === 'object' && | ||||
'state' in walletStateFromStorage && | 'state' in walletStateFromStorage && | ||||
typeof walletStateFromStorage.state === 'object' && | typeof walletStateFromStorage.state === 'object' && | ||||
'balances' in walletStateFromStorage.state && | 'balances' in walletStateFromStorage.state && | ||||
!('hydratedUtxoDetails' in walletStateFromStorage.state) && | !('hydratedUtxoDetails' in walletStateFromStorage.state) && | ||||
'slpBalancesAndUtxos' in walletStateFromStorage.state && | ('slpBalancesAndUtxos' in walletStateFromStorage.state || | ||||
('slpUtxos' in walletStateFromStorage.state && | |||||
'nonSlpUtxos' in walletStateFromStorage.state)) && | |||||
'tokens' in walletStateFromStorage.state | 'tokens' in walletStateFromStorage.state | ||||
); | ); | ||||
}; | }; | ||||
export const getWalletState = wallet => { | export const getWalletState = wallet => { | ||||
if (!wallet || !wallet.state) { | if (!wallet || !wallet.state) { | ||||
return { | return { | ||||
balances: { totalBalance: 0, totalBalanceInSatoshis: 0 }, | balances: { totalBalance: 0, totalBalanceInSatoshis: 0 }, | ||||
hydratedUtxoDetails: {}, | hydratedUtxoDetails: {}, | ||||
tokens: [], | tokens: [], | ||||
slpBalancesAndUtxos: {}, | slpUtxos: [], | ||||
nonSlpUtxos: [], | |||||
parsedTxHistory: [], | parsedTxHistory: [], | ||||
utxos: [], | utxos: [], | ||||
}; | }; | ||||
} | } | ||||
return wallet.state; | return wallet.state; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 242 Lines • Show Last 20 Lines |