Assignment 4: Create a Privacy Preserving ARC21 Order Book DEX

The goal of this final assignment is to create your own privacy preserving order book DEX (decentralized exchange).

An order book DEX is a trading platform that matches buy and sell orders using an on-chain list of orders. It allows users to interact with the smart-contract to create trading orders between two tokens, and other users to come and accept those orders. The first user can then come back to withdraw their funds.

This can all be done while preserving the privacy of the users involved. Here's how:

First, orders, represented as structs, will be stored publicly in mappings:

struct Order {
    sold_token_id: field,
    bought_token_id: field,
    sold_token_amount: u128,
    bought_token_amount: u128,
    accepted: bool,
    withdrawn: bool
}

mapping orders: field => Order;
// order_id -> order

When a user wants to create an order, he calls the create_order transition, providing the data above (with accepted and withdrawn set to false). The transition checks that the order id does not exist yet, and stores in the orders mapping this new order. The sold token (full amount) is transferred (private to public) from the user to the program as well in this transition, guarantying privacy of the creator of the order. Also a Record receipt is created and given to the creator.

record Receipt {
    owner: address,
    order_id: field
}

With this receipt, the creator will be able to get his sold token back if the order was not accepted and withdrawn yet, or get his bought token otherwise.

When another user wants to accept that order, he can call the accept_order transition. It both transfers (private to public) the bought token (full amount) to the program, and transfers (public to private) the sold amount to the user, it also updates the mapping order accepted field, and check the data was correctly provided.

The initial user can then consume his Receipt record by calling the withdraw transition, which transfers (public to private) the bought amount to the user, and updates the order accordingly.

If the accept_order transition was not called yet, he can remove his order by using the delete_order transition, getting his sold token transfered (public to private) back, and updating the order data.

Learning Objectives

By completing this assignment, you will:

  • Get a good glimpse of how real world applications are built in Leo

  • Create your first private DEX

Tasks

Write the program described above containing all 4 transitions:

  • create_order

  • accept_order

  • delete_order

  • withdraw

There is no need to create an interface for this program to complete the assignment, just writing the Leo program is enough.

Last updated