Skip to content

Instantly share code, notes, and snippets.

@crypto-perry
Created August 7, 2019 21:22
Show Gist options
  • Save crypto-perry/a04db72428f35ea59071226caf4eb0fe to your computer and use it in GitHub Desktop.
Save crypto-perry/a04db72428f35ea59071226caf4eb0fe to your computer and use it in GitHub Desktop.
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=
pragma solidity >=0.5.0;
interface IERC20 {
function transfer(address to, uint256 value) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
function totalSupply() external view returns (uint256);
function balanceOf(address who) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract ERC20OptionTrade {
enum TradeState {None, Open, Matched, Closed}
struct Trade {
address payable buyer;
address payable seller;
uint256 payment;
uint256 deposit;
uint256 amount;
uint256 expiration;
TradeState state;
}
event OpenTrade(uint256 indexed tradeId, address indexed buyer, address indexed seller, uint256 payment, uint256 deposit, uint256 amount, uint256 expiration);
event MatchTrade(uint256 indexed tradeId, address indexed buyer, address indexed seller);
event CloseTrade(uint256 indexed tradeId, address indexed buyer, address indexed seller, bool expired);
address owner;
IERC20 tokenProduct;
uint256 public tradeCounter = 0;
mapping (uint256 => Trade) public trades;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
constructor() public {
owner = msg.sender;
}
function() external payable {}
function setTokenProduct(address token) public onlyOwner {
tokenProduct = IERC20(token);
}
function wantToBuy(uint256 amountExpected, uint256 payment, uint256 depositRequired, uint256 expireAfter, address payable seller) public payable {
uint256 fee = computeFee(payment);
require(msg.value == payment + fee);
tradeCounter++;
trades[tradeCounter] = Trade(msg.sender, seller, payment, depositRequired, amountExpected, now + expireAfter * 1 hours, TradeState.Open);
emit OpenTrade(tradeCounter, msg.sender, seller, payment, depositRequired, amountExpected, now + expireAfter * 1 hours);
}
function wantToSell(uint256 amountExpected, uint256 paymentExpected, uint256 deposit, uint256 expireAfter, address payable buyer) public payable {
uint256 fee = computeFee(paymentExpected);
require(msg.value == deposit + fee);
tradeCounter++;
trades[tradeCounter] = Trade(buyer, msg.sender, paymentExpected, deposit, amountExpected, now + expireAfter * 1 hours, TradeState.Open);
emit OpenTrade(tradeCounter, buyer, msg.sender, paymentExpected, deposit, amountExpected, now + expireAfter * 1 hours);
}
function matchTrade(uint256 tradeId) public payable {
require(trades[tradeId].state == TradeState.Open);
// if (trades[tradeId].buyer == address(0x0)) {
// trades[tradeId].buyer = msg.sender;
// } else if (trades[tradeId].seller == address(0x0)) {
// trades[tradeId].seller = msg.sender;
// }
if (msg.sender == trades[tradeId].seller) {
require(msg.value == trades[tradeId].deposit + computeFee(trades[tradeId].payment));
} else if (msg.sender == trades[tradeId].buyer){
require(msg.value == trades[tradeId].payment + computeFee(trades[tradeId].payment));
} else {
revert("You cannot interact with this trade!");
}
trades[tradeId].state = TradeState.Matched;
emit MatchTrade(tradeId, trades[tradeId].buyer, trades[tradeId].seller);
}
function completeTrade(uint256 tradeId) public payable {
require(address(tokenProduct) != address(0x0));
require(trades[tradeId].state == TradeState.Matched);
trades[tradeId].state = TradeState.Closed;
require(tokenProduct.transferFrom(trades[tradeId].seller, trades[tradeId].buyer, trades[tradeId].amount));
trades[tradeId].seller.transfer(trades[tradeId].payment + trades[tradeId].deposit);
emit CloseTrade(tradeId, trades[tradeId].buyer, trades[tradeId].seller, false);
}
function claimDeposit(uint256 tradeId) public payable {
require(trades[tradeId].state == TradeState.Matched);
require(trades[tradeId].buyer == msg.sender && trades[tradeId].expiration < now);
trades[tradeId].state = TradeState.Closed;
trades[tradeId].buyer.transfer(trades[tradeId].payment + trades[tradeId].deposit);
emit CloseTrade(tradeId, trades[tradeId].buyer, trades[tradeId].seller, true);
}
function computeFee(uint256 value) private pure returns (uint256) {
uint256 fee = value * 5;
require(fee / 5 == value); // Check overflow
return fee / 1000; // This is the fee we take on each side (0.5% * price)
}
}
pragma solidity >=0.5.0;
contract ERC20Basic {
string public constant name = "ERC20BasicTest";
string public constant symbol = "TEST";
uint8 public constant decimals = 18;
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
event Transfer(address indexed from, address indexed to, uint tokens);
mapping(address => uint256) balances;
mapping(address => mapping (address => uint256)) allowed;
uint256 totalSupply_;
using SafeMath for uint256;
constructor(uint256 total) public {
totalSupply_ = total;
balances[msg.sender] = totalSupply_;
}
function totalSupply() public view returns (uint256) {
return totalSupply_;
}
function balanceOf(address tokenOwner) public view returns (uint) {
return balances[tokenOwner];
}
function transfer(address receiver, uint numTokens) public returns (bool) {
require(numTokens <= balances[msg.sender]);
balances[msg.sender] = balances[msg.sender].sub(numTokens);
balances[receiver] = balances[receiver].add(numTokens);
emit Transfer(msg.sender, receiver, numTokens);
return true;
}
function approve(address delegate, uint numTokens) public returns (bool) {
allowed[msg.sender][delegate] = numTokens;
emit Approval(msg.sender, delegate, numTokens);
return true;
}
function allowance(address owner, address delegate) public view returns (uint) {
return allowed[owner][delegate];
}
function transferFrom(address owner, address buyer, uint numTokens) public returns (bool) {
require(numTokens <= balances[owner]);
require(numTokens <= allowed[owner][msg.sender]);
balances[owner] = balances[owner].sub(numTokens);
allowed[owner][msg.sender] = allowed[owner][msg.sender].sub(numTokens);
balances[buyer] = balances[buyer].add(numTokens);
emit Transfer(owner, buyer, numTokens);
return true;
}
}
library SafeMath {
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment