Skip to content

Commit

Permalink
feat: add overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
nymmrx committed May 18, 2021
1 parent 9ab793f commit 8ff99c5
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 116 deletions.
2 changes: 2 additions & 0 deletions examples/vault.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ async function main() {

console.log("Yearn Multisig vault positions:");
console.log(positionsTable.toString());

console.log(await yearn.vaults.balances(YearnGovernance));
}

main();
27 changes: 14 additions & 13 deletions src/readers/ironbank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import {
} from "../types";
import { ChainId } from "../chain";
import { Reader } from "../common";
import { CallOverrides } from "@ethersproject/contracts";

export class IronBankReader<T extends ChainId> extends Reader<T> {
async get(addresses?: Address[]): Promise<IronBankMarket[]> {
const assetsStatic = await this.yearn.services.lens.adapters.ironBank.assetsStatic(addresses);
const assetsDynamic = await this.yearn.services.lens.adapters.ironBank.assetsDynamic(addresses);
async get(addresses?: Address[], overrides?: CallOverrides): Promise<IronBankMarket[]> {
const assetsStatic = await this.yearn.services.lens.adapters.ironBank.assetsStatic(addresses, overrides);
const assetsDynamic = await this.yearn.services.lens.adapters.ironBank.assetsDynamic(addresses, overrides);
const assets = new Array<IronBankMarket>();
for (const asset of assetsStatic) {
const dynamic = assetsDynamic.find(({ address }) => asset.address === address);
Expand All @@ -25,23 +26,23 @@ export class IronBankReader<T extends ChainId> extends Reader<T> {
}
return assets;
}
async getStatic(addresses?: Address[]): Promise<IronBankMarketStatic[]> {
return await this.yearn.services.lens.adapters.ironBank.assetsStatic(addresses);
async getStatic(addresses?: Address[], overrides?: CallOverrides): Promise<IronBankMarketStatic[]> {
return await this.yearn.services.lens.adapters.ironBank.assetsStatic(addresses, overrides);
}

async getDynamic(addresses?: Address[]): Promise<IronBankMarketDynamic[]> {
return await this.yearn.services.lens.adapters.ironBank.assetsDynamic(addresses);
async getDynamic(addresses?: Address[], overrides?: CallOverrides): Promise<IronBankMarketDynamic[]> {
return await this.yearn.services.lens.adapters.ironBank.assetsDynamic(addresses, overrides);
}

async positionsOf(address: Address, addresses?: Address[]): Promise<Position[]> {
return this.yearn.services.lens.adapters.ironBank.positionsOf(address, addresses);
async positionsOf(address: Address, addresses?: Address[], overrides?: CallOverrides): Promise<Position[]> {
return this.yearn.services.lens.adapters.ironBank.positionsOf(address, addresses, overrides);
}

async userMetadata(address: Address): Promise<CyTokenUserMetadata[]> {
return this.yearn.services.lens.adapters.ironBank.assetsUserMetadata(address);
async userMetadata(address: Address, overrides?: CallOverrides): Promise<CyTokenUserMetadata[]> {
return this.yearn.services.lens.adapters.ironBank.assetsUserMetadata(address, overrides);
}

async generalPositionOf(address: Address): Promise<IronBankPosition> {
return this.yearn.services.lens.adapters.ironBank.generalPositionOf(address);
async generalPositionOf(address: Address, overrides?: CallOverrides): Promise<IronBankPosition> {
return this.yearn.services.lens.adapters.ironBank.generalPositionOf(address, overrides);
}
}
11 changes: 6 additions & 5 deletions src/readers/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ import { Address, Integer, TypedMap, Usdc } from "../types";
import { Reader } from "../common";
import { ChainId } from "../chain";
import { Token, Balance, IconMap, Icon } from "../types";
import { CallOverrides } from "@ethersproject/contracts";

export class TokenReader<C extends ChainId> extends Reader<C> {
async price(from: Address, to: Address): Promise<Integer> {
return this.yearn.services.oracle.getPriceFromRouter(from, to);
}

async priceUsdc<T extends Address>(token: T): Promise<Usdc>;
async priceUsdc<T extends Address>(tokens: T[]): Promise<TypedMap<T, Usdc>>;
async priceUsdc<T extends Address>(tokens: T | T[]): Promise<TypedMap<T, Usdc> | Usdc> {
async priceUsdc<T extends Address>(token: T, overrides?: CallOverrides): Promise<Usdc>;
async priceUsdc<T extends Address>(tokens: T[], overrides?: CallOverrides): Promise<TypedMap<T, Usdc>>;
async priceUsdc<T extends Address>(tokens: T | T[], overrides?: CallOverrides): Promise<TypedMap<T, Usdc> | Usdc> {
if (Array.isArray(tokens)) {
const entries = await Promise.all(
tokens.map(async token => {
const price = await this.yearn.services.oracle.getPriceUsdc(token);
const price = await this.yearn.services.oracle.getPriceUsdc(token, overrides);
return [token, price];
})
);
return Object.fromEntries(entries) as TypedMap<T, Usdc>;
}
return this.yearn.services.oracle.getPriceUsdc(tokens);
return this.yearn.services.oracle.getPriceUsdc(tokens, overrides);
}

async balances(address: Address): Promise<Balance[]> {
Expand Down
26 changes: 14 additions & 12 deletions src/readers/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address, Balance, SdkError, Token, VaultDynamic, VaultStatic } from "..
import { Position, Vault } from "../types";
import { ChainId } from "../chain";
import { Reader } from "../common";
import { CallOverrides } from "@ethersproject/contracts";

export class VaultReader<T extends ChainId> extends Reader<T> {
async get(addresses?: Address[]): Promise<Vault[]> {
Expand All @@ -25,38 +26,39 @@ export class VaultReader<T extends ChainId> extends Reader<T> {
).then(arr => arr.flat());
}

async getStatic(addresses?: Address[]): Promise<VaultStatic[]> {
async getStatic(addresses?: Address[], overrides?: CallOverrides): Promise<VaultStatic[]> {
const adapters = Object.values(this.yearn.services.lens.adapters.vaults);
return await Promise.all(
adapters.map(async adapter => {
return await adapter.assetsStatic(addresses);
return await adapter.assetsStatic(addresses, overrides);
})
).then(arr => arr.flat());
}

async getDynamic(addresses?: Address[]): Promise<VaultDynamic[]> {
async getDynamic(addresses?: Address[], overrides?: CallOverrides): Promise<VaultDynamic[]> {
const adapters = Object.values(this.yearn.services.lens.adapters.vaults);
return await Promise.all(
adapters.map(async adapter => {
return await adapter.assetsDynamic(addresses);
return await adapter.assetsDynamic(addresses, overrides);
})
).then(arr => arr.flat());
}

async positionsOf(address: Address, addresses?: Address[]): Promise<Position[]> {
async positionsOf(address: Address, addresses?: Address[], overrides?: CallOverrides): Promise<Position[]> {
const adapters = Object.values(this.yearn.services.lens.adapters.vaults);
return await Promise.all(
adapters.map(adapter => {
return adapter.positionsOf(address, addresses);
return adapter.positionsOf(address, addresses, overrides);
})
).then(arr => arr.flat());
}

async balances(address: Address): Promise<Balance[]> {
async balances(address: Address, overrides?: CallOverrides): Promise<Balance[]> {
const tokens = await this.tokens();
const balances = await this.yearn.services.helper.tokenBalances(
address,
tokens.map(token => token.address)
tokens.map(token => token.address),
overrides
);
return balances.map(balance => {
const token = tokens.find(token => token.address === balance.address);
Expand All @@ -70,19 +72,19 @@ export class VaultReader<T extends ChainId> extends Reader<T> {
});
}

async tokens(): Promise<Token[]> {
async tokens(overrides?: CallOverrides): Promise<Token[]> {
const adapters = Object.values(this.yearn.services.lens.adapters.vaults);
return await Promise.all(
adapters.map(async adapter => {
const tokenAddresses = await adapter.tokens();
const tokens = await this.yearn.services.helper.tokens(tokenAddresses);
const tokenAddresses = await adapter.tokens(overrides);
const tokens = await this.yearn.services.helper.tokens(tokenAddresses, overrides);
const icons = this.yearn.services.icons.get(tokenAddresses);
return Promise.all(
tokens.map(async token => ({
...token,
icon: icons[token.address],
supported: {},
priceUsdc: await this.yearn.services.oracle.getPriceUsdc(token.address)
priceUsdc: await this.yearn.services.oracle.getPriceUsdc(token.address, overrides)
}))
);
})
Expand Down
33 changes: 18 additions & 15 deletions src/services/adapters/ironbank.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CallOverrides } from "@ethersproject/contracts";
import { struct, structArray } from "../../struct";
import { ContractService } from "../../common";
import { AdapterAbi } from "../../abi";
Expand Down Expand Up @@ -63,36 +64,38 @@ export class IronBankAdapter<T extends ChainId> extends ContractService {
}
}

async assetsStatic(addresses?: Address[]): Promise<IronBankMarketStatic[]> {
async assetsStatic(addresses?: Address[], overrides: CallOverrides = {}): Promise<IronBankMarketStatic[]> {
if (addresses) {
return await this.contract.read["assetsStatic(address[])"](addresses).then(structArray);
return await this.contract.read["assetsStatic(address[])"](addresses, overrides).then(structArray);
}
return await this.contract.read["assetsStatic()"]().then(structArray);
return await this.contract.read["assetsStatic()"](overrides).then(structArray);
}

async assetsDynamic(addresses?: Address[]): Promise<IronBankMarketDynamic[]> {
async assetsDynamic(addresses?: Address[], overrides: CallOverrides = {}): Promise<IronBankMarketDynamic[]> {
if (addresses) {
return await this.contract.read["assetsDynamic(address[])"](addresses).then(structArray);
return await this.contract.read["assetsDynamic(address[])"](addresses, overrides).then(structArray);
}
return await this.contract.read["assetsDynamic()"]().then(structArray);
return await this.contract.read["assetsDynamic()"](overrides).then(structArray);
}

async positionsOf(address: Address, addresses?: Address[]): Promise<Position[]> {
async positionsOf(address: Address, addresses?: Address[], overrides: CallOverrides = {}): Promise<Position[]> {
if (addresses) {
return await this.contract.read["assetsPositionsOf(address,address[])"](address, addresses).then(structArray);
return await this.contract.read["assetsPositionsOf(address,address[])"](address, addresses, overrides).then(
structArray
);
}
return await this.contract.read["assetsPositionsOf(address)"](address).then(structArray);
return await this.contract.read["assetsPositionsOf(address)"](address, overrides).then(structArray);
}

async generalPositionOf(address: Address): Promise<IronBankPosition> {
return await this.contract.read.adapterPositionOf(address).then(struct);
async generalPositionOf(address: Address, overrides: CallOverrides = {}): Promise<IronBankPosition> {
return await this.contract.read.adapterPositionOf(address, overrides).then(struct);
}

async assetsUserMetadata(address: Address): Promise<CyTokenUserMetadata[]> {
return await this.contract.read.assetsUserMetadata(address).then(structArray);
async assetsUserMetadata(address: Address, overrides: CallOverrides = {}): Promise<CyTokenUserMetadata[]> {
return await this.contract.read.assetsUserMetadata(address, overrides).then(structArray);
}

async tokens(): Promise<Address[]> {
return await this.contract.read.assetsTokensAddresses();
async tokens(overrides: CallOverrides = {}): Promise<Address[]> {
return await this.contract.read.assetsTokensAddresses(overrides);
}
}
58 changes: 38 additions & 20 deletions src/services/adapters/registry.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CallOverrides } from "@ethersproject/contracts";

import { ContractService } from "../../common";
import { ZeroAddress } from "../../helpers";
import { structArray } from "../../struct";
Expand All @@ -7,10 +9,10 @@ import { ChainId } from "../../chain";
import { Position, VaultStatic, VaultDynamic, Address } from "../../types";

export interface IRegistryAdapter {
assetsStatic(): Promise<VaultStatic[]>;
assetsDynamic(): Promise<VaultDynamic[]>;
positionsOf(address: Address, addresses?: Address[]): Promise<Position[]>;
tokens(): Promise<Address[]>;
assetsStatic(addresses?: Address[], overrides?: CallOverrides): Promise<VaultStatic[]>;
assetsDynamic(addresses?: Address[], overrides?: CallOverrides): Promise<VaultDynamic[]>;
positionsOf(address: Address, addresses?: Address[], overrides?: CallOverrides): Promise<Position[]>;
tokens(overrides?: CallOverrides): Promise<Address[]>;
}

const VaultV2MetadataAbi = `tuple(
Expand Down Expand Up @@ -38,38 +40,54 @@ export class RegistryV2Adapter<T extends ChainId> extends ContractService implem
}
}

async assetsStatic(addresses?: Address[]): Promise<VaultStatic[]> {
async assetsStatic(addresses?: Address[], overrides: CallOverrides = {}): Promise<VaultStatic[]> {
if (addresses) {
return await this.contract.read["assetsStatic(address[])"](addresses).then(structArray);
return await this.contract.read["assetsStatic(address[])"](addresses, overrides).then(structArray);
}
return await this.contract.read["assetsStatic()"]().then(structArray);
}

async assetsDynamic(addresses?: Address[]): Promise<VaultDynamic[]> {
async assetsDynamic(addresses?: Address[], overrides: CallOverrides = {}): Promise<VaultDynamic[]> {
if (addresses) {
return await this.contract.read["assetsDynamic(address[])"](addresses)
return await this.contract.read["assetsDynamic(address[])"](addresses, overrides)
.then(structArray)
.then((assets: any) => {
// FIXME: metadata polyfill
assets.metadata = {
.then((assets: any) =>
assets.map((asset: any) => ({
...asset,
metadata: {
...assets.metadata,
controller: ZeroAddress,
totalAssets: "0",
totalSupply: "0"
}
}))
);
}
return await this.contract.read["assetsDynamic()"]()
.then(structArray)
.then((assets: any) =>
assets.map((asset: any) => ({
...asset,
metadata: {
...assets.metadata,
controller: ZeroAddress,
totalAssets: "0",
totalSupply: "0"
};
});
}
return await this.contract.read["assetsDynamic()"]().then(structArray);
}
}))
);
}

async positionsOf(address: Address, addresses?: Address[]): Promise<Position[]> {
async positionsOf(address: Address, addresses?: Address[], overrides: CallOverrides = {}): Promise<Position[]> {
if (addresses) {
return await this.contract.read["assetsPositionsOf(address,address[])"](address, addresses).then(structArray);
return await this.contract.read["assetsPositionsOf(address,address[])"](address, addresses, overrides).then(
structArray
);
}
return await this.contract.read["assetsPositionsOf(address)"](address).then(structArray);
return await this.contract.read["assetsPositionsOf(address)"](address, overrides).then(structArray);
}

async tokens(): Promise<Address[]> {
return await this.contract.read.assetsTokensAddresses();
async tokens(overrides: CallOverrides = {}): Promise<Address[]> {
return await this.contract.read.assetsTokensAddresses(overrides);
}
}
22 changes: 14 additions & 8 deletions src/services/helper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CallOverrides } from "@ethersproject/contracts";
import { AllowanceAbi, TokenAbi, TokenBalanceAbi, TokenPriceAbi } from "../abi";
import { ChainId } from "../chain";
import { ContractService } from "../common";
Expand Down Expand Up @@ -29,19 +30,24 @@ export class HelperService<T extends ChainId> extends ContractService {
}
}

async tokens(addresses: Address[]): Promise<ERC20[]> {
return await this.contract.read.tokensMetadata(addresses).then(structArray);
async tokens(addresses: Address[], overrides: CallOverrides = {}): Promise<ERC20[]> {
return await this.contract.read.tokensMetadata(addresses, overrides).then(structArray);
}

async tokenPrices(addresses: Address[]): Promise<TokenPrice[]> {
return await this.contract.read.tokensPrices(addresses).then(structArray);
async tokenPrices(addresses: Address[], overrides: CallOverrides = {}): Promise<TokenPrice[]> {
return await this.contract.read.tokensPrices(addresses, overrides).then(structArray);
}

async tokenBalances(address: Address, tokens: Address[]): Promise<TokenBalance[]> {
return await this.contract.read.tokensBalances(address, tokens).then(structArray);
async tokenBalances(address: Address, tokens: Address[], overrides: CallOverrides = {}): Promise<TokenBalance[]> {
return await this.contract.read.tokensBalances(address, tokens, overrides).then(structArray);
}

async tokenAllowances(address: Address, tokens: Address[], spenders: Address[]): Promise<TokenAllowance[]> {
return await this.contract.read.allowance(address, tokens, spenders).then(structArray);
async tokenAllowances(
address: Address,
tokens: Address[],
spenders: Address[],
overrides: CallOverrides = {}
): Promise<TokenAllowance[]> {
return await this.contract.read.allowance(address, tokens, spenders, overrides).then(structArray);
}
}
2 changes: 1 addition & 1 deletion src/services/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class IconsService extends Service {
}

for (const token of yearn) {
this.supported.set(token.address, YearnAsset(token.address));
this.supported.set(token.name, YearnAsset(token.name));
}
}

Expand Down
Loading

0 comments on commit 8ff99c5

Please sign in to comment.