Skip to content

Instantly share code, notes, and snippets.

@dworznik
Created May 15, 2017 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dworznik/4835f5d3c0fda5e084828c1042b5a125 to your computer and use it in GitHub Desktop.
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;
}
}
@knowself
Copy link

// 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;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment