github.com/waltonchain/waltonchain_gwtc_src@v1.1.4-0.20201225072101-8a298c95a819/contracts/chequebook/contract/chequebook.sol (about)

     1  import "mortal";
     2  
     3  /// @title Chequebook for Wtc 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          uint256 diff = amount - sent[beneficiary];
    31          if (diff <= this.balance) {
    32  	    // update the cumulative amount before sending
    33              sent[beneficiary] = amount;
    34              if (!beneficiary.send(diff)) {
    35                  // Upon failure to execute send, revert everything
    36                  throw;
    37              }
    38          } else {
    39              // Upon failure, punish owner for writing a bounced cheque.
    40              // owner.sendToDebtorsPrison();
    41              Overdraft(owner);
    42              // Compensate beneficiary.
    43              suicide(beneficiary);
    44          }
    45      }
    46  }