Skip to content

Instantly share code, notes, and snippets.

@PeterBorah
Created June 22, 2016 05:16
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 PeterBorah/a325e76370f2a22f4a57db8c68c30c75 to your computer and use it in GitHub Desktop.
Save PeterBorah/a325e76370f2a22f4a57db8c68c30c75 to your computer and use it in GitHub Desktop.
contract TokenWithInvariants {
mapping(address => uint) public balanceOf;
uint public totalSupply;
modifier checkInvariants {
_
if (this.balance < totalSupply) throw;
}
function deposit(uint amount) checkInvariants {
// intentionally vulnerable
balanceOf[msg.sender] += amount;
totalSupply += amount;
}
function transfer(address to, uint value) checkInvariants {
if (balanceOf[msg.sender] >= value) {
balanceOf[to] += value;
balanceOf[msg.sender] -= value;
}
}
function withdraw() checkInvariants {
// intentionally vulnerable
uint balance = balanceOf[msg.sender];
if (msg.sender.call.value(balance)()) {
totalSupply -= balance;
balanceOf[msg.sender] = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment