The orderbook smart contract is the core component of our entire project.
It carries out the following functions:
- Records incoming buy/sell offer requests from the frontend and moves the base or counter asset from the user's Ethereum account to the smart contract account.
- Allows traders to view the recorded buy/sell offers on the smart contract.
- Accepts trade requests against already recorded buy/sell offers and executes the trade. As part of the trade, the orderbook updates the available amount in an offer, and transfers the base or counter asset both from the trader's account and to the trader's account.
- Emits an event to indicate when a new buy or sell offer is updated, or a new trade is carried out. This event will be subscribed to by our frontend app to update and display the orderbook in real time.
Now, let's write our orderbook...