Skip to content

Instantly share code, notes, and snippets.

@crypto-perry
Created September 16, 2019 13:06
Show Gist options
  • Save crypto-perry/5c4cdfbeba897f80151ff1c7d919df55 to your computer and use it in GitHub Desktop.
Save crypto-perry/5c4cdfbeba897f80151ff1c7d919df55 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=undefined&optimize=true&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);
}
library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
require(c >= a);
return c;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
if (a == 0) {
return 0;
}
c = a * b;
require(c / a == b);
return c;
}
}
contract ERC20OpenBuy {
using SafeMath for uint256;
event UpdateBuy(address maker, address token, uint256 ethPerToken, uint256 expiration, uint256 amountRemaining);
address public owner;
uint256 public feesGathered;
mapping (uint256 => uint256) public trades;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
constructor() public {
owner = msg.sender;
}
function addToBuyOffer(address token, uint256 ethPerToken, uint256 expiration) public payable {
uint256 tradeId = uint256(keccak256(abi.encodePacked(msg.sender, token, ethPerToken, expiration)));
trades[tradeId] = trades[tradeId].add(msg.value.mul(1000) / 1005);
emit UpdateBuy(msg.sender, token, ethPerToken, expiration, trades[tradeId]); // Log open BUY
}
function partialFillBuy(address token, uint256 ethPerToken, uint256 expiration, uint256 tokenAmount, address payable other) public {
uint256 tradeId = uint256(keccak256(abi.encodePacked(other, token, ethPerToken, expiration)));
uint256 ethPayment = tokenAmount.mul(ethPerToken);
require(trades[tradeId] >= ethPayment); // Make sure there is enough ether to pay for these tokens
trades[tradeId] = trades[tradeId] - ethPayment;
require(IERC20(token).transferFrom(msg.sender, other, tokenAmount)); // Take tokens from seller and give to buyer
uint256 fee = computeFee(ethPayment);
msg.sender.transfer(ethPayment - fee);
feesGathered = feesGathered.add(fee).add(fee);
emit UpdateBuy(other, token, ethPerToken, expiration, trades[tradeId]); // Log open BUY
}
function cancelBuyOffer(address token, uint256 ethPerToken, uint256 expiration) public {
uint256 tradeId = uint256(keccak256(abi.encodePacked(msg.sender, token, ethPerToken, expiration)));
uint256 ethAmount = trades[tradeId];
trades[tradeId] = 0;
msg.sender.transfer(ethAmount + computeFee(ethAmount)); // Refund eth + fee to buyer
emit UpdateBuy(msg.sender, token, ethPerToken, expiration, 0); // Log open BUY
}
function _withdrawFees() public onlyOwner {
uint256 amount = feesGathered;
feesGathered = 0;
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%)
}
function getExpirationAfter(uint256 amountOfHours) public view returns (uint256) {
return now + (amountOfHours * (1 hours));
}
function tradeInfo(address payable other, address token, uint256 ethPerToken, uint256 expiration, uint256 tokenAmount) public view
returns (uint256 _tradeId, uint256 _buySideTotal, uint256 _amountRemaining) {
_buySideTotal = tokenAmount.mul(ethPerToken);
_buySideTotal = _buySideTotal.add(computeFee(_buySideTotal));
address payable maker = other == address(0x0) ? msg.sender : other;
_tradeId = uint256(keccak256(abi.encodePacked(maker, token, ethPerToken, expiration)));
return (_tradeId, _buySideTotal, trades[_tradeId]);
}
function() external payable {
revert();
}
}
contract ERC20OpenSell {
using SafeMath for uint256;
event UpdateSell(address maker, address token, uint256 ethPerToken, uint256 expiration, uint256 amountRemaining);
address public owner;
uint256 public feesGathered;
mapping (uint256 => uint256) public trades;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
constructor() public {
owner = msg.sender;
}
function addToSellOffer(address token, uint256 ethPerToken, uint256 expiration, uint256 tokenAmount) public {
uint256 tradeId = uint256(keccak256(abi.encodePacked(msg.sender, token, ethPerToken, expiration)));
require(IERC20(token).transferFrom(msg.sender, address(this), tokenAmount)); // Take tokens from seller
trades[tradeId] = trades[tradeId].add(tokenAmount); // Update state
emit UpdateSell(msg.sender, token, ethPerToken, expiration, trades[tradeId]); // Log open SELL
}
function partialFillSell(address token, uint256 ethPerToken, uint256 expiration, address payable other) public payable {
uint256 tradeId = uint256(keccak256(abi.encodePacked(other, token, ethPerToken, expiration)));
uint256 ethPayment = msg.value.mul(1000) / 1005; // Without fee
uint256 tokenAmount = ethPayment / ethPerToken;
require(trades[tradeId] >= tokenAmount); // Make sure there are enough tokens to pay
trades[tradeId] = trades[tradeId] - tokenAmount; // Update state
require(IERC20(token).transfer(msg.sender, tokenAmount)); // Give tokens to buyer
uint256 fee = computeFee(ethPayment);
other.transfer(ethPayment - fee); // Pay the seller
feesGathered = feesGathered.add(fee).add(fee);
emit UpdateSell(other, token, ethPerToken, expiration, trades[tradeId]); // Log open BUY
}
function cancelSellOffer(address token, uint256 ethPerToken, uint256 expiration) public {
uint256 tradeId = uint256(keccak256(abi.encodePacked(msg.sender, token, ethPerToken, expiration)));
uint256 tokenAmount = trades[tradeId];
trades[tradeId] = 0;
IERC20(token).transfer(msg.sender, tokenAmount); // Refund seller
emit UpdateSell(msg.sender, token, ethPerToken, expiration, 0); // Log open SELL
}
function _withdrawFees() public onlyOwner {
uint256 amount = feesGathered;
feesGathered = 0;
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%)
}
function getExpirationAfter(uint256 amountOfHours) public view returns (uint256) {
return now + (amountOfHours * (1 hours));
}
function tradeInfo(address payable other, address token, uint256 ethPerToken, uint256 expiration, uint256 tokenAmount) public view
returns (uint256 _tradeId, uint256 _buySideTotal, uint256 _amountRemaining) {
_buySideTotal = tokenAmount.mul(ethPerToken);
_buySideTotal = _buySideTotal.add(computeFee(_buySideTotal));
address payable maker = other == address(0x0) ? msg.sender : other;
_tradeId = uint256(keccak256(abi.encodePacked(maker, token, ethPerToken, expiration)));
return (_tradeId, _buySideTotal, trades[_tradeId]);
}
function() external payable {
revert();
}
}
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, SellPaid, BuyPaid, Canceled, Matched, Closed, Expired}
struct Trade {
address payable buyer;
address payable seller;
string symbol;
uint256 payment;
uint256 amountOfTokens;
uint256 deposit;
uint256 expiration;
TradeState state;
}
event OpenTrade(uint256 tradeId, address indexed buyer, address indexed seller, string symbol, uint256 pricePerToken, uint256 amountOfTokens, uint256 depositPercentage, uint256 expiration, TradeState state);
event UpdateTrade(uint256 tradeId, address indexed buyer, address indexed seller, TradeState state);
address private owner;
uint256 public feesGathered;
mapping (uint256 => Trade) public trades;
mapping (bytes32 => IERC20) private tokens;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
constructor() public {
owner = msg.sender;
}
function() external payable {
revert();
}
function A_trade(bool wantToBuy, string memory symbol, uint256 amountOfTokens, uint256 pricePerToken, uint256 depositPercentage, uint256 expiration, address payable other) public payable {
require(tokens[convert(symbol)] != IERC20(0x0));
require(pricePerToken >= 1000); // min price so that divisions with 1000 never give remainder
Trade memory t;
(t.symbol, t.payment, t.amountOfTokens, t.deposit, t.expiration)
= (symbol, pricePerToken.mul(amountOfTokens), amountOfTokens, pricePerToken.mul(amountOfTokens).mul(depositPercentage) / 100, expiration);
uint256 paymentRequired;
(t.buyer, t.seller, t.state, paymentRequired) = wantToBuy
? (msg.sender, other, TradeState.BuyPaid, t.payment.add(computeFee(t.payment)))
: (other, msg.sender, TradeState.SellPaid, t.deposit.add(computeFee(t.payment)));
require(msg.value >= paymentRequired);
uint256 tradeId = uint256(keccak256(abi.encodePacked(t.buyer, t.seller, t.symbol, t.amountOfTokens, pricePerToken, depositPercentage, t.expiration)));
Trade storage existingTrade = trades[tradeId];
if (existingTrade.state == TradeState.None) {
emit OpenTrade(tradeId, t.buyer, t.seller, t.symbol, pricePerToken, t.amountOfTokens, depositPercentage, t.expiration, t.state);
trades[tradeId] = t;
} else if (t.state == TradeState.BuyPaid && existingTrade.state == TradeState.SellPaid
|| t.state == TradeState.SellPaid && existingTrade.state == TradeState.BuyPaid) {
existingTrade.state = TradeState.Matched;
emit UpdateTrade(tradeId, t.buyer, t.seller, existingTrade.state);
} else {
revert();
}
msg.sender.transfer(msg.value - paymentRequired);
}
function B_matchTrade(uint256 tradeId) public payable {
Trade storage t = trades[tradeId];
uint256 paymentRequired;
if(t.state == TradeState.SellPaid) {
if (t.buyer == address(0x0)) {
t.buyer = msg.sender;
} else {
require(msg.sender == t.buyer);
}
paymentRequired = t.payment.add(computeFee(t.payment));
} else if(t.state == TradeState.BuyPaid) {
if (t.seller == address(0x0)) {
t.seller = msg.sender;
} else {
require(msg.sender == t.seller);
}
paymentRequired = t.deposit.add(computeFee(t.payment));
} else {
revert();
}
require(msg.value >= paymentRequired);
t.state = TradeState.Matched;
emit UpdateTrade(tradeId, t.buyer, t.seller, t.state);
msg.sender.transfer(msg.value - paymentRequired);
}
function B_cancelOpenTrade(uint256 tradeId) public {
Trade storage t = trades[tradeId];
require(t.state == TradeState.SellPaid || t.state == TradeState.BuyPaid);
require(msg.sender == t.seller || msg.sender == t.buyer);
address payable actor; uint256 refund;
(actor, refund) = (t.state == TradeState.SellPaid)
? (t.seller, t.deposit.add(computeFee(t.payment)))
: (t.buyer, t.payment.add(computeFee(t.payment)));
t.state = TradeState.Canceled;
emit UpdateTrade(tradeId, t.buyer, t.seller, t.state);
actor.transfer(refund);
}
function C_completeTrade(uint256 tradeId) public {
Trade storage t = trades[tradeId];
require(t.state == TradeState.Matched);
t.state = TradeState.Closed;
feesGathered += computeFee(t.payment).mul(2);
require(tokens[convert(t.symbol)].transferFrom(t.seller, t.buyer, t.amountOfTokens));
t.seller.transfer(t.payment + t.deposit);
emit UpdateTrade(tradeId, t.buyer, t.seller, t.state);
}
function C_claimExpiredTrade(uint256 tradeId) public {
Trade storage t = trades[tradeId];
require(t.state == TradeState.Matched && msg.sender == t.buyer && t.expiration < now);
t.state = TradeState.Expired;
feesGathered += computeFee(t.payment).mul(2);
t.buyer.transfer(t.payment + t.deposit);
emit UpdateTrade(tradeId, t.buyer, t.seller, t.state);
}
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%)
}
function convert(string memory key) private pure returns (bytes32 ret) {
require(bytes(key).length <= 32);
assembly {
ret := mload(add(key, 32))
}
}
function getExpirationAfter(uint256 amountOfHours) public view returns (uint256) {
return now.add(amountOfHours.mul(1 hours));
}
function tradeInfo(bool wantToBuy, string memory symbol, uint256 amountOfTokens,
uint256 priceOfOneToken, uint256 depositPercentage, uint256 expiration, address payable other) public view
returns (uint256 _tradeId, uint256 _buySideTotal, uint256 _sellSideTotal, TradeState _state) {
_buySideTotal = amountOfTokens.mul(priceOfOneToken);
_sellSideTotal = depositPercentage.mul(_buySideTotal) / 100;
_sellSideTotal = _sellSideTotal.add(computeFee(_buySideTotal));
_buySideTotal = _buySideTotal.add(computeFee(_buySideTotal));
address payable buyer; address payable seller; (buyer, seller) = wantToBuy ? (msg.sender, other) : (other, msg.sender);
uint256 tradeId = uint256(keccak256(abi.encodePacked(buyer, seller, symbol, amountOfTokens, priceOfOneToken, depositPercentage, expiration)));
return (tradeId, _buySideTotal, _sellSideTotal, trades[tradeId].state);
}
function _setTokenAddress(string memory symbol, address token) public onlyOwner {
tokens[convert(symbol)] = IERC20(token);
}
function getTokenAddress(string memory symbol) public view returns (IERC20) {
return tokens[convert(symbol)];
}
}
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 c) {
c = a + b;
require(c >= a);
return c;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
if (a == 0) {
return 0;
}
c = a * b;
require(c / a == b);
return c;
}
}
contract ERC20Swap {
using SafeMath for uint256;
enum TradeState {None, SellPaid, BuyPaid}
event OpenTrade(address indexed buyer, address indexed seller, address token, uint256 ethAmount, uint256 tokenAmount, uint256 expiration, TradeState state);
event CloseTrade(address indexed buyer, address indexed seller, uint256 tradeId);
address public owner;
uint256 public feesGathered;
mapping (uint256 => TradeState) public trades;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
constructor() public {
owner = msg.sender;
}
function trade(bool wantToBuy, address payable other, address token, uint256 tokenAmount, uint256 ethAmount, uint256 expiration) public payable {
require(ethAmount > 100000);
uint256 tradeId = wantToBuy
? uint256(keccak256(abi.encodePacked(msg.sender, other, token, ethAmount, tokenAmount, expiration)))
: uint256(keccak256(abi.encodePacked(other, msg.sender, token, ethAmount, tokenAmount, expiration)));
TradeState state = trades[tradeId];
if (state == TradeState.None) { // If trade doesn't exist
if (wantToBuy) { // If trying to buy
require(msg.value == ethAmount.add(computeFee(ethAmount))); // Take eth and fee from buyer
trades[tradeId] = TradeState.BuyPaid; // Update state
emit OpenTrade(msg.sender, other, token, ethAmount, tokenAmount, expiration, TradeState.BuyPaid); // Log open BUY
} else { // If trying to sell
require(IERC20(token).transferFrom(msg.sender, address(this), tokenAmount)); // Take tokens from seller
trades[tradeId] = TradeState.SellPaid; // Update state
emit OpenTrade(other, msg.sender, token, ethAmount, tokenAmount, expiration, TradeState.SellPaid); // Log open SELL
}
} else if (wantToBuy && state == TradeState.SellPaid) { // If buyer closes the trade
require(expiration > now);
uint256 fee = computeFee(ethAmount);
require(msg.value == ethAmount.add(fee)); // Take eth and fee from buyer
trades[tradeId] = TradeState.None; // Close trade first to protect against reentrancy
require(IERC20(token).transfer(msg.sender, tokenAmount)); // Send tokens to buyer (from this contract as seller already paid us)
other.transfer(ethAmount - fee); // Send eth - fee to seller
feesGathered = feesGathered.add(fee).add(fee);
emit CloseTrade(msg.sender, other, tradeId);
} else if (!wantToBuy && state == TradeState.BuyPaid) { // If seller closes the trade
require(expiration > now);
trades[tradeId] = TradeState.None; // Close trade first to protect against reentrancy
require(IERC20(token).transferFrom(msg.sender, other, tokenAmount)); // Send tokens to buyer (directly from seller)
uint256 fee = computeFee(ethAmount);
msg.sender.transfer(ethAmount - fee); // Send eth - fee to seller
feesGathered = feesGathered.add(fee).add(fee);
emit CloseTrade(other, msg.sender, tradeId);
} else {
revert();
}
}
function cancelTrade(bool wantToBuy, address payable other, address token, uint256 tokenAmount, uint256 ethAmount, uint256 expiration) public {
uint256 tradeId = wantToBuy
? uint256(keccak256(abi.encodePacked(msg.sender, other, token, ethAmount, tokenAmount, expiration)))
: uint256(keccak256(abi.encodePacked(other, msg.sender, token, ethAmount, tokenAmount, expiration)));
TradeState state = trades[tradeId];
if (wantToBuy && state == TradeState.BuyPaid) { // If buyer cancels the trade
trades[tradeId] = TradeState.None; // Close trade first to protect against reentrancy
msg.sender.transfer(ethAmount + computeFee(ethAmount)); // Refund eth + fee to buyer
emit CloseTrade(msg.sender, other, tradeId);
} else if (!wantToBuy && state == TradeState.SellPaid) { // If seller cancels the trade
trades[tradeId] = TradeState.None; // Close trade first to protect against reentrancy
require(IERC20(token).transfer(msg.sender, tokenAmount)); // Refund tokens to seller
emit CloseTrade(other, msg.sender, tradeId);
} else {
revert();
}
}
function _withdrawFees() public onlyOwner {
uint256 amount = feesGathered;
feesGathered = 0;
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%)
}
function getExpirationAfter(uint256 amountOfHours) public view returns (uint256) {
return now + (amountOfHours * (1 hours));
}
function tradeInfo(bool wantToBuy, address payable other, address token, uint256 tokenAmount, uint256 ethAmount, uint256 expiration) public view
returns (uint256 _tradeId, uint256 _buySideTotal, TradeState) {
_buySideTotal = ethAmount.add(computeFee(ethAmount));
address payable buyer; address payable seller; (buyer, seller) = wantToBuy ? (msg.sender, other) : (other, msg.sender);
_tradeId = uint256(keccak256(abi.encodePacked(buyer, seller, token, ethAmount, tokenAmount, expiration)));
return (_tradeId, _buySideTotal, trades[_tradeId]);
}
function() external payable {
revert();
}
}
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 ERC20Dummy is IERC20 {
string public constant name = "ERC20DummyToken";
string public constant symbol = "DUMMY_TOKEN";
constructor() public {}
function totalSupply() public view returns (uint256) {
return 0;
}
function balanceOf(address tokenOwner) public view returns (uint) {
return 0;
}
function transfer(address receiver, uint numTokens) public returns (bool) {
revert();
}
function approve(address delegate, uint numTokens) public returns (bool) {
revert();
}
function allowance(address owner, address delegate) public view returns (uint) {
return 0;
}
function transferFrom(address owner, address buyer, uint numTokens) public returns (bool) {
revert();
}
}
contract ERC20Test is IERC20 {
using SafeMath for uint256;
string public constant name = "ERC20TestToken";
string public constant symbol = "TEST_TOKEN";
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_;
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] -= 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] -= numTokens;
allowed[owner][msg.sender] -= numTokens;
balances[buyer] = balances[buyer].add(numTokens);
emit Transfer(owner, buyer, numTokens);
return true;
}
}
library SafeMath {
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