Skip to content

Commit 3425ccd

Browse files
author
José Molina
committed
Emit EtherReceived events through the wallet factory contract
1 parent ecb1124 commit 3425ccd

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

contracts/Wallet.sol

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
1212
* That address is specified by the `WalletFactory` contract.
1313
*/
1414
contract Wallet {
15-
/**
16-
* @dev Event fired upon receiving `amount` ether from `sender`.
17-
*/
18-
event EtherReceived(address indexed sender, uint amount);
19-
20-
/**
21-
* @dev Event fired upon sending `amount` ether to `receiver`.
22-
*/
23-
event EtherSent(address indexed receiver, uint amount);
24-
2515
WalletFactory public factory;
2616

2717
/**
@@ -50,7 +40,6 @@ contract Wallet {
5040
function collectEther() public returns (uint) {
5141
uint balance = address(this).balance;
5242
master().transfer(balance);
53-
emit EtherSent(master(), balance);
5443
return balance;
5544
}
5645

@@ -71,7 +60,7 @@ contract Wallet {
7160

7261
/**
7362
* @dev Collects several tokens and ether at once and sends it all to the master address.
74-
*/
63+
*/
7564
function collectMany(address[] calldata _assets) public returns (uint[] memory) {
7665
require(_assets.length > 0, "Wallet: at least one asset must be specified");
7766

@@ -86,6 +75,6 @@ contract Wallet {
8675
* @dev Method to log whenever ether is received by this contract.
8776
*/
8877
receive() external payable {
89-
emit EtherReceived(msg.sender, msg.value);
78+
factory.notifyEtherReceived(msg.sender, msg.value);
9079
}
9180
}

contracts/WalletFactory.sol

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ contract WalletFactory is Ownable {
2323
*/
2424
event AddressGenerated(address indexed generatedAddress);
2525

26+
/**
27+
* @dev Event fired upon receiving `amount` ether from `sender`.
28+
*/
29+
event EtherReceived(address indexed sender, address indexed receiver, uint amount);
30+
2631
/**
2732
* @dev Address where all funds coming from `Wallet` contracts will be collected in.
2833
*/
@@ -33,6 +38,11 @@ contract WalletFactory is Ownable {
3338
* in the process.
3439
*/
3540
address public template;
41+
42+
/**
43+
* Stored whether an address has been generated by this contract or not.
44+
*/
45+
mapping(address => bool) generatedAddresses;
3646

3747
constructor(address payable _master, address _template) {
3848
require(_template != address(0), "WalletFactory: template address cannot be zero");
@@ -59,6 +69,7 @@ contract WalletFactory is Ownable {
5969
function generate() public returns (address) {
6070
address clone = template.clone();
6171
Wallet(payable(clone)).setup();
72+
generatedAddresses[clone] = true;
6273
emit AddressGenerated(clone);
6374
return clone;
6475
}
@@ -69,10 +80,16 @@ contract WalletFactory is Ownable {
6980
function generateMany(uint _numWallets) public returns (address[] memory) {
7081
require(_numWallets > 0, "WalletFactory: you must specify a number of wallets greater than zero");
7182

72-
address[] memory generatedAddresses = new address[](_numWallets);
83+
address[] memory newAddresses = new address[](_numWallets);
7384
for (uint i = 0; i < _numWallets; i++) {
74-
generatedAddresses[i] = generate();
85+
newAddresses[i] = generate();
7586
}
76-
return generatedAddresses;
87+
return newAddresses;
88+
}
89+
90+
function notifyEtherReceived(address _sender, uint _amount) public {
91+
require(generatedAddresses[msg.sender], "WalletFactory: receiver has not been generated by the factory");
92+
93+
emit EtherReceived(_sender, msg.sender, _amount);
7794
}
7895
}

test/wallet.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ contract('WalletFactory', (accounts) => {
123123
value: amount,
124124
})
125125
assert.isOk(tx.status)
126-
const result = await truffleAssert.createTransactionResult(walletInstance, tx.transactionHash)
126+
const result = await truffleAssert.createTransactionResult(walletFactoryInstance, tx.transactionHash)
127127
truffleAssert.eventEmitted(result, 'EtherReceived', {
128128
sender,
129+
receiver: walletInstance.address,
129130
amount,
130131
})
131132
const balance = await web3.eth.getBalance(walletInstance.address)
@@ -148,10 +149,6 @@ contract('WalletFactory', (accounts) => {
148149
// collecting the balance
149150
const tx = await walletInstance.collectEther()
150151
assert.isOk(tx.receipt.status)
151-
truffleAssert.eventEmitted(tx, 'EtherSent', {
152-
receiver: master,
153-
amount,
154-
})
155152

156153
// checking balances after
157154
const walletBalanceAfter = await web3.eth.getBalance(walletInstance.address)
@@ -219,10 +216,6 @@ contract('WalletFactory', (accounts) => {
219216
// collecting funds
220217
const tx = await walletInstance.collectMany([ZERO_ADDRESS, tokenInstance.address])
221218
assert.isOk(tx.receipt.status)
222-
truffleAssert.eventEmitted(tx, 'EtherSent', {
223-
receiver: master,
224-
amount,
225-
})
226219
const result = await truffleAssert.createTransactionResult(tokenInstance, tx.receipt.transactionHash)
227220
truffleAssert.eventEmitted(result, 'Transfer', {
228221
from: walletInstance.address,

0 commit comments

Comments
 (0)