Created
December 2, 2020 03:06
-
-
Save apogiatzis/a089890be81108a14f70f30ecf343052 to your computer and use it in GitHub Desktop.
Orderbook template contract for Blockchain for Fintect course
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.2; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.0/contracts/token/ERC20/ERC20.sol"; | |
contract Orderbook { | |
using SafeMath for uint256; | |
struct Order { | |
uint256 Amount; | |
uint256 Price; | |
uint256 TimeStamp; | |
address Trader; | |
bytes2 Status; // ('A'—Available, 'T'—Traded). | |
} | |
Order[] Buys; | |
Order[] Sells; | |
ERC20 public ERC20Base; | |
ERC20 public ERC20Counter; | |
constructor(address Base, address Counter) public { | |
ERC20Base = ERC20(Base); | |
ERC20Counter = ERC20(Counter); | |
} | |
event BuyAdded( | |
uint256 indexed Order_No, | |
uint256 Amt, | |
uint256 Price, | |
address trader | |
); | |
event SellAdded( | |
uint256 indexed Order_No, | |
uint256 Amt, | |
uint256 Price, | |
address trader | |
); | |
event TradeAdd( | |
uint256 indexed Order_No, | |
uint256 Amt, | |
uint256 Price, | |
address maker, // The trader who placed the order that is being traded | |
address taker // The trader who matches against the order. | |
); | |
function addBuy(uint256 Amt, uint256 BuyPrice) public returns (uint256) { | |
// Transfer from senders address to this contract Amt*BuyPrice base tokens; Hint: use transferFrom ERC20 interface function | |
// Create a new order; | |
// Add the order to the Buys Array; | |
// emit a BuyAdded event with the appropriate parameters | |
} | |
function addSell(uint256 Amt, uint256 SellPrice) public returns (uint256) { | |
// Transfer from senders address to this contract Amt counter tokens; Hint: use transferFrom ERC20 interface function | |
// Create a new order; | |
// Add the order to the Sells array | |
// emit a SellAdded event with the appropriate parameters | |
} | |
function viewLengthBuy() public view returns (uint256) { | |
// return length of buys array; | |
} | |
function viewLengthSell() public view returns (uint256) { | |
// return length of sells array; | |
} | |
function viewBuy(uint256 OrderNo) | |
public | |
view | |
returns ( | |
uint256, | |
uint256, | |
uint256, | |
address | |
) | |
{ | |
// return the fields of the specified sell order | |
} | |
function viewSell(uint256 OrderNo) | |
public | |
view | |
returns ( | |
uint256, | |
uint256, | |
uint256, | |
address | |
) | |
{ | |
// return the fields of the specified buy order | |
} | |
function trade( | |
uint256 OrderNo, | |
uint256 Amt, | |
uint256 TradePrice, | |
uint256 trade_type | |
) | |
public | |
returns ( | |
uint256, | |
uint256, | |
address | |
) | |
{ | |
// tradeType: 1 is Buy trade , 2 is Sell Trade | |
// ================================================================= | |
// Full Sell Order Fulfillment | |
// ================================================================= | |
// If is buy trade and the given sell order matches the amount: | |
// REQUIRE that trade price is larger or equal to sell order price, otherwise return "Invalid Price" error message | |
// Transfer from the buyer to the seller Amt*SellOrder.price base tokens | |
// Set the sell's order amount to 0 | |
// Set the sell's order status to T | |
// Transfer trade's amount counter tokens to buyer. Remember: The contract holds the counter tokens so it can directly send it to buyer | |
// emit a TradeAdd event with the appropriate parameters | |
// return Sell Order number that was fullfilled, amount and the buyer's address | |
// ================================================================= | |
// Partial Sell Order Fulfillment | |
// ================================================================= | |
// If is buy trade and the given sell order's amount is larger than the trade amount | |
// Transfer from the buyer to the seller Amt*SellOrder.price base tokens | |
// REQUIRE that trade price is larger or equal to sell order price, otherwise return "Invalid Price" error message | |
// Subtract the trade's amount from sell's order amount and set this as the new sell order amount | |
// Sell sell order's status to A | |
// Transfer trade's amount counter tokens to buyer. Remember: The contract holds the counter tokens so it can directly send it to buyer | |
// emit a TradeAdd event with the appropriate parameters | |
// return Sell Order number that was fullfilled, amount and the buyer's address | |
// ================================================================= | |
// Full Buy Order Fulfillment | |
// ================================================================= | |
// If is sell trade and the given buy order's amount is equal to the trade amount | |
// Transfer from seller to buyer counter tokens that equal the trade amount | |
// Set the buy order's amount to 0 | |
// Set the buy order's status to 'T' | |
// Transfer the trade's amount base tokens to seller | |
// emit a TradeAdd event with the appropriate parameters | |
// return Buy Order number that was fullfilled, amount and the seller's address | |
// ================================================================= | |
// Partial Buy Order Fulfillment | |
// ================================================================= | |
// If is sell trade and the given buy order's amount is larger than the trade amount | |
// Transfer from the seller to the buyer counter tokens that equal the trade amount | |
// Subtract the trade's amount from buy order's amount and set this as the new buy order amount | |
// Sell buy order's status to A | |
// Transfer trade's amount base tokens to seller. | |
// emit a TradeAdd event with the appropriate parameters | |
// return buy Order number that was fullfilled, amount and the seller's address | |
// For any other case REVERT the transaction and return "Invalid trade parameters" error message. Hint: use the "revert" operation | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment