Skip to content

Commit

Permalink
Merge pull request #8 from PufferFinance/fix-blocksec/staking
Browse files Browse the repository at this point in the history
Staking contracts fixes
  • Loading branch information
WalidOfNow authored Jun 27, 2024
2 parents 067b9f2 + 5a98736 commit de6833d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 52 deletions.
59 changes: 15 additions & 44 deletions mainnet-contracts/src/PufToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ contract PufToken is IPufStakingPool, ERC20, ERC20Permit {
"Migrate(address depositor,address migratorContract,address destination,address token,uint256 amount,uint256 signatureExpiry,uint256 nonce)"
);

/**
* @notice Standard Token Decimals
*/
uint256 internal constant _STANDARD_TOKEN_DECIMALS = 18;

/**
* @notice The underlying token decimals
*/
Expand Down Expand Up @@ -104,11 +99,7 @@ contract PufToken is IPufStakingPool, ERC20, ERC20Permit {
/**
* @inheritdoc IPufStakingPool
*/
function deposit(address from, address account, uint256 amount)
external
whenNotPaused
validateAddressAndAmount(account, amount)
{
function deposit(address from, address account, uint256 amount) external whenNotPaused {
_deposit(from, account, amount);
}

Expand All @@ -118,12 +109,9 @@ contract PufToken is IPufStakingPool, ERC20, ERC20Permit {
function withdraw(address recipient, uint256 amount) external validateAddressAndAmount(recipient, amount) {
_burn(msg.sender, amount);

uint256 deNormalizedAmount = _denormalizeAmount(amount);

TOKEN.safeTransfer(recipient, deNormalizedAmount);
TOKEN.safeTransfer(recipient, amount);

// Using the original deposit amount in the event (in this case it is denormalized amount)
emit Withdrawn(msg.sender, recipient, deNormalizedAmount);
emit Withdrawn(msg.sender, recipient, amount);
}

/**
Expand All @@ -132,7 +120,6 @@ contract PufToken is IPufStakingPool, ERC20, ERC20Permit {
function migrate(uint256 amount, address migratorContract, address destination)
external
onlyAllowedMigratorContract(migratorContract)
validateAddressAndAmount(destination, amount)
whenNotPaused
{
_migrate({ depositor: msg.sender, amount: amount, destination: destination, migratorContract: migratorContract });
Expand Down Expand Up @@ -185,19 +172,20 @@ contract PufToken is IPufStakingPool, ERC20, ERC20Permit {
totalDepositCap = newDepositCap;
}

function _deposit(address depositor, address account, uint256 amount) internal {
function _deposit(address depositor, address account, uint256 amount)
internal
validateAddressAndAmount(account, amount)
{
TOKEN.safeTransferFrom(msg.sender, address(this), amount);

uint256 normalizedAmount = _normalizeAmount(amount);

if (totalSupply() + normalizedAmount > totalDepositCap) {
if (totalSupply() + amount > totalDepositCap) {
revert TotalDepositCapReached();
}

// Mint puffToken to the account
_mint(account, normalizedAmount);
_mint(account, amount);

// If the user is deposiing using the factory, we emit the `depositor` from the parameters
// If the user is depositng using the factory, we emit the `depositor` from the parameters
if (msg.sender == address(PUFFER_FACTORY)) {
emit Deposited(depositor, account, amount);
} else {
Expand All @@ -209,38 +197,21 @@ contract PufToken is IPufStakingPool, ERC20, ERC20Permit {
/**
* @notice Transfers the Token from this contract using the migrator contract
*/
function _migrate(address depositor, uint256 amount, address destination, address migratorContract) internal {
function _migrate(address depositor, uint256 amount, address destination, address migratorContract)
internal
validateAddressAndAmount(destination, amount)
{
_burn(depositor, amount);

uint256 deNormalizedAmount = _denormalizeAmount(amount);

emit Migrated({
depositor: depositor,
destination: destination,
migratorContract: migratorContract,
amount: amount
});

TOKEN.safeIncreaseAllowance(migratorContract, deNormalizedAmount);
TOKEN.safeIncreaseAllowance(migratorContract, amount);

IMigrator(migratorContract).migrate({ depositor: depositor, destination: destination, amount: amount });
}

function _normalizeAmount(uint256 amount) internal view returns (uint256 normalizedAmount) {
if (_TOKEN_DECIMALS > _STANDARD_TOKEN_DECIMALS) {
return amount / (10 ** (_TOKEN_DECIMALS - _STANDARD_TOKEN_DECIMALS));
} else if (_TOKEN_DECIMALS < _STANDARD_TOKEN_DECIMALS) {
return amount * (10 ** (_STANDARD_TOKEN_DECIMALS - _TOKEN_DECIMALS));
}
return amount;
}

function _denormalizeAmount(uint256 amount) internal view returns (uint256 denormalizedAmount) {
if (_TOKEN_DECIMALS > _STANDARD_TOKEN_DECIMALS) {
return amount * (10 ** (_TOKEN_DECIMALS - _STANDARD_TOKEN_DECIMALS));
} else if (_TOKEN_DECIMALS < _STANDARD_TOKEN_DECIMALS) {
return amount / (10 ** (_STANDARD_TOKEN_DECIMALS - _TOKEN_DECIMALS));
}
return amount;
}
}
6 changes: 3 additions & 3 deletions mainnet-contracts/src/PufferL2Depositor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ contract PufferL2Depositor is IPufferL2Depositor, AccessManaged {
}

/**
* @notice Changes the status of `migrator` to `allowed`
* @notice Sets the deposit cap for the `token`
* @dev Restricted to Puffer DAO
*/
function setDepositCap(address token, uint256 newDepositCap) external onlySupportedTokens(token) restricted {
Expand Down Expand Up @@ -134,8 +134,8 @@ contract PufferL2Depositor is IPufferL2Depositor, AccessManaged {
revert InvalidToken();
}

string memory symbol = string(abi.encodePacked("puf ", ERC20(token).symbol()));
string memory name = string(abi.encodePacked("puf", ERC20(token).name()));
string memory symbol = string(abi.encodePacked("puf", ERC20(token).symbol()));
string memory name = string(abi.encodePacked("puf ", ERC20(token).name()));

// Reverts on duplicate token
address pufToken =
Expand Down
14 changes: 9 additions & 5 deletions mainnet-contracts/test/unit/PufferL2Staking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,18 @@ contract PufferL2Staking is UnitTestHelper {

PufToken pufToken = PufToken(depositor.tokens(address(sixDecimal)));

assertEq(pufToken.balanceOf(bob), 1 ether, "bob got 1 eth pufToken");
assertEq(pufToken.balanceOf(bob), amount, "bob got same amount in pufToken");
assertEq(sixDecimal.balanceOf(bob), 0, "0 token bob");

vm.expectEmit(true, true, true, true);
emit IPufStakingPool.Withdrawn(bob, bob, amount); // original deposit amount
pufToken.withdraw(bob, 1 ether);
pufToken.withdraw(bob, amount);

assertEq(sixDecimal.balanceOf(bob), amount, "bob got same amount");
}

// Deposit & withdraw 22 decimal token
function test_deposit_and_withdraw_22Decimal_approve() public {
function test_deposit_and_withdraw_twentyTwoDecimal_approve() public {
uint256 amount = 10 ** 22;

// This is a bad permit signature
Expand All @@ -199,12 +201,14 @@ contract PufferL2Staking is UnitTestHelper {

PufToken pufToken = PufToken(depositor.tokens(address(twentyTwoDecimal)));

assertEq(pufToken.balanceOf(bob), 1 ether, "bob got 1 eth pufToken");
assertEq(pufToken.balanceOf(bob), amount, "bob got same amount in pufToken");
assertEq(twentyTwoDecimal.balanceOf(bob), 0, "0 token bob");

vm.expectEmit(true, true, true, true);
emit IPufStakingPool.Withdrawn(bob, bob, amount); // original deposit amount
pufToken.withdraw(bob, 1 ether);
pufToken.withdraw(bob, amount);

assertEq(twentyTwoDecimal.balanceOf(bob), amount, "bob got same amount");
}

// Good Permit signature signature
Expand Down

0 comments on commit de6833d

Please sign in to comment.