Zenlink DEX Smart Contract is Open Source

Zenlink
Zenlink
Dec 15, 2020 · 3 min read

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.

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.

Workshop

The whole compilation and deployment procedure is relatively simple and easy.

Firstly, people download the repo to their local machine by using

git clone [git@github.com](mailto:git@github.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.

https://paritytech.github.io/canvas-ui/#/upload

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.

Learn more about Web3 Foundation by visiting their website. To learn more about Web3 Foundation’s latest developments, join them on Reddit or Twitter.

Zenlink Foundation Ltd.

A cross-chain DEX network based on Polkadot.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store