refactor(StakeVault): introduce `stakedToken()` API (#36)

Because the `stakedToken` property is `immutable`, solhint recommends to
make it in all caps. This commit changes the property to adhere to that
rule and also makes the property private.

To access the `stakedToken` there's now a `stakedToken()` function on
the contract.
This commit is contained in:
r4bbit 2023-11-07 09:40:38 +01:00 committed by GitHub
parent 70c74e2c2c
commit b5e513ce49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 10 deletions

View File

@ -1,17 +1,18 @@
ExecuteAccountTest:testDeployment() (gas: 26400)
ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 964423)
ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 982104)
LeaveTest:testDeployment() (gas: 26172)
LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 652901)
LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 670554)
LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 10562)
LockTest:testDeployment() (gas: 26400)
LockTest:test_RevertWhen_DecreasingLockTime() (gas: 967353)
LockTest:test_RevertWhen_DecreasingLockTime() (gas: 985034)
LockTest:test_RevertWhen_SenderIsNotVault() (gas: 10607)
MigrateTest:testDeployment() (gas: 26172)
MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 652740)
MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 670393)
MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 10629)
StakeManagerTest:testDeployment() (gas: 26172)
StakeTest:testDeployment() (gas: 26172)
StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 10638)
StakedTokenTest:testStakeToken() (gas: 7638)
UnstakeTest:testDeployment() (gas: 26355)
UnstakeTest:test_RevertWhen_FundsLocked() (gas: 963816)
UnstakeTest:test_RevertWhen_FundsLocked() (gas: 981497)
UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 10609)

View File

@ -16,18 +16,19 @@ contract StakeVault is Ownable {
error StakeVault__MigrationNotAvailable();
StakeManager private stakeManager;
ERC20 immutable stakedToken;
ERC20 private immutable STAKED_TOKEN;
event Staked(address from, address to, uint256 _amount, uint256 time);
constructor(address _owner, ERC20 _stakedToken, StakeManager _stakeManager) {
_transferOwnership(_owner);
stakedToken = _stakedToken;
STAKED_TOKEN = _stakedToken;
stakeManager = _stakeManager;
}
function stake(uint256 _amount, uint256 _time) external onlyOwner {
stakedToken.transferFrom(msg.sender, address(this), _amount);
STAKED_TOKEN.transferFrom(msg.sender, address(this), _amount);
stakeManager.stake(_amount, _time);
emit Staked(msg.sender, address(this), _amount, _time);
@ -39,12 +40,12 @@ contract StakeVault is Ownable {
function unstake(uint256 _amount) external onlyOwner {
stakeManager.unstake(_amount);
stakedToken.transferFrom(address(this), msg.sender, _amount);
STAKED_TOKEN.transferFrom(address(this), msg.sender, _amount);
}
function leave() external onlyOwner {
stakeManager.leave();
stakedToken.transferFrom(address(this), msg.sender, stakedToken.balanceOf(address(this)));
STAKED_TOKEN.transferFrom(address(this), msg.sender, STAKED_TOKEN.balanceOf(address(this)));
}
/**
@ -55,4 +56,8 @@ contract StakeVault is Ownable {
if (address(migrated) == address(0)) revert StakeVault__MigrationNotAvailable();
stakeManager = migrated;
}
function stakedToken() external view returns (ERC20) {
return STAKED_TOKEN;
}
}

43
test/StakeVault.t.sol Normal file
View File

@ -0,0 +1,43 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import { Test } from "forge-std/Test.sol";
import { Deploy } from "../script/Deploy.s.sol";
import { DeploymentConfig } from "../script/DeploymentConfig.s.sol";
import { StakeManager } from "../contracts/StakeManager.sol";
import { StakeVault } from "../contracts/StakeVault.sol";
contract StakeVaultTest is Test {
StakeManager internal stakeManager;
DeploymentConfig internal deploymentConfig;
StakeVault internal stakeVault;
address internal deployer;
address internal testUser = makeAddr("testUser");
address internal stakeToken;
function setUp() public virtual {
Deploy deployment = new Deploy();
(stakeManager, deploymentConfig) = deployment.run();
(deployer, stakeToken) = deploymentConfig.activeNetworkConfig();
vm.prank(testUser);
stakeVault = new StakeVault(testUser, ERC20(stakeToken), stakeManager);
}
}
contract StakedTokenTest is StakeVaultTest {
function setUp() public override {
StakeVaultTest.setUp();
}
function testStakeToken() public {
assertEq(address(stakeVault.stakedToken()), stakeToken);
}
}