Skip to content
Closed
Show file tree
Hide file tree
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
15 changes: 2 additions & 13 deletions contracts/Wallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
* That address is specified by the `WalletFactory` contract.
*/
contract Wallet {
/**
* @dev Event fired upon receiving `amount` ether from `sender`.
*/
event EtherReceived(address indexed sender, uint amount);

/**
* @dev Event fired upon sending `amount` ether to `receiver`.
*/
event EtherSent(address indexed receiver, uint amount);

WalletFactory public factory;

/**
Expand Down Expand Up @@ -50,7 +40,6 @@ contract Wallet {
function collectEther() public returns (uint) {
uint balance = address(this).balance;
master().transfer(balance);
emit EtherSent(master(), balance);
return balance;
}

Expand All @@ -71,7 +60,7 @@ contract Wallet {

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

Expand All @@ -86,6 +75,6 @@ contract Wallet {
* @dev Method to log whenever ether is received by this contract.
*/
receive() external payable {
emit EtherReceived(msg.sender, msg.value);
factory.notifyEtherReceived(msg.sender, msg.value);
}
}
23 changes: 20 additions & 3 deletions contracts/WalletFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ contract WalletFactory is Ownable {
*/
event AddressGenerated(address indexed generatedAddress);

/**
* @dev Event fired upon receiving `amount` ether from `sender`.
*/
event EtherReceived(address indexed sender, address indexed receiver, uint amount);

/**
* @dev Address where all funds coming from `Wallet` contracts will be collected in.
*/
Expand All @@ -33,6 +38,11 @@ contract WalletFactory is Ownable {
* in the process.
*/
address public template;

/**
* Stored whether an address has been generated by this contract or not.
*/
mapping(address => bool) generatedAddresses;

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

address[] memory generatedAddresses = new address[](_numWallets);
address[] memory newAddresses = new address[](_numWallets);
for (uint i = 0; i < _numWallets; i++) {
generatedAddresses[i] = generate();
newAddresses[i] = generate();
}
return generatedAddresses;
return newAddresses;
}

function notifyEtherReceived(address _sender, uint _amount) public {
require(generatedAddresses[msg.sender], "WalletFactory: receiver has not been generated by the factory");

emit EtherReceived(_sender, msg.sender, _amount);
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"coverage": "truffle run coverage"
},
"dependencies": {
"@openzeppelin/contracts": "^4.2.0"
"@openzeppelin/contracts": "^4.3.2"
},
"devDependencies": {
"solidity-coverage": "^0.7.16",
Expand Down
11 changes: 2 additions & 9 deletions test/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ contract('WalletFactory', (accounts) => {
value: amount,
})
assert.isOk(tx.status)
const result = await truffleAssert.createTransactionResult(walletInstance, tx.transactionHash)
const result = await truffleAssert.createTransactionResult(walletFactoryInstance, tx.transactionHash)
truffleAssert.eventEmitted(result, 'EtherReceived', {
sender,
receiver: walletInstance.address,
amount,
})
const balance = await web3.eth.getBalance(walletInstance.address)
Expand All @@ -148,10 +149,6 @@ contract('WalletFactory', (accounts) => {
// collecting the balance
const tx = await walletInstance.collectEther()
assert.isOk(tx.receipt.status)
truffleAssert.eventEmitted(tx, 'EtherSent', {
receiver: master,
amount,
})

// checking balances after
const walletBalanceAfter = await web3.eth.getBalance(walletInstance.address)
Expand Down Expand Up @@ -219,10 +216,6 @@ contract('WalletFactory', (accounts) => {
// collecting funds
const tx = await walletInstance.collectMany([ZERO_ADDRESS, tokenInstance.address])
assert.isOk(tx.receipt.status)
truffleAssert.eventEmitted(tx, 'EtherSent', {
receiver: master,
amount,
})
const result = await truffleAssert.createTransactionResult(tokenInstance, tx.receipt.transactionHash)
truffleAssert.eventEmitted(result, 'Transfer', {
from: walletInstance.address,
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1393,10 +1393,10 @@
find-up "^4.1.0"
fs-extra "^8.1.0"

"@openzeppelin/contracts@^4.2.0":
version "4.2.0"
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.2.0.tgz#260d921d99356e48013d9d760caaa6cea35dc642"
integrity sha512-LD4NnkKpHHSMo5z9MvFsG4g1xxZUDqV3A3Futu3nvyfs4wPwXxqOgMaxOoa2PeyGL2VNeSlbxT54enbQzGcgJQ==
"@openzeppelin/contracts@^4.3.2":
version "4.3.2"
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.3.2.tgz#ff80affd6d352dbe1bbc5b4e1833c41afd6283b6"
integrity sha512-AybF1cesONZStg5kWf6ao9OlqTZuPqddvprc0ky7lrUVOjXeKpmQ2Y9FK+6ygxasb+4aic4O5pneFBfwVsRRRg==

"@openzeppelin/test-helpers@^0.5.12":
version "0.5.12"
Expand Down