Skip to content
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

dxdaoTimeLock #754

Draft
wants to merge 2 commits into
base: arc-factory
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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/TimeLocking.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pragma solidity ^0.5.17;


contract DxDAOTimeLock {

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)("");

Choose a reason for hiding this comment

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

Why not msg.sender.transfer(address(this).balance) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because transfer is limited in gas. it might work for dxdao avatar though no promise for other contracts which consume gas on the fallback function.
https://diligence.consensys.net/blog/2019/09/stop-using-soliditys-transfer-now/

require(success, "sendEther failed.");
}

}
8 changes: 8 additions & 0 deletions contracts/test/Wallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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/dxdaotimelock.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 DxDAOTimeLock = artifacts.require("./DxDAOTimeLock.sol");
contract('DxDAOTimeLock', 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 dxDAOTimeLock = await DxDAOTimeLock.new(owner,releaseTime);
assert.equal(await dxDAOTimeLock.owner(), owner);
assert.equal(await dxDAOTimeLock.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(dxDAOTimeLock.address);
await wallet.pay(dxDAOTimeLock.address);
assert.equal(await web3.eth.getBalance(dxDAOTimeLock.address), web3.utils.toWei('10', "ether"));

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

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