Sub-Adapters 1

Preview and test each sub adapter.

Compound (compound)

Metadata

ID
compound
name

"Compound"

icon

Queries

Adapter Code

Check the entire code written for the Adapter.

Source code

Showing TS source.
1export const name = 'Compound Lending Rates';
2export const version = '0.1.0';
3export const license = 'MIT';
4
5const CTOKEN_ABI = [
6  'function supplyRatePerBlock() external view returns (uint)',
7  'function exchangeRateCurrent() external view returns (uint256)',
8];
9
10const blocksPerDay = 6570; // (13.15 seconds per block)
11const daysPerYear = 365;
12
13const MS_PER_YEAR = 365 * 24 * 60 * 60 * 1000;
14
15const markets: { [underlying: string]: string } = {
16  '0x6b175474e89094c44da98b954eedeac495271d0f': '0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643', // Dai
17  '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48': '0x39aa39c021dfbae8fac545936693ac917d5e7563', // USDC
18  '0xdac17f958d2ee523a2206206994597c13d831ec7': '0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9', // USDT
19}
20
21export function setup(sdk: Context) {
22  const getInterestRate = async (assetAddress: string): Promise<number> => {
23    const cToken = markets[assetAddress.toLowerCase()];
24    if (!cToken) {
25      return 0;
26    }
27
28    const contract = sdk.ethers.getContract(cToken, CTOKEN_ABI);
29
30    const supplyRate = await contract.supplyRatePerBlock();
31
32    const mantissa = 10 ** 18 // TODO: decimals
33
34    const apy = (Math.pow((supplyRate.toString() / mantissa * blocksPerDay + 1), daysPerYear)) - 1;
35
36    return apy;
37  }
38
39  const getInterestRateOverDateRange = async (assetAddress: string, startDate: string, endDate: string): Promise<number> => {
40    const cToken = markets[assetAddress.toLowerCase()];
41    if (!cToken) {
42      return 0;
43    }
44
45    const contract = sdk.ethers.getContract(cToken, CTOKEN_ABI);
46
47    const [startPrice, endPrice] = await Promise.all([
48      contract.exchangeRateCurrent({ blockTag: startDate }),
49      contract.exchangeRateCurrent({ blockTag: endDate }),
50    ]);
51
52    const percentOfYear = MS_PER_YEAR / (new Date(endDate).getTime() - new Date(startDate).getTime());
53
54    const apy = Math.pow(endPrice / startPrice, percentOfYear) - 1;
55
56    return apy;
57  }
58
59  sdk.register({
60    id: 'compound',
61    queries: {
62      apyCurrent: getInterestRate,
63      apyOverDateRange: getInterestRateOverDateRange,
64    },
65    metadata: {
66      name: 'Compound',
67      icon: sdk.ipfs.getDataURILoader('QmZpZsg829EnBxE2MPZykZpAfsxyRsu6EuGbtfTkf2EFNj', 'image/svg+xml'),
68    },
69  });
70}
71

It's something off?

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

Adapter Info

Version

0.1.0

License

MIT

IPFS CID

QmPyc3U1Jq5r7ywS5mkUmZnwBnsQGWmegx9fLNLz7dtMmV

CID (source)

QmYhatWWj1wpsbQxN2FyJnbASoFGLnpDTyRakHmsYZ3E7W

Author

mihal.eth