Skip to content

Commit

Permalink
chore: add another legacy test
Browse files Browse the repository at this point in the history
  • Loading branch information
0xApotheosis committed Sep 12, 2024
1 parent 7dc8223 commit 20329ca
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 61 deletions.
44 changes: 37 additions & 7 deletions test/LegacyTests.ts
Original file line number Diff line number Diff line change
@@ -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%
});
});
58 changes: 4 additions & 54 deletions test/utils.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -89,57 +92,6 @@ export async function deployStakingRewardsFixture() {
};
}

type Fixture<T> = () => Promise<T>;

interface Snapshot<T> {
restorer: { restore: () => Promise<void>; snapshotId: string };
fixture: Fixture<T>;
data: T;
gasUsed: bigint;
}

let snapshots: Snapshot<unknown>[] = [];

// 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<T>(fixture: Fixture<T>): Promise<T & { gasUsed: bigint }> {
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(
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 20329ca

Please sign in to comment.