Zenlink DEX Smart Contract is Open Source
Zenlink has received a Web3 Foundation grant to develop a smart contract implementation. This has been delivered successfully and is now open source. In this article, we will show how it works and explain why we implemented the Zenlink DEX Protocol in different ways and the underlying production logic.
According to the technical architecture of Substrate, there are mainly two ways to achieve features on a Substrate blockchain.
The first way is to build a pallet. This could be imported into the chain runtime using coding. This is the most comprehensive way. The runtime can be modified to suit individual requirements.
The second way is to write smart contracts with an ink! pallet then deploy it to the chain. The benefit of this method is that people compile and deploy the smart contract to the parachain ink! module, rather than modify the source code of the parachain.
Zenlink aims to help parachains in the ecosystem by offering them a simple way to implement dex functionalities. In order to create a variety of different ways for teams to integrate dex functionalities into parachains, we created technical solutions for anyone building a complex decentralized exchange using Substrate technology.
“In the past, we have delivered the pallet implementation. Today, the smart contract implementation has been completed and is now open source. It will greatly enrich our ecosystem and provide more options for parachain builder teams.”
— Leo Guo, Head of Zenlink China
Zenlink DEX Smart Contract Overview
The Zenlink DEX Smart Contract consists of two parts: Exchange and Factory.
The Exchange contract is the core of the ZenLink Dex Protocol. It implements the following interfaces:
- Initializing token trading pairs.
- Token swaps.
- Adding/extracting liquidity.
- Defining the liquidity constant function used throughout the protocol.
The Factory contract can be used to create exchange contracts for any ERC20 token that does not already have one. It also functions as a registry of ERC20 tokens that have been added to the system, and the exchange with which they are associated.
The whole compilation and deployment procedure is relatively simple and easy.
Firstly, people download the repo to their local machine by using
git clone [firstname.lastname@example.org](mailto:email@example.com):zenlinkpro/zenlink-dex-contract.git
Secondly, we recommend using docker to run a substrate node and compile the contracts.
docker run -p 9944:9944 zenlinkpro/dex:zenlink-canvas-nodecd erc20sudo docker run --rm -v "$PWD":/build -w /build zenlinkpro/dex:zenlink_contract_builder cargo +nightly-2020-10-06-x86_64-unknown-linux-gnu contract buildsudo docker run --rm -v "$PWD":/build -w /build zenlinkpro/dex:zenlink_contract_builder cargo +nightly-2020-10-06-x86_64-unknown-linux-gnu contract generate-metadata
Then we can find the erc20.wasm and metadata.json in the target folder.
Because the factory project depends on the exchange project, the command must be run in the factory project parent folder.
sudo docker run --rm -v "$PWD":/build -w /build/factory zenlinkpro/dex:zenlink_contract_builder cargo +nightly-2020-10-06-x86_64-unknown-linux-gnu contract build
Thirdly, people need to upload and deploy the .wasm files to the blockchain by using canvas-ui.
Finally, people can test the dex functionalities, such as initialize_factory, create_exchange, token swap, etc.
For more setup and test details, please visit Zenlink DEX Smart Contract Repo.
About Zenlink and the Web3 Foundation Grant
The Web3 Foundation Grants Program seeks to allocate resources to projects that are beneficial to the advancement of the Polkadot ecosystem. It has supported many famous ecosystem projects, such as Acala, PolkaWorld, PolkaWallet, etc.
We are honored that the Zenlink DEX Smart Contract implementation is also a recipient of a Web3 Grant. Zenlink is currently building on Substrate, using its technology to share pooled liquidity across digital assets and parachains in the Polkadot network.