Skip to content

Instantly share code, notes, and snippets.

@shanefontaine
Last active October 14, 2018 02:23
Show Gist options
  • Save shanefontaine/c9d180806a04c4ef890aba93b38d3a32 to your computer and use it in GitHub Desktop.
Save shanefontaine/c9d180806a04c4ef890aba93b38d3a32 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.24;
contract Escrow {
event Deposited(address indexed payee, uint256 weiAmount);
event Withdrawn(address indexed payee, uint256 weiAmount);
mapping(address => uint256) private _deposits;
function depositsOf(address payee) public view returns (uint256) {
return _deposits[payee];
}
/**
* @dev Stores the sent amount as credit to be withdrawn.
* @param payee The destination address of the funds.
*/
function deposit(address payee) public payable {
uint256 amount = msg.value;
_deposits[payee] = _deposits[payee] + (amount);
emit Deposited(payee, amount);
}
/**
* @dev Withdraw accumulated balance for a payee.
* @param payee The address whose funds will be withdrawn and transferred to.
*/
function withdraw(address payee) public {
uint256 payment = _deposits[payee];
assert(address(this).balance >= payment);
_deposits[payee] = 0;
payee.transfer(payment);
emit Withdrawn(payee, payment);
}
}
contract PullPayment {
Escrow private _escrow;
constructor() public {
_escrow = new Escrow();
}
/**
* @dev Withdraw accumulated balance.
* @param payee Whose balance will be withdrawn.
*/
function withdrawPayments(address payee) public {
_escrow.withdraw(payee);
}
/**
* @dev Returns the credit owed to an address.
* @param dest The creditor's address.
*/
function payments(address dest) public view returns (uint256) {
return _escrow.depositsOf(dest);
}
/**
* @dev Called by the payer to store the sent amount as credit to be pulled.
* @param dest The destination address of the funds.
* @param amount The amount to transfer.
*/
function _asyncTransfer(address dest, uint256 amount) {
_escrow.deposit.value(amount)(dest);
}
}
contract test is PullPayment {
function() public payable {}
function theTest() public payable {
_asyncTransfer(msg.sender, 100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment