From 20329ca17ec5c3ec9113651ccea1763eeee51a4b Mon Sep 17 00:00:00 2001 From: Apotheosis <97164662+0xApotheosis@users.noreply.github.com> Date: Thu, 12 Sep 2024 10:21:13 +1000 Subject: [PATCH] chore: add another legacy test --- test/LegacyTests.ts | 44 ++++++++++++++++++++++++++++------ test/utils.ts | 58 ++++----------------------------------------- 2 files changed, 41 insertions(+), 61 deletions(-) diff --git a/test/LegacyTests.ts b/test/LegacyTests.ts index d6caa92..e4331b7 100644 --- a/test/LegacyTests.ts +++ b/test/LegacyTests.ts @@ -1,11 +1,41 @@ -import { deployStakingRewardsFixture, loadFixtureWithGas } from './utils'; +import { loadFixture, time } from '@nomicfoundation/hardhat-toolbox-viem/network-helpers'; +import { deployStakingRewardsFixture, REWARDS_DURATION_SECONDS } from './utils'; import { expect } from 'chai'; - +import { parseEther } from 'viem'; +import hre from 'hardhat'; describe('StakingRewards', () => { - describe('Deployment', function () { - it('deploy cost [ @skip-on-coverage ]', async () => { - const { gasUsed } = await loadFixtureWithGas(deployStakingRewardsFixture); - expect(gasUsed.toString()).to.eq('2388750'); - }); + it('gets rewardsDuration', async () => { + const { stakingRewards } = await loadFixture(deployStakingRewardsFixture); + expect(await stakingRewards.read.rewardsDuration()).to.equal(REWARDS_DURATION_SECONDS); + }); + + it('should distribute full rewards for staking entire period', async () => { + const { stakingRewards, stakingToken, rewardsToken, stakingAccount1, rewardsDistribution } = await loadFixture(deployStakingRewardsFixture); + + // Setup + const startTime = await time.latest(); + const rewardAmount = parseEther('10'); + const stakeAmount = parseEther('2'); + await stakingToken.write.transfer([stakingAccount1.account.address, stakeAmount], { account: stakingAccount1.account }); + await stakingToken.write.approve([stakingRewards.address, stakeAmount], { account: stakingAccount1.account.address }); + await rewardsToken.write.transfer([stakingRewards.address, rewardAmount], { account: rewardsDistribution.account }); + + // Stake + await stakingRewards.write.stake([stakeAmount], { account: stakingAccount1.account.address }); + + // Start rewards + await stakingRewards.write.notifyRewardAmount([rewardAmount], { account: rewardsDistribution.account.address } ); + + // Fast-forward to end of reward period + await time.increaseTo(startTime + Number(REWARDS_DURATION_SECONDS)); + + // Unstake and claim rewards + await stakingRewards.write.exit([], { account: stakingAccount1.account }); + + // Assertions + const rewardAmountRead = await rewardsToken.read.balanceOf([stakingAccount1.account.address]); + const expectedReward = Number(rewardAmount); + + expect(Number(rewardAmountRead)).to.be.closeTo(expectedReward, expectedReward / 10000); // Within 0.01% }); }); diff --git a/test/utils.ts b/test/utils.ts index a72eeba..3968b6e 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -1,9 +1,12 @@ import { BigNumber, providers, utils, Contract } from 'ethers'; import { parseEther } from 'viem'; import hre from 'hardhat'; -import { takeSnapshot } from '@nomicfoundation/hardhat-toolbox-viem/network-helpers'; export const DEFAULT_SUPPLY = 1e8; +export const REWARDS_DURATION_SECONDS = 60n * 60n * 24n * 7n; // 7 days +const PERMIT_TYPEHASH = utils.keccak256( + utils.toUtf8Bytes('Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)'), +); export async function mockToken({ accounts, @@ -89,57 +92,6 @@ export async function deployStakingRewardsFixture() { }; } - type Fixture = () => Promise; - - interface Snapshot { - restorer: { restore: () => Promise; snapshotId: string }; - fixture: Fixture; - data: T; - gasUsed: bigint; - } - -let snapshots: Snapshot[] = []; - -// This has been adapted from @nomicfoundation/hardhat-network-helpers/src/loadFixture.ts to include the gas used during the fixture deployment. -export async function loadFixtureWithGas(fixture: Fixture): Promise { - if (fixture.name === '') { - throw new Error('FixtureAnonymousFunctionError'); - } - - const snapshot = snapshots.find((s) => s.fixture === fixture); - - if (snapshot !== undefined) { - await snapshot.restorer.restore(); - snapshots = snapshots.filter( - (s) => - Number(s.restorer.snapshotId) <= Number(snapshot.restorer.snapshotId), - ); - - return { ...snapshot.data as T, gasUsed: snapshot.gasUsed }; - } else { - const client = await hre.viem.getPublicClient(); - const initialGasUsed = await client.getBlockNumber().then(bn => client.getBlock({ blockNumber: bn })).then(b => b.gasUsed); - const data = await fixture(); - const finalGasUsed = await client.getBlockNumber().then(bn => client.getBlock({ blockNumber: bn })).then(b => b.gasUsed); - const gasUsed = finalGasUsed - initialGasUsed; - - const restorer = await takeSnapshot(); - - snapshots.push({ - restorer, - fixture, - data, - gasUsed, - }); - - return { ...data, gasUsed }; - } -} - -const PERMIT_TYPEHASH = utils.keccak256( - utils.toUtf8Bytes('Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)'), -); - function getDomainSeparator(name: string, tokenAddress: string, chainId: number) { return utils.keccak256( utils.defaultAbiCoder.encode( @@ -188,8 +140,6 @@ export async function getApprovalDigest( ); } -export const REWARDS_DURATION = 60 * 60 * 24 * 135; - export function expandTo18Decimals(n: number): BigNumber { return BigNumber.from(n).mul(BigNumber.from(10).pow(18)); }