Skip to content

Commit dc6dd31

Browse files
authored
Separate Vault from Avatar (#750)
* Emit SendEther on GenericCall * Separate Valut from Avatar * Bump version
1 parent 32ae08d commit dc6dd31

File tree

6 files changed

+52
-40
lines changed

6 files changed

+52
-40
lines changed

contracts/controller/Avatar.sol

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,12 @@ pragma solidity ^0.5.17;
22

33
import "@daostack/infra-experimental/contracts/Reputation.sol";
44
import "./DAOToken.sol";
5-
import "@openzeppelin/contracts-ethereum-package/contracts/ownership/Ownable.sol";
5+
import "./Vault.sol";
66
import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol";
77
import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/SafeERC20.sol";
88
import "@openzeppelin/upgrades/contracts/Initializable.sol";
99

1010

11-
//Proxy contracts cannot recive eth via fallback function.
12-
//For now , we will use this vault to overcome that
13-
contract Vault is Ownable {
14-
event ReceiveEther(address indexed _sender, uint256 _value);
15-
16-
/**
17-
* @dev enables this contract to receive ethers
18-
*/
19-
function() external payable {
20-
emit ReceiveEther(msg.sender, msg.value);
21-
}
22-
23-
function sendEther(uint256 _amountInWei, address payable _to) external onlyOwner returns(bool) {
24-
// solhint-disable-next-line avoid-call-value
25-
(bool success, ) = _to.call.value(_amountInWei)("");
26-
require(success, "sendEther failed.");
27-
}
28-
}
29-
30-
3111
/**
3212
* @title An Avatar holds tokens, reputation and ether for a controller
3313
*/
@@ -41,7 +21,6 @@ contract Avatar is Initializable, Ownable {
4121
mapping(string=>string) public db;
4222

4323
event GenericCall(address indexed _contract, bytes _data, uint _value, bool _success);
44-
event SendEther(uint256 _amountInWei, address indexed _to);
4524
event ExternalTokenTransfer(address indexed _externalToken, address indexed _to, uint256 _value);
4625
event ExternalTokenTransferFrom(address indexed _externalToken, address _from, address _to, uint256 _value);
4726
event ExternalTokenApproval(address indexed _externalToken, address _spender, uint256 _value);
@@ -104,7 +83,6 @@ contract Avatar is Initializable, Ownable {
10483
*/
10584
function sendEther(uint256 _amountInWei, address payable _to) external onlyOwner returns(bool) {
10685
vault.sendEther(_amountInWei, _to);
107-
emit SendEther(_amountInWei, _to);
10886
return true;
10987
}
11088

contracts/controller/Vault.sol

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pragma solidity ^0.5.17;
2+
3+
import "@openzeppelin/contracts-ethereum-package/contracts/ownership/Ownable.sol";
4+
5+
6+
//Proxy contracts cannot recive eth via fallback function.
7+
//For now , we will use this vault to overcome that
8+
contract Vault is Ownable {
9+
event ReceiveEther(address indexed _sender, uint256 _value);
10+
event SendEther(address indexed _to, uint256 _value);
11+
12+
/**
13+
* @dev enables this contract to receive ethers
14+
*/
15+
function() external payable {
16+
emit ReceiveEther(msg.sender, msg.value);
17+
}
18+
19+
function sendEther(uint256 _amountInWei, address payable _to) external onlyOwner returns(bool) {
20+
// solhint-disable-next-line avoid-call-value
21+
(bool success, ) = _to.call.value(_amountInWei)("");
22+
require(success, "sendEther failed.");
23+
emit SendEther(_to, _amountInWei);
24+
}
25+
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@daostack/arc-experimental",
3-
"version": "0.1.1-rc.18",
3+
"version": "0.1.1-rc.19",
44
"description": "A platform for building DAOs",
55
"files": [
66
"contracts/",

test/avatar.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const helpers = require('./helpers');
22
const Avatar = artifacts.require("./Avatar.sol");
3+
const Vault = artifacts.require("./Vault.sol");
34
const ERC20Mock = artifacts.require('./test/ERC20Mock.sol');
45
const ActionMock = artifacts.require('./test/ActionMock.sol');
56
const SchemeMock = artifacts.require('./test/SchemeMock.sol');
@@ -73,8 +74,15 @@ contract('Avatar', accounts => {
7374
var avatarBalance = await web3.eth.getBalance(vault)/web3.utils.toWei('1', "ether");
7475
assert.equal(avatarBalance,1);
7576
var tx = await avatar.sendEther(web3.utils.toWei('1', "ether"),otherAvatar.address);
76-
assert.equal(tx.logs.length, 1);
77-
assert.equal(tx.logs[0].event, "SendEther");
77+
var vaultContract = await Vault.at(vault);
78+
await vaultContract.getPastEvents('SendEther', {
79+
filter: {_addr: avatar.address}, // Using an array means OR: e.g. 20 or 23
80+
fromBlock: tx.blockNumber,
81+
toBlock: 'latest'
82+
})
83+
.then(function(events){
84+
assert.equal(events[0].event,"SendEther");
85+
});
7886
avatarBalance =await web3.eth.getBalance(vault)/web3.utils.toWei('1', "ether");
7987
assert.equal(avatarBalance,0);
8088
var otherVault = await otherAvatar.vault();

test/controller.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const helpers = require('./helpers');
22
const Controller = artifacts.require("./Controller.sol");
33
const Reputation = artifacts.require("./Reputation.sol");
44
const Avatar = artifacts.require("./Avatar.sol");
5+
const Vault = artifacts.require("./Vault.sol");
56
const DAOToken = artifacts.require("./DAOToken.sol");
67
const GlobalConstraintMock = artifacts.require('./test/GlobalConstraintMock.sol');
78
const ActionMock = artifacts.require('./test/ActionMock.sol');
@@ -407,16 +408,16 @@ contract('Controller', accounts => {
407408
await web3.eth.sendTransaction({from:accounts[0],to:avatar.address, value: web3.utils.toWei('1', "ether")});
408409
//send some ether from an organization's avatar to the otherAvatar
409410
var tx = await controller.sendEther(web3.utils.toWei('1', "ether"),otherAvatar.address);
410-
await avatar.getPastEvents('SendEther', {
411+
var vault = await Vault.at(await avatar.vault());
412+
await vault.getPastEvents('SendEther', {
411413
filter: {_addr: avatar.address}, // Using an array means OR: e.g. 20 or 23
412414
fromBlock: tx.blockNumber,
413415
toBlock: 'latest'
414416
})
415417
.then(function(events){
416418
assert.equal(events[0].event,"SendEther");
417419
});
418-
var vault = await avatar.vault();
419-
var avatarBalance = await web3.eth.getBalance(vault)/web3.utils.toWei('1', "ether");
420+
var avatarBalance = await web3.eth.getBalance(vault.address)/web3.utils.toWei('1', "ether");
420421
assert.equal(avatarBalance, 0);
421422
var otherVault = await otherAvatar.vault();
422423
var otherAvatarBalance = await web3.eth.getBalance(otherVault)/web3.utils.toWei('1', "ether");
@@ -585,16 +586,16 @@ contract('Controller', accounts => {
585586
var globalConstraintsCount =await controller.globalConstraintsCount();
586587
assert.equal(globalConstraintsCount[0],0);
587588
var tx = await controller.sendEther(web3.utils.toWei('1', "ether"),otherAvatar.address);
588-
await avatar.getPastEvents('SendEther', {
589-
filter: {_addr: avatar.address}, // Using an array means OR: e.g. 20 or 23
590-
fromBlock: tx.blockNumber,
591-
toBlock: 'latest'
592-
})
593-
.then(function(events){
594-
assert.equal(events[0].event,"SendEther");
595-
});
596-
var vault = await avatar.vault();
597-
var avatarBalance = await web3.eth.getBalance(vault)/web3.utils.toWei('1', "ether");
589+
var vault = await Vault.at(await avatar.vault());
590+
await vault.getPastEvents('SendEther', {
591+
filter: {_addr: avatar.address}, // Using an array means OR: e.g. 20 or 23
592+
fromBlock: tx.blockNumber,
593+
toBlock: 'latest'
594+
})
595+
.then(function(events){
596+
assert.equal(events[0].event,"SendEther");
597+
});
598+
var avatarBalance = await web3.eth.getBalance(vault.address)/web3.utils.toWei('1', "ether");
598599
assert.equal(avatarBalance, 0);
599600
var otherVault = await otherAvatar.vault();
600601
var otherAvatarBalance = await web3.eth.getBalance(otherVault)/web3.utils.toWei('1', "ether");

0 commit comments

Comments
 (0)