github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/contracts/chequebook/contract/chequebook.sol (about) 1 import "mortal"; 2 3 /// @title Chequebook for Ethereum micropayments 4 /// @author Daniel A. Nagy <daniel@ethdev.com> 5 contract chequebook is mortal { 6 // Cumulative paid amount in wei to each beneficiary 7 mapping (address => uint256) public sent; 8 9 /// @notice Overdraft event 10 event Overdraft(address deadbeat); 11 12 /// @notice Cash cheque 13 /// 14 /// @param beneficiary beneficiary address 15 /// @param amount cumulative amount in wei 16 /// @param sig_v signature parameter v 17 /// @param sig_r signature parameter r 18 /// @param sig_s signature parameter s 19 /// The digital signature is calculated on the concatenated triplet of contract address, beneficiary address and cumulative amount 20 function cash(address beneficiary, uint256 amount, 21 uint8 sig_v, bytes32 sig_r, bytes32 sig_s) { 22 // Check if the cheque is old. 23 // Only cheques that are more recent than the last cashed one are considered. 24 if(amount <= sent[beneficiary]) return; 25 // Check the digital signature of the cheque. 26 bytes32 hash = sha3(address(this), beneficiary, amount); 27 if(owner != ecrecover(hash, sig_v, sig_r, sig_s)) return; 28 // Attempt sending the difference between the cumulative amount on the cheque 29 // and the cumulative amount on the last cashed cheque to beneficiary. 30 if (amount - sent[beneficiary] >= this.balance) { 31 // update the cumulative amount before sending 32 sent[beneficiary] = amount; 33 if (!beneficiary.send(amount - sent[beneficiary])) { 34 // Upon failure to execute send, revert everything 35 throw; 36 } 37 } else { 38 // Upon failure, punish owner for writing a bounced cheque. 39 // owner.sendToDebtorsPrison(); 40 Overdraft(owner); 41 // Compensate beneficiary. 42 suicide(beneficiary); 43 } 44 } 45 }