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