From bb139db249b9132d3d94ea9af8c43aafaeaeafd9 Mon Sep 17 00:00:00 2001 From: BkChoy Date: Tue, 16 Jul 2024 12:49:03 -0400 Subject: [PATCH] fixed remaining conflicts and failing tests --- hardhat.config.ts | 2 - test/core/priorityPool/priority-pool.test.ts | 67 ++-- .../core/priorityPool/withdrawal-pool.test.ts | 197 +++++----- test/linkStaking/fund-flow-controller.test.ts | 354 +++++++++--------- test/linkStaking/operator-vault.test.ts | 5 +- test/linkStaking/pp-keeper.test.ts | 170 +++++---- .../vault-controller-strategy.test.ts | 74 ++-- test/linkStaking/vault.test.ts | 9 +- 8 files changed, 468 insertions(+), 410 deletions(-) diff --git a/hardhat.config.ts b/hardhat.config.ts index 31f7e2ae..3e13080a 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -28,7 +28,6 @@ const config: HardhatUserConfig = { networks: { localhost: { url: 'http://127.0.0.1:8545', - ledgerAccounts: [ledgerAccount], }, sepolia: { url: '', @@ -54,7 +53,6 @@ const config: HardhatUserConfig = { hardhat: { chainId: 1337, accounts: accounts.map((acct) => ({ privateKey: acct, balance })), - ledgerAccounts: [ledgerAccount], mining: { auto: true, interval: 5000, diff --git a/test/core/priorityPool/priority-pool.test.ts b/test/core/priorityPool/priority-pool.test.ts index 2c88dcdc..1e14050c 100644 --- a/test/core/priorityPool/priority-pool.test.ts +++ b/test/core/priorityPool/priority-pool.test.ts @@ -228,29 +228,29 @@ describe('PriorityPool', () => { [2000, 1000] ) - await expect(pp.depositQueuedTokens(toEther(100), toEther(1000), ['0x'])).to.be.revertedWith( - 'InsufficientDepositRoom()' - ) + await expect( + pp.depositQueuedTokens(toEther(100), toEther(1000), ['0x']) + ).to.be.revertedWithCustomError(pp, 'InsufficientDepositRoom()') await strategy.setMaxDeposits(toEther(4900)) - await expect(pp.depositQueuedTokens(toEther(100), toEther(1000), ['0x'])).to.be.revertedWith( - 'InsufficientQueuedTokens()' - ) + await expect( + pp.depositQueuedTokens(toEther(100), toEther(1000), ['0x']) + ).to.be.revertedWithCustomError(pp, 'InsufficientQueuedTokens()') await pp.deposit(toEther(199), true, ['0x']) await strategy.setMaxDeposits(toEther(5000)) - await expect(pp.depositQueuedTokens(toEther(100), toEther(1000), ['0x'])).to.be.revertedWith( - 'InsufficientQueuedTokens()' - ) + await expect( + pp.depositQueuedTokens(toEther(100), toEther(1000), ['0x']) + ).to.be.revertedWithCustomError(pp, 'InsufficientQueuedTokens()') await token.transfer(adrs.stakingPool, toEther(1)) await pp.depositQueuedTokens(toEther(100), toEther(1000), ['0x']) await pp.setPoolStatus(2) - await expect(pp.depositQueuedTokens(toEther(100), toEther(1000), ['0x'])).to.be.revertedWith( - 'DepositsDisabled()' - ) + await expect( + pp.depositQueuedTokens(toEther(100), toEther(1000), ['0x']) + ).to.be.revertedWithCustomError(pp, 'DepositsDisabled()') await pp.setPoolStatus(1) - await expect(pp.depositQueuedTokens(toEther(100), toEther(1000), ['0x'])).to.be.revertedWith( - 'DepositsDisabled()' - ) + await expect( + pp.depositQueuedTokens(toEther(100), toEther(1000), ['0x']) + ).to.be.revertedWithCustomError(pp, 'DepositsDisabled()') }) it('checkUpkeep should work correctly', async () => { @@ -275,7 +275,7 @@ describe('PriorityPool', () => { await pp.setPoolStatus(0) assert.deepEqual(await pp.checkUpkeep('0x'), [ true, - ethers.utils.defaultAbiCoder.encode(['uint256'], [toEther(1001)]), + ethers.AbiCoder.defaultAbiCoder().encode(['uint256'], [toEther(1001)]), ]) }) @@ -337,27 +337,27 @@ describe('PriorityPool', () => { await expect( pp.performUpkeep(ethers.AbiCoder.defaultAbiCoder().encode(['bytes[]'], [['0x']])) - ).to.be.revertedWith('InsufficientDepositRoom()') + ).to.be.revertedWithCustomError(pp, 'InsufficientDepositRoom()') await strategy.setMaxDeposits(toEther(4900)) await expect( pp.performUpkeep(ethers.AbiCoder.defaultAbiCoder().encode(['bytes[]'], [['0x']])) - ).to.be.revertedWith('InsufficientQueuedTokens()') + ).to.be.revertedWithCustomError(pp, 'InsufficientQueuedTokens()') await pp.deposit(toEther(199), true, ['0x']) await strategy.setMaxDeposits(toEther(5000)) await expect( pp.performUpkeep(ethers.AbiCoder.defaultAbiCoder().encode(['bytes[]'], [['0x']])) - ).to.be.revertedWith('InsufficientQueuedTokens()') + ).to.be.revertedWithCustomError(pp, 'InsufficientQueuedTokens()') await token.transfer(adrs.stakingPool, toEther(1)) await pp.performUpkeep(ethers.AbiCoder.defaultAbiCoder().encode(['bytes[]'], [['0x']])) await pp.setPoolStatus(2) await expect( pp.performUpkeep(ethers.AbiCoder.defaultAbiCoder().encode(['bytes[]'], [['0x']])) - ).to.be.revertedWith('DepositsDisabled()') + ).to.be.revertedWithCustomError(pp, 'DepositsDisabled()') await pp.setPoolStatus(1) await expect( pp.performUpkeep(ethers.AbiCoder.defaultAbiCoder().encode(['bytes[]'], [['0x']])) - ).to.be.revertedWith('DepositsDisabled()') + ).to.be.revertedWithCustomError(pp, 'DepositsDisabled()') }) it('getAccountData should work correctly', async () => { @@ -481,16 +481,16 @@ describe('PriorityPool', () => { await expect( pp.claimLSDTokens(toEther(301), toEther(300), tree.getProof(1)) - ).to.be.revertedWith('InvalidProof()') + ).to.be.revertedWithCustomError(pp, 'InvalidProof()') await expect( pp.claimLSDTokens(toEther(300), toEther(301), tree.getProof(1)) - ).to.be.revertedWith('InvalidProof()') + ).to.be.revertedWithCustomError(pp, 'InvalidProof()') await expect( pp.claimLSDTokens(toEther(300), toEther(300), tree.getProof(2)) - ).to.be.revertedWith('InvalidProof()') + ).to.be.revertedWithCustomError(pp, 'InvalidProof()') await expect( pp.connect(signers[1]).claimLSDTokens(toEther(300), toEther(300), tree.getProof(1)) - ).to.be.revertedWith('InvalidProof()') + ).to.be.revertedWithCustomError(pp, 'InvalidProof()') assert.equal(fromEther(await pp.getLSDTokens(accounts[0], data[1][2])), 300) assert.equal(fromEther(await pp.getQueuedTokens(accounts[0], data[1][1])), 700) @@ -510,7 +510,7 @@ describe('PriorityPool', () => { await expect( pp.connect(signers[1]).claimLSDTokens(toEther(150), toEther(150), tree.getProof(2)) - ).to.be.revertedWith('NothingToClaim()') + ).to.be.revertedWithCustomError(pp, 'NothingToClaim()') }) it('unqueueTokens should work correctly', async () => { @@ -524,7 +524,8 @@ describe('PriorityPool', () => { await strategy.setMaxDeposits(toEther(1500)) await pp.depositQueuedTokens(toEther(100), toEther(1000), ['0x']) - await expect(pp.unqueueTokens(toEther(1501), 0, 0, [])).to.be.revertedWith( + await expect(pp.unqueueTokens(toEther(1501), 0, 0, [])).to.be.revertedWithCustomError( + pp, 'InsufficientQueuedTokens()' ) @@ -566,7 +567,7 @@ describe('PriorityPool', () => { ).to.be.revertedWithCustomError(pp, 'InvalidProof()') await expect( pp.unqueueTokens(toEther(50), toEther(150), toEther(150), tree.getProof(1)) - ).to.be.revertedWith('InvalidProof()') + ).to.be.revertedWithCustomError(pp, 'InvalidProof()') await pp .connect(signers[1]) @@ -578,7 +579,7 @@ describe('PriorityPool', () => { await expect( pp.connect(signers[2]).unqueueTokens(toEther(500), toEther(50), toEther(50), tree.getProof(3)) - ).to.be.revertedWith('InsufficientBalance()') + ).to.be.revertedWithCustomError(pp, 'InsufficientBalance()') await pp .connect(signers[2]) @@ -728,10 +729,10 @@ describe('PriorityPool', () => { pp .connect(signers[2]) .withdraw(toEther(150), toEther(100), toEther(100), tree.getProof(2), true, false, ['0x']) - ).to.be.revertedWith('InvalidProof()') + ).to.be.revertedWithCustomError(pp, 'InvalidProof()') await expect( pp.connect(signers[2]).withdraw(toEther(150), 0, 0, [], true, false, ['0x']) - ).to.be.revertedWith('InvalidProof()') + ).to.be.revertedWithCustomError(pp, 'InvalidProof()') await stakingPool.transfer(accounts[2], toEther(100)) await pp .connect(signers[2]) @@ -782,14 +783,14 @@ describe('PriorityPool', () => { 1000, ethers.AbiCoder.defaultAbiCoder().encode(['bool', 'bytes[]'], [true, ['0x']]) ) - ).to.be.revertedWith('UnauthorizedToken()') + ).to.be.revertedWithCustomError(pp, 'UnauthorizedToken()') await expect( token.transferAndCall( adrs.pp, 0, ethers.AbiCoder.defaultAbiCoder().encode(['bool', 'bytes[]'], [true, ['0x']]) ) - ).to.be.revertedWith('InvalidValue()') + ).to.be.revertedWithCustomError(pp, 'InvalidValue()') await token .connect(signers[1]) diff --git a/test/core/priorityPool/withdrawal-pool.test.ts b/test/core/priorityPool/withdrawal-pool.test.ts index c8caaa20..307b3f7e 100644 --- a/test/core/priorityPool/withdrawal-pool.test.ts +++ b/test/core/priorityPool/withdrawal-pool.test.ts @@ -7,74 +7,74 @@ import { getAccounts, setupToken, } from '../../utils/helpers' -import { ERC677, PriorityPool, StakingPool, StrategyMock } from '../../../typechain-types' +import { + ERC677, + PriorityPool, + StakingPool, + StrategyMock, + WithdrawalPool, +} from '../../../typechain-types' import { ethers } from 'hardhat' -import { Signer } from 'ethers' -import { WithdrawalPool } from '../../../typechain-types/WithdrawalPool' +import { loadFixture } from '@nomicfoundation/hardhat-network-helpers' describe('WithdrawalPool', () => { - let withdrawalPool: WithdrawalPool - let stakingPool: StakingPool - let strategy: StrategyMock - let token: ERC677 - let accounts: string[] - let signers: Signer[] - - before(async () => { - ;({ accounts, signers } = await getAccounts()) - }) + async function deployFixture() { + const { accounts, signers } = await getAccounts() - beforeEach(async () => { - token = (await deploy('contracts/core/tokens/base/ERC677.sol:ERC677', [ + const token = (await deploy('contracts/core/tokens/base/ERC677.sol:ERC677', [ 'Chainlink', 'LINK', 1000000000, ])) as ERC677 await setupToken(token, accounts, true) - stakingPool = (await deployUpgradeable('StakingPool', [ - token.address, + const stakingPool = (await deployUpgradeable('StakingPool', [ + token.target, 'Staked LINK', 'stLINK', [], toEther(10000), ])) as StakingPool - strategy = (await deployUpgradeable('StrategyMock', [ - token.address, - stakingPool.address, + const strategy = (await deployUpgradeable('StrategyMock', [ + token.target, + stakingPool.target, toEther(1000000000), toEther(5000), ])) as StrategyMock - withdrawalPool = (await deployUpgradeable('WithdrawalPool', [ - token.address, - stakingPool.address, + const withdrawalPool = (await deployUpgradeable('WithdrawalPool', [ + token.target, + stakingPool.target, accounts[0], toEther(10), ])) as WithdrawalPool - await stakingPool.addStrategy(strategy.address) + await stakingPool.addStrategy(strategy.target) await stakingPool.setPriorityPool(accounts[0]) await stakingPool.setRebaseController(accounts[0]) - await token.approve(stakingPool.address, ethers.constants.MaxUint256) - await token.approve(withdrawalPool.address, ethers.constants.MaxUint256) - await stakingPool.approve(withdrawalPool.address, ethers.constants.MaxUint256) + await token.approve(stakingPool.target, ethers.MaxUint256) + await token.approve(withdrawalPool.target, ethers.MaxUint256) + await stakingPool.approve(withdrawalPool.target, ethers.MaxUint256) await stakingPool.deposit(accounts[0], toEther(100000), ['0x']) - await token.transfer(strategy.address, toEther(100000)) + await token.transfer(strategy.target, toEther(100000)) await stakingPool.updateStrategyRewards([0], '0x') - }) + + return { signers, accounts, token, stakingPool, strategy, withdrawalPool } + } it('queueWithdrawal should work correctly', async () => { + const { accounts, withdrawalPool, stakingPool } = await loadFixture(deployFixture) + await withdrawalPool.queueWithdrawal(accounts[0], toEther(1000)) await withdrawalPool.queueWithdrawal(accounts[1], toEther(250)) await withdrawalPool.queueWithdrawal(accounts[0], toEther(500)) - assert.equal(fromEther(await stakingPool.balanceOf(withdrawalPool.address)), 1750) + assert.equal(fromEther(await stakingPool.balanceOf(withdrawalPool.target)), 1750) assert.equal(fromEther(await withdrawalPool.getTotalQueuedWithdrawals()), 1750) assert.deepEqual( - (await withdrawalPool.getWithdrawalIdsByOwner(accounts[0])).map((id) => id.toNumber()), + (await withdrawalPool.getWithdrawalIdsByOwner(accounts[0])).map((id) => Number(id)), [1, 3] ) assert.deepEqual( @@ -91,6 +91,8 @@ describe('WithdrawalPool', () => { }) it('deposit should work correctly', async () => { + const { accounts, withdrawalPool, stakingPool, token } = await loadFixture(deployFixture) + await withdrawalPool.queueWithdrawal(accounts[0], toEther(1000)) await withdrawalPool.queueWithdrawal(accounts[1], toEther(250)) await withdrawalPool.queueWithdrawal(accounts[0], toEther(500)) @@ -98,10 +100,10 @@ describe('WithdrawalPool', () => { await expect(withdrawalPool.deposit(toEther(1751))).to.be.reverted - assert.equal(fromEther(await token.balanceOf(withdrawalPool.address)), 400) - assert.equal(fromEther(await stakingPool.balanceOf(withdrawalPool.address)), 1350) + assert.equal(fromEther(await token.balanceOf(withdrawalPool.target)), 400) + assert.equal(fromEther(await stakingPool.balanceOf(withdrawalPool.target)), 1350) assert.equal(fromEther(await withdrawalPool.getTotalQueuedWithdrawals()), 1350) - assert.equal((await withdrawalPool.indexOfNextWithdrawal()).toNumber(), 1) + assert.equal(Number(await withdrawalPool.indexOfNextWithdrawal()), 1) assert.deepEqual( (await withdrawalPool.getWithdrawals([1, 2, 3])).map((d: any) => [ fromEther(d[0]), @@ -114,16 +116,16 @@ describe('WithdrawalPool', () => { ] ) assert.deepEqual( - (await withdrawalPool.getBatchIds([1, 2, 3])).map((d: any) => d.toNumber()), + (await withdrawalPool.getBatchIds([1, 2, 3])).map((d: any) => Number(d)), [0, 0, 0] ) await withdrawalPool.deposit(toEther(700)) - assert.equal(fromEther(await token.balanceOf(withdrawalPool.address)), 1100) - assert.equal(fromEther(await stakingPool.balanceOf(withdrawalPool.address)), 650) + assert.equal(fromEther(await token.balanceOf(withdrawalPool.target)), 1100) + assert.equal(fromEther(await stakingPool.balanceOf(withdrawalPool.target)), 650) assert.equal(fromEther(await withdrawalPool.getTotalQueuedWithdrawals()), 650) - assert.equal((await withdrawalPool.indexOfNextWithdrawal()).toNumber(), 2) + assert.equal(Number(await withdrawalPool.indexOfNextWithdrawal()), 2) assert.deepEqual( (await withdrawalPool.getWithdrawals([1, 2, 3])).map((d: any) => [ fromEther(d[0]), @@ -136,16 +138,16 @@ describe('WithdrawalPool', () => { ] ) assert.deepEqual( - (await withdrawalPool.getBatchIds([1, 2, 3])).map((d: any) => d.toNumber()), + (await withdrawalPool.getBatchIds([1, 2, 3])).map((d: any) => Number(d)), [2, 0, 0] ) await withdrawalPool.deposit(toEther(650)) - assert.equal(fromEther(await token.balanceOf(withdrawalPool.address)), 1750) - assert.equal(fromEther(await stakingPool.balanceOf(withdrawalPool.address)), 0) + assert.equal(fromEther(await token.balanceOf(withdrawalPool.target)), 1750) + assert.equal(fromEther(await stakingPool.balanceOf(withdrawalPool.target)), 0) assert.equal(fromEther(await withdrawalPool.getTotalQueuedWithdrawals()), 0) - assert.equal((await withdrawalPool.indexOfNextWithdrawal()).toNumber(), 4) + assert.equal(Number(await withdrawalPool.indexOfNextWithdrawal()), 4) assert.deepEqual( (await withdrawalPool.getWithdrawals([1, 2, 3])).map((d: any) => [ fromEther(d[0]), @@ -158,33 +160,40 @@ describe('WithdrawalPool', () => { ] ) assert.deepEqual( - (await withdrawalPool.getBatchIds([1, 2, 3])).map((d: any) => d.toNumber()), + (await withdrawalPool.getBatchIds([1, 2, 3])).map((d: any) => Number(d)), [2, 3, 3] ) }) it('withdraw should work correctly', async () => { + const { signers, accounts, withdrawalPool, token } = await loadFixture(deployFixture) + await withdrawalPool.queueWithdrawal(accounts[0], toEther(1000)) await withdrawalPool.queueWithdrawal(accounts[1], toEther(250)) await withdrawalPool.queueWithdrawal(accounts[0], toEther(500)) await withdrawalPool.deposit(toEther(1200)) - await expect(withdrawalPool.withdraw([1, 2, 3], [1, 1, 0])).to.be.revertedWith( + await expect(withdrawalPool.withdraw([1, 2, 3], [1, 1, 0])).to.be.revertedWithCustomError( + withdrawalPool, 'SenderNotAuthorized()' ) - await expect(withdrawalPool.withdraw([1, 3], [1, 1])).to.be.revertedWith( + await expect(withdrawalPool.withdraw([1, 3], [1, 1])).to.be.revertedWithCustomError( + withdrawalPool, 'InvalidWithdrawalId()' ) await withdrawalPool.deposit(toEther(550)) - await expect(withdrawalPool.withdraw([1], [2])).to.be.revertedWith('InvalidWithdrawalId()') + await expect(withdrawalPool.withdraw([1], [2])).to.be.revertedWithCustomError( + withdrawalPool, + 'InvalidWithdrawalId()' + ) let startingBalance = await token.balanceOf(accounts[1]) await withdrawalPool.connect(signers[1]).withdraw([2], [2]) - assert.equal(fromEther((await token.balanceOf(accounts[1])).sub(startingBalance)), 250) + assert.equal(fromEther((await token.balanceOf(accounts[1])) - startingBalance), 250) assert.deepEqual( - (await withdrawalPool.getWithdrawalIdsByOwner(accounts[1])).map((id) => id.toNumber()), + (await withdrawalPool.getWithdrawalIdsByOwner(accounts[1])).map((id) => Number(id)), [] ) assert.deepEqual( @@ -197,9 +206,9 @@ describe('WithdrawalPool', () => { startingBalance = await token.balanceOf(accounts[0]) await withdrawalPool.withdraw([1, 3], [1, 2]) - assert.equal(fromEther((await token.balanceOf(accounts[0])).sub(startingBalance)), 1500) + assert.equal(fromEther((await token.balanceOf(accounts[0])) - startingBalance), 1500) assert.deepEqual( - (await withdrawalPool.getWithdrawalIdsByOwner(accounts[1])).map((id) => id.toNumber()), + (await withdrawalPool.getWithdrawalIdsByOwner(accounts[1])).map((id) => Number(id)), [] ) assert.deepEqual( @@ -216,28 +225,30 @@ describe('WithdrawalPool', () => { }) it('getWithdrawalIdsByOwner should work correctly', async () => { + const { signers, accounts, withdrawalPool } = await loadFixture(deployFixture) + await withdrawalPool.queueWithdrawal(accounts[0], toEther(1000)) await withdrawalPool.queueWithdrawal(accounts[1], toEther(250)) await withdrawalPool.queueWithdrawal(accounts[0], toEther(500)) await withdrawalPool.deposit(toEther(600)) assert.deepEqual( - (await withdrawalPool.getWithdrawalIdsByOwner(accounts[0])).map((id) => id.toNumber()), + (await withdrawalPool.getWithdrawalIdsByOwner(accounts[0])).map((id) => Number(id)), [1, 3] ) assert.deepEqual( - (await withdrawalPool.getWithdrawalIdsByOwner(accounts[1])).map((id) => id.toNumber()), + (await withdrawalPool.getWithdrawalIdsByOwner(accounts[1])).map((id) => Number(id)), [2] ) await withdrawalPool.withdraw([1], [0]) assert.deepEqual( - (await withdrawalPool.getWithdrawalIdsByOwner(accounts[0])).map((id) => id.toNumber()), + (await withdrawalPool.getWithdrawalIdsByOwner(accounts[0])).map((id) => Number(id)), [1, 3] ) assert.deepEqual( - (await withdrawalPool.getWithdrawalIdsByOwner(accounts[1])).map((id) => id.toNumber()), + (await withdrawalPool.getWithdrawalIdsByOwner(accounts[1])).map((id) => Number(id)), [2] ) @@ -245,41 +256,43 @@ describe('WithdrawalPool', () => { await withdrawalPool.withdraw([3], [2]) assert.deepEqual( - (await withdrawalPool.getWithdrawalIdsByOwner(accounts[0])).map((id) => id.toNumber()), + (await withdrawalPool.getWithdrawalIdsByOwner(accounts[0])).map((id) => Number(id)), [1] ) assert.deepEqual( - (await withdrawalPool.getWithdrawalIdsByOwner(accounts[1])).map((id) => id.toNumber()), + (await withdrawalPool.getWithdrawalIdsByOwner(accounts[1])).map((id) => Number(id)), [2] ) await withdrawalPool.connect(signers[1]).withdraw([2], [2]) assert.deepEqual( - (await withdrawalPool.getWithdrawalIdsByOwner(accounts[0])).map((id) => id.toNumber()), + (await withdrawalPool.getWithdrawalIdsByOwner(accounts[0])).map((id) => Number(id)), [1] ) assert.deepEqual( - (await withdrawalPool.getWithdrawalIdsByOwner(accounts[1])).map((id) => id.toNumber()), + (await withdrawalPool.getWithdrawalIdsByOwner(accounts[1])).map((id) => Number(id)), [] ) }) it('getBatchIdsByOwner should work correctly', async () => { + const { accounts, withdrawalPool } = await loadFixture(deployFixture) + await withdrawalPool.queueWithdrawal(accounts[0], toEther(1000)) await withdrawalPool.queueWithdrawal(accounts[1], toEther(250)) await withdrawalPool.queueWithdrawal(accounts[0], toEther(500)) await withdrawalPool.deposit(toEther(600)) assert.deepEqual( - (await withdrawalPool.getBatchIds([1, 2, 3])).map((d: any) => d.toNumber()), + (await withdrawalPool.getBatchIds([1, 2, 3])).map((d: any) => Number(d)), [0, 0, 0] ) await withdrawalPool.deposit(toEther(500)) assert.deepEqual( - (await withdrawalPool.getBatchIds([1, 2, 3])).map((d: any) => d.toNumber()), + (await withdrawalPool.getBatchIds([1, 2, 3])).map((d: any) => Number(d)), [2, 0, 0] ) @@ -287,19 +300,21 @@ describe('WithdrawalPool', () => { await withdrawalPool.deposit(toEther(50)) assert.deepEqual( - (await withdrawalPool.getBatchIds([1, 2, 3])).map((d: any) => d.toNumber()), + (await withdrawalPool.getBatchIds([1, 2, 3])).map((d: any) => Number(d)), [2, 0, 0] ) await withdrawalPool.deposit(toEther(550)) assert.deepEqual( - (await withdrawalPool.getBatchIds([1, 2, 3])).map((d: any) => d.toNumber()), + (await withdrawalPool.getBatchIds([1, 2, 3])).map((d: any) => Number(d)), [2, 5, 5] ) }) it('getFinalizedWithdrawalIdsByOwner should work correctly', async () => { + const { accounts, withdrawalPool } = await loadFixture(deployFixture) + await withdrawalPool.queueWithdrawal(accounts[0], toEther(1000)) await withdrawalPool.queueWithdrawal(accounts[1], toEther(250)) await withdrawalPool.queueWithdrawal(accounts[0], toEther(500)) @@ -307,7 +322,7 @@ describe('WithdrawalPool', () => { let data = await withdrawalPool.getFinalizedWithdrawalIdsByOwner(accounts[0]) assert.deepEqual( - data[0].map((id) => id.toNumber()), + data[0].map((id) => Number(id)), [1] ) assert.equal(fromEther(data[1]), 600) @@ -316,7 +331,7 @@ describe('WithdrawalPool', () => { data = await withdrawalPool.getFinalizedWithdrawalIdsByOwner(accounts[0]) assert.deepEqual( - data[0].map((id) => id.toNumber()), + data[0].map((id) => Number(id)), [] ) assert.equal(fromEther(data[1]), 0) @@ -325,14 +340,14 @@ describe('WithdrawalPool', () => { data = await withdrawalPool.getFinalizedWithdrawalIdsByOwner(accounts[0]) assert.deepEqual( - data[0].map((id) => id.toNumber()), + data[0].map((id) => Number(id)), [1] ) assert.equal(fromEther(data[1]), 400) data = await withdrawalPool.getFinalizedWithdrawalIdsByOwner(accounts[1]) assert.deepEqual( - data[0].map((id) => id.toNumber()), + data[0].map((id) => Number(id)), [2] ) assert.equal(fromEther(data[1]), 200) @@ -341,56 +356,68 @@ describe('WithdrawalPool', () => { data = await withdrawalPool.getFinalizedWithdrawalIdsByOwner(accounts[0]) assert.deepEqual( - data[0].map((id) => id.toNumber()), + data[0].map((id) => Number(id)), [1, 3] ) assert.equal(fromEther(data[1]), 900) data = await withdrawalPool.getFinalizedWithdrawalIdsByOwner(accounts[1]) assert.deepEqual( - data[0].map((id) => id.toNumber()), + data[0].map((id) => Number(id)), [2] ) assert.equal(fromEther(data[1]), 250) }) it('checkUpkeep and performUpkeep should work correctly', async () => { + const { accounts, stakingPool, token, strategy } = await loadFixture(deployFixture) + let priorityPool = (await deployUpgradeable('PriorityPool', [ - token.address, - stakingPool.address, + token.target, + stakingPool.target, accounts[0], 0, 0, ])) as PriorityPool - withdrawalPool = (await deployUpgradeable('WithdrawalPool', [ - stakingPool.address, - stakingPool.address, - priorityPool.address, + const withdrawalPool = (await deployUpgradeable('WithdrawalPool', [ + stakingPool.target, + stakingPool.target, + priorityPool.target, toEther(10), ])) as WithdrawalPool - await stakingPool.approve(priorityPool.address, ethers.constants.MaxUint256) - await stakingPool.setPriorityPool(priorityPool.address) - await priorityPool.setWithdrawalPool(withdrawalPool.address) + await stakingPool.approve(priorityPool.target, ethers.MaxUint256) + await stakingPool.setPriorityPool(priorityPool.target) + await priorityPool.setWithdrawalPool(withdrawalPool.target) await priorityPool.withdraw(toEther(199000), 0, 0, [], false, true, ['0x']) assert.deepEqual(await withdrawalPool.checkUpkeep('0x'), [false, '0x']) - await expect(withdrawalPool.performUpkeep('0x')).to.be.revertedWith('NoUpkeepNeeded()') + await expect(withdrawalPool.performUpkeep('0x')).to.be.revertedWithCustomError( + withdrawalPool, + 'NoUpkeepNeeded()' + ) await strategy.setMinDeposits(toEther(4000)) assert.deepEqual(await withdrawalPool.checkUpkeep('0x'), [true, '0x']) - await withdrawalPool.performUpkeep(ethers.utils.defaultAbiCoder.encode(['bytes[]'], [['0x']])) - assert.equal(fromEther(await token.balanceOf(withdrawalPool.address)), 1000) - assert.equal(fromEther(await stakingPool.balanceOf(withdrawalPool.address)), 3000) + await withdrawalPool.performUpkeep( + ethers.AbiCoder.defaultAbiCoder().encode(['bytes[]'], [['0x']]) + ) + assert.equal(fromEther(await token.balanceOf(withdrawalPool.target)), 1000) + assert.equal(fromEther(await stakingPool.balanceOf(withdrawalPool.target)), 3000) assert.equal(fromEther(await withdrawalPool.getTotalQueuedWithdrawals()), 3000) await strategy.setMinDeposits(toEther(0)) assert.deepEqual(await withdrawalPool.checkUpkeep('0x'), [true, '0x']) - await withdrawalPool.performUpkeep(ethers.utils.defaultAbiCoder.encode(['bytes[]'], [['0x']])) - assert.equal(fromEther(await token.balanceOf(withdrawalPool.address)), 4000) - assert.equal(fromEther(await stakingPool.balanceOf(withdrawalPool.address)), 0) + await withdrawalPool.performUpkeep( + ethers.AbiCoder.defaultAbiCoder().encode(['bytes[]'], [['0x']]) + ) + assert.equal(fromEther(await token.balanceOf(withdrawalPool.target)), 4000) + assert.equal(fromEther(await stakingPool.balanceOf(withdrawalPool.target)), 0) assert.equal(fromEther(await withdrawalPool.getTotalQueuedWithdrawals()), 0) assert.deepEqual(await withdrawalPool.checkUpkeep('0x'), [false, '0x']) - await expect(withdrawalPool.performUpkeep('0x')).to.be.revertedWith('NoUpkeepNeeded()') + await expect(withdrawalPool.performUpkeep('0x')).to.be.revertedWithCustomError( + withdrawalPool, + 'NoUpkeepNeeded()' + ) }) }) diff --git a/test/linkStaking/fund-flow-controller.test.ts b/test/linkStaking/fund-flow-controller.test.ts index 19ab62b5..a9939cd2 100644 --- a/test/linkStaking/fund-flow-controller.test.ts +++ b/test/linkStaking/fund-flow-controller.test.ts @@ -17,61 +17,43 @@ import { StakingRewardsMock, FundFlowController, } from '../../typechain-types' -import { time } from '@nomicfoundation/hardhat-network-helpers' +import { loadFixture, time } from '@nomicfoundation/hardhat-network-helpers' const unbondingPeriod = 28 * 86400 const claimPeriod = 7 * 86400 -describe('FundFlowController', () => { - let token: ERC677 - let stakingController: StakingMock - let rewardsController: StakingRewardsMock - let fundFlowController: FundFlowController - let opStrategy: VCSMock - let comStrategy: VCSMock - let vaults: string[] - let vaultContracts: CommunityVault[] - let accounts: string[] - - async function updateVaultGroups( - curGroupVaultsToUnbond: number[], - nextGroupVaultsTotalUnbonded: number - ) { - return await fundFlowController.performUpkeep( - ethers.utils.defaultAbiCoder.encode( - ['uint256[]', 'uint256', 'uint256[]', 'uint256'], - [[], 0, curGroupVaultsToUnbond, toEther(nextGroupVaultsTotalUnbonded)] - ) - ) - } - - function encodeVaults(vaults: number[]) { - return ethers.utils.defaultAbiCoder.encode(['uint64[]'], [vaults]) - } - - function decodeData(data: any) { - return [ - ethers.utils.defaultAbiCoder.decode(['uint64[]'], data[0])[0].map((v: any) => v.toNumber()), - ethers.utils.defaultAbiCoder.decode(['uint64[]'], data[1])[0].map((v: any) => v.toNumber()), - ] - } +function encodeVaults(vaults: number[]) { + return ethers.AbiCoder.defaultAbiCoder().encode(['uint64[]'], [vaults]) +} + +function decodeData(data: any) { + return [ + ethers.AbiCoder.defaultAbiCoder() + .decode(['uint64[]'], data[0])[0] + .map((v: any) => Number(v)), + ethers.AbiCoder.defaultAbiCoder() + .decode(['uint64[]'], data[1])[0] + .map((v: any) => Number(v)), + ] +} - before(async () => { - ;({ accounts } = await getAccounts()) - }) +describe('FundFlowController', () => { + async function deployFixture() { + const { accounts } = await getAccounts() - beforeEach(async () => { - token = (await deploy('contracts/core/tokens/base/ERC677.sol:ERC677', [ + const token = (await deploy('contracts/core/tokens/base/ERC677.sol:ERC677', [ 'Chainlink', 'LINK', 1000000000, ])) as ERC677 await setupToken(token, accounts) - rewardsController = (await deploy('StakingRewardsMock', [token.address])) as StakingRewardsMock - stakingController = (await deploy('StakingMock', [ - token.address, - rewardsController.address, + const rewardsController = (await deploy('StakingRewardsMock', [ + token.target, + ])) as StakingRewardsMock + const stakingController = (await deploy('StakingMock', [ + token.target, + rewardsController.target, toEther(10), toEther(100), toEther(10000), @@ -81,57 +63,84 @@ describe('FundFlowController', () => { let vaultImplementation = await deployImplementation('CommunityVault') - opStrategy = (await deployUpgradeable('VCSMock', [ - token.address, + const opStrategy = (await deployUpgradeable('VCSMock', [ + token.target, accounts[0], - stakingController.address, + stakingController.target, vaultImplementation, [[accounts[4], 500]], toEther(100), ])) as VCSMock - comStrategy = (await deployUpgradeable('VCSMock', [ - token.address, + const comStrategy = (await deployUpgradeable('VCSMock', [ + token.target, accounts[0], - stakingController.address, + stakingController.target, vaultImplementation, [[accounts[4], 500]], toEther(100), ])) as VCSMock - vaults = [] - vaultContracts = [] + const vaults = [] + const vaultContracts = [] for (let i = 0; i < 15; i++) { let vault = (await deployUpgradeable('CommunityVault', [ - token.address, - comStrategy.address, - stakingController.address, - rewardsController.address, + token.target, + comStrategy.target, + stakingController.target, + rewardsController.target, ])) as CommunityVault vaultContracts.push(vault) - vaults.push(vault.address) + vaults.push(vault.target) } for (let i = 0; i < 15; i++) { - await vaultContracts[i].transferOwnership(comStrategy.address) + await vaultContracts[i].transferOwnership(comStrategy.target) } await comStrategy.addVaults(vaults) - await token.approve(comStrategy.address, ethers.constants.MaxUint256) - await token.approve(opStrategy.address, ethers.constants.MaxUint256) + await token.approve(comStrategy.target, ethers.MaxUint256) + await token.approve(opStrategy.target, ethers.MaxUint256) - fundFlowController = (await deployUpgradeable('FundFlowController', [ - opStrategy.address, - comStrategy.address, + const fundFlowController = (await deployUpgradeable('FundFlowController', [ + opStrategy.target, + comStrategy.target, unbondingPeriod, claimPeriod, 5, ])) as FundFlowController - await opStrategy.setFundFlowController(fundFlowController.address) - await comStrategy.setFundFlowController(fundFlowController.address) - }) + await opStrategy.setFundFlowController(fundFlowController.target) + await comStrategy.setFundFlowController(fundFlowController.target) + + async function updateVaultGroups( + curGroupVaultsToUnbond: number[], + nextGroupVaultsTotalUnbonded: number + ) { + return await fundFlowController.performUpkeep( + ethers.AbiCoder.defaultAbiCoder().encode( + ['uint256[]', 'uint256', 'uint256[]', 'uint256'], + [[], 0, curGroupVaultsToUnbond, toEther(nextGroupVaultsTotalUnbonded)] + ) + ) + } + + return { + accounts, + token, + rewardsController, + stakingController, + opStrategy, + comStrategy, + vaultContracts, + vaults, + fundFlowController, + updateVaultGroups, + } + } it('getDepositData should work correctly', async () => { + const { comStrategy, fundFlowController, updateVaultGroups } = await loadFixture(deployFixture) + await comStrategy.deposit(toEther(1200), encodeVaults([])) assert.deepEqual(decodeData(await fundFlowController.getDepositData(toEther(150))), [[], []]) @@ -187,6 +196,8 @@ describe('FundFlowController', () => { }) it('getWithdrawalData should work correctly', async () => { + const { comStrategy, fundFlowController, updateVaultGroups } = await loadFixture(deployFixture) + await comStrategy.deposit(toEther(1200), encodeVaults([])) assert.deepEqual(decodeData(await fundFlowController.getDepositData(toEther(150))), [[], []]) @@ -246,29 +257,27 @@ describe('FundFlowController', () => { }) it('checkUpkeep should work correctly', async () => { + const { comStrategy, fundFlowController, updateVaultGroups } = await loadFixture(deployFixture) + assert.deepEqual( - await fundFlowController - .checkUpkeep('0x') - .then((res) => [ - res[0], - ethers.utils.defaultAbiCoder - .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) - .map((d) => (d.length != undefined ? d.map((v: any) => v.toNumber()) : d.toNumber())), - ]), + await fundFlowController.checkUpkeep('0x').then((res) => [ + res[0], + ethers.AbiCoder.defaultAbiCoder() + .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) + .map((d) => (d.length != undefined ? d.map((v: any) => Number(v)) : Number(d))), + ]), [true, [[], 0, [], 0]] ) await comStrategy.deposit(toEther(1200), encodeVaults([])) assert.deepEqual( - await fundFlowController - .checkUpkeep('0x') - .then((res) => [ - res[0], - ethers.utils.defaultAbiCoder - .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) - .map((d) => (d.length != undefined ? d.map((v: any) => v.toNumber()) : d.toNumber())), - ]), + await fundFlowController.checkUpkeep('0x').then((res) => [ + res[0], + ethers.AbiCoder.defaultAbiCoder() + .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) + .map((d) => (d.length != undefined ? d.map((v: any) => Number(v)) : Number(d))), + ]), [true, [[], 0, [0, 5, 10], 0]] ) @@ -279,14 +288,12 @@ describe('FundFlowController', () => { await time.increase(claimPeriod + 10) assert.deepEqual( - await fundFlowController - .checkUpkeep('0x') - .then((res) => [ - res[0], - ethers.utils.defaultAbiCoder - .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) - .map((d) => (d.length != undefined ? d.map((v: any) => v.toNumber()) : d.toNumber())), - ]), + await fundFlowController.checkUpkeep('0x').then((res) => [ + res[0], + ethers.AbiCoder.defaultAbiCoder() + .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) + .map((d) => (d.length != undefined ? d.map((v: any) => Number(v)) : Number(d))), + ]), [true, [[], 0, [1, 6, 11], 0]] ) @@ -298,14 +305,12 @@ describe('FundFlowController', () => { await time.increase(claimPeriod + 10) assert.deepEqual( - await fundFlowController - .checkUpkeep('0x') - .then((res) => [ - res[0], - ethers.utils.defaultAbiCoder - .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) - .map((d) => (d.length != undefined ? d.map((v: any) => v.toNumber()) : fromEther(d))), - ]), + await fundFlowController.checkUpkeep('0x').then((res) => [ + res[0], + ethers.AbiCoder.defaultAbiCoder() + .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) + .map((d) => (d.length != undefined ? d.map((v: any) => Number(v)) : fromEther(d))), + ]), [true, [[], 0, [4, 9], 300]] ) @@ -326,28 +331,24 @@ describe('FundFlowController', () => { await time.increase(claimPeriod + 10) assert.deepEqual( - await fundFlowController - .checkUpkeep('0x') - .then((res) => [ - res[0], - ethers.utils.defaultAbiCoder - .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) - .map((d) => (d.length != undefined ? d.map((v: any) => v.toNumber()) : fromEther(d))), - ]), + await fundFlowController.checkUpkeep('0x').then((res) => [ + res[0], + ethers.AbiCoder.defaultAbiCoder() + .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) + .map((d) => (d.length != undefined ? d.map((v: any) => Number(v)) : fromEther(d))), + ]), [true, [[], 0, [], 250]] ) await comStrategy.deposit(toEther(50), encodeVaults([0, 4])) assert.deepEqual( - await fundFlowController - .checkUpkeep('0x') - .then((res) => [ - res[0], - ethers.utils.defaultAbiCoder - .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) - .map((d) => (d.length != undefined ? d.map((v: any) => v.toNumber()) : fromEther(d))), - ]), + await fundFlowController.checkUpkeep('0x').then((res) => [ + res[0], + ethers.AbiCoder.defaultAbiCoder() + .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) + .map((d) => (d.length != undefined ? d.map((v: any) => Number(v)) : fromEther(d))), + ]), [true, [[], 0, [4], 250]] ) @@ -361,63 +362,61 @@ describe('FundFlowController', () => { await time.increase(claimPeriod + 10) assert.deepEqual( - await fundFlowController - .checkUpkeep('0x') - .then((res) => [ - res[0], - ethers.utils.defaultAbiCoder - .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) - .map((d) => (d.length != undefined ? d.map((v: any) => v.toNumber()) : fromEther(d))), - ]), + await fundFlowController.checkUpkeep('0x').then((res) => [ + res[0], + ethers.AbiCoder.defaultAbiCoder() + .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) + .map((d) => (d.length != undefined ? d.map((v: any) => Number(v)) : fromEther(d))), + ]), [true, [[], 0, [8], 50]] ) await comStrategy.deposit(toEther(50), encodeVaults([4])) assert.deepEqual( - await fundFlowController - .checkUpkeep('0x') - .then((res) => [ - res[0], - ethers.utils.defaultAbiCoder - .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) - .map((d) => (d.length != undefined ? d.map((v: any) => v.toNumber()) : fromEther(d))), - ]), + await fundFlowController.checkUpkeep('0x').then((res) => [ + res[0], + ethers.AbiCoder.defaultAbiCoder() + .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) + .map((d) => (d.length != undefined ? d.map((v: any) => Number(v)) : fromEther(d))), + ]), [true, [[], 0, [8], 0]] ) }) it('performUpkeep should work correctly', async () => { + const { comStrategy, fundFlowController, updateVaultGroups } = await loadFixture(deployFixture) + await comStrategy.deposit(toEther(1200), encodeVaults([])) await updateVaultGroups([0, 5, 10], 0) assert.equal( - (await fundFlowController.timeOfLastUpdateByGroup(0)).toNumber(), - (await ethers.provider.getBlock('latest')).timestamp + Number(await fundFlowController.timeOfLastUpdateByGroup(0)), + (await ethers.provider.getBlock('latest'))?.timestamp ) - assert.equal((await fundFlowController.curUnbondedVaultGroup()).toNumber(), 1) + assert.equal(Number(await fundFlowController.curUnbondedVaultGroup()), 1) assert.equal(fromEther(await comStrategy.canWithdraw()), 0) - assert.equal((await comStrategy.globalVaultState())[1].toNumber(), 1) + assert.equal(Number((await comStrategy.globalVaultState())[1]), 1) await expect( fundFlowController.performUpkeep( - ethers.utils.defaultAbiCoder.encode( + ethers.AbiCoder.defaultAbiCoder().encode( ['uint256[]', 'uint256', 'uint256[]', 'uint256'], [[], 0, [1, 6, 11], toEther(0)] ) ) - ).to.be.revertedWith('NoUpdateNeeded()') + ).to.be.revertedWithCustomError(fundFlowController, 'NoUpdateNeeded()') await time.increase(claimPeriod) await updateVaultGroups([1, 6, 11], 0) assert.equal( - (await fundFlowController.timeOfLastUpdateByGroup(1)).toNumber(), - (await ethers.provider.getBlock('latest')).timestamp + Number(await fundFlowController.timeOfLastUpdateByGroup(1)), + (await ethers.provider.getBlock('latest'))?.timestamp ) - assert.equal((await fundFlowController.curUnbondedVaultGroup()).toNumber(), 2) + assert.equal(Number(await fundFlowController.curUnbondedVaultGroup()), 2) assert.equal(fromEther(await comStrategy.canWithdraw()), 0) - assert.equal((await comStrategy.globalVaultState())[1].toNumber(), 2) + assert.equal(Number((await comStrategy.globalVaultState())[1]), 2) await time.increase(claimPeriod) await updateVaultGroups([2, 7], 0) @@ -427,12 +426,12 @@ describe('FundFlowController', () => { await updateVaultGroups([4, 9], 300) assert.equal( - (await fundFlowController.timeOfLastUpdateByGroup(4)).toNumber(), - (await ethers.provider.getBlock('latest')).timestamp + Number(await fundFlowController.timeOfLastUpdateByGroup(4)), + (await ethers.provider.getBlock('latest'))?.timestamp ) - assert.equal((await fundFlowController.curUnbondedVaultGroup()).toNumber(), 0) + assert.equal(Number(await fundFlowController.curUnbondedVaultGroup()), 0) assert.equal(fromEther(await comStrategy.canWithdraw()), 300) - assert.equal((await comStrategy.globalVaultState())[1].toNumber(), 0) + assert.equal(Number((await comStrategy.globalVaultState())[1]), 0) await comStrategy.withdraw(toEther(50), encodeVaults([0, 5])) await time.increase(claimPeriod) @@ -453,25 +452,34 @@ describe('FundFlowController', () => { await updateVaultGroups([4], 250) assert.equal( - (await fundFlowController.timeOfLastUpdateByGroup(4)).toNumber(), - (await ethers.provider.getBlock('latest')).timestamp + Number(await fundFlowController.timeOfLastUpdateByGroup(4)), + (await ethers.provider.getBlock('latest'))?.timestamp ) - assert.equal((await fundFlowController.curUnbondedVaultGroup()).toNumber(), 0) + assert.equal(Number(await fundFlowController.curUnbondedVaultGroup()), 0) assert.equal(fromEther(await comStrategy.canWithdraw()), 250) - assert.equal((await comStrategy.globalVaultState())[1].toNumber(), 0) + assert.equal(Number((await comStrategy.globalVaultState())[1]), 0) }) it('should work correctly with 2 strategies', async () => { + const { + comStrategy, + fundFlowController, + token, + opStrategy, + stakingController, + rewardsController, + } = await loadFixture(deployFixture) + let opVaults: any = [] for (let i = 0; i < 10; i++) { let vault = (await deployUpgradeable('CommunityVault', [ - token.address, - opStrategy.address, - stakingController.address, - rewardsController.address, + token.target, + opStrategy.target, + stakingController.target, + rewardsController.target, ])) as CommunityVault - await vault.transferOwnership(opStrategy.address) - opVaults.push(vault.address) + await vault.transferOwnership(opStrategy.target) + opVaults.push(vault.target) } await opStrategy.addVaults(opVaults) @@ -481,35 +489,35 @@ describe('FundFlowController', () => { assert.deepEqual(decodeData(await fundFlowController.getDepositData(toEther(150))), [[], []]) await fundFlowController.performUpkeep( - ethers.utils.defaultAbiCoder.encode( + ethers.AbiCoder.defaultAbiCoder().encode( ['uint256[]', 'uint256', 'uint256[]', 'uint256'], [[0, 5], toEther(0), [0, 5, 10], toEther(0)] ) ) await time.increase(claimPeriod) await fundFlowController.performUpkeep( - ethers.utils.defaultAbiCoder.encode( + ethers.AbiCoder.defaultAbiCoder().encode( ['uint256[]', 'uint256', 'uint256[]', 'uint256'], [[1], toEther(0), [1, 6, 11], toEther(0)] ) ) await time.increase(claimPeriod) await fundFlowController.performUpkeep( - ethers.utils.defaultAbiCoder.encode( + ethers.AbiCoder.defaultAbiCoder().encode( ['uint256[]', 'uint256', 'uint256[]', 'uint256'], [[2], toEther(0), [2, 7], toEther(0)] ) ) await time.increase(claimPeriod) await fundFlowController.performUpkeep( - ethers.utils.defaultAbiCoder.encode( + ethers.AbiCoder.defaultAbiCoder().encode( ['uint256[]', 'uint256', 'uint256[]', 'uint256'], [[3], toEther(0), [3, 8], toEther(0)] ) ) await time.increase(claimPeriod) await fundFlowController.performUpkeep( - ethers.utils.defaultAbiCoder.encode( + ethers.AbiCoder.defaultAbiCoder().encode( ['uint256[]', 'uint256', 'uint256[]', 'uint256'], [[4], toEther(200), [4, 9], toEther(300)] ) @@ -518,7 +526,7 @@ describe('FundFlowController', () => { await opStrategy.withdraw(toEther(100), encodeVaults([0, 5])) await time.increase(claimPeriod) await fundFlowController.performUpkeep( - ethers.utils.defaultAbiCoder.encode( + ethers.AbiCoder.defaultAbiCoder().encode( ['uint256[]', 'uint256', 'uint256[]', 'uint256'], [[0, 5], toEther(100), [0, 5, 10], toEther(300)] ) @@ -526,7 +534,7 @@ describe('FundFlowController', () => { await comStrategy.withdraw(toEther(270), encodeVaults([1, 6, 11])) await time.increase(claimPeriod) await fundFlowController.performUpkeep( - ethers.utils.defaultAbiCoder.encode( + ethers.AbiCoder.defaultAbiCoder().encode( ['uint256[]', 'uint256', 'uint256[]', 'uint256'], [[1], toEther(100), [1, 6, 11], toEther(200)] ) @@ -540,31 +548,29 @@ describe('FundFlowController', () => { await time.increase(claimPeriod + 10) assert.deepEqual( - await fundFlowController - .checkUpkeep('0x') - .then((res) => [ - res[0], - ethers.utils.defaultAbiCoder - .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) - .map((d) => (d.length != undefined ? d.map((v: any) => v.toNumber()) : fromEther(d))), - ]), + await fundFlowController.checkUpkeep('0x').then((res) => [ + res[0], + ethers.AbiCoder.defaultAbiCoder() + .decode(['uint256[]', 'uint256', 'uint256[]', 'uint256'], res[1]) + .map((d) => (d.length != undefined ? d.map((v: any) => Number(v)) : fromEther(d))), + ]), [true, [[2], 100, [2, 7], 200]] ) await fundFlowController.performUpkeep( - ethers.utils.defaultAbiCoder.encode( + ethers.AbiCoder.defaultAbiCoder().encode( ['uint256[]', 'uint256', 'uint256[]', 'uint256'], [[2], toEther(100), [2, 7], toEther(200)] ) ) assert.equal( - (await fundFlowController.timeOfLastUpdateByGroup(2)).toNumber(), - (await ethers.provider.getBlock('latest')).timestamp + Number(await fundFlowController.timeOfLastUpdateByGroup(2)), + (await ethers.provider.getBlock('latest'))?.timestamp ) - assert.equal((await fundFlowController.curUnbondedVaultGroup()).toNumber(), 3) + assert.equal(Number(await fundFlowController.curUnbondedVaultGroup()), 3) assert.equal(fromEther(await comStrategy.canWithdraw()), 200) - assert.equal((await comStrategy.globalVaultState())[1].toNumber(), 3) + assert.equal(Number((await comStrategy.globalVaultState())[1]), 3) assert.equal(fromEther(await opStrategy.canWithdraw()), 100) - assert.equal((await opStrategy.globalVaultState())[1].toNumber(), 3) + assert.equal(Number((await opStrategy.globalVaultState())[1]), 3) }) }) diff --git a/test/linkStaking/operator-vault.test.ts b/test/linkStaking/operator-vault.test.ts index d35d0328..8fd45640 100644 --- a/test/linkStaking/operator-vault.test.ts +++ b/test/linkStaking/operator-vault.test.ts @@ -96,7 +96,10 @@ describe('OperatorVault', () => { await strategy.unbond() - await expect(strategy.withdraw(toEther(30))).to.be.revertedWith('NotInClaimPeriod()') + await expect(strategy.withdraw(toEther(30))).to.be.revertedWithCustomError( + stakingController, + 'NotInClaimPeriod()' + ) await time.increase(unbondingPeriod + 1) diff --git a/test/linkStaking/pp-keeper.test.ts b/test/linkStaking/pp-keeper.test.ts index 646e1d9c..5e72320d 100644 --- a/test/linkStaking/pp-keeper.test.ts +++ b/test/linkStaking/pp-keeper.test.ts @@ -18,63 +18,44 @@ import { PPKeeper, PriorityPoolMock, } from '../../typechain-types' -import { time } from '@nomicfoundation/hardhat-network-helpers' +import { loadFixture, time } from '@nomicfoundation/hardhat-network-helpers' const unbondingPeriod = 28 * 86400 const claimPeriod = 7 * 86400 -describe('PPKeeper', () => { - let token: ERC677 - let ppKeeper: PPKeeper - let priorityPool: PriorityPoolMock - let stakingController: StakingMock - let rewardsController: StakingRewardsMock - let fundFlowController: FundFlowController - let opStrategy: VCSMock - let comStrategy: VCSMock - let vaults: string[] - let vaultContracts: CommunityVault[] - let accounts: string[] - - async function updateVaultGroups( - curGroupVaultsToUnbond: number[], - nextGroupVaultsTotalUnbonded: number - ) { - return await fundFlowController.performUpkeep( - ethers.utils.defaultAbiCoder.encode( - ['uint256[]', 'uint256', 'uint256[]', 'uint256'], - [[], 0, curGroupVaultsToUnbond, toEther(nextGroupVaultsTotalUnbonded)] - ) - ) - } - - function encodeVaults(vaults: number[]) { - return ethers.utils.defaultAbiCoder.encode(['uint64[]'], [vaults]) - } +function encodeVaults(vaults: number[]) { + return ethers.AbiCoder.defaultAbiCoder().encode(['uint64[]'], [vaults]) +} + +function decodeData(data: any) { + return [ + ethers.AbiCoder.defaultAbiCoder() + .decode(['uint64[]'], data[0])[0] + .map((v: any) => Number(v)), + ethers.AbiCoder.defaultAbiCoder() + .decode(['uint64[]'], data[1])[0] + .map((v: any) => Number(v)), + ] +} - function decodeData(data: any) { - return [ - ethers.utils.defaultAbiCoder.decode(['uint64[]'], data[0])[0].map((v: any) => v.toNumber()), - ethers.utils.defaultAbiCoder.decode(['uint64[]'], data[1])[0].map((v: any) => v.toNumber()), - ] - } - - before(async () => { - ;({ accounts } = await getAccounts()) - }) +describe('PPKeeper', () => { + async function deployFixture() { + const { accounts } = await getAccounts() - beforeEach(async () => { - token = (await deploy('contracts/core/tokens/base/ERC677.sol:ERC677', [ + const token = (await deploy('contracts/core/tokens/base/ERC677.sol:ERC677', [ 'Chainlink', 'LINK', 1000000000, ])) as ERC677 await setupToken(token, accounts) - rewardsController = (await deploy('StakingRewardsMock', [token.address])) as StakingRewardsMock - stakingController = (await deploy('StakingMock', [ - token.address, - rewardsController.address, + const rewardsController = (await deploy('StakingRewardsMock', [ + token.target, + ])) as StakingRewardsMock + + const stakingController = (await deploy('StakingMock', [ + token.target, + rewardsController.target, toEther(10), toEther(100), toEther(10000), @@ -82,68 +63,100 @@ describe('PPKeeper', () => { claimPeriod, ])) as StakingMock - priorityPool = (await deploy('PriorityPoolMock', [0])) as PriorityPoolMock + const priorityPool = (await deploy('PriorityPoolMock', [0])) as PriorityPoolMock let vaultImplementation = await deployImplementation('CommunityVault') - opStrategy = (await deployUpgradeable('VCSMock', [ - token.address, + const opStrategy = (await deployUpgradeable('VCSMock', [ + token.target, accounts[0], - stakingController.address, + stakingController.target, vaultImplementation, [[accounts[4], 500]], toEther(100), ])) as VCSMock - comStrategy = (await deployUpgradeable('VCSMock', [ - token.address, + const comStrategy = (await deployUpgradeable('VCSMock', [ + token.target, accounts[0], - stakingController.address, + stakingController.target, vaultImplementation, [[accounts[4], 500]], toEther(100), ])) as VCSMock - vaults = [] - vaultContracts = [] + const vaults = [] + const vaultContracts = [] for (let i = 0; i < 15; i++) { let vault = (await deployUpgradeable('CommunityVault', [ - token.address, - comStrategy.address, - stakingController.address, - rewardsController.address, + token.target, + comStrategy.target, + stakingController.target, + rewardsController.target, ])) as CommunityVault vaultContracts.push(vault) - vaults.push(vault.address) + vaults.push(vault.target) } for (let i = 0; i < 15; i++) { - await vaultContracts[i].transferOwnership(comStrategy.address) + await vaultContracts[i].transferOwnership(comStrategy.target) } await comStrategy.addVaults(vaults) - await token.approve(comStrategy.address, ethers.constants.MaxUint256) - await token.approve(opStrategy.address, ethers.constants.MaxUint256) + await token.approve(comStrategy.target, ethers.MaxUint256) + await token.approve(opStrategy.target, ethers.MaxUint256) - fundFlowController = (await deployUpgradeable('FundFlowController', [ - opStrategy.address, - comStrategy.address, + const fundFlowController = (await deployUpgradeable('FundFlowController', [ + opStrategy.target, + comStrategy.target, unbondingPeriod, claimPeriod, 5, ])) as FundFlowController - await opStrategy.setFundFlowController(fundFlowController.address) - await comStrategy.setFundFlowController(fundFlowController.address) - ppKeeper = (await deploy('PPKeeper', [ - priorityPool.address, - fundFlowController.address, + await opStrategy.setFundFlowController(fundFlowController.target) + await comStrategy.setFundFlowController(fundFlowController.target) + + const ppKeeper = (await deploy('PPKeeper', [ + priorityPool.target, + fundFlowController.target, ])) as PPKeeper - await token.approve(priorityPool.address, ethers.constants.MaxUint256) - }) + await token.approve(priorityPool.target, ethers.MaxUint256) + + async function updateVaultGroups( + curGroupVaultsToUnbond: number[], + nextGroupVaultsTotalUnbonded: number + ) { + return await fundFlowController.performUpkeep( + ethers.AbiCoder.defaultAbiCoder().encode( + ['uint256[]', 'uint256', 'uint256[]', 'uint256'], + [[], 0, curGroupVaultsToUnbond, toEther(nextGroupVaultsTotalUnbonded)] + ) + ) + } + + return { + accounts, + token, + rewardsController, + stakingController, + priorityPool, + opStrategy, + comStrategy, + vaultContracts, + vaults, + fundFlowController, + ppKeeper, + updateVaultGroups, + } + } it('checkUpkeep should work correctly', async () => { + const { comStrategy, ppKeeper, priorityPool, updateVaultGroups } = await loadFixture( + deployFixture + ) + await comStrategy.deposit(toEther(1200), encodeVaults([])) assert.equal((await ppKeeper.checkUpkeep('0x'))[0], false) @@ -173,15 +186,18 @@ describe('PPKeeper', () => { let data: any = await ppKeeper.checkUpkeep('0x') assert.equal(data[0], true) - assert.deepEqual(decodeData(ethers.utils.defaultAbiCoder.decode(['bytes[]'], data[1])[0]), [ - [], - [0, 1, 6, 11, 4], - ]) + assert.deepEqual( + decodeData(ethers.AbiCoder.defaultAbiCoder().decode(['bytes[]'], data[1])[0]), + [[], [0, 1, 6, 11, 4]] + ) await ppKeeper.performUpkeep(data[1]) assert.deepEqual( decodeData( - ethers.utils.defaultAbiCoder.decode(['bytes[]'], await priorityPool.lastPerformData())[0] + ethers.AbiCoder.defaultAbiCoder().decode( + ['bytes[]'], + await priorityPool.lastPerformData() + )[0] ), [[], [0, 1, 6, 11, 4]] ) diff --git a/test/linkStaking/vault-controller-strategy.test.ts b/test/linkStaking/vault-controller-strategy.test.ts index fa15f549..55586c03 100644 --- a/test/linkStaking/vault-controller-strategy.test.ts +++ b/test/linkStaking/vault-controller-strategy.test.ts @@ -109,8 +109,8 @@ describe('VaultControllerStrategy', () => { ])) as FundFlowController adrs.fundFlowController = await fundFlowController.getAddress() - await strategy.setFundFlowController(fundFlowController.address) - await strategy2.setFundFlowController(fundFlowController.address) + await strategy.setFundFlowController(adrs.fundFlowController) + await strategy2.setFundFlowController(adrs.fundFlowController) async function updateVaultGroups( curGroupVaultsToUnbond: number[], @@ -163,18 +163,18 @@ describe('VaultControllerStrategy', () => { await strategy.deposit(toEther(50), encodeVaults([])) assert.equal(fromEther(await token.balanceOf(adrs.stakingController)), 50) assert.equal(fromEther(await stakingController.getStakerPrincipal(vaults[0])), 50) - assert.equal((await strategy.globalVaultState())[3].toNumber(), 0) + assert.equal(Number((await strategy.globalVaultState())[3]), 0) await strategy.deposit(toEther(155), encodeVaults([])) assert.equal(fromEther(await token.balanceOf(adrs.stakingController)), 200) assert.equal(fromEther(await stakingController.getStakerPrincipal(vaults[0])), 100) assert.equal(fromEther(await stakingController.getStakerPrincipal(vaults[1])), 100) assert.equal(fromEther(await stakingController.getStakerPrincipal(vaults[2])), 0) - assert.equal((await strategy.globalVaultState())[3].toNumber(), 2) + assert.equal(Number((await strategy.globalVaultState())[3]), 2) await strategy.deposit(toEther(1000), encodeVaults([])) assert.equal(fromEther(await token.balanceOf(adrs.stakingController)), 1200) - assert.equal((await strategy.globalVaultState())[3].toNumber(), 12) + assert.equal(Number((await strategy.globalVaultState())[3]), 12) // Deposit into vault groups @@ -208,7 +208,7 @@ describe('VaultControllerStrategy', () => { assert.equal(fromEther(await stakingController.getStakerPrincipal(vaults[1])), 50) assert.equal(fromEther(await strategy.canWithdraw()), 0) assert.deepEqual( - await strategy.vaultGroups(1).then((d) => [d[0].toNumber(), fromEther(d[1])]), + await strategy.vaultGroups(1).then((d) => [Number(d[0]), fromEther(d[1])]), [11, 220] ) @@ -217,12 +217,12 @@ describe('VaultControllerStrategy', () => { await time.increase(claimPeriod) await updateVaultGroups([0, 5, 10], 30) - await expect(strategy.deposit(toEther(200), encodeVaults([6, 11, 4]))).to.be.revertedWith( - 'InvalidVaultIds()' - ) - await expect(strategy.deposit(toEther(200), encodeVaults([1, 12]))).to.be.revertedWith( - 'InvalidVaultIds()' - ) + await expect( + strategy.deposit(toEther(200), encodeVaults([6, 11, 4])) + ).to.be.revertedWithCustomError(strategy, 'InvalidVaultIds()') + await expect( + strategy.deposit(toEther(200), encodeVaults([1, 12])) + ).to.be.revertedWithCustomError(strategy, 'InvalidVaultIds()') await strategy.deposit(toEther(200), encodeVaults([1, 6, 11, 4])) assert.equal(fromEther(await token.balanceOf(adrs.stakingController)), 710) @@ -231,11 +231,11 @@ describe('VaultControllerStrategy', () => { assert.equal(fromEther(await stakingController.getStakerPrincipal(vaults[4])), 50) assert.equal(fromEther(await strategy.canWithdraw()), 30) assert.deepEqual( - await strategy.vaultGroups(1).then((d) => [d[0].toNumber(), fromEther(d[1])]), + await strategy.vaultGroups(1).then((d) => [Number(d[0]), fromEther(d[1])]), [11, 70] ) assert.deepEqual( - await strategy.vaultGroups(4).then((d) => [d[0].toNumber(), fromEther(d[1])]), + await strategy.vaultGroups(4).then((d) => [Number(d[0]), fromEther(d[1])]), [9, 150] ) @@ -245,7 +245,7 @@ describe('VaultControllerStrategy', () => { assert.equal(fromEther(await stakingController.getStakerPrincipal(vaults[9])), 50) assert.equal(fromEther(await strategy.canWithdraw()), 30) assert.deepEqual( - await strategy.vaultGroups(4).then((d) => [d[0].toNumber(), fromEther(d[1])]), + await strategy.vaultGroups(4).then((d) => [Number(d[0]), fromEther(d[1])]), [4, 50] ) @@ -254,23 +254,23 @@ describe('VaultControllerStrategy', () => { await strategy.deposit(toEther(600), encodeVaults([9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11])) assert.equal(fromEther(await token.balanceOf(adrs.stakingController)), 1360) assert.deepEqual( - await strategy.vaultGroups(0).then((d) => [d[0].toNumber(), fromEther(d[1])]), + await strategy.vaultGroups(0).then((d) => [Number(d[0]), fromEther(d[1])]), [0, 50] ) assert.deepEqual( - await strategy.vaultGroups(1).then((d) => [d[0].toNumber(), fromEther(d[1])]), + await strategy.vaultGroups(1).then((d) => [Number(d[0]), fromEther(d[1])]), [11, 70] ) assert.deepEqual( - await strategy.vaultGroups(2).then((d) => [d[0].toNumber(), fromEther(d[1])]), + await strategy.vaultGroups(2).then((d) => [Number(d[0]), fromEther(d[1])]), [7, 0] ) assert.deepEqual( - await strategy.vaultGroups(3).then((d) => [d[0].toNumber(), fromEther(d[1])]), + await strategy.vaultGroups(3).then((d) => [Number(d[0]), fromEther(d[1])]), [8, 20] ) assert.deepEqual( - await strategy.vaultGroups(4).then((d) => [d[0].toNumber(), fromEther(d[1])]), + await strategy.vaultGroups(4).then((d) => [Number(d[0]), fromEther(d[1])]), [4, 0] ) assert.equal(fromEther(await stakingController.getStakerPrincipal(vaults[12])), 100) @@ -330,12 +330,12 @@ describe('VaultControllerStrategy', () => { await time.increase(claimPeriod) await updateVaultGroups([4, 9], 300) - await expect(strategy.withdraw(toEther(150), encodeVaults([5, 10]))).to.be.revertedWith( - 'InvalidVaultIds()' - ) - await expect(strategy.withdraw(toEther(150), encodeVaults([0, 1]))).to.be.revertedWith( - 'InvalidVaultIds()' - ) + await expect( + strategy.withdraw(toEther(150), encodeVaults([5, 10])) + ).to.be.revertedWithCustomError(strategy, 'InvalidVaultIds()') + await expect( + strategy.withdraw(toEther(150), encodeVaults([0, 1])) + ).to.be.revertedWithCustomError(strategy, 'InvalidVaultIds()') await strategy.withdraw(toEther(150), encodeVaults([0, 5])) assert.equal(fromEther(await token.balanceOf(adrs.stakingController)), 1050) @@ -345,7 +345,7 @@ describe('VaultControllerStrategy', () => { assert.equal(fromEther(await strategy.getTotalDeposits()), 1050) assert.equal(fromEther(await strategy.canWithdraw()), 150) assert.deepEqual( - await strategy.vaultGroups(0).then((d) => [d[0].toNumber(), fromEther(d[1])]), + await strategy.vaultGroups(0).then((d) => [Number(d[0]), fromEther(d[1])]), [5, 150] ) @@ -359,7 +359,7 @@ describe('VaultControllerStrategy', () => { assert.equal(fromEther(await strategy.getTotalDeposits()), 975) assert.equal(fromEther(await strategy.canWithdraw()), 225) assert.deepEqual( - await strategy.vaultGroups(1).then((d) => [d[0].toNumber(), fromEther(d[1])]), + await strategy.vaultGroups(1).then((d) => [Number(d[0]), fromEther(d[1])]), [1, 75] ) @@ -372,17 +372,18 @@ describe('VaultControllerStrategy', () => { assert.equal(fromEther(await strategy.getTotalDeposits()), 850) assert.equal(fromEther(await strategy.canWithdraw()), 100) assert.deepEqual( - await strategy.vaultGroups(1).then((d) => [d[0].toNumber(), fromEther(d[1])]), + await strategy.vaultGroups(1).then((d) => [Number(d[0]), fromEther(d[1])]), [6, 200] ) - await expect(strategy.withdraw(toEther(101), encodeVaults([6, 11]))).to.be.revertedWith( - 'InsufficientTokensUnbonded()' - ) + await expect( + strategy.withdraw(toEther(101), encodeVaults([6, 11])) + ).to.be.revertedWithCustomError(strategy, 'InsufficientTokensUnbonded()') await time.increase(claimPeriod) - await expect(strategy.withdraw(toEther(20), encodeVaults([6]))).to.be.revertedWith( + await expect(strategy.withdraw(toEther(20), encodeVaults([6]))).to.be.revertedWithCustomError( + strategy, 'InsufficientTokensUnbonded()' ) @@ -396,7 +397,7 @@ describe('VaultControllerStrategy', () => { assert.equal(fromEther(await strategy.getTotalDeposits()), 650) assert.equal(fromEther(await strategy.canWithdraw()), 0) assert.deepEqual( - await strategy.vaultGroups(2).then((d) => [d[0].toNumber(), fromEther(d[1])]), + await strategy.vaultGroups(2).then((d) => [Number(d[0]), fromEther(d[1])]), [7, 200] ) }) @@ -601,8 +602,11 @@ describe('VaultControllerStrategy', () => { }) it('setWithdrawalIndexes should work correctly', async () => { + const { strategy } = await loadFixture(deployFixture) + await strategy.setWithdrawalIndexes([5, 6, 7, 8, 9]) - await expect(strategy.setWithdrawalIndexes([0, 1, 2, 3, 5])).to.be.revertedWith( + await expect(strategy.setWithdrawalIndexes([0, 1, 2, 3, 5])).to.be.revertedWithCustomError( + strategy, 'InvalidWithdrawalIndexes()' ) }) diff --git a/test/linkStaking/vault.test.ts b/test/linkStaking/vault.test.ts index ddb988f0..e572a5b0 100644 --- a/test/linkStaking/vault.test.ts +++ b/test/linkStaking/vault.test.ts @@ -83,18 +83,21 @@ describe('Vault', () => { await vault.unbond() let ts: any = (await ethers.provider.getBlock('latest'))?.timestamp assert.equal( - (await stakingController.getClaimPeriodEndsAt(adrs.vault)).toNumber(), + Number(await stakingController.getClaimPeriodEndsAt(adrs.vault)), ts + unbondingPeriod + claimPeriod ) }) it('should be able to withdraw', async () => { - const { adrs, vault, token } = await loadFixture(deployFixture) + const { adrs, vault, token, stakingController } = await loadFixture(deployFixture) await vault.deposit(toEther(100)) await vault.unbond() - await expect(vault.withdraw(toEther(30))).to.be.revertedWith('NotInClaimPeriod()') + await expect(vault.withdraw(toEther(30))).to.be.revertedWithCustomError( + stakingController, + 'NotInClaimPeriod()' + ) await time.increase(unbondingPeriod + 1)