Skip to content

Commit

Permalink
updated metis staking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BkChoy committed Apr 22, 2024
1 parent 41238dc commit 5188534
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 40 deletions.
4 changes: 4 additions & 0 deletions contracts/linkStaking/test/VCSMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ contract VCSMock is VaultControllerStrategy {
function deployVault(bytes memory _data) external {
_deployVault(_data);
}

function addFeeBypassUpdate(address _receiver, uint256 _feeBasisPoints) external {
fees.push(Fee(_receiver, _feeBasisPoints));
}
}
2 changes: 1 addition & 1 deletion contracts/metisStaking/SequencerVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ contract SequencerVault is Initializable, UUPSUpgradeable, OwnableUpgradeable {

uint256 claimedRewards;
if (_minRewards != 0 && rewards >= _minRewards) {
if (principal + rewards <= vaultController.getVaultDepositMax()) {
if ((principal + rewards) <= vaultController.getVaultDepositMax()) {
lockingPool.relock(seqId, 0, true);
} else {
lockingPool.withdrawRewards{value: msg.value}(seqId, _l2Gas);
Expand Down
2 changes: 0 additions & 2 deletions contracts/metisStaking/interfaces/ISequencerVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ interface ISequencerVault {

function deposit(uint256 _amount) external;

function relockRewards() external returns (uint256);

function upgradeToAndCall(address _newImplementation, bytes memory _data) external;

function upgradeTo(address _newImplementation) external;
Expand Down
21 changes: 14 additions & 7 deletions contracts/metisStaking/test/MetisLockingInfoMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,25 @@ contract MetisLockingInfoMock {
token.safeTransferFrom(_owner, address(this), _amount);
}

function increaseLocked(address _owner, uint256 _amount) external {
if (_amount + locked[_owner] > maxLock) revert InvalidAmount();
locked[_owner] += _amount;
function increaseLocked(
address _owner,
uint256 _amount,
uint256 _rewardsAmount
) external {
if (_amount + _rewardsAmount + locked[_owner] > maxLock) revert InvalidAmount();
locked[_owner] += _amount + _rewardsAmount;
token.safeTransferFrom(_owner, address(this), _amount);
}

function setDepositLimits(uint256 _minLock, uint256 _maxLock) external {
minLock = _minLock;
function setManager(address _manager) external {
manager = _manager;
}

function setMaxLock(uint256 _maxLock) external {
maxLock = _maxLock;
}

function setManager(address _manager) external {
manager = _manager;
function setMinLock(uint256 _minLock) external {
maxLock = _minLock;
}
}
21 changes: 16 additions & 5 deletions contracts/metisStaking/test/MetisLockingPoolMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
interface ILockingInfoMock {
function newSequencer(address _owner, uint256 _amount) external;

function increaseLocked(address _owner, uint256 _amount) external;
function increaseLocked(
address _owner,
uint256 _amount,
uint256 _rewardsAmount
) external;
}

/**
Expand Down Expand Up @@ -47,6 +51,7 @@ contract MetisLockingPoolMock {
mapping(address => uint256) public seqOwners;

error InvalidAmount();
error InvalidMsgValue();

constructor(address _token, address _lockingInfo) {
token = IERC20(_token);
Expand All @@ -70,13 +75,19 @@ contract MetisLockingPoolMock {
function relock(
uint256 _seqId,
uint256 _amount,
bool
bool _lockReward
) external {
sequencers[_seqId].amount += _amount;
lockingInfo.increaseLocked(msg.sender, _amount);
uint256 rewards;
if (_lockReward) {
rewards = sequencers[_seqId].reward;
sequencers[_seqId].reward = 0;
}
sequencers[_seqId].amount += _amount + rewards;
lockingInfo.increaseLocked(msg.sender, _amount, rewards);
}

function withdrawRewards(uint256 _seqId, uint32) external {
function withdrawRewards(uint256 _seqId, uint32) external payable {
if (msg.value == 0) revert InvalidMsgValue();
sequencers[_seqId].reward = 0;
}

Expand Down
13 changes: 12 additions & 1 deletion contracts/metisStaking/test/SequencerVCSMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import "../interfaces/ISequencerVault.sol";
import "../interfaces/IMetisLockingInfo.sol";

/**
* @title Sequencer VCS Mock
Expand All @@ -14,6 +15,7 @@ contract SequencerVCSMock {
using SafeERC20 for IERC20;

IERC20 public token;
IMetisLockingInfo public lockingInfo;

uint256 public operatorRewardPercentage;
uint256 public withdrawalPercentage;
Expand All @@ -25,15 +27,21 @@ contract SequencerVCSMock {

constructor(
address _token,
address _lockingInfo,
uint256 _operatorRewardPercentage,
uint256 _withdrawalPercentage
) {
token = IERC20(_token);
lockingInfo = IMetisLockingInfo(_lockingInfo);
operatorRewardPercentage = _operatorRewardPercentage;
withdrawalPercentage = _withdrawalPercentage;
rewardRecipient = address(9);
}

function getVaultDepositMax() external returns (uint256) {
return lockingInfo.maxLock();
}

function deposit(uint256 _amount) external {
token.transferFrom(msg.sender, address(this), _amount);
vault.deposit(_amount);
Expand All @@ -46,13 +54,14 @@ contract SequencerVCSMock {

function updateDeposits(uint256 _minRewards)
external
payable
returns (
uint256,
uint256,
uint256
)
{
return vault.updateDeposits(_minRewards, 0);
return vault.updateDeposits{value: msg.value}(_minRewards, 0);
}

function handleIncomingL2Rewards(uint256 _amount) external {
Expand All @@ -67,4 +76,6 @@ contract SequencerVCSMock {
function setWithdrawalPercentage(uint256 _withdrawalPercentage) external {
withdrawalPercentage = _withdrawalPercentage;
}

receive() external payable {}
}
10 changes: 5 additions & 5 deletions test/core/staking-pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ describe('StakingPool', () => {
})

it('fee splitting should work correctly', async () => {
await stakingPool.addFee(accounts[0], 2000)
await stakingPool.addFee(accounts[0], 1000)
await strategy1.setFeeBasisPoints(1000)
await strategy3.setFeeBasisPoints(1000)

Expand All @@ -399,17 +399,17 @@ describe('StakingPool', () => {

assert.equal(
fromEther(await stakingPool.balanceOf(accounts[1])),
2196,
2248,
'Account-1 balance incorrect'
)
assert.equal(
fromEther(await stakingPool.balanceOf(accounts[2])),
1098,
1124,
'Account-2 balance incorrect'
)
assert.equal(
fromEther(await stakingPool.balanceOf(accounts[3])),
2196,
2248,
'Account-3 balance incorrect'
)

Expand All @@ -420,7 +420,7 @@ describe('StakingPool', () => {
)
assert.equal(
fromEther(await stakingPool.balanceOf(accounts[0])),
160 + 260,
160 + 130,
'Strategy fee balance incorrect'
)
assert.equal(
Expand Down
18 changes: 14 additions & 4 deletions test/linkStaking/vault-controller-strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
CommunityVault,
StakingRewardsMock,
CommunityVaultV2Mock,
StakingPool,
} from '../../typechain-types'
import { Interface } from 'ethers/lib/utils'

Expand Down Expand Up @@ -257,7 +258,7 @@ describe('VaultControllerStrategy', () => {
await strategy.deposit(toEther(300))

await rewardsController.setReward(vaults[0], toEther(100))
await strategy.addFee(accounts[3], 1000)
await strategy.addFeeBypassUpdate(accounts[3], 1000)
let data = await strategy.callStatic.updateDeposits('0x')

assert.equal(fromEther(data.depositChange), 100)
Expand Down Expand Up @@ -343,7 +344,7 @@ describe('VaultControllerStrategy', () => {
let newVaultImplementation = (await deployImplementation('CommunityVaultV2Mock')) as string
await strategy.setVaultImplementation(newVaultImplementation)

await strategy.upgradeVaults(0, 5, '0x')
await strategy.upgradeVaults([0, 1, 2, 3, 4], ['0x', '0x', '0x', '0x', '0x'])
for (let i = 0; i < 5; i++) {
let vault = (await ethers.getContractAt(
'CommunityVaultV2Mock',
Expand All @@ -352,14 +353,23 @@ describe('VaultControllerStrategy', () => {
assert.equal(await vault.isUpgraded(), true)
}

await strategy.upgradeVaults(5, 5, vaultInterface.encodeFunctionData('initializeV2', [2]))
await strategy.upgradeVaults(
[5, 6, 7, 8, 9],
[
vaultInterface.encodeFunctionData('initializeV2', [5]),
vaultInterface.encodeFunctionData('initializeV2', [6]),
vaultInterface.encodeFunctionData('initializeV2', [7]),
vaultInterface.encodeFunctionData('initializeV2', [8]),
vaultInterface.encodeFunctionData('initializeV2', [9]),
]
)
for (let i = 5; i < 10; i++) {
let vault = (await ethers.getContractAt(
'CommunityVaultV2Mock',
vaults[i]
)) as CommunityVaultV2Mock
assert.equal(await vault.isUpgraded(), true)
assert.equal((await vault.getVersion()).toNumber(), 2)
assert.equal((await vault.getVersion()).toNumber(), i)
}
})

Expand Down
14 changes: 13 additions & 1 deletion test/metisStaking/sequencer-rewards-ccip-receiver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CCIPTokenPoolMock,
SequencerRewardsCCIPReceiver,
SequencerVCSMock,
MetisLockingInfoMock,
} from '../../typechain-types'
import { Signer } from 'ethers'

Expand Down Expand Up @@ -58,7 +59,18 @@ describe('SequencerRewardsCCIPReceiver', () => {

await router.applyRampUpdates([], [], [[77, offRamp.address]])

strategy = (await deploy('SequencerVCSMock', [token.address, 1000, 5000])) as SequencerVCSMock
let metisLockingInfo = (await deploy('MetisLockingInfoMock', [
token.address,
toEther(100),
toEther(10000),
])) as MetisLockingInfoMock

strategy = (await deploy('SequencerVCSMock', [
token.address,
metisLockingInfo.address,
1000,
5000,
])) as SequencerVCSMock

ccipReceiver = (await deploy('SequencerRewardsCCIPReceiver', [
router.address,
Expand Down
25 changes: 22 additions & 3 deletions test/metisStaking/sequencer-vault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ describe('SequencerVault', () => {
metisLockingInfo.address,
])) as MetisLockingPoolMock

strategy = (await deploy('SequencerVCSMock', [token.address, 1000, 5000])) as SequencerVCSMock
strategy = (await deploy('SequencerVCSMock', [
token.address,
metisLockingInfo.address,
1000,
5000,
])) as SequencerVCSMock

vault = (await deployUpgradeable('SequencerVault', [
token.address,
Expand Down Expand Up @@ -113,6 +118,7 @@ describe('SequencerVault', () => {
})

it('updateDeposits should work correctly', async () => {
await metisLockingInfo.setMaxLock(toEther(100))
await strategy.deposit(toEther(100))
await metisLockingPool.addReward(1, toEther(10))
assert.deepEqual(
Expand Down Expand Up @@ -164,14 +170,27 @@ describe('SequencerVault', () => {
assert.equal(fromEther(await vault.trackedTotalDeposits()), 114)

assert.deepEqual(
(await strategy.callStatic.updateDeposits(toEther(19))).map((v) => fromEther(v)),
(await strategy.callStatic.updateDeposits(toEther(19), { value: toEther(1) })).map((v) =>
fromEther(v)
),
[95, 0, 19]
)
await strategy.updateDeposits(toEther(19))
await strategy.updateDeposits(toEther(19), { value: toEther(1) })
assert.equal(fromEther(await vault.getPendingRewards()), 0)
assert.equal(fromEther(await vault.unclaimedRewards()), 1.4)
assert.equal(fromEther(await vault.trackedTotalDeposits()), 95)

await metisLockingInfo.setMaxLock(toEther(120))
await metisLockingPool.addReward(1, toEther(7))
assert.deepEqual(
(await strategy.callStatic.updateDeposits(toEther(7))).map((v) => fromEther(v)),
[102, 0.7, 0]
)
await strategy.updateDeposits(toEther(7))
assert.equal(fromEther(await vault.getPendingRewards()), 0)
assert.equal(fromEther(await vault.unclaimedRewards()), 2.1)
assert.equal(fromEther(await vault.trackedTotalDeposits()), 102)

await expect(vault.updateDeposits(0, 0)).to.be.revertedWith('SenderNotAuthorized()')
})

Expand Down
Loading

0 comments on commit 5188534

Please sign in to comment.