Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
icodezjb committed Mar 17, 2021
2 parents e3bd5fe + f222c0c commit b976b47
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 93 deletions.
256 changes: 163 additions & 93 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ It should at least work until the following commits

## Example
The following shows how to deploy the ZenLink Dex Module using Rococo as an example.
The current stable version of ZenLink Dex Module is based on Cumulus rococo-v1@48558 a2bf666eba6be5b3a3b0d9ce9459bc4a714.

### Clone cumulus code

```
git clone https://github.com/paritytech/cumulus.git
cd cumulus
git checkout rococo-v1
git checkout 48558a2bf666eba6be5b3a3b0d9ce9459bc4a714
git checkout 24b1ee6bd1d96f255889f167e59ef9c9399a6305
```

### Clone Zenlink DEX Module code
Expand All @@ -36,8 +35,9 @@ git clone [email protected]:zenlinkpro/Zenlink-DEX-Module.git

### Add Zenlink DEX Module in runtime
1. Copy the Zenlink Dex Module into the Cumulus code folder
2. Modify the project file
2. Config runtime
```
**1. Modify the project file**
// runtime/Cargo.toml
[package]
Expand Down Expand Up @@ -65,11 +65,9 @@ std = [
...
zenlink-protocol-runtime-api = { path = "../zenlink-protocol/rpc/runtime-api" }
zenlink-protocol-rpc = { path = "../zenlink-protocol/rpc" }
...
/// Add RPC API to node
# node/src/service.rs
**2.Add RPC API to node**
//node/src/service.rs
...
pub async fn start_node(
parachain_config: Configuration,
Expand Down Expand Up @@ -98,45 +96,56 @@ pub async fn start_node(
}
...
// Add Zenlink DEX Module to runtime
// runtime/src/lib.rs
**3 Add Zenlink DEX Module to runtime.**
// We can refer to the `example` folder
// runtime/src/zenlink.rs
...
// use Zenlink DEX Module
use zenlink_protocol::{
AccountId32Aliases, AssetId, FilterAssetLocation, Junction, LocationInverter, MultiAsset,
MultiLocation, NetworkId, Origin as ZenlinkOrigin, PairInfo, ParentIsDefault,
RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SovereignSignedViaLocation, TokenBalance, Transactor, XcmCfg,
XcmExecutor,
AccountId32Aliases, Junction, LocationInverter, MultiLocation, NetworkId, OperationalAsset,
Origin as ZenlinkOrigin, ParaChainWhiteList, ParentIsDefault, RelayChainAsNative, Sibling,
SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,
SovereignSignedViaLocation, TokenBalance, Transactor, XcmCfg, XcmExecutor,
};
...
// Zenlink DEX Module instantiation
impl zenlink_protocol::Config for Runtime {
type Event = Event;
type TokenBalance = TokenBalance;
type XcmExecutor = XcmExecutor<XcmConfig>;
type UpwardMessageSender = MessageBroker;
type HrmpMessageSender = MessageBroker;
type NativeCurrency = Balances;
type AccountIdConverter = LocationConverter;
type AccountId32Converter = AccountId32Converter;
type ParaId = ParachainInfo;
type ModuleId = DEXModuleId;
type Event = Event;
type XcmExecutor = XcmExecutor<XcmConfig>;
type UpwardMessageSender = ParachainSystem;
type HrmpMessageSender = ParachainSystem;
type NativeCurrency = Balances;
type AccountIdConverter = LocationConverter;
type AccountId32Converter = AccountId32Converter;
type ParaId = ParachainInfo;
type ModuleId = DEXModuleId;
type TargetChains = SiblingParachains;
// If you want the assets generated by a module to realize cross-chain circulation in the zenlink protocol.
// Then this module must implement the OperationalAsset trait.
type OperationalAsset = OtherAssets;
}
...
// Set XCM message processor as Zenlink DEX Module
impl cumulus_message_broker::Config for Runtime {
type DownwardMessageHandlers = ZenlinkProtocol;
type HrmpMessageHandlers = ZenlinkProtocol;
**4. Set Balance module index.**
// Change the `NATIVE_CURRENCY_MODULE_INDEX` value in zenlink-protocol/primitives.rs
// `Make NATIVE_CURRENCY_MODULE_INDEX` equal to the index of the balances module
// In this example NATIVE_CURRENCY_MODULE_INDEX == 2
**5.Set XCM message processor as Zenlink DEX Module**
impl cumulus_pallet_parachain_system::Config for Runtime {
type Event = Event;
type OnValidationData = ();
type SelfParaId = ParachainInfo;
type DownwardMessageHandlers = ZenlinkProtocol;
type HrmpMessageHandlers = ZenlinkProtocol;
}
...
// Add Zenlink DEX Module to runtime
**5.Add Zenlink DEX Module to runtime**
// runtime/src/zenlink.rs
construct_runtime! {
pub enum Runtime where
Block = Block,
Expand All @@ -145,51 +154,77 @@ construct_runtime! {
{
...
// ASSETS_PALLET_INDEX
ZenlinkProtocol: zenlink_protocol::{Module, Origin, Call, Storage, Event<T>} = 10,
ZenlinkProtocol: zenlink_protocol::{Module, Origin, Call, Storage, Event<T>},
...
}
}
...
**6.Add Zenlink rpc**
impl_runtime_apis! {
...
// RPC implementation
impl zenlink_protocol_runtime_api::ZenlinkProtocolApi<Block, AccountId, TokenBalance> for Runtime {
fn get_assets() -> Vec<AssetId> {
ZenlinkProtocol::assets_list()
}
fn get_balance(
asset_id: AssetId,
owner: AccountId
) -> TokenBalance {
ZenlinkProtocol::asset_balance_of(&asset_id, &owner)
}
fn get_all_pairs() -> Vec<PairInfo<AccountId, TokenBalance>> {
ZenlinkProtocol::get_all_pairs()
}
fn get_owner_pairs(
owner: AccountId
) -> Vec<PairInfo<AccountId, TokenBalance>> {
ZenlinkProtocol::get_owner_pairs(&owner)
}
//buy amount token price
fn get_amount_in_price(
path: Vec<AssetId>
) -> TokenBalance {
ZenlinkProtocol::get_in_price(path)
}
//sell amount token price
fn get_amount_out_price(
path: Vec<AssetId>
) -> TokenBalance {
ZenlinkProtocol::get_out_price(path)
}
}
impl zenlink_protocol_runtime_api::ZenlinkProtocolApi<Block, AccountId> for Runtime {
fn get_assets() -> Vec<AssetId> {
ZenlinkProtocol::assets_list()
}
fn get_balance(
asset_id: AssetId,
owner: AccountId
) -> TokenBalance {
ZenlinkProtocol::multi_asset_balance_of(&asset_id, &owner)
}
fn get_sovereigns_info(
asset_id: AssetId
) -> Vec<(u32, AccountId, TokenBalance)> {
ZenlinkProtocol::get_sovereigns_info(&asset_id)
}
fn get_all_pairs() -> Vec<PairInfo<AccountId, TokenBalance>> {
ZenlinkProtocol::get_all_pairs()
}
fn get_owner_pairs(
owner: AccountId
) -> Vec<PairInfo<AccountId, TokenBalance>> {
ZenlinkProtocol::get_owner_pairs(&owner)
}
//buy amount token price
fn get_amount_in_price(
supply: TokenBalance,
path: Vec<AssetId>
) -> TokenBalance {
ZenlinkProtocol::desired_in_amount(supply, path)
}
//sell amount token price
fn get_amount_out_price(
supply: TokenBalance,
path: Vec<AssetId>
) -> TokenBalance {
ZenlinkProtocol::supply_out_amount(supply, path)
}
fn get_estimate_lptoken(
token_0: AssetId,
token_1: AssetId,
amount_0_desired: TokenBalance,
amount_1_desired: TokenBalance,
amount_0_min: TokenBalance,
amount_1_min: TokenBalance,
) -> TokenBalance{
ZenlinkProtocol::get_estimate_lptoken(
token_0,
token_1,
amount_0_desired,
amount_1_desired,
amount_0_min,
amount_1_min)
}
}
}
...
```
Expand Down Expand Up @@ -281,40 +316,75 @@ Transaction type

