Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Agilulfo1820 committed Jul 23, 2024
1 parent e3e82b8 commit 5ce629a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 34 deletions.
2 changes: 1 addition & 1 deletion apps/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"scripts": {
"test": "npx hardhat test --network hardhat ",
"test:solo": "npx hardhat test --network vechain_solo",
"contracts:test": "yarn test:solo",
"contracts:test": "yarn test",
"compile": "npx hardhat compile",
"deploy:solo": "npx hardhat run scripts --network vechain_solo",
"deploy:testnet": "npx hardhat run scripts --network vechain_testnet",
Expand Down
44 changes: 23 additions & 21 deletions apps/contracts/test/EcoEarn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ describe('ecoearn', () => {

describe('Allocations', () => {
it('Should track allocations for a cycle correctly', async () => {
const { ecoearn, token, owner, admin } = await getAndDeployContracts();
const { ecoearn, token, x2EarnRewardsPool, appId, owner, admin } = await getAndDeployContracts();

// Simulate receiving tokens from X Allocations Round
await token.connect(owner).mint(admin, ethers.parseEther('6700'));

// Allowance
await token.connect(admin).approve(await ecoearn.getAddress(), ethers.parseEther(Number.MAX_SAFE_INTEGER.toString()));
await token.connect(admin).approve(await x2EarnRewardsPool.getAddress(), ethers.parseEther('6700'));
await x2EarnRewardsPool.connect(admin).deposit(ethers.parseEther('6700'), appId);

// Claim allocation for the current cycle
await ecoearn.connect(admin).claimAllocation(await token.balanceOf(admin.address));

expect(await token.balanceOf(await ecoearn.getAddress())).to.equal(ethers.parseEther('6700'));
// Set rewards for the current cycle
await ecoearn.connect(admin).setRewardsAmount(await token.balanceOf(admin.address));

await waitForNextCycle(ecoearn); // Assure cycle can be triggered

Expand All @@ -35,17 +34,17 @@ describe('ecoearn', () => {
});

it('Should track allocations for multiple cycles correctly', async () => {
const { ecoearn, token, owner, admin } = await getAndDeployContracts();
const { ecoearn, token, x2EarnRewardsPool, appId, owner, admin } = await getAndDeployContracts();

await receiveAllocations(ecoearn, token, owner, admin, '6700');
await receiveAllocations(ecoearn, token, owner, admin, '6700', x2EarnRewardsPool, appId);

await waitForNextCycle(ecoearn);

await ecoearn.connect(admin).triggerCycle();

expect(await ecoearn.getCurrentCycle()).to.equal(1);

await receiveAllocations(ecoearn, token, owner, admin, '6700');
await receiveAllocations(ecoearn, token, owner, admin, '6700', x2EarnRewardsPool, appId);

await waitForNextCycle(ecoearn);

Expand All @@ -57,9 +56,9 @@ describe('ecoearn', () => {

describe('Rewards', () => {
it('Should track valid submissions correctly', async () => {
const { ecoearn, owner, admin, account3, token } = await getAndDeployContracts();
const { ecoearn, owner, admin, account3, token, x2EarnRewardsPool, appId } = await getAndDeployContracts();

await receiveAllocations(ecoearn, token, owner, admin, '6700');
await receiveAllocations(ecoearn, token, owner, admin, '6700', x2EarnRewardsPool, appId);

await waitForNextCycle(ecoearn);

Expand All @@ -79,21 +78,24 @@ describe('ecoearn', () => {
});

it('Should be able to receive expected rewards', async () => {
const { ecoearn, token, owner, account3, admin } = await getAndDeployContracts();
const { ecoearn, token, owner, account3, account4, admin, x2EarnRewardsPool, appId } = await getAndDeployContracts();

await receiveAllocations(ecoearn, token, owner, admin, '6700');
await receiveAllocations(ecoearn, token, owner, admin, '6700', x2EarnRewardsPool, appId);

await waitForNextCycle(ecoearn);

await ecoearn.connect(admin).triggerCycle();

await ecoearn.connect(admin).registerValidSubmission(owner.address, ethers.parseEther('1'));
expect(await token.balanceOf(account4.address)).to.equal(ethers.parseEther('0'));
expect(await token.balanceOf(account3.address)).to.equal(ethers.parseEther('0'));

await ecoearn.connect(admin).registerValidSubmission(account4.address, ethers.parseEther('1'));

await ecoearn.connect(admin).registerValidSubmission(account3.address, ethers.parseEther('1'));

await ecoearn.connect(admin).registerValidSubmission(owner.address, ethers.parseEther('1'));
await ecoearn.connect(admin).registerValidSubmission(account4.address, ethers.parseEther('1'));

expect(await token.balanceOf(owner.address)).to.equal(
expect(await token.balanceOf(account4.address)).to.equal(
ethers.parseEther('2'), // Received 2 tokens
);

Expand All @@ -103,9 +105,9 @@ describe('ecoearn', () => {
});

it('Should calculate correctly rewards left', async () => {
const { ecoearn, token, owner, account3, admin, account4, otherAccounts } = await getAndDeployContracts();
const { ecoearn, token, owner, account3, admin, account4, otherAccounts, x2EarnRewardsPool, appId } = await getAndDeployContracts();

await receiveAllocations(ecoearn, token, owner, admin, '50'); // Receive 50 tokens
await receiveAllocations(ecoearn, token, owner, admin, '50', x2EarnRewardsPool, appId); // Receive 50 tokens

await waitForNextCycle(ecoearn);

Expand All @@ -132,9 +134,9 @@ describe('ecoearn', () => {

describe('Withdrawals', () => {
it("Should be able to withdraw if user's did not claim all their rewards", async () => {
const { ecoearn, token, owner, admin, account3 } = await getAndDeployContracts();
const { ecoearn, token, owner, admin, account3, x2EarnRewardsPool, appId } = await getAndDeployContracts();

await receiveAllocations(ecoearn, token, owner, admin, '6700');
await receiveAllocations(ecoearn, token, owner, admin, '6700', x2EarnRewardsPool, appId);

await waitForNextCycle(ecoearn);

Expand All @@ -148,7 +150,7 @@ describe('ecoearn', () => {

await waitForNextCycle(ecoearn);

await receiveAllocations(ecoearn, token, owner, admin, '6700');
await receiveAllocations(ecoearn, token, owner, admin, '6700', x2EarnRewardsPool, appId);

await waitForNextCycle(ecoearn);

Expand Down
9 changes: 6 additions & 3 deletions apps/contracts/test/helpers/allocation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HardhatEthersSigner } from '@nomicfoundation/hardhat-ethers/signers';
import { EcoEarn, B3TR_Mock } from '../../typechain-types';
import { EcoEarn, B3TR_Mock, X2EarnRewardsPoolMock } from '../../typechain-types';
import { ethers } from 'ethers';

export const receiveAllocations = async (
Expand All @@ -8,10 +8,13 @@ export const receiveAllocations = async (
owner: HardhatEthersSigner,
admin: HardhatEthersSigner,
amount: string,
x2EarnRewardsPool: X2EarnRewardsPoolMock,
appId: string,
) => {
await token.connect(owner).mint(admin, ethers.parseEther(amount));

await token.connect(admin).approve(await mugshot.getAddress(), ethers.parseEther(Number.MAX_SAFE_INTEGER.toString()));
await token.connect(admin).approve(await x2EarnRewardsPool.getAddress(), ethers.parseEther(Number.MAX_SAFE_INTEGER.toString()));
await x2EarnRewardsPool.connect(admin).deposit(ethers.parseEther(amount), appId);

await mugshot.connect(admin).claimAllocation(ethers.parseEther(amount));
await mugshot.connect(admin).setRewardsAmount(ethers.parseEther(amount));
};
26 changes: 17 additions & 9 deletions apps/contracts/test/helpers/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@ export const getAndDeployContracts = async () => {
await x2EarnApps.waitForDeployment();

const X2EarnRewardsPoolContract = await ethers.getContractFactory('X2EarnRewardsPoolMock');
const x2EarnRewardsPool = await X2EarnRewardsPoolContract.deploy(owner.address, await rewardToken.getAddress(), await x2EarnApps.getAddress());
const x2EarnRewardsPool = await X2EarnRewardsPoolContract.deploy(admin.address, await rewardToken.getAddress(), await x2EarnApps.getAddress());
await x2EarnRewardsPool.waitForDeployment();

await x2EarnApps.addApp(owner.address, owner.address, 'EcoEarn');
await x2EarnApps.addApp(admin.address, admin.address, 'EcoEarn');
const APP_ID = await x2EarnApps.hashAppName('EcoEarn');

await rewardToken.approve(await x2EarnRewardsPool.getAddress(), ethers.parseEther('10000'));
await x2EarnRewardsPool.deposit(ethers.parseEther('2000'), APP_ID);

const ecoEarn = await ethers.getContractFactory('EcoEarn');
const ecoEarnInstance = await ecoEarn.deploy(
owner.address,
admin.address,
await x2EarnRewardsPool.getAddress(),
CYCLE_DURATION,
MAX_SUBMISSIONS_PER_CYCLE,
Expand All @@ -39,7 +36,18 @@ export const getAndDeployContracts = async () => {
await ecoEarnInstance.waitForDeployment();
const ecoEarnAddress = await ecoEarnInstance.getAddress();

await x2EarnApps.addRewardDistributor(APP_ID, ecoEarnAddress);

return { token: rewardToken, ecoearn: ecoEarnInstance, x2EarnApps, x2EarnRewardsPool, owner, admin, account3, account4, otherAccounts };
await x2EarnApps.connect(admin).addRewardDistributor(APP_ID, ecoEarnAddress);

return {
token: rewardToken,
ecoearn: ecoEarnInstance,
x2EarnApps,
x2EarnRewardsPool,
appId: APP_ID,
owner,
admin,
account3,
account4,
otherAccounts,
};
};

0 comments on commit 5ce629a

Please sign in to comment.