inline contract functions to save gas for buckets

ERC20Bucket from 47,372 to 46,971
NFTBucket   from 53,056 to 52,225
This commit is contained in:
Andrea Franz 2020-04-30 01:06:18 +02:00
parent ce4df7d040
commit c2251bd20d
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
1 changed files with 9 additions and 16 deletions

View File

@ -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");
}
}