-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(neuron-ui): add global apy estimation (#1092)
* feat(neuron-ui): add global apy estimation * test(neuron-ui): fix the test of calculateGlobalAPY
- Loading branch information
Showing
7 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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
12 changes: 12 additions & 0 deletions
12
packages/neuron-ui/src/tests/calculation/calculateGlobalAPY/fixtures.ts
This file contains 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,12 @@ | ||
export default { | ||
'return 0 if the genesis block is not loaded': { | ||
currentTime: Date.now(), | ||
genesisTime: undefined, | ||
expectAPY: 0, | ||
}, | ||
'one period and one handrand days': { | ||
currentTime: new Date('2023-04-10').getTime(), | ||
genesisTime: new Date('2019-01-01').getTime(), | ||
expectAPY: 0.02369552868619654, | ||
}, | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/neuron-ui/src/tests/calculation/calculateGlobalAPY/index.test.ts
This file contains 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,16 @@ | ||
import calculateGlobalAPY from 'utils/calculateGlobalAPY' | ||
import fixtures from './fixtures' | ||
|
||
describe('calculate the global apy', () => { | ||
const fixtureTable = Object.entries(fixtures).map(([title, { currentTime, genesisTime, expectAPY }]) => [ | ||
title, | ||
currentTime, | ||
genesisTime, | ||
expectAPY, | ||
]) | ||
|
||
test.each(fixtureTable)(`%s`, async (_title, currentTime, genesisTime, expectAPY) => { | ||
const apy = await calculateGlobalAPY(currentTime, genesisTime) | ||
expect(apy).toBe(expectAPY === 0 ? 0 : +expectAPY.toFixed(2)) | ||
}) | ||
}) |
This file contains 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 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,48 @@ | ||
import { getBlockByNumber } from '../services/chain' | ||
|
||
const INITIAL_OFFER = BigInt(33600000000) | ||
const SECONDARY_OFFER = BigInt(1344000000) | ||
const DAYS_PER_PERIOD = 365 * 4 * 1 | ||
const MILLI_SECONDS_PER_DAY = 24 * 3600 * 1000 | ||
const PERIOD_LENGTH = DAYS_PER_PERIOD * MILLI_SECONDS_PER_DAY | ||
|
||
let cachedGenesisTimestamp: number | undefined | ||
|
||
export default async (now: number, initialTimestamp: number | undefined = cachedGenesisTimestamp) => { | ||
let genesisTimestamp = initialTimestamp | ||
if (genesisTimestamp === undefined) { | ||
genesisTimestamp = await getBlockByNumber('0x0') | ||
.then(b => { | ||
cachedGenesisTimestamp = +b.header.timestamp | ||
return cachedGenesisTimestamp | ||
}) | ||
.catch(() => undefined) | ||
} | ||
if (genesisTimestamp === undefined || now <= genesisTimestamp) { | ||
return 0 | ||
} | ||
|
||
const pastPeriods = BigInt(now - genesisTimestamp) / BigInt(PERIOD_LENGTH) | ||
const pastDays = Math.ceil(((now - genesisTimestamp) % PERIOD_LENGTH) / MILLI_SECONDS_PER_DAY) | ||
|
||
const realSecondaryOffer = | ||
BigInt(4) * SECONDARY_OFFER * pastPeriods + | ||
(BigInt(4) * SECONDARY_OFFER * BigInt(pastDays)) / BigInt(DAYS_PER_PERIOD) | ||
|
||
let realPrimaryOffer = BigInt(0) | ||
|
||
let PRIMARY_OFFER = INITIAL_OFFER | ||
for (let i = 0; i < Number(pastPeriods); i++) { | ||
PRIMARY_OFFER /= BigInt(2) | ||
const offer = PRIMARY_OFFER | ||
realPrimaryOffer += offer | ||
} | ||
|
||
PRIMARY_OFFER /= BigInt(2) | ||
|
||
const primaryOfferFraction = (BigInt(pastDays) * PRIMARY_OFFER) / BigInt(DAYS_PER_PERIOD) | ||
realPrimaryOffer += primaryOfferFraction | ||
|
||
const totalOffer = INITIAL_OFFER + realPrimaryOffer + realSecondaryOffer | ||
return +(Number(SECONDARY_OFFER) / Number(totalOffer)).toFixed(2) | ||
} |