Close

Order-Fulfilling Smart Contract

A project log for EthExchange

Open source, decentralized exchange on the Ethereum blockchain

dylan-brophyDylan Brophy 06/18/2019 at 17:250 Comments

Market prices change when people trade at new prices.  People trade at new prices when orders at the old price are no longer fulfilled.

Before my new smart contract, any token would be fixed to a certain price unless that price was manually changed.  My new contract allows the price to change by automatically matching buy and sell orders together.

Let's say Alice places a buy order for 100 tokens at a price of 1.2 dollars, but no previous sell orders have been placed at or below this price.  The smart contract takes Alice's money, but lists her buy order.

Now let's say Bob places a sell order for 250 tokens at a price of 1.05 dollars.  The smart contract now completes Alice's order, sending her 100 of Bob's sold tokens, and gives Bob his money.  But Bob is still selling 150 more tokens, so the contract takes these and lists a sell order.

This is what all exchanges do every day, what the stock market does, etc.  But those systems are centralized.  This smart contract exists on the Ethereum blockchain, and it is decentralized.

My current smart contract can be viewed on Etherscan here (along with source code):

https://ropsten.etherscan.io/address/0x2400b7c684f1ddc738b7dcc4527c3b9af45cdac5#contracts

The contract is not perfect (and this one has at least one minor bug), but as a proof of concept, it does seem to work.  Note that it is on the testnet - DO NOT SEND IT REAL ETHEREUM - you will be VERY SAD if you do.

Next I need to weed out the bugs and add two more functions: placeBuyOrder() and placeSellOrder().  These functions will allow orders to be placed at any price.

Discussions