Skip to content

Instantly share code, notes, and snippets.

@heikoheiko
Created January 17, 2019 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heikoheiko/214dbbd954e0f97e0e13b2fefdc7c753 to your computer and use it in GitHub Desktop.
Save heikoheiko/214dbbd954e0f97e0e13b2fefdc7c753 to your computer and use it in GitHub Desktop.
/*
Minimal IOU based off-chain payment system.
Assumption is, that there is a contract which manages user deposits.
deposit_contract trusts RaidenIOU contract and allows it to claim funds.
users can withdraw their (remaining) deposit form deposit_contract after a timeout.
*/
contract RaidenIOU {
address deposit_contract;
mapping bytes32 => uint public settled_sessions;
RaidenIOU(
address _deposit_contract)
public)
{
deposit_contract = _deposit_contract;
}
function available_funds(address _address) returns uint {
// call deposit contract and return deposit of _address
}
function active_deposit(address _address) returns uint {
// call deposit contract and returns True if the user didn't initiate
// withdrawl of deposit
}
/// in order to claim, recveiver needs to provide a signed message by sender
/// which contains amount, receiver, sender, expiration, sig
function claim(bytes signed_IOU) returns uint {
amount, receiver, sender, expiration = _decode(signed_IOU);
_key = concatenate(receiver, sender, expiration);
// must not be claimed before
require(_key not in settled_sessions);
// claim as much as possible
transferable = min(amount, available_funds(sender));
// register to avoid double claiming
settled_sessions[_key] = expiration;
// event SessionSettled(_key, expiration);
deposit_contract.please_transfer(transferable, sender, receiver);
return transferable;
}
/// func to cleanup storage once IOU expired
/// free gas for cleaner, alternatively allow only receiver to claim the free slot.
function cleanup(_key) {
require(settled_sessions[_key] <= block.timestamp);
settled_sessions[_key] = expiration;
}
function claim_multiple() {} // optional optimization
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment