Skip to content

Commit

Permalink
Move withdrawal functionality into a mixin contract
Browse files Browse the repository at this point in the history
  • Loading branch information
ngrinkevich committed Apr 28, 2019
1 parent 942c440 commit 6379be6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
35 changes: 35 additions & 0 deletions contracts/solidity/contracts/DelayedWithdrawal.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}
24 changes: 2 additions & 22 deletions contracts/solidity/contracts/KeepRandomBeaconImplV1.sol
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6379be6

Please sign in to comment.