This article is a development report of a Dex223 decentralized exchange.
We’re building an exchange to speed up adoption of the ERC-223 token standard. ERC-223 was created to solve a security problem in the older ERC-20 standard. Security issue of ERC-20 caused $200M losses on Ethereum in 2023.
Take a look at other Dex223 development reports.
In the previous report it was written that our prototype is undergoing internal testing. Currently we have a basic version of Dex223 exchange deployed on Sepolia and BSC testnets.
You can find and try it out here: https://test-app.dex223.io/
We have already announced the public testing, also you can request test tokens in our telegram discussions channel.
After the release of the test version we already received numerous bug reports and issue reports. We've created a document to track the bugs reported by our team members and community members and this document will be published later (most likely in the next report) but at this point we prioritize the support of Auto-listing feature in our smart-contracts over fixing the issues. After the support of auto-listing we will switch to the issue fixing iteration and at this point we will spend approximately 2 weeks working on all the issues reported (including the issues related to the auto-listing).
The concept of auto-listing was finalized during the previous month and during July we've finished its technical implementation. The smart-contracts can be found here: https://github.com/EthereumCommonwealth/Dex223-contracts/blob/main/contracts/dex-core/Autolisting.sol
There are few auto-listing contracts deployed on Sepolia testnet already:
-
Listing registry: https://sepolia.etherscan.io/address/0x4fd0ff10833d6c90f0995ddefd10a1ef03b24790
-
Autolisting (Dex223 free) https://sepolia.etherscan.io/address/0x7333d5141f645b354e1517892a725db88a840436
-
Autolistign (Dex223 payable) https://sepolia.etherscan.io/address/0x87af0fadc21420d0a572b4709b81cf2e368552d7
-
Autolisting (Custom) https://sepolia.etherscan.io/address/0xb83b6a34802bb4149834110c28e3e0e270d804a8
We have a version of dev UI which already supports auto-listing contracts that we deployed (this one: https://dex-exchange-git-feature-autolisting-absolute-wallet.vercel.app/en/swap) but we will extend the auto-listings support on the UI side in the future. Currently it is possible to:
- Enable / disable Dex223 auto-listing contracts
- List your own (test) tokens through our auto-listing contracts
- Create your own (auto) listing contracts and manually add them in the Dex223 UI
We will need the UI for listing tokens through our contracts to make this feature as user-friendly as possible but the current implementation is fully suitable for token developers to get their tokens on board without any interactions with the Dex223 team.
Auto-listing smart-contracts are required to start the implementation of margin trading as they are an integral part of it.
The implementation of margin trading has begun.
We've recorded an overview video describing the process from user's perspective just today and I hope it will be published soon. In this report I would like to discuss the margin trading in more details.
There are three main problems with margin-trading implementation for DEX'es:
- It is absolutely not possible to implement a liquidation engine with smart-contracts. Liquidations are time-sensitive and computation intensive operations. Smart-contracts can't do such things.
- Price feeds. Smart-contracts can only read data from other smart-contracts.
- There is no automatization in smart-contracts. A smart-contract can only respond to some action of a caller. A smart-contract can only verify conditions when called and do X or Y based on the results of the verification but it can not initiate any process on its own based on time or some criteria.
In Dex223 we address this problems by (1) delegating liquidations to third party executors and (2) placing the requirements to determine the conditions for funds borrowing on the lender.
In our implementation we are not trying to build the liquidation engine with smart-contracts but instead we create a system of incentives for calling a function that will tell a contract to check if some position is subject to liquidation based on the price of the assets held within this position. In this case our contracts will only need to respond to verification requests and in case of insufficient balance in a verifiable position - they will liquidate it and reward the address who triggered this verification. This is what smart-contracts absolutely can do.
The problem of price feeds is a tricky one but we have few options here. We can identify the prices of assets based on the liquidity in our Dex223 pools and we can do the same with Uniswap pools. There are LINK price oracles. We can design and deploy "weighted price feed" smart-contract that will take LINK price, Dex223 price and Uniswap prices into account, weight them and calculate the resulting weighted price. It is hard to predict which option is better now so we are giving lenders the freedom of picking any of this options and we will let the market decide here.
Now it's time to showcase some UI templates and describe the workflow.
In Dex223 there need to be two types of participants in the system in order for it to operate properly: lenders and borrowers.
-
Lenders can provide their funds for borrowers to trade with in exchange for specified interest.
-
Borrowers can take this funds and actually use them for trading, but they need to return the funds and the end of the specified period of time and they need to pay back the loan in any circumstances.
We need to define two terms: "lending order" and "position".
The lending order is a structure created by the lender that holds funds available for borrowing and records the set of rules regarding the use of borrowable funds defined by the lender. The lender defines which currencies are accepted as a collateral, the interest rate, the duration of the borrowing period, the maximum leverage which borrower is allowed to use, a set of assets that borrowers is allowed to buy with the borrowed funds.
If the borrower agrees with the terms of using the funds specified in the "lending order" then he can deposit a collateral and gain access to the funds available for borrowing. In this case the funds will be move from the balance of the "lending order" to the balance of the borrower's "position".
It is important to note that when the lending order is created the funds are deposited to the Margin Trading Module smart-contract and they will not leave the balance of the smart-contract upon borrowing. When borrower is putting a collateral to take a loan the collateral is also deposited to the Margin Trading Module contract. From this moment the smart-contract considers that those funds (collateral + loan) are allocated for the borrower and they belong to some position (with a specific ID) that the borrower can access. The borrower can not withdraw any funds from an active position. In order to withdraw the funds the borrower must close the position and it requires paying back the loan + interest first.
The borrower can use funds from his position's balance (collateral + loan) to execute margin swaps.
Margin swap is a type of command that the borrower can issue to the Margin Trading Module smart-contract which will cause the contract to perform a trading operation with funds held in borrower's position if the trading operation is allowed by the rules of the lending order that this funds are loaned from.
Margin Swap will deposit the resulting tokens (bought via swap on Dex223 market) to the balance of the borrower's position in Margin Trading Module smart-contract.
Funds borrowed from different lending orders do not combine and instead create two different margin positions (each with its own rules and restrictions). Therefore executing a margin swap requires borrower to explicitly specify which position to use for this operation.
Here is lending order creation flow:
And this is how orders will be displayed:
Here you can access our updated Figma that contains our latest changes including the Margin Trading UI: