Skip to content

Commit

Permalink
refactor: splut out and test calculateStakedPctOfSupply
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandermendes committed Nov 14, 2024
1 parent 98b8494 commit ecd6041
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 132 deletions.
10 changes: 5 additions & 5 deletions src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { useAccount, useChainId, useSwitchChain } from 'wagmi';
import { parseFixed } from '@ethersproject/bignumber';
import {
approveStaking,
calculateStakedPctOfSupply,
claim,
claimAll,
getDeposit,
Expand Down Expand Up @@ -40,6 +39,7 @@ import { erc20abi } from './abis/erc20.js';
import { AuroraNetwork } from './types/network.js';
import { config } from './config.js';
import { logger } from './logger.js';
import { calculateStakedPctOfSupply } from './utils/supply.js';

type StakingProviderProps = {
network: AuroraNetwork;
Expand Down Expand Up @@ -319,11 +319,11 @@ export const StakingProvider = ({
return;
}

const newStakedPct = calculateStakedPctOfSupply(
const newStakedPct = calculateStakedPctOfSupply({
totalStaked,
newPrice,
newMarketCap,
);
auroraPrice: newPrice,
auroraMarketCap: newMarketCap,
});

setStakedPct(newStakedPct);
}, [
Expand Down
13 changes: 0 additions & 13 deletions src/utils/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,6 @@ export const getVoteSupply = (voteSchedule: StreamSchedule): BigNumber => {
return circulatingSupply;
};

export const calculateStakedPctOfSupply = (
totalStaked: BigNumber,
auroraPrice: number,
auroraMarketCap: number,
): number => {
const circulatingSupply = auroraMarketCap / auroraPrice;
// Compounding staked AURORA
const stakedAurora = Number(ethers.utils.formatUnits(totalStaked, 18));
const pct = (stakedAurora * 100) / circulatingSupply;

return pct;
};

export const getIsPaused = async (
provider: providers.JsonRpcProvider,
networkConfig: AuroraNetworkConfig,
Expand Down
20 changes: 20 additions & 0 deletions src/utils/supply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { BigNumber, ethers } from 'ethers';

export const calculateStakedPctOfSupply = ({
totalStaked,
auroraPrice,
auroraMarketCap,
}: {
totalStaked: BigNumber;
auroraPrice: number;
auroraMarketCap: number;
}): number => {
const circulatingSupply = auroraMarketCap / auroraPrice;
const compoundingStakedAurora = Number(
ethers.utils.formatUnits(totalStaked, 18),
);

const pct = (compoundingStakedAurora * 100) / circulatingSupply;

return pct;
};
115 changes: 1 addition & 114 deletions tests/utils/apr.test.ts → tests/fixtures/streams-schedule.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { BigNumber } from 'ethers';
import { calculateAprs } from '../../src/utils/apr';

jest.useFakeTimers();

const currentDate = new Date('2024-11-13T12:00:00Z');

// A snapshot of data logged from Aurora Plus
const streamsSchedule = [
export const streamsSchedule = [
{
scheduleTimes: [
BigNumber.from('0x6283b870'),
Expand Down Expand Up @@ -118,111 +113,3 @@ const streamsSchedule = [
],
},
];

const totalStaked = BigNumber.from('50000000000000000000000000');
const streamDecimals = [18, 18, 18, 18, 18, 18];
const streamPrices = [
0.135344, 0.00007611, 0.00141883, 0.00000127, 0.245603, 0,
];

describe('APR', () => {
beforeEach(() => {
jest.setSystemTime(new Date(currentDate));
});

describe('calculateAprs', () => {
it('returns the expected APR', async () => {
const result = calculateAprs({
streamsSchedule,
streamPrices,
streamDecimals,
totalStaked,
});

expect(result).toEqual({
aurora: 13.65590130885746,
streams: [0, 0, 0, 0, 0],
total: 13.65590130885746,
});
});

it('modifies the APR according to the total amount staked', async () => {
const result = calculateAprs({
streamsSchedule,
streamPrices,
streamDecimals,
totalStaked: BigNumber.from('100000000000000000000000000'),
});

expect(result).toEqual({
aurora: 6.82795065442873,
streams: [0, 0, 0, 0, 0],
total: 6.82795065442873,
});
});

it('returns an APR of 0 if all rewards are active after the current date', async () => {
jest.setSystemTime(new Date('2020-01-01T12:00:00Z'));

const result = calculateAprs({
streamsSchedule,
streamPrices,
streamDecimals,
totalStaked,
});

expect(result).toEqual({
aurora: 0,
streams: [0, 0, 0, 0, 0],
total: 0,
});
});

it('returns an APR of 0 if all rewards were active before the current date', async () => {
jest.setSystemTime(new Date('2030-01-01T12:00:00Z'));

const result = calculateAprs({
streamsSchedule,
streamPrices,
streamDecimals,
totalStaked,
});

expect(result).toEqual({
aurora: 0,
streams: [0, 0, 0, 0, 0],
total: 0,
});
});

it('returns an APR of 0 if all stream prices are zero', async () => {
jest.setSystemTime(new Date('2020-01-01T12:00:00Z'));

const result = calculateAprs({
streamsSchedule,
streamPrices: Array(streamPrices.length).fill(0),
streamDecimals,
totalStaked,
});

expect(result).toEqual({
aurora: 0,
streams: [0, 0, 0, 0, 0],
total: 0,
});
});

it('throws if a stream price is missing', async () => {
jest.setSystemTime(new Date('2020-01-01T12:00:00Z'));

expect(() => {
calculateAprs({
streamsSchedule,
streamPrices: [],
streamDecimals,
totalStaked,
});
}).toThrow('No stream price at position 0');
});
});
});
116 changes: 116 additions & 0 deletions tests/specs/utils/apr.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { BigNumber } from 'ethers';
import { calculateAprs } from '../../../src/utils/apr';
import { streamsSchedule } from '../../fixtures/streams-schedule';
import { getNumberTo18Decimals } from '../../utils/get-number-to-18-decimals';

jest.useFakeTimers();

const currentDate = new Date('2024-11-13T12:00:00Z');

const totalStaked = getNumberTo18Decimals(50000000);
const streamDecimals = [18, 18, 18, 18, 18, 18];
const streamPrices = [
0.135344, 0.00007611, 0.00141883, 0.00000127, 0.245603, 0,
];

describe('APR', () => {
beforeEach(() => {
jest.setSystemTime(new Date(currentDate));
});

describe('calculateAprs', () => {
it('returns the expected APR', async () => {
const result = calculateAprs({
streamsSchedule,
streamPrices,
streamDecimals,
totalStaked,
});

expect(result).toEqual({
aurora: 13.65590130885746,
streams: [0, 0, 0, 0, 0],
total: 13.65590130885746,
});
});

it('modifies the APR according to the total amount staked', async () => {
const result = calculateAprs({
streamsSchedule,
streamPrices,
streamDecimals,
totalStaked: BigNumber.from('100000000000000000000000000'),
});

expect(result).toEqual({
aurora: 6.82795065442873,
streams: [0, 0, 0, 0, 0],
total: 6.82795065442873,
});
});

it('returns an APR of 0 if all rewards are active after the current date', async () => {
jest.setSystemTime(new Date('2020-01-01T12:00:00Z'));

const result = calculateAprs({
streamsSchedule,
streamPrices,
streamDecimals,
totalStaked,
});

expect(result).toEqual({
aurora: 0,
streams: [0, 0, 0, 0, 0],
total: 0,
});
});

it('returns an APR of 0 if all rewards were active before the current date', async () => {
jest.setSystemTime(new Date('2030-01-01T12:00:00Z'));

const result = calculateAprs({
streamsSchedule,
streamPrices,
streamDecimals,
totalStaked,
});

expect(result).toEqual({
aurora: 0,
streams: [0, 0, 0, 0, 0],
total: 0,
});
});

it('returns an APR of 0 if all stream prices are zero', async () => {
jest.setSystemTime(new Date('2020-01-01T12:00:00Z'));

const result = calculateAprs({
streamsSchedule,
streamPrices: Array(streamPrices.length).fill(0),
streamDecimals,
totalStaked,
});

expect(result).toEqual({
aurora: 0,
streams: [0, 0, 0, 0, 0],
total: 0,
});
});

it('throws if a stream price is missing', async () => {
jest.setSystemTime(new Date('2020-01-01T12:00:00Z'));

expect(() => {
calculateAprs({
streamsSchedule,
streamPrices: [],
streamDecimals,
totalStaked,
});
}).toThrow('No stream price at position 0');
});
});
});
26 changes: 26 additions & 0 deletions tests/specs/utils/supply.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { calculateStakedPctOfSupply } from '../../../src/utils/supply';
import { getNumberTo18Decimals } from '../../utils/get-number-to-18-decimals';

describe('APR', () => {
describe('calculateStakedPctOfSupply', () => {
it('returns the expected result with 1% staked', () => {
const result = calculateStakedPctOfSupply({
totalStaked: getNumberTo18Decimals(1000),
auroraPrice: 0.1,
auroraMarketCap: 10000,
});

expect(result).toBe(1);
});

it('returns the expected result with 50% staked', () => {
const result = calculateStakedPctOfSupply({
totalStaked: getNumberTo18Decimals(50000),
auroraPrice: 0.1,
auroraMarketCap: 10000,
});

expect(result).toBe(50);
});
});
});
5 changes: 5 additions & 0 deletions tests/utils/get-number-to-18-decimals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { BigNumber } from 'ethers';

export const getNumberTo18Decimals = (number: number) => {
return BigNumber.from(`${number}${'0'.repeat(18)}`);
};

0 comments on commit ecd6041

Please sign in to comment.