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.1.1';
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 markets: { [underlying: string]: string } = {
13  '0x6b175474e89094c44da98b954eedeac495271d0f': '0xdA816459F1AB5631232FE5e97a05BBBb94970c95', // Dai
14  '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48': '0x5f18C75AbDAe578b483E5F43f12a39cF75b973a9', // USDC
15  '0xdac17f958d2ee523a2206206994597c13d831ec7': '0x7Da96a3891Add058AdA2E826306D812C638D87a7', // USDT
16}
17
18export function setup(sdk: Context) {
19  const getInterestRateLoader = (daysToSample: number) => async (assetAddress: string): Promise<number> => {
20    const vault = markets[assetAddress.toLowerCase()];
21    if (!vault) {
22      return 0;
23    }
24
25    const contract = sdk.ethers.getContract(vault, VAULT_ABI);
26
27    const currentBlock = await sdk.ethers.getProvider('ethereum').getBlockNumber();
28    const dayAgoBlock = currentBlock - (blocksPerDay * daysToSample);
29
30    const nowPrice = await contract.pricePerShare({ blockTag: currentBlock });
31    const dayAgoPrice = await contract.pricePerShare({ blockTag: dayAgoBlock });
32
33    const apy = Math.pow(nowPrice / dayAgoPrice, (daysPerYear / daysToSample)) - 1;
34
35    return apy;
36  }
37
38  sdk.register({
39    id: 'yearn-vaults',
40    queries: {
41      apyCurrent: getInterestRateLoader(3),
42      apyPrevious30Days: getInterestRateLoader(30),
43      apyPreviousDateRange: (numDays: number, asset: string) => getInterestRateLoader(numDays)(asset),
44    },
45    metadata: {
46      name: 'Yearn',
47      icon: sdk.ipfs.getDataURILoader('QmPj9PoFfMCAtne9qovR9iJwyMRiEsCyfZs7r65CwBiH9F', 'image/svg+xml'),
48    },
49  });
50}
51

It's something off?

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

Adapter Info

Version

0.1.1

License

MIT

IPFS CID

QmPzVpdARnRRKRJwHYYXjumSuTaxNehvx9Q3A46L7GsZUv

CID (source)

QmZUdGrVv34K1r17ymGjeuDpSJygq6uQE9RipH41bE1jfi

Author

mihal.eth