Skip to content

dxdaoTimeLock #754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: arc-factory
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions contracts/misc/TimeLock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pragma solidity ^0.5.17;


contract TimeLock {

address public owner;
uint256 public releaseTime;

constructor(address _owner, uint256 _releaseTime) public {
owner = _owner;
releaseTime = _releaseTime;
}

function () external payable {
}

function withdraw() external {
require(msg.sender == owner, "only owner can withdraw");
// solhint-disable-next-line not-rely-on-time
require(releaseTime < now, "cannot withdraw before releaseTime");
// solhint-disable-next-line avoid-call-value
(bool success, ) = owner.call.value(address(this).balance)("");
require(success, "sendEther failed.");
}

}
8 changes: 8 additions & 0 deletions contracts/test/Wallet.sol
Original file line number Diff line number Diff line change
@@ -27,4 +27,12 @@ contract Wallet is Ownable {
emit Pay(_beneficiary, amount);
}

function genericCall(address _contract, bytes memory _encodedABI)
public
returns(bool success, bytes memory returnValue) {
// solhint-disable-next-line avoid-low-level-calls
(success, returnValue) = _contract.call(_encodedABI);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why instead of sending the _encodeABI we execute the withdraw function with the signature that we already know it will have?

(success, returnValue) = _contract.call(abi.encodeWithSignature("withdraw()"));

Doing this we make sure that the ONLY function that can be executed is the withdraw()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a public generic function which can call any contract specific Abis.
this is just a test helper contract and is not part of any dao.

require(success, "call fail");
}

}
45 changes: 45 additions & 0 deletions test/timelock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const helpers = require('./helpers');

const Wallet = artifacts.require("./Wallet.sol");
const TimeLock = artifacts.require("./TimeLock.sol");
contract('TimeLock', accounts => {

it("sendEther", async () => {
var wallet = await Wallet.new();
await wallet.initialize(accounts[0]);
var owner = wallet.address;
var block = await web3.eth.getBlock("latest");
var releaseTime = block.timestamp + (30*60*60*24);
var timeLock = await TimeLock.new(owner,releaseTime);
assert.equal(await timeLock.owner(), owner);
assert.equal(await timeLock.releaseTime(), releaseTime);

//send funds to wallet
await web3.eth.sendTransaction({from:accounts[0],to:owner, value: web3.utils.toWei('10', "ether")});
assert.equal(await web3.eth.getBalance(owner), web3.utils.toWei('10', "ether"));
await wallet.pay(timeLock.address);
await wallet.pay(timeLock.address);
assert.equal(await web3.eth.getBalance(timeLock.address), web3.utils.toWei('10', "ether"));

var encodedABI = await new web3.eth.Contract(timeLock.abi)
.methods
.withdraw()
.encodeABI();

try {
await wallet.genericCall(timeLock.address, encodedABI);
throw 'cannot withdraw before time';
} catch (error) {
helpers.assertVMException(error);
}
await helpers.increaseTime((30*60*60*24)+1);
try {
await timeLock.withdraw();
throw 'only Owner can withdraw';
} catch (error) {
helpers.assertVMException(error);
}
await wallet.genericCall(timeLock.address, encodedABI);
assert.equal(await web3.eth.getBalance(owner), web3.utils.toWei('10', "ether"));
});
});