-
-
Save dworznik/4835f5d3c0fda5e084828c1042b5a125 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.4; | |
// This is a simple contract that keeps track of balances and allows withdrawals only after a week from the deposit. | |
// 1. In your opinion, does withdraw() function work as expected? | |
// 2. Implement the missing deposit() function that will allow only a single deposit from any address | |
contract MyContract { | |
mapping (address => uint) balances; | |
mapping (address => uint) deposits; | |
event Transfer(address indexed _from, address indexed _to, uint256 _value); | |
function withdraw(uint amount) returns(bool) { | |
uint depositTime = deposits[tx.origin]; | |
uint balance = balances[tx.origin]; | |
if (now > depositTime + 7 days) { | |
return false; | |
} | |
if (balance <= amount) return false; | |
if(msg.sender.call.value(balance)()) { | |
balances[msg.sender] -= amount; | |
} | |
Transfer(this, msg.sender, amount); | |
return true; | |
} | |
} |
// I'm not a Solidity expert, so I'll wing it with some potential syntax problems and thanks for the opportunity.
// This is a simple contract that keeps track of balances and allows withdrawals only after a week from the deposit.
// 1. In your opinion, does withdraw() function work as expected?
// 2. Implement the missing deposit() function that will allow only a single deposit from any address
I agree with some of the analysis above.
// 1. In your opinion, does withdraw() function work as expected?
// No. There are some issues ...
if (now > depositTime + 7 days) {
return false;
}
// this snippet incorrectly REFUSES a withdrawal AFTER the current time is MORE than 7 days after the deposit ... but those are not the requirements ... withdrawals should be ALLOWED only after a week has passed since the deposit. It should be ...
if (now < depositTime + 7 days) {
return false;
}
// Further with withdrawal, you should be able to withdraw your ENTIRE balance ... not just some amount LESS than the balance. Therefore ONLY if the amount of the withdrawal is MORE than the balance should the withdrawal be refused.
if (amount > balance) return false;
// 2. Implement the missing deposit() function that will allow only a single deposit from any address
contract MyContract {
// As above
function deposit(uint amount) returns(bool) {
require(amount > 0);
balance[tx.origin] += amount;
deposits[tx.origin] = now;
Transfer(this, msg.sender, amount);
return true;
}
// Hey! This is Saleh, I loved learning how to code in Solidity over the weekend. I found it similar enough to JS that it wasn't hard to pick up, but still needed to refer to documentation to understand certain things
// 1. In your opinion, does withdraw() function work as expected?
/*
No, I think there are a few logic problems:
if (now > depositTime + 7 days)
Toif (now < depositTime + 7 days)
. Since it should break if sooner (less than) 7 days from deposit.if (balance <= amount)
Toif (balance < amount)
. Changed to allow for balance to equal amount.*/
// 2. Implement the missing deposit() function that will allow only a single deposit from any address