Skip to content

Instantly share code, notes, and snippets.

@eordano
Last active August 20, 2017 02:59
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 eordano/76551e96d91ae126aaea1ce78b868e5e to your computer and use it in GitHub Desktop.
Save eordano/76551e96d91ae126aaea1ce78b868e5e to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.11;
contract Token {
function transfer(address _to, uint256 _value);
function balanceOf(address _owner) constant returns (uint256 balance);
}
contract BuyToken {
// Ether holder, the Party that has Ether and will receive Tokens
address buyer;
// Token holder, the Party that has tokens and will receive Ether
address seller;
bool succeeded; // Swap executed
// Prices for the contract
uint256 tokens;
uint256 eth;
// Token to be swapped
Token token;
function BuyTokenEscrow (
address _buyer,
address _seller,
uint256 _tokens,
uint256 _ether,
address _token
) {
buyer = _buyer;
seller = _seller;
tokens = _tokens;
eth = _ether;
token = _token;
}
modifier onlyBuyer () { if (msg.sender != buyer) revert(); _; }
modifier onlySeller () { if (msg.sender != seller) revert(); _; }
modifier beforeSwap () { if (succeeded) revert(); _; }
modifier afterSwap () { if (!succeeded) revert(); _; }
function () payable {}
function refundEth () onlyBuyer beforeSwap {
buyer.transfer(this.balance);
}
function refundTokens () onlySeller beforeSwap {
token.transfer(seller, token.balanceOf(this));
}
function makeSwap () {
if (this.balance != eth) revert();
if (token.balanceOf(this) != tokens) revert();
succeeded = true;
}
function withdraw() onlySeller afterSwap {
msg.sender.transfer(this.balance);
}
function withdrawTokens() onlyBuyer afterSwap {
token.transfer(msg.sender, token.balanceOf(this));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment