diff options
author | Viktor TrĂ³n <viktor.tron@gmail.com> | 2016-08-28 19:34:59 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2016-08-29 19:39:25 +0800 |
commit | 6c8b023298694175d05bf2a14c85f44d73625695 (patch) | |
tree | 8a72be292168dbdabaaf31a97d41f6d6d68ee597 /contracts/chequebook/contract/chequebook.sol | |
parent | 5fc032a9d16ac6ea1dc020f06e44c24c94a361a7 (diff) | |
download | go-tangerine-6c8b023298694175d05bf2a14c85f44d73625695.tar go-tangerine-6c8b023298694175d05bf2a14c85f44d73625695.tar.gz go-tangerine-6c8b023298694175d05bf2a14c85f44d73625695.tar.bz2 go-tangerine-6c8b023298694175d05bf2a14c85f44d73625695.tar.lz go-tangerine-6c8b023298694175d05bf2a14c85f44d73625695.tar.xz go-tangerine-6c8b023298694175d05bf2a14c85f44d73625695.tar.zst go-tangerine-6c8b023298694175d05bf2a14c85f44d73625695.zip |
contracts/chequebook: add chequebook contract wrapper
The wrapper code is adapted from the swarm/services/chequebook package
with the following mostly cosmetic changes:
* The code now uses the new Go API interfaces to query balances. Some
minor functional changes were required to make this work.
* The package no longer depends on swarm/services/swap/swap. References
to swap.Promise are replaced by interface{}, the base type of Promise.
This is temporary.
* The contract wrapper has been regenerated with latest abigen
and solc v0.3.6.
* There is a new generator that creates the 'deployed code' variable.
* Documentation comments now follow the recommended godoc style.
* [CHEQUEBOOK] log prefixes are gone.
* LGPL license headers have been added to all files.
Diffstat (limited to 'contracts/chequebook/contract/chequebook.sol')
-rw-r--r-- | contracts/chequebook/contract/chequebook.sol | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/contracts/chequebook/contract/chequebook.sol b/contracts/chequebook/contract/chequebook.sol new file mode 100644 index 000000000..cb19d0b27 --- /dev/null +++ b/contracts/chequebook/contract/chequebook.sol @@ -0,0 +1,43 @@ +import "mortal"; + +/// @title Chequebook for Ethereum micropayments +/// @author Daniel A. Nagy <daniel@ethdev.com> +contract chequebook is mortal { + // Cumulative paid amount in wei to each beneficiary + mapping (address => uint256) public sent; + + /// @notice Overdraft event + event Overdraft(address deadbeat); + + /// @notice Cash cheque + /// + /// @param beneficiary beneficiary address + /// @param amount cumulative amount in wei + /// @param sig_v signature parameter v + /// @param sig_r signature parameter r + /// @param sig_s signature parameter s + /// The digital signature is calculated on the concatenated triplet of contract address, beneficiary address and cumulative amount + function cash(address beneficiary, uint256 amount, + uint8 sig_v, bytes32 sig_r, bytes32 sig_s) { + // Check if the cheque is old. + // Only cheques that are more recent than the last cashed one are considered. + if(amount <= sent[beneficiary]) return; + // Check the digital signature of the cheque. + bytes32 hash = sha3(address(this), beneficiary, amount); + if(owner != ecrecover(hash, sig_v, sig_r, sig_s)) return; + // Attempt sending the difference between the cumulative amount on the cheque + // and the cumulative amount on the last cashed cheque to beneficiary. + if (amount - sent[beneficiary] >= this.balance) { + if (beneficiary.send(amount - sent[beneficiary])) { + // Upon success, update the cumulative amount. + sent[beneficiary] = amount; + } + } else { + // Upon failure, punish owner for writing a bounced cheque. + // owner.sendToDebtorsPrison(); + Overdraft(owner); + // Compensate beneficiary. + suicide(beneficiary); + } + } +} |