|
1 | 1 | const utils = require('../utils'); |
2 | 2 | const sdk = require('@defillama/sdk'); |
3 | | -const { request, gql } = require('graphql-request'); |
4 | | -const { chunkArray, calculateTrancheTotal } = require('./helpers'); |
5 | 3 | const { getBlocksByTime, getData } = require('../utils'); |
6 | 4 |
|
7 | 5 | const ADDRESSES = { |
8 | 6 | base: { |
9 | | - AvantisJuniorTranche: '0x944766f715b51967E56aFdE5f0Aa76cEaCc9E7f9', |
10 | | - AvantisSeniorTranche: '0x83084cB182162473d6FEFfCd3Aa48BA55a7B66F7', |
| 7 | + AvantisVault: '0x944766f715b51967E56aFdE5f0Aa76cEaCc9E7f9', |
11 | 8 | USDC: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', |
12 | 9 | }, |
13 | 10 | }; |
14 | 11 |
|
15 | | -const SUBGRAPH_URL = |
16 | | - 'https://subgraph.satsuma-prod.com/052b6e8d4af9/avantis/avantis-mainnet/version/v0.1.9/api'; |
17 | | - |
18 | | -const feeFetchCount = 10000; |
19 | | - |
20 | | -const feeDistributedQuery = gql` |
21 | | - query FeesDistributeds($first: Int!, $skip: Int!, $start: Int!, $end: Int!) { |
22 | | - feesDistributeds( |
23 | | - first: $first |
24 | | - skip: $skip |
25 | | - where: { timestamp_gte: $start, timestamp_lte: $end } |
26 | | - orderBy: timestamp |
27 | | - orderDirection: asc |
28 | | - ) { |
29 | | - id |
30 | | - } |
31 | | - } |
32 | | -`; |
33 | | - |
34 | | -const vmToTrancheQuery = gql` |
35 | | - query VmToTrancheTransfers($transactionHashes: [String!]!, $to: [String!]!) { |
36 | | - vmtoTrancheTransfers( |
37 | | - where: { transactionHash_in: $transactionHashes, to_in: $to } |
38 | | - ) { |
39 | | - value |
40 | | - to |
41 | | - } |
42 | | - } |
43 | | -`; |
44 | | - |
45 | | -const fetchFeeDistributedIds = async (timestamp, skip = 0) => { |
46 | | - const { feesDistributeds } = await request( |
47 | | - SUBGRAPH_URL, |
48 | | - feeDistributedQuery, |
49 | | - { |
50 | | - first: feeFetchCount, |
51 | | - skip: skip, |
52 | | - start: timestamp - 7 * 86400, |
53 | | - end: timestamp, |
54 | | - } |
55 | | - ); |
56 | | - |
57 | | - let ids = feesDistributeds.map((f) => f.id); |
58 | | - |
59 | | - if (ids.length === feeFetchCount) { |
60 | | - ids = ids.concat( |
61 | | - await fetchFeeDistributedIds(timestamp, skip + feeFetchCount) |
62 | | - ); |
63 | | - } |
64 | | - |
65 | | - return ids; |
66 | | -}; |
67 | | - |
68 | | -const fetchTransfersForFeeDistributedIds = async (ids) => { |
69 | | - const chunkedIds = chunkArray(ids, 1000); |
70 | | - |
71 | | - let totalJunior = 0; |
72 | | - let totalSenior = 0; |
73 | | - |
74 | | - for (const chunk of chunkedIds) { |
75 | | - const { vmtoTrancheTransfers } = await request( |
76 | | - SUBGRAPH_URL, |
77 | | - vmToTrancheQuery, |
78 | | - { |
79 | | - transactionHashes: chunk, |
80 | | - to: [ |
81 | | - ADDRESSES.base.AvantisJuniorTranche, |
82 | | - ADDRESSES.base.AvantisSeniorTranche, |
83 | | - ], |
84 | | - } |
85 | | - ); |
86 | | - |
87 | | - totalJunior += calculateTrancheTotal( |
88 | | - vmtoTrancheTransfers, |
89 | | - ADDRESSES.base.AvantisJuniorTranche |
90 | | - ); |
91 | | - totalSenior += calculateTrancheTotal( |
92 | | - vmtoTrancheTransfers, |
93 | | - ADDRESSES.base.AvantisSeniorTranche |
94 | | - ); |
95 | | - } |
96 | | - |
97 | | - return { totalJunior: totalJunior / 7, totalSenior: totalSenior / 7 }; |
98 | | -}; |
99 | | - |
100 | | -// NOTE: OUR SUBGRAPH IS NOT CAUGHT UP TO DATE, SO WE ARE USING THE API FOR NOW |
101 | | -// ----------------------------------------------------------------------------- |
102 | | -// We will reenable time travel once our subgraph is caught up |
103 | 12 | const main = async (timestamp = null) => { |
104 | | - // timestamp = timestamp ? parseInt(timestamp) : Math.floor(Date.now() / 1000); |
105 | | - |
106 | | - // NOTE: OUR SUBGRAPH IS NOT CAUGHT UP TO DATE, SO WE ARE USING THE API FOR NOW |
107 | | - // ----------------------------------------------------------------------------- |
108 | | - // Get total fees distributed for junior and senior tranches |
109 | | - // const feesDistributedIds = await fetchFeeDistributedIds(timestamp); |
110 | | - // const { totalJunior, totalSenior } = await fetchTransfersForFeeDistributedIds( |
111 | | - // feesDistributedIds |
112 | | - // ); |
113 | | - |
114 | | - // const [block] = await getBlocksByTime([timestamp], 'base'); |
115 | | - |
116 | 13 | const { meta } = await getData( |
117 | 14 | 'https://api.avantisfi.com/v1/vault/returns-7-days' |
118 | 15 | ); |
119 | 16 |
|
120 | | - // Get TVL for junior and senior tranches |
121 | | - let [juniorTvl, seniorTvl] = await Promise.all([ |
122 | | - sdk.api.abi.call({ |
123 | | - abi: 'erc20:balanceOf', |
124 | | - target: ADDRESSES.base.USDC, |
125 | | - params: [ADDRESSES.base.AvantisJuniorTranche], |
126 | | - chain: 'base', |
127 | | - // block: block, |
128 | | - }), |
129 | | - sdk.api.abi.call({ |
130 | | - abi: 'erc20:balanceOf', |
131 | | - target: ADDRESSES.base.USDC, |
132 | | - params: [ADDRESSES.base.AvantisSeniorTranche], |
133 | | - chain: 'base', |
134 | | - // block: block, |
135 | | - }), |
136 | | - ]); |
137 | | - |
138 | | - juniorTvl = juniorTvl.output / 1e6; |
139 | | - seniorTvl = seniorTvl.output / 1e6; |
| 17 | + let vaultTvl = await sdk.api.abi.call({ |
| 18 | + abi: 'erc20:balanceOf', |
| 19 | + target: ADDRESSES.base.USDC, |
| 20 | + params: [ADDRESSES.base.AvantisVault], |
| 21 | + chain: 'base', |
| 22 | + // block: block, |
| 23 | + }); |
140 | 24 |
|
141 | | - // Calculate daily returns for junior and senior tranches |
142 | | - // const juniorDailyReturns = totalJunior / juniorTvl; |
143 | | - // const seniorDailyReturns = totalSenior / seniorTvl; |
144 | | - const juniorDailyReturns = meta.averageJrFees / juniorTvl; |
145 | | - const seniorDailyReturns = meta.averageSrFees / seniorTvl; |
| 25 | + vaultTvl = vaultTvl.output / 1e6; |
146 | 26 |
|
147 | | - // Calculate APY for junior and senior tranches |
148 | | - const juniorApy = (1 + juniorDailyReturns) ** 365 - 1; |
149 | | - const seniorApy = (1 + seniorDailyReturns) ** 365 - 1; |
| 27 | + const dailyReturns = meta.averageJrFees / vaultTvl; |
| 28 | + const apy = (1 + dailyReturns) ** 365 - 1; |
150 | 29 |
|
151 | 30 | return [ |
152 | 31 | { |
153 | | - pool: `AVANTIS-${ADDRESSES.base.AvantisJuniorTranche}-base`.toLowerCase(), |
154 | | - chain: 'base', |
155 | | - project: 'avantis', |
156 | | - symbol: 'USDC', |
157 | | - poolMeta: 'junior', |
158 | | - tvlUsd: juniorTvl, |
159 | | - apyBase: juniorApy * 100, |
160 | | - url: 'https://www.avantisfi.com/earn/junior', |
161 | | - }, |
162 | | - { |
163 | | - pool: `AVANTIS-${ADDRESSES.base.AvantisSeniorTranche}-base`.toLowerCase(), |
| 32 | + pool: `AVANTIS-${ADDRESSES.base.AvantisVault}-base`.toLowerCase(), |
164 | 33 | chain: 'base', |
165 | 34 | project: 'avantis', |
166 | 35 | symbol: 'USDC', |
167 | | - poolMeta: 'senior', |
168 | | - tvlUsd: seniorTvl, |
169 | | - apyBase: seniorApy * 100, |
170 | | - url: 'https://www.avantisfi.com/earn/senior', |
| 36 | + poolMeta: 'vault', |
| 37 | + tvlUsd: vaultTvl, |
| 38 | + apyBase: apy * 100, |
| 39 | + url: 'https://www.avantisfi.com/earn/avantis-vault', |
171 | 40 | }, |
172 | 41 | ]; |
173 | 42 | }; |
|
0 commit comments