Skip to content

Commit

Permalink
dont show zero balances on error (#897)
Browse files Browse the repository at this point in the history
* dont show zero balances on error

* remove test code

* address comments
  • Loading branch information
mholtzman authored Jun 15, 2022
1 parent 231cd1c commit 5ba73bf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
59 changes: 32 additions & 27 deletions main/externalData/balances/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,51 +70,56 @@ export default function (eth: EthereumProvider) {
}

async function getTokenBalance (token: TokenDefinition, owner: string) {
try {
const functionData = erc20Interface.encodeFunctionData('balanceOf', [owner])

const response = await eth.request({
method: 'eth_call',
jsonrpc: '2.0',
id: id += 1,
chainId: addHexPrefix(token.chainId.toString(16)),
params: [{ to: token.address, value: '0x0', data: functionData }, 'latest']
})
const functionData = erc20Interface.encodeFunctionData('balanceOf', [owner])

const response = await eth.request({
method: 'eth_call',
jsonrpc: '2.0',
id: id += 1,
chainId: addHexPrefix(token.chainId.toString(16)),
params: [{ to: token.address, value: '0x0', data: functionData }, 'latest']
})

const result = erc20Interface.decodeFunctionResult('balanceOf', response)
const result = erc20Interface.decodeFunctionResult('balanceOf', response)

return result.balance.toHexString()
} catch (e) {
log.warn(`could not load balance for token with address ${token.address}`, e)
return '0x0'
}
return result.balance.toHexString()
}

async function getTokenBalancesFromContracts (owner: string, tokens: TokenDefinition[]) {
const balances = tokens.map(async token => {
const rawBalance = await getTokenBalance(token, owner)
try {
const rawBalance = await getTokenBalance(token, owner)

return {
...token,
...createBalance(rawBalance, token.decimals)
return {
...token,
...createBalance(rawBalance, token.decimals)
}
} catch (e) {
log.warn(`could not load balance for token with address ${token.address}`, e)
return undefined
}
})

const loadedBalances = await Promise.all(balances)

return Promise.all(balances)
return loadedBalances.filter(bal => bal !== undefined) as Balance[]
}

async function getTokenBalancesFromMulticall (owner: string, tokens: TokenDefinition[], chainId: number) {
const calls = balanceCalls(owner, tokens)

const results = await multicall(chainId, eth).batchCall(calls)

return results.map((result, i) => {
const balance = result.success ? result.returnValues[0] : createBalance('0x0', tokens[i].decimals)
return {
...tokens[i],
...balance
return results.reduce((acc, result, i) => {
if (result.success) {
acc.push({
...tokens[i],
...result.returnValues[0]
})
}
})

return acc
}, [] as Balance[])
}

return {
Expand Down
5 changes: 0 additions & 5 deletions test/main/externalData/balances/scan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,6 @@ describe('#getTokenBalances', () => {
balance: addHexPrefix(padToEven(new BigNumber('6245100000').toString(16))),
displayBalance: '6245.1'
},
{
...zrxToken,
balance: '0x0',
displayBalance: '0'
},
{
...badgerDaoToken,
balance: addHexPrefix(padToEven(new BigNumber('17893000000000000000').toString(16))),
Expand Down

0 comments on commit 5ba73bf

Please sign in to comment.