```
{
"PairId": "u32",
"Pair": {
"token_0": "AssetId",
"token_1": "AssetId",
"account": "AccountId",
"total_liquidity": "TokenBalance"
},
"PairInfo": {
"token_0": "AssetId",
"token_1": "AssetId",
"account": "AccountId",
"total_liquidity": "TokenBalance",
"holding_liquidity": "TokenBalance",
"reserve_0": "TokenBalance",
"reserve_1": "TokenBalance"
},
"AssetId": {
"_enum": {
"NativeCurrency": null,
"ParaCurrency": "u32"
"Address": "AccountId",
"LookupSource": "AccountId",
"RefCount": "u32",
"AccountInfo": "AccountInfoWithRefCount",
"PairId": "u32",
"Pair": {
"token_0": "AssetId",
"token_1": "AssetId",
"account": "AccountId",
"total_liquidity": "TokenBalance",
"lp_asset_id": "AssetId"
},
"PairInfo": {
"token_0": "AssetId",
"token_1": "AssetId",
"account": "AccountId",
"total_liquidity": "TokenBalance",
"holding_liquidity": "TokenBalance",
"reserve_0": "TokenBalance",
"reserve_1": "TokenBalance",
"lp_asset_id": "AssetId"
},
"AssetId": {
"chain_id": "u32",
"module_index": "u8",
"asset_index": "u32"
},
"TokenId": "u32",
"AssetProperty":{
"_enum": {
"FOREIGN": null,
"LP": "LpProperty"
}
},
"TokenBalance": "u128"
},
"LpProperty": {
"token_0": "AssetId",
"token_1": "AssetId"
},
"TokenBalance": "u128"
}
```

Switch to 'Settings' -> 'Developer', input the above json, then save.

![Setting Type](./images/setting.png)

### Add liquidity
There is a trading pair on Parachain300. Call the following method.
![Add Liquidity](./images/add_liquidity.png)
### Cross chain transfer
In this example, the Balances module index equal 2 and it just has one asset.
So the AssetId of native currency is (200,2,0).
It means the first asset of the second module on parachain 200.
Now we transfer it to parachain 300.

![Cross_transfer](./images/cross_chain_transfer.png)
We can see the event in parachain 300.
It means that the zenlink protocol first issued an asset, and then minted 1,000,000.

![Cross_transfer](./images/cross_transfer_event.png)

We can see the result in zenlink protocol storage.

![Cross_transfer](./images/cross_transfer_result.png)

### Create Pair
Now we have two assets in parachain 300.
So we can create a pair.
![Create_Pair](./images/create_pair.png)

### Add liquidity
![Create_Pair](./images/add_liquidity.png)

### Swap
![Swap](./images/swap.png)
![Swap](./images/swap.png)
Binary file modified images/add_liquidity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/create_pair.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/cross_chain_transfer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/cross_transfer_event.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/cross_transfer_result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/swap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b976b47

Please sign in to comment.