Skip to content

Instantly share code, notes, and snippets.

@bijeebuss
Created June 9, 2018 02:20
Show Gist options
  • Save bijeebuss/84d31558107e146381ba623b21b1b5d3 to your computer and use it in GitHub Desktop.
Save bijeebuss/84d31558107e146381ba623b21b1b5d3 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.23;
contract Venmo {
// charge(address _friend, uint256 _amount)
// pay(uint256 _chargeId)
// decline(uint256 _chargeId)
// withdraw()
// deposit()
struct Charge {
address initiator;
address friend;
uint amount;
bool declined;
bool paid;
}
// chargeId => amount
mapping(address => uint) public balances;
mapping(uint => Charge) public charges;
uint public lastChargeId;
function charge(address _friend, uint _amount) public {
require(_friend != 0);
require(_amount != 0);
charges[lastChargeId] = Charge({
friend: _friend,
amount: _amount,
declined: false,
paid: false,
initiator: msg.sender
});
lastChargeId += 1;
}
function decline(uint _chargeId) public {
require(charges[_chargeId].friend == msg.sender);
require(!charges[_chargeId].declined);
require(!charges[_chargeId].paid);
charges[_chargeId].declined = true;
}
function pay(uint _chargeId) public payable {
Charge currentCharge = charges[_chargeId];
require(currentCharge.friend == msg.sender);
require(!currentCharge.declined);
require(!currentCharge.paid);
require(balances[msg.sender] + msg.value >= currentCharge.amount);
uint amountDue = currentCharge.amount;
if (msg.value > amountDue) {
balances[msg.sender] += (msg.value - amountDue);
} else if (msg.value < amountDue) {
amountDue -= msg.value;
balances[msg.sender] -= amountDue;
}
currentCharge.paid = true;
balances[currentCharge.initiator] += currentCharge.amount;
}
function() public payable {
deposit();
}
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function withdraw(uint _amount) public {
require(balances[msg.sender] >= _amount);
balances[msg.sender] -= _amount;
require(msg.sender.call.value(_amount)());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment