-
-
Save dworznik/4835f5d3c0fda5e084828c1042b5a125 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// 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 ...
// 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 ...
// 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.
// 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) {
}