Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dont show zero balances on error #897

Merged
merged 3 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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