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

Finance: ask token directly for vault balance #1049

Merged
merged 1 commit into from
Nov 23, 2019
Merged
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
23 changes: 18 additions & 5 deletions apps/finance/app/src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ import {
tokenDataOverride,
} from './lib/token-utils'
import { addressesEqual } from './lib/web3-utils'
import tokenBalanceOfAbi from './abi/token-balanceof.json'
import tokenDecimalsAbi from './abi/token-decimals.json'
import tokenNameAbi from './abi/token-name.json'
import tokenSymbolAbi from './abi/token-symbol.json'
import vaultBalanceAbi from './abi/vault-balance.json'
import vaultGetInitializationBlockAbi from './abi/vault-getinitializationblock.json'
import vaultEventAbi from './abi/vault-events.json'

const tokenAbi = [].concat(tokenDecimalsAbi, tokenNameAbi, tokenSymbolAbi)
const tokenAbi = [].concat(
tokenBalanceOfAbi,
tokenDecimalsAbi,
tokenNameAbi,
tokenSymbolAbi
)
const vaultAbi = [].concat(
vaultBalanceAbi,
vaultGetInitializationBlockAbi,
Expand Down Expand Up @@ -316,7 +322,7 @@ async function updateBalances(
const updatedState = reloadEntireToken
? await newBalanceEntry(tokenContract, tokenAddress, settings)
: {
amount: await loadTokenBalance(tokenAddress, settings),
amount: await loadTokenBalance(tokenContract, tokenAddress, settings),
}
newBalances[balancesIndex] = {
...newBalances[balancesIndex],
Expand Down Expand Up @@ -354,7 +360,7 @@ function updateTransactions(transactions, transactionDetails) {

async function newBalanceEntry(tokenContract, tokenAddress, settings) {
const [balance, decimals, name, symbol] = await Promise.all([
loadTokenBalance(tokenAddress, settings),
loadTokenBalance(tokenContract, tokenAddress, settings),
loadTokenDecimals(tokenContract, tokenAddress, settings),
loadTokenName(tokenContract, tokenAddress, settings),
loadTokenSymbol(tokenContract, tokenAddress, settings),
Expand All @@ -372,8 +378,15 @@ async function newBalanceEntry(tokenContract, tokenAddress, settings) {
}
}

function loadTokenBalance(tokenAddress, { vault }) {
return vault.contract.balance(tokenAddress).toPromise()
function loadTokenBalance(tokenContract, tokenAddress, { ethToken, vault }) {
if (addressesEqual(tokenAddress, ethToken.address)) {
return vault.contract.balance(tokenAddress).toPromise()
} else {
// Prefer using the token contract directly to ask for the Vault's balance
// Web3.js does not handle revert strings yet, so a failing call to Vault.balance()
// results in organizations looking like whales.
return tokenContract.balanceOf(vault.address).toPromise()
}
}

async function loadTokenDecimals(tokenContract, tokenAddress, { network }) {
Expand Down