Skip to content

Instantly share code, notes, and snippets.

@erubboli
Last active February 12, 2020 18:13
Show Gist options
  • Save erubboli/020acf74579cd57353c8f2efc4bf409d to your computer and use it in GitHub Desktop.
Save erubboli/020acf74579cd57353c8f2efc4bf409d to your computer and use it in GitHub Desktop.
*.sol linguist-language=Solidity
contract SeedDex {
mapping (address => mapping (address => uint)) private tokens;
mapping (address => mapping (bytes32 => bool)) private orders;
mapping (address => mapping (bytes32 => uint)) private orderFills;
// make an Order
function order(
address tokenGet,
uint amountGet,
address tokenGive,
uint amountGive,
uint expires,
uint nonce) public {
require(expires > block.number, "expires must be in the future");
require(isValidPair(tokenGet, tokenGive), "Not a valid pair");
require(canBeTransferred(tokenGet, msg.sender, amountGet), "Token quota exceeded");
bytes32 hash = sha256(abi.encodePacked(this, tokenGet, amountGet, tokenGive, amountGive, expires, nonce));
orders[msg.sender][hash] = true;
emit Order(tokenGet, amountGet, tokenGive, amountGive, expires, nonce, msg.sender);
}
// Take an order from the book
function trade(
address tokenGet,
uint amountGet,
address tokenGive,
uint amountGive,
uint expires,
uint nonce,
address user,
uint amount) public {
require(block.number <= expires, "Order Expired");
require(isValidPair(tokenGet, tokenGive), "Not a valid pair");
require(canBeTransferred(tokenGet, msg.sender, amountGet), "Token quota exceeded");
bytes32 hash = sha256(abi.encodePacked(this, tokenGet, amountGet, tokenGive, amountGive, expires, nonce));
require(orders[user][hash], "Order does not exist");
require(orderFills[user][hash].add(amount) <= amountGet, "Order amount exceeds maximum availability");
tradeBalances(tokenGet, amountGet, tokenGive, amountGive, user, amount);
orderFills[user][hash] = orderFills[user][hash].add(amount);
uint executedAmount = amountGive.mul(amount) / amountGet;
emit Trade(tokenGet, amountGet, tokenGive, amountGive, expires, nonce, user, amount, executeAmount, msg.sender);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment