Skip to content

Commit

Permalink
ethena switch to 8h reward calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
slasher125 committed Mar 18, 2024
1 parent 8f9df80 commit b7628b8
Showing 1 changed file with 4 additions and 44 deletions.
48 changes: 4 additions & 44 deletions src/adaptors/ethena/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ const utils = require('../utils');
const USDe = '0x4c9edd5852cd905f086c759e8383e09bff1e68b3';
const sUSDe = '0x9D39A5DE30e57443BfF2A8307A4256c8797A3497';

const getWeekNb = (timestamp) => {
const date = new Date(timestamp * 1000);
const startOfYear = new Date(date.getFullYear(), 0, 1);
const pastDaysOfYear = (date - startOfYear) / 86400000;
const weekNumber = Math.ceil((pastDaysOfYear + startOfYear.getDay() + 1) / 7);

return weekNumber;
};

const apy = async () => {
const totalSupply =
(
Expand All @@ -30,22 +21,11 @@ const apy = async () => {

const tvlUsd = totalSupply * price;

// for apr, fetch rewards received from logs
// rewards are currently emitted weekly on thursday (though sometimes there are multiple tx within the same
// week)

// grab the last 7days of rewards
const currentBlock = await sdk.api.util.getLatestBlock('ethereum');
const toBlock = currentBlock.number;
const secondsInWeek = 7 * 60 * 60 * 24;
const timestampWeekAgo = currentBlock.timestamp - secondsInWeek;
const [fromBlock] = await utils.getBlocksByTime(
[timestampWeekAgo],
'ethereum'
);
const topic =
'0xbb28dd7cd6be6f61828ea9158a04c5182c716a946a6d2f31f4864edb87471aa6';
let logs = (
const logs = (
await sdk.api.util.getLogs({
target: '0x9D39A5DE30e57443BfF2A8307A4256c8797A3497',
topic: '',
Expand All @@ -56,30 +36,10 @@ const apy = async () => {
chain: 'ethereum',
})
).output.sort((a, b) => b.blockNumber - a.blockNumber);
// get the timestamp
logs = await Promise.all(
logs.map(async (i) => ({
...i,
timestamp: await sdk.api.util.getTimestamp(i.blockNumber),
}))
);

// extract the week
logs = logs.map((i) => ({ ...i, week: getWeekNb(i.timestamp) }));
// note: ethena changed the frequeny of rewards being send to the contract
// from weekly to every ~8h breaking the current calculation here.
// for now i'll default to using the previous weeks rewards (as these are complete).
// once enough
const previousWeek = [...new Set(logs.map((i) => i.week))].sort(
(a, b) => b - a
)[1];
const rewardsReceived = logs.filter((i) => i.week === previousWeek);
const rewardsReceivedSum = rewardsReceived.reduce(
(acc, i) => acc + parseInt(i.data / 1e18),
0
);

const aprBase = (rewardsReceivedSum / tvlUsd) * 52 * 100;
// rewards are now beeing streamed every 8hours, which we scale up to a year
const rewardsReceived = parseInt(logs[0].data / 1e18);
const aprBase = ((rewardsReceived * 3 * 365) / tvlUsd) * 100;
// weekly compoounding
const apyBase = utils.aprToApy(aprBase, 52);
return [
Expand Down

0 comments on commit b7628b8

Please sign in to comment.