From ffcd6d1b9a948a14179bd0e4f443d315b0cbc73c Mon Sep 17 00:00:00 2001 From: Keith Date: Thu, 14 Nov 2019 10:38:58 +0800 Subject: [PATCH 1/3] refactor(neuron-ui): improve precision of calculateAPY --- packages/neuron-ui/src/utils/calculateAPY.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/neuron-ui/src/utils/calculateAPY.ts b/packages/neuron-ui/src/utils/calculateAPY.ts index b4bec8fc05..f06be346bf 100644 --- a/packages/neuron-ui/src/utils/calculateAPY.ts +++ b/packages/neuron-ui/src/utils/calculateAPY.ts @@ -1,7 +1,7 @@ const YEAR = 365 * 24 * 60 * 60 * 1000 +const BASE = 10000000 export default (interest: string, amount: string, duration: string) => { - const BASE = 10000 - const v = +((BigInt(interest) * BigInt(BASE)) / BigInt(amount)).toString() * (YEAR / +duration) - return `${(v / BASE).toFixed(2)}` + const v = (BigInt(interest) * BigInt(YEAR) * BigInt(BASE)) / (BigInt(amount) * BigInt(duration)) + return `${(Number(v) / BASE).toFixed(2)}` } From c24c809de14688fd160788c82964e91062dc40df Mon Sep 17 00:00:00 2001 From: Keith Date: Thu, 14 Nov 2019 11:16:01 +0800 Subject: [PATCH 2/3] fix(neuron-ui): display percentage of apy --- packages/neuron-ui/src/utils/calculateAPY.ts | 2 +- packages/neuron-ui/src/utils/calculateGlobalAPY.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/neuron-ui/src/utils/calculateAPY.ts b/packages/neuron-ui/src/utils/calculateAPY.ts index f06be346bf..0748ea8c8b 100644 --- a/packages/neuron-ui/src/utils/calculateAPY.ts +++ b/packages/neuron-ui/src/utils/calculateAPY.ts @@ -3,5 +3,5 @@ const BASE = 10000000 export default (interest: string, amount: string, duration: string) => { const v = (BigInt(interest) * BigInt(YEAR) * BigInt(BASE)) / (BigInt(amount) * BigInt(duration)) - return `${(Number(v) / BASE).toFixed(2)}` + return `${(Number(v) / (BASE / 100)).toFixed(2)}` } diff --git a/packages/neuron-ui/src/utils/calculateGlobalAPY.ts b/packages/neuron-ui/src/utils/calculateGlobalAPY.ts index 4c189275e5..b80971bec1 100644 --- a/packages/neuron-ui/src/utils/calculateGlobalAPY.ts +++ b/packages/neuron-ui/src/utils/calculateGlobalAPY.ts @@ -44,5 +44,5 @@ export default async (now: number, initialTimestamp: number | undefined = cached realPrimaryOffer += primaryOfferFraction const totalOffer = INITIAL_OFFER + realPrimaryOffer + realSecondaryOffer - return +(Number(SECONDARY_OFFER) / Number(totalOffer)).toFixed(2) + return +(Number(SECONDARY_OFFER) / (Number(totalOffer) / 100)).toFixed(2) } From 05f6b6448cdf4f0d3c74ec920d0172ad7671d63d Mon Sep 17 00:00:00 2001 From: Keith Date: Thu, 14 Nov 2019 11:30:29 +0800 Subject: [PATCH 3/3] test(neuron-ui): fix tests of calculateGlobalAPY --- .../src/tests/calculation/calculateGlobalAPY/fixtures.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/neuron-ui/src/tests/calculation/calculateGlobalAPY/fixtures.ts b/packages/neuron-ui/src/tests/calculation/calculateGlobalAPY/fixtures.ts index 3b0eb42968..8033cbe2d6 100644 --- a/packages/neuron-ui/src/tests/calculation/calculateGlobalAPY/fixtures.ts +++ b/packages/neuron-ui/src/tests/calculation/calculateGlobalAPY/fixtures.ts @@ -7,6 +7,6 @@ export default { 'one period and one handrand days': { currentTime: new Date('2023-04-10').getTime(), genesisTime: new Date('2019-01-01').getTime(), - expectAPY: 0.02369552868619654, + expectAPY: 2.369552868619654, }, }