From 6379be6307161e55b0f65664154f4fc2d8a7cc94 Mon Sep 17 00:00:00 2001 From: Nik G Date: Sun, 28 Apr 2019 11:28:22 -0500 Subject: [PATCH] Move withdrawal functionality into a mixin contract --- .../solidity/contracts/DelayedWithdrawal.sol | 35 +++++++++++++++++++ .../contracts/KeepRandomBeaconImplV1.sol | 24 ++----------- 2 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 contracts/solidity/contracts/DelayedWithdrawal.sol diff --git a/contracts/solidity/contracts/DelayedWithdrawal.sol b/contracts/solidity/contracts/DelayedWithdrawal.sol new file mode 100644 index 0000000000..f9e257b63c --- /dev/null +++ b/contracts/solidity/contracts/DelayedWithdrawal.sol @@ -0,0 +1,35 @@ +pragma solidity ^0.5.4; + +import "openzeppelin-solidity/contracts/ownership/Ownable.sol"; +import "openzeppelin-solidity/contracts/math/SafeMath.sol"; + + +/** + * @title Delayed Withdrawal + * @dev A base contract to allow delayed funds withdrawal from the contract. + */ +contract DelayedWithdrawal is Ownable { + using SafeMath for uint256; + + uint256 internal _withdrawalDelay; + uint256 internal _pendingWithdrawal; + + /** + * @dev Initiate withdrawal of this contract balance to the owner. + */ + function initiateWithdrawal() public onlyOwner { + _pendingWithdrawal = block.timestamp + _withdrawalDelay; + } + + /** + * @dev Finish withdrawal of this contract balance to the owner. + */ + function finishWithdrawal(address payable payee) public onlyOwner { + require(_pendingWithdrawal > 0, "Pending withdrawal timestamp must be set and be greater than zero."); + require(block.timestamp >= _pendingWithdrawal, "The current time must pass the pending withdrawal timestamp."); + + // Reset pending withdrawal before sending to prevent re-entrancy attacks + _pendingWithdrawal = 0; + payee.transfer(address(this).balance); + } +} diff --git a/contracts/solidity/contracts/KeepRandomBeaconImplV1.sol b/contracts/solidity/contracts/KeepRandomBeaconImplV1.sol index b07e8ee234..ce0703e162 100644 --- a/contracts/solidity/contracts/KeepRandomBeaconImplV1.sol +++ b/contracts/solidity/contracts/KeepRandomBeaconImplV1.sol @@ -1,6 +1,7 @@ pragma solidity ^0.5.4; import "openzeppelin-solidity/contracts/ownership/Ownable.sol"; +import "./DelayedWithdrawal.sol"; import "solidity-bytes-utils/contracts/BytesLib.sol"; import "./BLS.sol"; @@ -19,7 +20,7 @@ interface GroupContract { * up-to-date logic for threshold random number generation. Updated contracts * must inherit from this contract and have to be initialized under updated version name */ -contract KeepRandomBeaconImplV1 is Ownable { +contract KeepRandomBeaconImplV1 is Ownable, DelayedWithdrawal { using BytesLib for bytes; @@ -29,8 +30,6 @@ contract KeepRandomBeaconImplV1 is Ownable { uint256 internal _requestCounter; uint256 internal _minPayment; - uint256 internal _withdrawalDelay; - uint256 internal _pendingWithdrawal; address internal _groupContract; uint256 internal _previousEntry; @@ -112,25 +111,6 @@ contract KeepRandomBeaconImplV1 is Ownable { return _requestCounter; } - /** - * @dev Initiate withdrawal of this contract balance to the owner. - */ - function initiateWithdrawal() public onlyOwner { - _pendingWithdrawal = block.timestamp + _withdrawalDelay; - } - - /** - * @dev Finish withdrawal of this contract balance to the owner. - */ - function finishWithdrawal(address payable payee) public onlyOwner { - require(_pendingWithdrawal > 0, "Pending withdrawal timestamp must be set and be greater than zero."); - require(block.timestamp >= _pendingWithdrawal, "The current time must pass the pending withdrawal timestamp."); - - // Reset pending withdrawal before sending to prevent re-entrancy attacks - _pendingWithdrawal = 0; - payee.transfer(address(this).balance); - } - /** * @dev Set the minimum payment that is required before a relay entry occurs. * @param minPayment is the value in wei that is required to be payed for the process to start.