Sub-Adapters 1
Preview and test each sub adapter.
Rari Capital Fuse Markets (rari-fuse)
Metadata
- ID
- rari-fuse
- name
"Rari Capital Fuse Markets"
- icon
Queries
Adapter Code
Check the entire code written for the Adapter.
Source code
Showing TS source.
1export const name = 'Rari APY';
2export const version = '0.1.0';
3export const license = 'MIT';
4export const description = 'Calculates the APY of the popular Rari Fuse pools, and returns the APY from the highest yield pool';
5
6const CTOKEN_ABI = [
7 'function supplyRatePerBlock() external view returns (uint)',
8 'function exchangeRateCurrent() external view returns (uint256)',
9];
10
11const blocksPerDay = 6570; // (13.15 seconds per block)
12const daysPerYear = 365;
13const MS_PER_YEAR = 365 * 24 * 60 * 60 * 1000;
14
15const markets: { [underlying: string]: string[] } = {
16 // Right now, we just check a few popular pools
17 // Would be great to check more pools, but we need to be careful to not have unbounded queries
18 // Would be amazing to have a subgraph to track this
19 '0x6b175474e89094c44da98b954eedeac495271d0f': [ // Dai
20 '0x989273ec41274C4227bCB878C2c26fdd3afbE70d', // 6
21 '0x7322b10db09687fe8889ad8e87f333f95104839f', // 7
22 '0x7e9ce3caa9910cc048590801e64174957ed41d43', // 8
23 '0x8E4E0257A4759559B4B1AC087fe8d80c63f20D19', // 18
24 ],
25 '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48': [ // USDC
26 '0x94C49563a3950424a2a7790c3eF5458A2A359C7e', // 3
27 '0xdb55b77f5e8a1a41931684cf9e4881d24e6b6cc9', // 6
28 '0x53De5A7B03dc24Ff5d25ccF7Ad337a0425Dfd8D1', // 7
29 ],
30 '0xdac17f958d2ee523a2206206994597c13d831ec7': [ // USDT
31 '0x8691927a91a032c23b895130074669f52cf6b1e7', // 6
32 '0x6f95d4d251053483f41c8718c30f4f3c404a8cf2', // 18
33 ],
34}
35
36export function setup(sdk: Context) {
37 const getInterestRate = async (cToken: string): Promise<number> => {
38 const contract = sdk.ethers.getContract(cToken, CTOKEN_ABI);
39 const supplyRate = await contract.supplyRatePerBlock();
40
41 const mantissa = 10 ** 18 // TODO: decimals
42 const apy = (Math.pow((supplyRate.toString() / mantissa * blocksPerDay + 1), daysPerYear)) - 1;
43 return apy;
44 }
45
46 const getAPYForFuse = async (token: string) => {
47 const marketsForUndelrying = markets[token];
48 const apys = await Promise.all(marketsForUndelrying.map(getInterestRate));
49 const topAPY = apys.reduce((top: number, current: number) => Math.max(top, current), 0);
50 return topAPY;
51 };
52
53 const getInterestRateOverDateRangeForPool = async (pool: string, startDate: string, endDate: string): Promise<number> => {
54 const contract = sdk.ethers.getContract(pool, CTOKEN_ABI);
55
56 const [startPrice, endPrice] = await Promise.all([
57 contract.exchangeRateCurrent({ blockTag: startDate }),
58 contract.exchangeRateCurrent({ blockTag: endDate }),
59 ]);
60
61 const percentOfYear = MS_PER_YEAR / (new Date(endDate).getTime() - new Date(startDate).getTime());
62
63 const apy = Math.pow(endPrice / startPrice, percentOfYear) - 1;
64
65 return apy;
66 }
67
68 const getInterestRateOverDateRange = async (assetAddress: string, startDate: string, endDate: string): Promise<number> => {
69 const marketsForUndelrying = markets[assetAddress];
70 const apys = await Promise.all(marketsForUndelrying.map((pool: string) => getInterestRateOverDateRangeForPool(pool, startDate, endDate)));
71 const topAPY = apys.reduce((top: number, current: number) => Math.max(top, current), 0);
72 return topAPY;
73 }
74
75 sdk.register({
76 id: 'rari-fuse',
77 queries: {
78 apyCurrent: getAPYForFuse,
79 apyOverDateRange: getInterestRateOverDateRange,
80 },
81 metadata: {
82 name: 'Rari Capital Fuse Markets',
83 icon: sdk.ipfs.getDataURILoader('QmSoPnrN9369x4V7K6dYSVcX1NsHfdrJeVG5zaDJaskVrq', 'image/png'),
84 },
85 });
86}
It's something off?
Report it to the discussion board on Discord, we will take care of it.