diff --git a/apps/contracts/package.json b/apps/contracts/package.json index 45348aa..b169c0f 100644 --- a/apps/contracts/package.json +++ b/apps/contracts/package.json @@ -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", diff --git a/apps/contracts/test/EcoEarn.test.ts b/apps/contracts/test/EcoEarn.test.ts index d042927..658c79d 100644 --- a/apps/contracts/test/EcoEarn.test.ts +++ b/apps/contracts/test/EcoEarn.test.ts @@ -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 @@ -35,9 +34,9 @@ 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); @@ -45,7 +44,7 @@ describe('ecoearn', () => { 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); @@ -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); @@ -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 ); @@ -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); @@ -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); @@ -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); diff --git a/apps/contracts/test/helpers/allocation.ts b/apps/contracts/test/helpers/allocation.ts index 89ee04b..17719be 100644 --- a/apps/contracts/test/helpers/allocation.ts +++ b/apps/contracts/test/helpers/allocation.ts @@ -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 ( @@ -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)); }; diff --git a/apps/contracts/test/helpers/deploy.ts b/apps/contracts/test/helpers/deploy.ts index 96a10aa..39871fb 100644 --- a/apps/contracts/test/helpers/deploy.ts +++ b/apps/contracts/test/helpers/deploy.ts @@ -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, @@ -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, + }; };