Skip to content

Commit

Permalink
Updates balancesActions.js and tokenList.json (#2026)
Browse files Browse the repository at this point in the history
  • Loading branch information
comountainclimber authored Nov 29, 2020
1 parent 6befd56 commit 930dfe8
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 31 deletions.
59 changes: 41 additions & 18 deletions app/actions/balancesActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { COIN_DECIMAL_LENGTH } from '../core/formatters'
import { toBigNumber } from '../core/math'
import { findNetworkByDeprecatedLabel } from '../core/networks'

const MAX_SCRIPT_HASH_CHUNK_SIZE = 10
const MAX_SCRIPT_HASH_CHUNK_SIZE = 3

type Props = {
net: string,
Expand Down Expand Up @@ -73,7 +73,10 @@ function determineIfBalanceUpdated(
}

async function getBalances({ net, address }: Props) {
const { soundEnabled, tokens } = await getSettings()
const { soundEnabled, tokens } = (await getSettings()) || {
tokens: [],
soundEnabled: true,
}
const network = findNetworkByDeprecatedLabel(net)

let endpoint = await getNode(net)
Expand All @@ -88,25 +91,45 @@ async function getBalances({ net, address }: Props) {
if (!inMemoryAddress) adressHasChanged = false
else if (inMemoryAddress !== address) adressHasChanged = true

const chunks = tokens
.filter(token => !token.isUserGenerated && token.networkId === network.id)
.reduce((accum, currVal) => {
if (!accum.length) {
accum.push([currVal.scriptHash])
const chunks =
tokens.length &&
tokens
.filter(token => !token.isUserGenerated && token.networkId === network.id)
.reduce((accum, currVal) => {
const chunk = {
scriptHash: currVal.scriptHash,
symbol: currVal.symbol,
}
if (!accum.length) {
accum.push([chunk])
return accum
}

if (accum[accum.length - 1].length < MAX_SCRIPT_HASH_CHUNK_SIZE) {
accum[accum.length - 1].push(chunk)
} else {
accum.push([chunk])
}
return accum
}
}, [])

const promiseMap = chunks.map(async chunk => {
// NOTE: because the RPC nodes will respond with the contract
// symbol name, we need to use our original token list
// in case two tokens have the same symbol (SWTH vs SWTH OLD)
const balanceResults = await api.nep5.getTokenBalances(
endpoint,
chunk.map(({ scriptHash }) => scriptHash),
address,
)
const hashBasedBalance = {}

if (accum[accum.length - 1].length < MAX_SCRIPT_HASH_CHUNK_SIZE) {
accum[accum.length - 1].push(currVal.scriptHash)
} else {
accum.push([currVal.scriptHash])
}
return accum
}, [])
chunk.forEach((token, i) => {
hashBasedBalance[token.symbol] = Object.values(balanceResults)[i]
})
return hashBasedBalance
})

const promiseMap = chunks.map(chunk =>
api.nep5.getTokenBalances(endpoint, chunk, address),
)
const results = await Promise.all(promiseMap)

const parsedTokenBalances = results.reduce((accum, currBalance) => {
Expand Down
Loading

0 comments on commit 930dfe8

Please sign in to comment.