Skip to content

Instantly share code, notes, and snippets.

@3esmit

3esmit/PaymentChannel.sol

Last active Dec 10, 2018
Embed
What would you like to do?
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.1+commit.c8a2cb62.js&optimize=false&gist=fce237c187d1f54eacb2c434553b2dd2
pragma solidity >=0.5.0 <0.6.0;
interface Token {
function approve(address _spender, uint256 _value) external returns (bool success);
function allowance(address _owner, address _spender) external view returns (uint256 remaining);
function transferFrom(address _from, address _to, uint256 _value) external returns (bool success);
}
interface PaymentChannel {}
contract PaymentChannelERC20 is PaymentChannel {
constructor(Token _token, address _recipient, uint256 _duration) public {}
}
contract PaymentChannelETH is PaymentChannel {
constructor(address payable _recipient, uint256 _duration) public payable {}
}
contract ChannelFactory {
function newChannel(address payable _recipient, uint256 _duration) external payable returns(PaymentChannel _channel){
_channel = (new PaymentChannelETH).value(msg.value)(_recipient, _duration);
}
function newChannel(Token _token, address _recipient, uint256 _duration) external returns(PaymentChannel _channel) {
_channel = new PaymentChannelERC20(_token, _recipient, _duration);
_token.transferFrom(msg.sender, address(_channel), _token.allowance(msg.sender, address(this)));
}
}
contract Account {
function openChannel(ChannelFactory _factory, Token _token, address payable _recipient, uint256 _duration, uint256 _amount) external {
_token.approve(address(_factory), _amount);
_factory.newChannel(_token, _recipient, _duration);
}
function openChannel(ChannelFactory _factory, address payable _recipient, uint256 _duration, uint256 _amount) external {
_factory.newChannel.value(_amount)(_recipient, _duration);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.