This commit is contained in:
Daniel A. Nagy 2017-09-25 08:20:27 +00:00 committed by GitHub
commit 5ba96eeb13

View file

@ -1,7 +1,9 @@
import "mortal"; pragma solidity ^0.4.3;
import "mortal.sol" as mortal;
/// @title Chequebook for Ethereum micropayments /// @title Chequebook for Ethereum micropayments
/// @author Daniel A. Nagy <daniel@ethdev.com> /// @author Daniel A. Nagy <daniel@ethereum.org>
contract chequebook is mortal { contract chequebook is mortal {
// Cumulative paid amount in wei to each beneficiary // Cumulative paid amount in wei to each beneficiary
mapping (address => uint256) public sent; mapping (address => uint256) public sent;
@ -9,6 +11,14 @@ contract chequebook is mortal {
/// @notice Overdraft event /// @notice Overdraft event
event Overdraft(address deadbeat); event Overdraft(address deadbeat);
/// @notice Deposit event
event Deposit(uint256 amount);
/// @notice Top up chequebook
function() payable {
Deposit(msg.value);
}
/// @notice Cash cheque /// @notice Cash cheque
/// ///
/// @param beneficiary beneficiary address /// @param beneficiary beneficiary address
@ -27,17 +37,20 @@ contract chequebook is mortal {
if(owner != ecrecover(hash, sig_v, sig_r, sig_s)) return; if(owner != ecrecover(hash, sig_v, sig_r, sig_s)) return;
// Attempt sending the difference between the cumulative amount on the cheque // Attempt sending the difference between the cumulative amount on the cheque
// and the cumulative amount on the last cashed cheque to beneficiary. // and the cumulative amount on the last cashed cheque to beneficiary.
if (amount - sent[beneficiary] >= this.balance) { diff = amount - sent[beneficiary];
if (beneficiary.send(amount - sent[beneficiary])) { if (diff <= this.balance) {
// Upon success, update the cumulative amount. // update the cumulative amount before sending
sent[beneficiary] = amount; sent[beneficiary] = amount;
if (!beneficiary.send(diff)) {
// Upon failure to execute send, revert everything
throw;
} }
} else { } else {
// Upon failure, punish owner for writing a bounced cheque. // Upon failure, punish owner for writing a bounced cheque.
// owner.sendToDebtorsPrison(); // owner.sendToDebtorsPrison();
Overdraft(owner); Overdraft(owner);
// Compensate beneficiary. // Compensate beneficiary.
suicide(beneficiary); selfdestruct(beneficiary);
} }
} }
} }