Last active
October 29, 2017 22:05
-
-
Save m-bo-one/4cafd9934a384f95fe159423b9c67656 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.18; | |
contract Debt { | |
address owner; | |
int public balance; | |
mapping(address => int) public debts; | |
event Borrow(address user, int amount); | |
event Refund(address user, int amount); | |
function Debt(int _balance) public { | |
owner = msg.sender; | |
balance = _balance; | |
} | |
modifier onlyOwner() { | |
require(owner == msg.sender); | |
_; | |
} | |
// check pottential balance change | |
modifier canBorrow(int amount) { | |
require(balance - amount >= 0 && owner != msg.sender); | |
_; | |
} | |
// check for oversend amount, we do not send short change | |
modifier canRefund(address borrower, int amount) { | |
require(debts[borrower] - amount >= 0 && borrower != msg.sender); | |
_; | |
} | |
// send amount only positive | |
modifier isValidAmount(int amount) { | |
require(amount > 0); | |
_; | |
} | |
// give debt | |
function borrow(int amount) public | |
isValidAmount(amount) | |
canBorrow(amount) | |
{ | |
debts[msg.sender] += amount; | |
balance -= amount; | |
Borrow(msg.sender, amount); | |
} | |
// return money back | |
function refund(address borrower, int amount) public | |
onlyOwner() | |
isValidAmount(amount) | |
canRefund(borrower, amount) | |
{ | |
debts[borrower] -= amount; | |
balance += amount; | |
Refund(msg.sender, amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment