Skip to content

Commit d1b792d

Browse files
authored
resolv adapter. add RLP pool (#1557)
1 parent 84568d3 commit d1b792d

File tree

1 file changed

+142
-57
lines changed

1 file changed

+142
-57
lines changed

src/adaptors/resolv/index.js

Lines changed: 142 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,155 @@ const ethers = require('ethers');
55

66
const stUSR = '0x6c8984bc7DBBeDAf4F6b2FD766f16eBB7d10AAb4';
77
const USR = '0x66a1E37c9b0eAddca17d3662D6c05F4DECf3e110';
8-
8+
const RLP = '0x4956b52aE2fF65D74CA2d61207523288e4528f96';
99
const rewardDistributor = '0xbE23BB6D817C08E7EC4Cd0adB0E23156189c1bA9';
10+
const rlpPriceStorage = '0x31319866778a5223633bd745780BB6d59406371E';
11+
1012
const topic0rewardDistributed = '0x3863fc447b7dde3f3f5a5ca0b5b06a5fd3570963a1a29918f09036746293f658';
11-
const rewardDistributedInterface = new ethers.utils.Interface(['event RewardDistributed(bytes32 indexed idempotencyKey, uint256 totalShares, uint256 totalUSRBefore, uint256 totalUSRAfter, uint256 stakingReward, uint256 feeReward)']);
12-
13-
const calculateApy = (logDescription) => {
14-
const totalUsrBefore = logDescription.args.totalUSRBefore;
15-
const totalUsrAfter = logDescription.args.totalUSRAfter;
16-
const totalShares = logDescription.args.totalShares;
17-
const sharesRateBefore = totalUsrBefore / totalShares;
18-
const sharesRateAfter = totalUsrAfter / totalShares;
13+
const topic0priceSet = '0xee47534b2400bc7be3fbdc39f0283b8643fc472a30ffd7324e6161b4b2b91f24';
14+
15+
const rewardDistributedInterface = new ethers.utils.Interface([
16+
'event RewardDistributed(bytes32 indexed idempotencyKey, uint256 totalShares, uint256 totalUSRBefore, uint256 totalUSRAfter, uint256 stakingReward, uint256 feeReward)'
17+
]);
18+
const priceSetInterface = new ethers.utils.Interface([
19+
'event PriceSet(bytes32 indexed key, uint256 price)'
20+
]);
21+
22+
const DAY_IN_MS = 24 * 60 * 60 * 1000;
23+
24+
const getTotalSupply = async (tokenAddress, chain = 'ethereum') => {
25+
try {
26+
const { output } = await sdk.api.abi.call({
27+
target: tokenAddress,
28+
abi: 'erc20:totalSupply',
29+
chain
30+
});
31+
return output / 1e18;
32+
} catch (error) {
33+
console.error(`Error fetching total supply for ${tokenAddress}:`, error);
34+
throw error;
35+
}
36+
};
37+
38+
const getTokenPrice = async (tokenAddress) => {
39+
try {
40+
const priceKey = `ethereum:${tokenAddress}`;
41+
const { data } = await axios.get(`https://coins.llama.fi/prices/current/${priceKey}`);
42+
return data.coins[priceKey].price;
43+
} catch (error) {
44+
console.error(`Error fetching price for ${tokenAddress}:`, error);
45+
throw error;
46+
}
47+
};
48+
49+
const calculateStUSRApy = (logDescription) => {
50+
const { totalUSRBefore, totalUSRAfter, totalShares } = logDescription.args;
51+
const sharesRateBefore = totalUSRBefore / totalShares;
52+
const sharesRateAfter = totalUSRAfter / totalShares;
1953
return ((sharesRateAfter - sharesRateBefore) / sharesRateBefore) * 365;
2054
};
21-
const apy = async () => {
22-
const totalSupply = (
23-
await sdk.api.abi.call({
24-
target: stUSR,
25-
abi: 'erc20:totalSupply',
26-
chain: 'ethereum'
27-
})
28-
).output / 1e18;
29-
30-
const priceKey = `ethereum:${USR}`;
31-
const price = (
32-
await axios.get(`https://coins.llama.fi/prices/current/${priceKey}`)
33-
).data.coins[priceKey].price;
34-
35-
const tvl = totalSupply * price;
36-
37-
const currentBlock = await sdk.api.util.getLatestBlock('ethereum');
38-
const currentDate = new Date(currentBlock.timestamp * 1000);
39-
40-
const startOfDay = new Date(currentDate).setHours(0, 0, 0, 0);
41-
const previousStartOfDay = startOfDay - 24 * 60 * 60 * 1000; // Subtract 1 day in milliseconds
42-
43-
const [fromBlock] = await utils.getBlocksByTime([previousStartOfDay / 1000], 'ethereum');
44-
const toBlock = currentBlock.block;
45-
const logs = (
46-
await sdk.api.util.getLogs({
47-
target: rewardDistributor,
48-
topic: '',
49-
toBlock: toBlock,
50-
fromBlock: fromBlock,
51-
keys: [],
55+
56+
const stUsrPool = async () => {
57+
try {
58+
const totalSupply = await getTotalSupply(stUSR);
59+
const price = await getTokenPrice(USR);
60+
const tvl = totalSupply * price;
61+
62+
const currentBlock = await sdk.api.util.getLatestBlock('ethereum');
63+
const currentDate = new Date(currentBlock.timestamp * 1000);
64+
const previousStartOfDay = new Date(currentDate).setHours(0, 0, 0, 0) - DAY_IN_MS;
65+
66+
const [fromBlock] = await utils.getBlocksByTime([previousStartOfDay / 1000], 'ethereum');
67+
const toBlock = currentBlock.block;
68+
69+
const logs = (
70+
await sdk.api.util.getLogs({
71+
target: rewardDistributor,
72+
topic: '',
73+
fromBlock,
74+
toBlock,
75+
keys: [],
76+
chain: 'ethereum',
77+
topics: [topic0rewardDistributed]
78+
})
79+
).output.sort((a, b) => a.blockNumber - b.blockNumber);
80+
81+
let aprBase = 0;
82+
if (logs.length > 0) {
83+
const parsedLog = rewardDistributedInterface.parseLog(logs[logs.length - 1]);
84+
aprBase = calculateStUSRApy(parsedLog);
85+
}
86+
87+
return {
88+
pool: stUSR,
89+
symbol: 'stUSR',
90+
chain: 'ethereum',
91+
project: 'resolv',
92+
tvlUsd: tvl,
93+
apyBase: aprBase * 100
94+
};
95+
} catch (error) {
96+
console.error('Error fetching stUSR pool data:', error);
97+
throw error;
98+
}
99+
};
100+
101+
const rlpPool = async () => {
102+
try {
103+
const totalSupply = await getTotalSupply(RLP);
104+
const currentBlock = await sdk.api.util.getLatestBlock('ethereum');
105+
const currentDate = new Date(currentBlock.timestamp * 1000);
106+
const previousStartOfDay = new Date(currentDate).setHours(0, 0, 0, 0) - 2 * DAY_IN_MS;
107+
108+
const [fromBlock] = await utils.getBlocksByTime([previousStartOfDay / 1000], 'ethereum');
109+
const toBlock = currentBlock.block;
110+
111+
const logs = (
112+
await sdk.api.util.getLogs({
113+
target: rlpPriceStorage,
114+
topic: '',
115+
fromBlock,
116+
toBlock,
117+
keys: [],
118+
chain: 'ethereum',
119+
topics: [topic0priceSet]
120+
})
121+
).output.sort((a, b) => a.blockNumber - b.blockNumber);
122+
123+
let aprBase = 0;
124+
if (logs.length >= 2) {
125+
const lastLpPrice = priceSetInterface.parseLog(logs[logs.length - 1]).args.price;
126+
const previousLpPrice = priceSetInterface.parseLog(logs[logs.length - 2]).args.price;
127+
128+
aprBase = ((lastLpPrice - previousLpPrice) / previousLpPrice) * 365;
129+
}
130+
131+
const price = logs.length > 0
132+
? priceSetInterface.parseLog(logs[logs.length - 1]).args.price / 1e18
133+
: await getTokenPrice(RLP);
134+
const tvl = totalSupply * price;
135+
136+
return {
137+
pool: RLP,
138+
symbol: 'RLP',
52139
chain: 'ethereum',
53-
topics: [topic0rewardDistributed]
54-
})
55-
).output.sort((a, b) => a.blockNumber - b.blockNumber);
56-
57-
let aprBase = 0;
58-
if (logs.length > 0) {
59-
const logToProcess = logs.length === 2 ? logs[logs.length - 1] : logs[0];
60-
const parsedLog = rewardDistributedInterface.parseLog(logToProcess);
61-
aprBase = calculateApy(parsedLog);
140+
project: 'resolv',
141+
tvlUsd: tvl,
142+
apyBase: aprBase * 100
143+
};
144+
} catch (error) {
145+
console.error('Error fetching RLP pool data:', error);
146+
throw error;
62147
}
148+
};
63149

64-
return [{
65-
pool: stUSR,
66-
symbol: 'stUSR',
67-
chain: 'ethereum',
68-
project: 'resolv',
69-
tvlUsd: tvl,
70-
apyBase: aprBase * 100
71-
}];
150+
const apy = async () => {
151+
try {
152+
return [await stUsrPool(), await rlpPool()];
153+
} catch (error) {
154+
console.error('Error fetching APYs:', error);
155+
throw error;
156+
}
72157
};
73158

74159
module.exports = {

0 commit comments

Comments
 (0)