Skip to content

Commit

Permalink
fix: change if statement to switch for fetching token balances (#234)
Browse files Browse the repository at this point in the history
* change-if-to-switch

* Fix for error message

* Added default case

* Revert "Added default case"

* Default Case

* Fixed yarn lint errors

* Changed case syntax

* fix: remove thrown error, return empty array
  • Loading branch information
MahmoudHamdy02 authored Mar 7, 2022
1 parent f4a2322 commit d9ab3e4
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/interfaces/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,30 @@ export class TokenInterface<C extends ChainId> extends ServiceInterface<C> {
.balances(address)
.then(balances => balances.filter(token => token.balance !== "0"));

if (this.chainId === 1 || this.chainId === 1337) {
let zapperBalances: Balance[] = [];
try {
zapperBalances = await this.yearn.services.zapper.balances(address);
} catch (error) {
console.error(error);
switch (this.chainId) {
case 1:
case 1337: {
let zapperBalances: Balance[] = [];
try {
zapperBalances = await this.yearn.services.zapper.balances(address);
} catch (error) {
console.error(error);
}
const vaultBalanceAddresses = new Set(vaultBalances.map(balance => balance.address));
zapperBalances = zapperBalances.filter(balance => !vaultBalanceAddresses.has(balance.address));
return zapperBalances.concat(vaultBalances);
}
const vaultBalanceAddresses = new Set(vaultBalances.map(balance => balance.address));
zapperBalances = zapperBalances.filter(balance => !vaultBalanceAddresses.has(balance.address));
return zapperBalances.concat(vaultBalances);
}

if (this.chainId === 250 || this.chainId === 42161) {
let ironBankTokens = await this.yearn.ironBank.balances(address);
const vaultBalanceAddresses = new Set(vaultBalances.map(balance => balance.address));
ironBankTokens = ironBankTokens.filter(balance => !vaultBalanceAddresses.has(balance.address));
return ironBankTokens.concat(vaultBalances);
case 250:
case 42161: {
let ironBankTokens = await this.yearn.ironBank.balances(address);
const vaultBalanceAddresses = new Set(vaultBalances.map(balance => balance.address));
ironBankTokens = ironBankTokens.filter(balance => !vaultBalanceAddresses.has(balance.address));
return ironBankTokens.concat(vaultBalances);
}
default:
console.error(`the chain ${this.chainId} hasn't been implemented yet`);
return [];
}

console.error(`the chain ${this.chainId} hasn't been implemented yet`);

return [];
}

/**
Expand Down

0 comments on commit d9ab3e4

Please sign in to comment.