Created
August 9, 2019 20:41
-
-
Save crypto-perry/b75e3dc32feb4e35809a4c6702130dd4 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=true&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
library SafeMath { | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
require(c >= a); | |
return c; | |
} | |
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
if (a == 0) { | |
return 0; | |
} | |
uint256 c = a * b; | |
require(c / a == b); | |
return c; | |
} | |
} | |
contract ERC20OptionTrade { | |
using SafeMath for uint256; | |
enum TradeState {None, Sell, Buy, Matched, Closed} | |
struct Trade { | |
address payable buyer; | |
address payable seller; | |
string symbol; | |
uint256 payment; | |
uint256 deposit; | |
uint256 amountOfTokens; | |
uint256 expiration; | |
TradeState state; | |
} | |
event OpenTrade(uint256 tradeId, address buyer, address seller, string symbol, uint256 payment, uint256 deposit, uint256 amount, uint256 expiration, TradeState state); | |
event MatchTrade(uint256 tradeId, address buyer, address seller); | |
event CloseTrade(uint256 tradeId, address buyer, address seller, bool expired); | |
address owner; | |
uint256 feesGathered = 0; | |
mapping (uint256 => Trade) public trades; | |
mapping (bytes32 => IERC20) private tokens; | |
modifier onlyOwner { | |
require(msg.sender == owner); | |
_; | |
} | |
constructor() public { | |
owner = msg.sender; | |
} | |
function() external payable {} | |
function convert(string memory key) private pure returns (bytes32 ret) { | |
require(bytes(key).length <= 32); | |
assembly { | |
ret := mload(add(key, 32)) | |
} | |
} | |
function getExpirationTimeIn(uint256 amountOfHours) public view returns (uint256) { | |
return now.add(amountOfHours.mul(1 hours)); | |
} | |
function setTokenProduct(string memory symbol, address token) public onlyOwner { | |
tokens[convert(symbol)] = IERC20(token); | |
} | |
function getTokenInfo(string memory symbol) public view returns (IERC20) { | |
return tokens[convert(symbol)]; | |
} | |
function trade(bool wantToBuy, string memory symbol, uint256 amountOfTokens, uint256 priceOfOneToken, uint256 depositPercentage, uint256 expiration, address payable other) public payable { | |
require(tokens[convert(symbol)] != IERC20(0x0)); | |
uint256 payment = amountOfTokens.mul(priceOfOneToken); | |
require(depositPercentage <= 100); | |
uint256 depositRequired = depositPercentage.mul(payment) / 100; | |
uint256 fee = computeFee(payment); | |
if (wantToBuy) { | |
require(msg.value >= payment.add(fee)); | |
msg.sender.transfer(msg.value - payment.add(fee)); | |
uint256 tradeId = uint256(keccak256(abi.encodePacked(msg.sender, other, symbol, amountOfTokens, priceOfOneToken, depositPercentage, expiration))); | |
if (trades[tradeId].state == TradeState.None) { | |
emit OpenTrade(tradeId, msg.sender, other, symbol, payment, depositRequired, amountOfTokens, expiration, TradeState.Buy); | |
trades[tradeId] = Trade(msg.sender, other, symbol, payment, depositRequired, amountOfTokens, expiration, TradeState.Buy); | |
} else if (trades[tradeId].state == TradeState.Sell) { | |
emit MatchTrade(tradeId, msg.sender, other); | |
trades[tradeId].state = TradeState.Matched; | |
} else { | |
revert(); | |
} | |
} else { | |
require(msg.value >= depositRequired.add(fee)); | |
msg.sender.transfer(msg.value - depositRequired.add(fee)); | |
uint256 tradeId = uint256(keccak256(abi.encodePacked(other, msg.sender, symbol, amountOfTokens, priceOfOneToken, depositPercentage, expiration))); | |
if (trades[tradeId].state == TradeState.None) { | |
emit OpenTrade(tradeId, other, msg.sender, symbol, payment, depositRequired, amountOfTokens, expiration, TradeState.Sell); | |
trades[tradeId] = Trade(other, msg.sender, symbol, payment, depositRequired, amountOfTokens, expiration, TradeState.Sell); | |
} else if (trades[tradeId].state == TradeState.Buy) { | |
emit MatchTrade(tradeId, other, msg.sender); | |
trades[tradeId].state = TradeState.Matched; | |
} else { | |
revert(); | |
} | |
} | |
} | |
function cancelOpenTrade(uint256 tradeId) public { | |
if (trades[tradeId].state == TradeState.Sell) { | |
require(trades[tradeId].seller == msg.sender); | |
msg.sender.transfer(trades[tradeId].deposit.add(computeFee(trades[tradeId].payment))); | |
} else if (trades[tradeId].state == TradeState.Buy) { | |
require(trades[tradeId].buyer == msg.sender); | |
msg.sender.transfer(trades[tradeId].payment.add(computeFee(trades[tradeId].payment))); | |
} else { | |
revert(); | |
} | |
trades[tradeId].state = TradeState.Closed; | |
emit CloseTrade(tradeId, trades[tradeId].buyer, trades[tradeId].seller, false); | |
} | |
function completeTrade(uint256 tradeId) public { | |
require(tokens[convert(trades[tradeId].symbol)] != IERC20(0x4)); | |
require(trades[tradeId].state == TradeState.Matched); | |
trades[tradeId].state = TradeState.Closed; | |
require(tokens[convert(trades[tradeId].symbol)].transferFrom(trades[tradeId].seller, trades[tradeId].buyer, trades[tradeId].amountOfTokens)); | |
trades[tradeId].seller.transfer(trades[tradeId].payment.add(trades[tradeId].deposit)); | |
feesGathered += computeFee(trades[tradeId].payment).mul(2); | |
emit CloseTrade(tradeId, trades[tradeId].buyer, trades[tradeId].seller, false); | |
} | |
function claimDeposit(uint256 tradeId) public { | |
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); | |
feesGathered += computeFee(trades[tradeId].payment).mul(2); | |
emit CloseTrade(tradeId, trades[tradeId].buyer, trades[tradeId].seller, true); | |
} | |
function withdrawFees(uint256 amount) public onlyOwner { | |
require(feesGathered >= amount); | |
feesGathered -= amount; | |
msg.sender.transfer(amount); | |
} | |
function computeFee(uint256 value) private pure returns (uint256) { | |
return value.mul(5) / 1000; // This is the fee we take on each side (0.5% * payment) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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