From c2251bd20d82e61d530534611ed3caea2e3cab04 Mon Sep 17 00:00:00 2001 From: Andrea Franz Date: Thu, 30 Apr 2020 01:06:18 +0200 Subject: [PATCH] inline contract functions to save gas for buckets ERC20Bucket from 47,372 to 46,971 NFTBucket from 53,056 to 52,225 --- contracts/Bucket.sol | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/contracts/Bucket.sol b/contracts/Bucket.sol index 35a4d9e..a481bff 100644 --- a/contracts/Bucket.sol +++ b/contracts/Bucket.sol @@ -67,14 +67,21 @@ abstract contract Bucket { function bucketType() virtual external returns (uint256); function redeem(Redeem calldata _redeem, bytes calldata _sig) external { - validateRedeem(_redeem, maxTxDelayInBlocks, expirationTime, startTime); + // validate Redeem + require(_redeem.blockNumber < block.number, "transaction cannot be in the future"); + require(_redeem.blockNumber >= (block.number - maxTxDelayInBlocks), "transaction too old"); + require(_redeem.blockHash == blockhash(_redeem.blockNumber), "invalid block hash"); + require(block.timestamp < expirationTime, "expired redeemable"); + require(block.timestamp > startTime, "reedeming not yet started"); address recipient = recoverSigner(DOMAIN_SEPARATOR, _redeem, _sig); Redeemable storage redeemable = redeemables[recipient]; require(redeemable.recipient == recipient, "not found"); - validateCode(_redeem, redeemable.code); + // validate code + bytes32 codeHash = keccak256(abi.encodePacked(_redeem.code)); + require(codeHash == redeemable.code, "invalid code"); uint256 data = redeemable.data; @@ -109,15 +116,6 @@ abstract contract Bucket { require(block.timestamp >= _expirationTime, "not expired yet"); } - function validateRedeem(Redeem memory _redeem, uint256 _maxTxDelayInBlocks, uint256 _expirationTime, uint256 _startTime) internal view { - require(_redeem.blockNumber < block.number, "transaction cannot be in the future"); - require(_redeem.blockNumber >= (block.number - _maxTxDelayInBlocks), "transaction too old"); - require(_redeem.blockHash == blockhash(_redeem.blockNumber), "invalid block hash"); - - require(block.timestamp < _expirationTime, "expired redeemable"); - require(block.timestamp > _startTime, "reedeming not yet started"); - } - function hashRedeem(Redeem memory _redeem) internal pure returns (bytes32) { return keccak256(abi.encode( REDEEM_TYPEHASH, @@ -155,9 +153,4 @@ abstract contract Bucket { return ecrecover(digest, v, r, s); } - - function validateCode(Redeem memory _redeem, bytes32 _code) internal pure { - bytes32 codeHash = keccak256(abi.encodePacked(_redeem.code)); - require(codeHash == _code, "invalid code"); - } }