Skip to content

Commit

Permalink
refactor: replace ClonesWithCallData with ClonesWithImmutableArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeframLou committed Jan 23, 2022
1 parent 6a0ab54 commit e8b05aa
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 186 deletions.
18 changes: 9 additions & 9 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
testCorrectness_createVestedERC20(bytes32,bytes32,uint8,address,uint56) (runs: 256, μ: 78560, ~: 78565)
testGas_createVestedERC20(bytes32,bytes32,uint8,address,uint56) (runs: 256, μ: 69628, ~: 69633)
testCorrectness_wrapAndClaim_beforeVestStart(uint224) (runs: 256, μ: 15857, ~: 15859)
testCorrectness_wrapAndClaim_duringVest(uint224) (runs: 256, μ: 103410, ~: 103412)
testCorrectness_wrapAndClaim_transfer(uint224) (runs: 256, μ: 173705, ~: 173707)
testFail_wrap_afterVestEnd(uint224) (runs: 256, μ: 8375, ~: 8377)
testGas_redeem() (gas: 31018)
testGas_transfer() (gas: 28219)
testGas_wrap() (gas: 8057)
testCorrectness_createVestedERC20(bytes32,bytes32,uint8,address,uint56) (runs: 256, μ: 76483, ~: 76485)
testGas_createVestedERC20(bytes32,bytes32,uint8,address,uint56) (runs: 256, μ: 67551, ~: 67553)
testCorrectness_wrapAndClaim_beforeVestStart(uint224) (runs: 256, μ: 15314, ~: 15315)
testCorrectness_wrapAndClaim_duringVest(uint224) (runs: 256, μ: 102867, ~: 102868)
testCorrectness_wrapAndClaim_transfer(uint224) (runs: 256, μ: 153262, ~: 153263)
testFail_wrap_afterVestEnd(uint224) (runs: 256, μ: 7832, ~: 7833)
testGas_redeem() (gas: 30954)
testGas_transfer() (gas: 28155)
testGas_wrap() (gas: 7993)
15 changes: 3 additions & 12 deletions src/VestedERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,19 @@ contract VestedERC20 is ERC20 {
/// @notice The token that is vested
/// @return _underlying The address of the underlying token
function underlying() public pure returns (address _underlying) {
uint256 offset = _getImmutableVariablesOffset();
assembly {
_underlying := shr(0x60, calldataload(add(offset, 0x41)))
}
return _getArgAddress(0x41);
}

/// @notice The Unix timestamp (in seconds) of the start of the vest
/// @return _startTimestamp The vest start timestamp
function startTimestamp() public pure returns (uint64 _startTimestamp) {
uint256 offset = _getImmutableVariablesOffset();
assembly {
_startTimestamp := shr(0xc0, calldataload(add(offset, 0x55)))
}
return _getArgUint64(0x55);
}

/// @notice The Unix timestamp (in seconds) of the end of the vest
/// @return _endTimestamp The vest end timestamp
function endTimestamp() public pure returns (uint64 _endTimestamp) {
uint256 offset = _getImmutableVariablesOffset();
assembly {
_endTimestamp := shr(0xc0, calldataload(add(offset, 0x5d)))
}
return _getArgUint64(0x5d);
}

/// -----------------------------------------------------------------------
Expand Down
9 changes: 4 additions & 5 deletions src/VestedERC20Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

pragma solidity ^0.8.11;

import {ClonesWithImmutableArgs} from "@clones/ClonesWithImmutableArgs.sol";

import {ERC20} from "./lib/ERC20.sol";
import {VestedERC20} from "./VestedERC20.sol";
import {ClonesWithCallData} from "./lib/ClonesWithCallData.sol";

/// @title VestedERC20Factory
/// @author zefram.eth
Expand All @@ -14,7 +15,7 @@ contract VestedERC20Factory {
/// Library usage
/// -----------------------------------------------------------------------

using ClonesWithCallData for address;
using ClonesWithImmutableArgs for address;

/// -----------------------------------------------------------------------
/// Errors
Expand Down Expand Up @@ -70,9 +71,7 @@ contract VestedERC20Factory {
mstore(add(ptr, 0x75), shl(0xc0, startTimestamp))
mstore(add(ptr, 0x7d), shl(0xc0, endTimestamp))
}
vestedERC20 = VestedERC20(
address(implementation).cloneWithCallDataProvision(ptr)
);
vestedERC20 = VestedERC20(address(implementation).clone(ptr));
emit CreateVestedERC20(vestedERC20);
}
}
142 changes: 0 additions & 142 deletions src/lib/ClonesWithCallData.sol

This file was deleted.

25 changes: 7 additions & 18 deletions src/lib/ERC20.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

import {Clone} from "@clones/Clone.sol";

/// @notice Modern and gas efficient ERC20 + EIP-2612 implementation.
/// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/tokens/ERC20.sol)
/// @author Modified from Uniswap (https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol)
/// @dev Do not manually set balances without updating totalSupply, as the sum of all user balances must not exceed it.
abstract contract ERC20 {
abstract contract ERC20 is Clone {
/*///////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -33,28 +35,15 @@ abstract contract ERC20 {
//////////////////////////////////////////////////////////////*/

function name() external pure returns (string memory) {
uint256 offset = _getImmutableVariablesOffset();
bytes32 nameBytes;
assembly {
nameBytes := calldataload(offset)
}
return string(abi.encodePacked(nameBytes));
return string(abi.encodePacked(_getArgUint256(0)));
}

function symbol() external pure returns (string memory) {
uint256 offset = _getImmutableVariablesOffset();
bytes32 symbolBytes;
assembly {
symbolBytes := calldataload(add(offset, 0x20))
}
return string(abi.encodePacked(symbolBytes));
return string(abi.encodePacked(_getArgUint256(0x20)));
}

function decimals() external pure returns (uint8 _decimals) {
uint256 offset = _getImmutableVariablesOffset();
assembly {
_decimals := shr(0xf8, calldataload(add(offset, 0x40)))
}
function decimals() external pure returns (uint8) {
return _getArgUint8(0x40);
}

/*///////////////////////////////////////////////////////////////
Expand Down

0 comments on commit e8b05aa

Please sign in to comment.