github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/contracts/chequebook/contract/chequebook.sol (about)

     1  pragma solidity ^0.4.18;
     2  
     3  import "./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      // Allow sending ether to the chequebook.
    15      function() public payable { }
    16  
    17      /// @notice Cash cheque
    18      ///
    19      /// @param beneficiary beneficiary address
    20      /// @param amount cumulative amount in wei
    21      /// @param sig_v signature parameter v
    22      /// @param sig_r signature parameter r
    23      /// @param sig_s signature parameter s
    24      /// The digital signature is calculated on the concatenated triplet of contract address, beneficiary address and cumulative amount
    25      function cash(address beneficiary, uint256 amount, uint8 sig_v, bytes32 sig_r, bytes32 sig_s) public {
    26          // Check if the cheque is old.
    27          // Only cheques that are more recent than the last cashed one are considered.
    28          require(amount > sent[beneficiary]);
    29          // Check the digital signature of the cheque.
    30          bytes32 hash = keccak256(address(this), beneficiary, amount);
    31          require(owner == ecrecover(hash, sig_v, sig_r, sig_s));
    32          // Attempt sending the difference between the cumulative amount on the cheque
    33          // and the cumulative amount on the last cashed cheque to beneficiary.
    34          uint256 diff = amount - sent[beneficiary];
    35          if (diff <= this.balance) {
    36              // update the cumulative amount before sending
    37              sent[beneficiary] = amount;
    38              beneficiary.transfer(diff);
    39          } else {
    40              // Upon failure, punish owner for writing a bounced cheque.
    41              // owner.sendToDebtorsPrison();
    42              Overdraft(owner);
    43              // Compensate beneficiary.
    44              selfdestruct(beneficiary);
    45          }
    46      }
    47  }