diff --git a/examples/vault.mjs b/examples/vault.mjs index 7692dd47..74908b54 100644 --- a/examples/vault.mjs +++ b/examples/vault.mjs @@ -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(); diff --git a/src/readers/ironbank.ts b/src/readers/ironbank.ts index 8258de1a..68a9483a 100644 --- a/src/readers/ironbank.ts +++ b/src/readers/ironbank.ts @@ -10,11 +10,12 @@ import { } from "../types"; import { ChainId } from "../chain"; import { Reader } from "../common"; +import { CallOverrides } from "@ethersproject/contracts"; export class IronBankReader extends Reader { - async get(addresses?: Address[]): Promise { - 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 { + 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(); for (const asset of assetsStatic) { const dynamic = assetsDynamic.find(({ address }) => asset.address === address); @@ -25,23 +26,23 @@ export class IronBankReader extends Reader { } return assets; } - async getStatic(addresses?: Address[]): Promise { - return await this.yearn.services.lens.adapters.ironBank.assetsStatic(addresses); + async getStatic(addresses?: Address[], overrides?: CallOverrides): Promise { + return await this.yearn.services.lens.adapters.ironBank.assetsStatic(addresses, overrides); } - async getDynamic(addresses?: Address[]): Promise { - return await this.yearn.services.lens.adapters.ironBank.assetsDynamic(addresses); + async getDynamic(addresses?: Address[], overrides?: CallOverrides): Promise { + return await this.yearn.services.lens.adapters.ironBank.assetsDynamic(addresses, overrides); } - async positionsOf(address: Address, addresses?: Address[]): Promise { - return this.yearn.services.lens.adapters.ironBank.positionsOf(address, addresses); + async positionsOf(address: Address, addresses?: Address[], overrides?: CallOverrides): Promise { + return this.yearn.services.lens.adapters.ironBank.positionsOf(address, addresses, overrides); } - async userMetadata(address: Address): Promise { - return this.yearn.services.lens.adapters.ironBank.assetsUserMetadata(address); + async userMetadata(address: Address, overrides?: CallOverrides): Promise { + return this.yearn.services.lens.adapters.ironBank.assetsUserMetadata(address, overrides); } - async generalPositionOf(address: Address): Promise { - return this.yearn.services.lens.adapters.ironBank.generalPositionOf(address); + async generalPositionOf(address: Address, overrides?: CallOverrides): Promise { + return this.yearn.services.lens.adapters.ironBank.generalPositionOf(address, overrides); } } diff --git a/src/readers/token.ts b/src/readers/token.ts index f69a123b..ed6f9a10 100644 --- a/src/readers/token.ts +++ b/src/readers/token.ts @@ -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 extends Reader { async price(from: Address, to: Address): Promise { return this.yearn.services.oracle.getPriceFromRouter(from, to); } - async priceUsdc(token: T): Promise; - async priceUsdc(tokens: T[]): Promise>; - async priceUsdc(tokens: T | T[]): Promise | Usdc> { + async priceUsdc(token: T, overrides?: CallOverrides): Promise; + async priceUsdc(tokens: T[], overrides?: CallOverrides): Promise>; + async priceUsdc(tokens: T | T[], overrides?: CallOverrides): Promise | 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; } - return this.yearn.services.oracle.getPriceUsdc(tokens); + return this.yearn.services.oracle.getPriceUsdc(tokens, overrides); } async balances(address: Address): Promise { diff --git a/src/readers/vault.ts b/src/readers/vault.ts index 3b1b7a4b..3ffb0f27 100644 --- a/src/readers/vault.ts +++ b/src/readers/vault.ts @@ -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 extends Reader { async get(addresses?: Address[]): Promise { @@ -25,38 +26,39 @@ export class VaultReader extends Reader { ).then(arr => arr.flat()); } - async getStatic(addresses?: Address[]): Promise { + async getStatic(addresses?: Address[], overrides?: CallOverrides): Promise { 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 { + async getDynamic(addresses?: Address[], overrides?: CallOverrides): Promise { 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 { + async positionsOf(address: Address, addresses?: Address[], overrides?: CallOverrides): Promise { 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 { + async balances(address: Address, overrides?: CallOverrides): Promise { 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); @@ -70,19 +72,19 @@ export class VaultReader extends Reader { }); } - async tokens(): Promise { + async tokens(overrides?: CallOverrides): Promise { 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) })) ); }) diff --git a/src/services/adapters/ironbank.ts b/src/services/adapters/ironbank.ts index 626b538c..4101d9fe 100644 --- a/src/services/adapters/ironbank.ts +++ b/src/services/adapters/ironbank.ts @@ -1,3 +1,4 @@ +import { CallOverrides } from "@ethersproject/contracts"; import { struct, structArray } from "../../struct"; import { ContractService } from "../../common"; import { AdapterAbi } from "../../abi"; @@ -63,36 +64,38 @@ export class IronBankAdapter extends ContractService { } } - async assetsStatic(addresses?: Address[]): Promise { + async assetsStatic(addresses?: Address[], overrides: CallOverrides = {}): Promise { 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 { + async assetsDynamic(addresses?: Address[], overrides: CallOverrides = {}): Promise { 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 { + async positionsOf(address: Address, addresses?: Address[], overrides: CallOverrides = {}): Promise { 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 { - return await this.contract.read.adapterPositionOf(address).then(struct); + async generalPositionOf(address: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.adapterPositionOf(address, overrides).then(struct); } - async assetsUserMetadata(address: Address): Promise { - return await this.contract.read.assetsUserMetadata(address).then(structArray); + async assetsUserMetadata(address: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.assetsUserMetadata(address, overrides).then(structArray); } - async tokens(): Promise { - return await this.contract.read.assetsTokensAddresses(); + async tokens(overrides: CallOverrides = {}): Promise { + return await this.contract.read.assetsTokensAddresses(overrides); } } diff --git a/src/services/adapters/registry.ts b/src/services/adapters/registry.ts index fd12b2ad..115068e9 100644 --- a/src/services/adapters/registry.ts +++ b/src/services/adapters/registry.ts @@ -1,3 +1,5 @@ +import { CallOverrides } from "@ethersproject/contracts"; + import { ContractService } from "../../common"; import { ZeroAddress } from "../../helpers"; import { structArray } from "../../struct"; @@ -7,10 +9,10 @@ import { ChainId } from "../../chain"; import { Position, VaultStatic, VaultDynamic, Address } from "../../types"; export interface IRegistryAdapter { - assetsStatic(): Promise; - assetsDynamic(): Promise; - positionsOf(address: Address, addresses?: Address[]): Promise; - tokens(): Promise; + assetsStatic(addresses?: Address[], overrides?: CallOverrides): Promise; + assetsDynamic(addresses?: Address[], overrides?: CallOverrides): Promise; + positionsOf(address: Address, addresses?: Address[], overrides?: CallOverrides): Promise; + tokens(overrides?: CallOverrides): Promise; } const VaultV2MetadataAbi = `tuple( @@ -38,38 +40,54 @@ export class RegistryV2Adapter extends ContractService implem } } - async assetsStatic(addresses?: Address[]): Promise { + async assetsStatic(addresses?: Address[], overrides: CallOverrides = {}): Promise { 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 { + async assetsDynamic(addresses?: Address[], overrides: CallOverrides = {}): Promise { 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 { + async positionsOf(address: Address, addresses?: Address[], overrides: CallOverrides = {}): Promise { 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 { - return await this.contract.read.assetsTokensAddresses(); + async tokens(overrides: CallOverrides = {}): Promise { + return await this.contract.read.assetsTokensAddresses(overrides); } } diff --git a/src/services/helper.ts b/src/services/helper.ts index 6f107bbe..3177b26e 100644 --- a/src/services/helper.ts +++ b/src/services/helper.ts @@ -1,3 +1,4 @@ +import { CallOverrides } from "@ethersproject/contracts"; import { AllowanceAbi, TokenAbi, TokenBalanceAbi, TokenPriceAbi } from "../abi"; import { ChainId } from "../chain"; import { ContractService } from "../common"; @@ -29,19 +30,24 @@ export class HelperService extends ContractService { } } - async tokens(addresses: Address[]): Promise { - return await this.contract.read.tokensMetadata(addresses).then(structArray); + async tokens(addresses: Address[], overrides: CallOverrides = {}): Promise { + return await this.contract.read.tokensMetadata(addresses, overrides).then(structArray); } - async tokenPrices(addresses: Address[]): Promise { - return await this.contract.read.tokensPrices(addresses).then(structArray); + async tokenPrices(addresses: Address[], overrides: CallOverrides = {}): Promise { + return await this.contract.read.tokensPrices(addresses, overrides).then(structArray); } - async tokenBalances(address: Address, tokens: Address[]): Promise { - return await this.contract.read.tokensBalances(address, tokens).then(structArray); + async tokenBalances(address: Address, tokens: Address[], overrides: CallOverrides = {}): Promise { + return await this.contract.read.tokensBalances(address, tokens, overrides).then(structArray); } - async tokenAllowances(address: Address, tokens: Address[], spenders: Address[]): Promise { - return await this.contract.read.allowance(address, tokens, spenders).then(structArray); + async tokenAllowances( + address: Address, + tokens: Address[], + spenders: Address[], + overrides: CallOverrides = {} + ): Promise { + return await this.contract.read.allowance(address, tokens, spenders, overrides).then(structArray); } } diff --git a/src/services/icons.ts b/src/services/icons.ts index 82d76f2b..78d4f4ae 100644 --- a/src/services/icons.ts +++ b/src/services/icons.ts @@ -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)); } } diff --git a/src/services/lens.ts b/src/services/lens.ts index 4dfaf8de..56f128a2 100644 --- a/src/services/lens.ts +++ b/src/services/lens.ts @@ -6,6 +6,7 @@ import { Context } from "../context"; import { IRegistryAdapter, RegistryV2Adapter } from "./adapters/registry"; import { IronBankAdapter } from "./adapters/ironbank"; +import { CallOverrides } from "@ethersproject/contracts"; // FIXME: no export const LensAbi = ["function getRegistries() external view returns (address[] memory)"]; @@ -54,19 +55,19 @@ export class LensService extends ContractService { } } - async getRegistries(): Promise { - return await this.contract.read.getRegistries(); + async getRegistries(overrides: CallOverrides = {}): Promise { + return await this.contract.read.getRegistries(overrides); } - async getAssets(): Promise { - return await this.contract.read.getAssets().then(structArray); + async getAssets(overrides: CallOverrides = {}): Promise { + return await this.contract.read.getAssets(overrides).then(structArray); } - async getAssetsFromAdapter(adapter: Address): Promise { - return await this.contract.read.getAssetsFromAdapter(adapter).then(structArray); + async getAssetsFromAdapter(adapter: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.getAssetsFromAdapter(adapter, overrides).then(structArray); } - async getPositions(address: string): Promise { - return await this.contract.read.getPositionsOf(address).then(structArray); + async getPositions(address: string, overrides: CallOverrides = {}): Promise { + return await this.contract.read.getPositionsOf(address, overrides).then(structArray); } } diff --git a/src/services/oracle.ts b/src/services/oracle.ts index 6b128a21..29ccc133 100644 --- a/src/services/oracle.ts +++ b/src/services/oracle.ts @@ -3,6 +3,7 @@ import { ChainId } from "../chain"; import { Context } from "../context"; import { int } from "../helpers"; import { Address, Integer } from "../types"; +import { CallOverrides } from "@ethersproject/contracts"; export const OracleAbi = [ // Oracle general @@ -50,77 +51,77 @@ export class OracleService extends ContractService { } } - async getCalculations(): Promise { - return await this.contract.read.calculations(); + async getCalculations(overrides: CallOverrides = {}): Promise { + return await this.contract.read.calculations(overrides); } - async getPriceUsdc(token: Address): Promise { - return await this.contract.read.getPriceUsdcRecommended(token).then(int); + async getPriceUsdc(token: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.getPriceUsdcRecommended(token, overrides).then(int); } - async getUsdcAddress(): Promise { - return await this.contract.read.usdcAddress().then(int); + async getUsdcAddress(overrides: CallOverrides = {}): Promise { + return await this.contract.read.usdcAddress(overrides).then(int); } // Calculations Curve - async isCurveLpToken(lpToken: Address): Promise { - return await this.contract.read.isCurveLpToken(lpToken); + async isCurveLpToken(lpToken: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.isCurveLpToken(lpToken, overrides); } - async getCurvePriceUsdc(lpToken: Address): Promise { - return await this.contract.read.getCurvePriceUsdc(lpToken).then(int); + async getCurvePriceUsdc(lpToken: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.getCurvePriceUsdc(lpToken, overrides).then(int); } - async getBasePrice(lpToken: Address): Promise { - return await this.contract.read.getBasePrice(lpToken).then(int); + async getBasePrice(lpToken: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.getBasePrice(lpToken, overrides).then(int); } - async getVirtualPrice(lpToken: Address): Promise { - return await this.contract.read.getVirtualPrice(lpToken).then(int); + async getVirtualPrice(lpToken: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.getVirtualPrice(lpToken, overrides).then(int); } - async getFirstUnderlyingCoinFromPool(pool: Address): Promise
{ - return await this.contract.read.getFirstUnderlyingCoinFromPool(pool); + async getFirstUnderlyingCoinFromPool(pool: Address, overrides: CallOverrides = {}): Promise
{ + return await this.contract.read.getFirstUnderlyingCoinFromPool(pool, overrides); } - async getCurveRegistryAddress(): Promise { - return await this.contract.read.usdcAddress().then(int); + async getCurveRegistryAddress(overrides: CallOverrides = {}): Promise { + return await this.contract.read.usdcAddress(overrides).then(int); } // Calculations: Iron Bank - async isIronBankMarket(token: Address): Promise { - return await this.contract.read.isIronBankMarket(token); + async isIronBankMarket(token: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.isIronBankMarket(token, overrides); } - async getIronBankMarketPriceUsdc(token: Address): Promise { - return await this.contract.read.getIronBankMarketPriceUsdc(token).then(int); + async getIronBankMarketPriceUsdc(token: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.getIronBankMarketPriceUsdc(token, overrides).then(int); } - async getIronBankMarkets(): Promise { - return await this.contract.read.getIronBankMarkets(); + async getIronBankMarkets(overrides: CallOverrides = {}): Promise { + return await this.contract.read.getIronBankMarkets(overrides); } // Calculations: Sushiswap - async isLpToken(token: Address): Promise { - return await this.contract.read.isLpToken(token); + async isLpToken(token: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.isLpToken(token, overrides); } - async getPriceFromRouter(token0: Address, token1: Address): Promise { - return await this.contract.read.getPriceFromRouter(token0, token1).then(int); + async getPriceFromRouter(token0: Address, token1: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.getPriceFromRouter(token0, token1, overrides).then(int); } - async getPriceFromRouterUsdc(token: Address): Promise { - return await this.contract.read.getPriceFromRouterUsdc(token).then(int); + async getPriceFromRouterUsdc(token: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.getPriceFromRouterUsdc(token, overrides).then(int); } - async getLpTokenTotalLiquidityUsdc(token: Address): Promise { - return await this.contract.read.getLpTokenTotalLiquidityUsdc(token).then(int); + async getLpTokenTotalLiquidityUsdc(token: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.getLpTokenTotalLiquidityUsdc(token, overrides).then(int); } - async getLpTokenPriceUsdc(token: Address): Promise { - return await this.contract.read.getLpTokenPriceUsdc(token).then(int); + async getLpTokenPriceUsdc(token: Address, overrides: CallOverrides = {}): Promise { + return await this.contract.read.getLpTokenPriceUsdc(token, overrides).then(int); } }