A crypto exchange platform written in Golang that simulates an order book for trading cryptocurrencies. The platform supports placing limit and market orders, canceling orders, and retrieving the current state of the order book.
- Order Book Management: Maintains a list of buy and sell orders with price and size.
- Order Types:
- Limit Orders: Place orders at a specific price.
- Market Orders: Match orders with the best available price.
- Order Matching: Automatically matches buy and sell orders based on price and size.
- Order Cancellation: Cancel existing orders by their ID.
- Ethereum Integration: Simulates ETH transfers using the Ethereum blockchain.
- REST API: Exposes endpoints for interacting with the exchange.
main.go: Entry point of the application. Sets up the REST API and initializes the exchange.orderbook/orderbook.go: Core logic for managing the order book, including placing and matching orders.orderbook/orderbook_test.go: Unit tests for the order book functionality.util.go: Utility functions for interacting with the Ethereum blockchain.Makefile: Build and test automation.apiTest.http: HTTP request examples for testing the REST API.
-
POST
/order -
Request Body:
{ "userID": 8, "type": "LIMIT", "bid": true, "size": 20, "price": 9000.0, "market": "ETH" } -
Response:
- For limit orders:
{ "message": "Limit Order placed successfully" } - For market orders:
{ "matches": [...] }
- For limit orders:
-
GET
/book/:market -
Response:
{ "TotalBidVolume": 100.0, "TotalAskVolume": 200.0, "Asks": [...], "Bids": [...] }
-
DELETE
/order/:id -
Response:
{ "message": "Limit Order Deleted successfully" }
- Go 1.18 or later
- Ethereum client (e.g., Geth or Ganache)
make(optional, for build automation)
-
Clone the repository:
git clone https://github.com/your-repo/crypto-exchange.git cd crypto-exchange -
Install dependencies:
go mod tidy
-
Start an Ethereum client (e.g., Ganache) locally.
-
Run the application:
go run main.go
-
Use the provided
apiTest.httpfile to test the API using tools like Postman or VS Code REST Client.
- Build and run the application:
make run- Run tests:
make test- Clean build artifacts:
make cleanUnit tests for the order book are located in orderbook/orderbook_test.go
The platform uses the Ethereum blockchain to simulate ETH transfers. Ensure that your Ethereum client is running and accessible at http://localhost:8545. Update the private keys and addresses in main.go as needed.
- Place a limit order to buy ETH at a specific price.
- Place a market order to sell ETH, which matches the existing buy order.
- Retrieve the order book to view the updated state.
- Cancel an order by its ID.
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Submit a pull request with a detailed description of your changes.
This project is licensed under the GPL-3.0 License. See the LICENSE file for details.
- Echo Framework for building REST APIs.
- Go-Ethereum for Ethereum blockchain integration.