Repository navigation
swell-liquid-restaking: leave the rate moves of reprices that minted no fee shares out of the apy #3111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
swell-liquid-restaking: leave the rate moves of reprices that minted no fee shares out of the apy #3111
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
4aa1d55
swell-liquid-restaking: divide out the rate moves of reprices that mi…
0xshubhs 08b61d5
swell: share the backed reprice apy with swETH, price tvl in eth, and…
0xshubhs 676a3d7
swell: check the newest Reprice log against the contract's own last-r…
0xshubhs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| const sdk = require('@defillama/sdk'); | ||
| const { BigNumber } = require('ethers'); | ||
|
|
||
| const { getPriceApiData } = require('../utils'); | ||
|
|
||
| const WAD = 10n ** 18n; | ||
|
|
||
| const repriceEvent = (rateField) => | ||
| `event Reprice(uint256 newEthReserves, uint256 ${rateField}, uint256 nodeOperatorRewards, uint256 swellTreasuryRewards, uint256 totalETHDeposited)`; | ||
|
|
||
| async function getRepriceEvents(token, rateField) { | ||
| const timestampNow = Math.floor(Date.now() / 1000); | ||
| const timestamp14dayAgo = timestampNow - 86400 * 14; | ||
|
|
||
| const blockNow = await sdk.api.util.getLatestBlock('ethereum'); | ||
| const block14dayAgo = (await getPriceApiData(`/block/ethereum/${timestamp14dayAgo}`)).height; | ||
|
|
||
| const [repriceEvents, treasuryPct, nodeOperatorPct] = await Promise.all([ | ||
| sdk.getEventLogs({ | ||
| target: token, | ||
| eventAbi: repriceEvent(rateField), | ||
| fromBlock: block14dayAgo, | ||
| toBlock: blockNow.number, | ||
| chain: 'ethereum', | ||
| }), | ||
| ...['swellTreasuryRewardPercentage', 'nodeOperatorRewardPercentage'].map(async (name) => | ||
| BigInt((await sdk.api.abi.call({ target: token, abi: `uint256:${name}` })).output) | ||
| ), | ||
| ]); | ||
| const chargesFees = treasuryPct + nodeOperatorPct > 0n; | ||
|
|
||
| repriceEvents.sort((a, b) => Number(a.blockNumber) - Number(b.blockNumber)); | ||
| let prevRate; | ||
| let unbacked = WAD; | ||
| return repriceEvents.map((ev) => { | ||
| const rate = BigInt(ev.args[rateField]); | ||
| const feeShares = BigInt(ev.args.nodeOperatorRewards) + BigInt(ev.args.swellTreasuryRewards); | ||
| if (prevRate !== undefined && chargesFees && feeShares === 0n) unbacked = (unbacked * rate) / prevRate; | ||
| prevRate = rate; | ||
| ev.backedRate = ((rate * WAD) / unbacked).toString(); | ||
| return ev; | ||
| }); | ||
| } | ||
|
|
||
| function calcRate(eventNow, eventPrev) { | ||
| const rateDelta = Number(eventNow.backedRate) / Number(eventPrev.backedRate) - 1; | ||
| const blockDelta = Number(eventNow.blockNumber) - Number(eventPrev.blockNumber); | ||
| const timeDeltaSeconds = blockDelta * 12; | ||
| return (rateDelta * 365 * 100) / (timeDeltaSeconds / 86400); | ||
| } | ||
|
|
||
| async function getClosestTo7dAgo(repriceEvents) { | ||
| const timestampNow = Math.floor(Date.now() / 1000); | ||
| const timestamp7DaysAgo = timestampNow - 86400 * 7; | ||
|
|
||
| let closestBefore = null; | ||
| let closestAfter = null; | ||
| let minDiffBefore = Infinity; | ||
| let minDiffAfter = Infinity; | ||
|
|
||
| for (const repriceEvent of repriceEvents) { | ||
| const eventTimestamp = await sdk.api.util.getTimestamp(repriceEvent.blockNumber, 'ethereum'); | ||
| const timeDiff = eventTimestamp - timestamp7DaysAgo; | ||
|
|
||
| if (timeDiff <= 0 && Math.abs(timeDiff) < minDiffBefore) { | ||
| minDiffBefore = Math.abs(timeDiff); | ||
| closestBefore = repriceEvent; | ||
| } else if (timeDiff > 0 && timeDiff < minDiffAfter) { | ||
| minDiffAfter = timeDiff; | ||
| closestAfter = repriceEvent; | ||
| } | ||
| } | ||
|
|
||
| return { closestBefore, closestAfter, timestamp7DaysAgo }; | ||
| } | ||
|
|
||
| async function interpolate7d(closestBefore, closestAfter, timestamp7DaysAgo) { | ||
| const beforeTimestamp = await sdk.api.util.getTimestamp(closestBefore.blockNumber, 'ethereum'); | ||
| const afterTimestamp = await sdk.api.util.getTimestamp(closestAfter.blockNumber, 'ethereum'); | ||
|
|
||
| const timeDiff = afterTimestamp - beforeTimestamp; | ||
| const rateBefore = BigNumber.from(closestBefore.backedRate); | ||
| const rateAfter = BigNumber.from(closestAfter.backedRate); | ||
|
|
||
| const rateDiff = rateAfter.sub(rateBefore); | ||
| const timeRatio = BigNumber.from(Math.floor((timestamp7DaysAgo - beforeTimestamp) / timeDiff * 1e18).toString()); | ||
|
|
||
| return rateBefore.add(rateDiff.mul(timeRatio).div(BigNumber.from(10).pow(18))); | ||
| } | ||
|
|
||
| async function getRepriceApy(token, rateField) { | ||
| const repriceEvents = await getRepriceEvents(token, rateField); | ||
| if (repriceEvents.length < 2) return { apyBase: 0 }; | ||
| repriceEvents.sort((a, b) => Number(b.blockNumber) - Number(a.blockNumber)); | ||
|
|
||
| const eventNow = repriceEvents[0]; | ||
| const apyBase = calcRate(eventNow, repriceEvents[1]); | ||
|
|
||
| const { closestBefore, closestAfter, timestamp7DaysAgo } = await getClosestTo7dAgo(repriceEvents); | ||
| if (!closestBefore || !closestAfter) return { apyBase }; | ||
|
|
||
| const interpolatedRate = await interpolate7d(closestBefore, closestAfter, timestamp7DaysAgo); | ||
| const endTime = await sdk.api.util.getTimestamp(eventNow.blockNumber, 'ethereum'); | ||
| const currentRate = BigNumber.from(eventNow.backedRate); | ||
| const sevenDayRateChange = currentRate.mul(WAD.toString()).div(interpolatedRate).sub(WAD.toString()); | ||
| const timeElapsed = BigNumber.from(endTime - timestamp7DaysAgo); | ||
| const apyBase7d = sevenDayRateChange.mul(365 * 86400).div(timeElapsed).mul(100).toString() / 1e18; | ||
|
|
||
| return { apyBase, apyBase7d }; | ||
| } | ||
|
|
||
| async function getTvl(token) { | ||
| const [totalSupply, rate] = await Promise.all( | ||
| ['totalSupply', 'getRate'].map(async (name) => | ||
| (await sdk.api.abi.call({ target: token, abi: `uint256:${name}` })).output / 1e18 | ||
| ) | ||
| ); | ||
| const ethPrice = (await getPriceApiData('/prices/current/coingecko:ethereum')).coins['coingecko:ethereum'].price; | ||
| return { tvlUsd: totalSupply * rate * ethPrice, rate }; | ||
| } | ||
|
|
||
| module.exports = { getRepriceApy, getTvl }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.