Sub-Adapters 1

Preview and test each sub adapter.

Yearn (yearn-vaults)

Metadata

ID
yearn-vaults
name

"Yearn"

icon

Queries

Adapter Code

Check the entire code written for the Adapter.

Source code

Showing TS source.
1export const name = 'Yearn Vault Yield Rates';
2export const version = '0.2.0';
3export const license = 'MIT';
4
5const VAULT_ABI = [
6  'function pricePerShare() external view returns (uint256)',
7];
8
9const blocksPerDay = 6570; // (13.15 seconds per block)
10const daysPerYear = 365;
11
12const MS_PER_YEAR = 365 * 24 * 60 * 60 * 1000;
13
14const markets: { [underlying: string]: string } = {
15  '0x6b175474e89094c44da98b954eedeac495271d0f': '0xdA816459F1AB5631232FE5e97a05BBBb94970c95', // Dai
16  '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48': '0x5f18C75AbDAe578b483E5F43f12a39cF75b973a9', // USDC
17  '0xdac17f958d2ee523a2206206994597c13d831ec7': '0x7Da96a3891Add058AdA2E826306D812C638D87a7', // USDT
18}
19
20export function setup(sdk: Context) {
21  const getInterestRateLoader = (daysToSample: number) => async (assetAddress: string): Promise<number> => {
22    const vault = markets[assetAddress.toLowerCase()];
23    if (!vault) {
24      return 0;
25    }
26
27    const contract = sdk.ethers.getContract(vault, VAULT_ABI);
28
29    const currentBlock = await sdk.ethers.getProvider('ethereum').getBlockNumber();
30    const dayAgoBlock = currentBlock - (blocksPerDay * daysToSample);
31
32    const nowPrice = await contract.pricePerShare({ blockTag: currentBlock });
33    const dayAgoPrice = await contract.pricePerShare({ blockTag: dayAgoBlock });
34
35    const apy = Math.pow(nowPrice / dayAgoPrice, (daysPerYear / daysToSample)) - 1;
36
37    return apy;
38  }
39
40  const getInterestRateOverDateRange = async (assetAddress: string, startDate: string, endDate: string): Promise<number> => {
41    const vault = markets[assetAddress.toLowerCase()];
42    if (!vault) {
43      return 0;
44    }
45
46    const contract = sdk.ethers.getContract(vault, VAULT_ABI);
47
48    const [startPrice, endPrice] = await Promise.all([
49      contract.pricePerShare({ blockTag: startDate }),
50      contract.pricePerShare({ blockTag: endDate }),
51    ]);
52
53    const percentOfYear = MS_PER_YEAR / (new Date(endDate).getTime() - new Date(startDate).getTime());
54
55    const apy = Math.pow(endPrice / startPrice, percentOfYear) - 1;
56
57    return apy;
58  }
59
60  sdk.register({
61    id: 'yearn-vaults',
62    queries: {
63      apyCurrent: getInterestRateLoader(3),
64      apyPrevious30Days: getInterestRateLoader(30),
65      apyPreviousDateRange: (numDays: number, asset: string) => getInterestRateLoader(numDays)(asset),
66      apyOverDateRange: getInterestRateOverDateRange,
67    },
68    metadata: {
69      name: 'Yearn',
70      icon: sdk.ipfs.getDataURILoader('QmPj9PoFfMCAtne9qovR9iJwyMRiEsCyfZs7r65CwBiH9F', 'image/svg+xml'),
71    },
72  });
73}
74

It's something off?

Report it to the discussion board on Discord, we will take care of it.

Adapter Info

Version

0.2.0

License

MIT

IPFS CID

QmZxASsS3qm7PoiPN9vFxQTe4sPcJQbEhT3gKWRNG1UV9a

CID (source)

QmW5mAjocGcbnt1sQHfveJVXtNsof4qeG4yEavM5iBaNMz

Author

mihal.eth