Skip to content

Instantly share code, notes, and snippets.

@crypto-perry
Created July 24, 2019 22:53
Show Gist options
  • Save crypto-perry/f742d15a1df1ecbbc479d1c7ce247111 to your computer and use it in GitHub Desktop.
Save crypto-perry/f742d15a1df1ecbbc479d1c7ce247111 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;
import './tradeoracleinterface.sol';
contract AlgorandVerifier is TradeOracle {
mapping (uint256 => bool) blockheaders;
function addBlockHeader(uint256 bh) public {
blockheaders[bh] = true;
}
function verifyTrade(uint256 buyer, uint256 seller, uint amount, bytes32 tradeId, bytes calldata proof) external returns (bool) {
return false;
}
}
pragma solidity >=0.5.0;
import './tradeoracleinterface.sol';
contract FuturesEscrow {
enum TradeState {Void, Open, Filled, Closed}
struct Trade {
address payable buyer;
address payable seller;
uint price;
uint deposit;
uint amount;
uint expiration;
TradeState state;
}
mapping (bytes32 => Trade) trades;
mapping (address => uint) balances;
mapping (address => uint256) hashAddresses;
TradeOracle tradeVerifier;
address owner;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
constructor() public {
owner = msg.sender;
}
function setTradeVerifier(address tradeOracle) public onlyOwner {
tradeVerifier = TradeOracle(tradeOracle);
}
function openTrade(bytes32 tradeId, address payable seller, uint price, uint deposit, uint amount, uint expireAfter) public payable {
require(trades[tradeId].state == TradeState.Void);
require (msg.value >= (price * 1005)/1000);
require(expireAfter > 0);
trades[tradeId] = Trade(msg.sender, seller, price, deposit, product, now + expireAfter, TradeState.Open);
}
function fillTrade(bytes32 tradeId) public payable {
Trade memory trade = trades[tradeId];
require(trade.seller == msg.sender);
require(trade.state == TradeState.Open);
require(trade.expiration > now);
require(msg.value >= trade.deposit + (trade.price * 5)/1000);
trades[tradeId].state = TradeState.Filled;
}
function cancelOpenTrade(bytes32 tradeId) public {
Trade memory trade = trades[tradeId];
require(trade.buyer == msg.sender);
require(trade.state == TradeState.Open);
trades[tradeId].state = TradeState.Closed;
msg.sender.transfer(trade.price);
}
function claimExpiredTrade(bytes32 tradeId) public {
Trade memory trade = trades[tradeId];
require(trade.buyer == msg.sender);
require(trade.state == TradeState.Filled);
require(trade.expiration < now);
trades[tradeId].state = TradeState.Closed;
msg.sender.transfer(trade.price + trade.deposit);
}
function completeTrade(bytes32 tradeId, bytes memory proof) public {
Trade memory trade = trades[tradeId];
require(trade.state == TradeState.Filled);
require(trade.expiration > now);
require(tradeVerifier.verifyTrade(hashAddresses[trade.buyer], hashAddresses[trade.seller], trade.amount, tradeId, proof));
trades[tradeId].state = TradeState.Closed;
trade.seller.transfer(trade.deposit + trade.price);
}
function linkHashAddress(uint256 addressHash) public {
require(hashAddresses[msg.sender] == 0);
hashAddresses[msg.sender] = addressHash;
}
function linkHashAddress(address ethAddress, uint256 addressHash) public onlyOwner {
hashAddresses[ethAddress] = addressHash;
}
function() external payable {
balances[msg.sender] += msg.value;
}
function withdraw(uint amount) public {
require(balances[msg.sender] >= amount);
balances[msg.sender] -= amount;
msg.sender.transfer(amount);
}
function makeTrade(bytes32 tradeId, address payable buyer, address payable seller, uint price, uint deposit, uint amount, uint expireAfter) public onlyOwner {
require(balances[buyer] >= (price * 101)/100);
require(balances[seller] >= deposit + price/100);
require(trades[tradeId].state == TradeState.Void);
balances[buyer] -= (price * 101)/100;
balances[seller] -= (deposit + price/100);
trades[tradeId] = Trade(buyer, seller, price, deposit, product, now + expireAfter, TradeState.Filled);
}
}
// this line is added to create a gist. Empty file is not allowed.
pragma solidity >=0.5.0;
interface TradeOracle {
function verifyTrade(uint256 buyer, uint256 seller, uint amount, bytes32 tradeId, bytes calldata proof) external returns (bool);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment