Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

241 update spacewalk dependencies and configure the lp token for use with the zenlink pallet #246

Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
302bf39
Implemented initial draft code for the conversion between ZenlinkAsse…
adelarja Jun 14, 2023
9988ad4
added PARA_CHAIN_ID constant
adelarja Jun 14, 2023
d9f5b03
Fixed shifting issue
adelarja Jun 14, 2023
c003c2b
Improved format and added hard-coded values for Stellar assets
adelarja Jun 14, 2023
fe5b292
Added BRL, TZS and USDC issuers
adelarja Jun 15, 2023
94282f4
Added StellarNative
adelarja Jun 15, 2023
81fa633
Added Err(()) return to match statement
adelarja Jun 15, 2023
6f1ff80
Fixed type issues
adelarja Jun 15, 2023
b63df99
Added TryFrom for WrappedCurrency to CurrencyId. Improved format
adelarja Jun 16, 2023
82cb978
changed 'zenlink_id_to_currency_id' function
adelarja Jun 16, 2023
aaa621d
Added unit tests for the conversion functions
adelarja Jun 16, 2023
73c7e81
Abstracted methods into separate file
adelarja Jun 19, 2023
5bf9a43
Changed module
adelarja Jun 19, 2023
a5564d3
Refactored runtime configuration of zenlink
adelarja Jun 19, 2023
19abbde
Improved code format. Added unit tests
adelarja Jun 20, 2023
e573c15
Update Cargo.toml
adelarja Jun 20, 2023
d8c0655
Update Cargo.toml
adelarja Jun 20, 2023
901d3c4
Extended statement to check also the issuer in the match of conversio…
adelarja Jun 21, 2023
2a53682
Merge branch '241-update-spacewalk-dependencies-and-configure-the-lp-…
adelarja Jun 21, 2023
db97794
Renamed test varible and test name.
adelarja Jun 21, 2023
1239e98
Refactored zenlink_id_to_currency_id function. Now it checks that the…
adelarja Jun 21, 2023
c8ce574
Added unit test for Err result when converting between zenlink asset …
adelarja Jun 21, 2023
c34634e
Asset type is checked now. renamed variables
adelarja Jul 6, 2023
7f45510
Added stellar module with issuer definitions.
adelarja Jul 6, 2023
ee098ab
Changed spacewalk dependency commit hash
adelarja Jul 6, 2023
b4733f5
Fixed spacewalk dependencies in node
adelarja Jul 7, 2023
6d3df88
Implemented GenerateLpAssetId to change the generate_lp_asset_id func…
adelarja Jul 10, 2023
b58616f
Refactored zenlink stuffs in runtime common. Added zenlink configurat…
adelarja Jul 11, 2023
8aa58dd
Merge branch 'main' of github.com:pendulum-chain/pendulum into 241-up…
adelarja Jul 14, 2023
190f2e8
Added zenlink protocol runtime api configuration for pendulum.
adelarja Jul 14, 2023
51ff2d6
Fixed primite issue
adelarja Jul 14, 2023
6817a12
Deleted unnecessary brackets
adelarja Jul 14, 2023
c57c37c
Updated spacewalk dependencies hash
adelarja Jul 14, 2023
6cf7d2e
Added comment with USDC issuer address.
adelarja Jul 17, 2023
e0717ef
Added comment with BRL issuer address.
adelarja Jul 17, 2023
55a8a15
Added comment with TZS issuer address.
adelarja Jul 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ codec = { package = "parity-scale-codec", version = "3.0.0" }
serde = { version = "1.0.145", features = ["derive"] }
jsonrpsee = { version = "0.16.2", features = ["server"] }

module-issue-rpc = { git = "https://github.com/pendulum-chain/spacewalk", rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae"}
module-oracle-rpc = { git = "https://github.com/pendulum-chain/spacewalk", rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae"}
module-redeem-rpc = { git = "https://github.com/pendulum-chain/spacewalk", rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae"}
module-replace-rpc = { git = "https://github.com/pendulum-chain/spacewalk", rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae"}
module-vault-registry-rpc = { git = "https://github.com/pendulum-chain/spacewalk", rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae"}
module-issue-rpc = { git = "https://github.com/pendulum-chain/spacewalk", rev = "d1188915d10735f5ff32f0301705f0aa091cd905"}
module-oracle-rpc = { git = "https://github.com/pendulum-chain/spacewalk", rev = "d1188915d10735f5ff32f0301705f0aa091cd905"}
module-redeem-rpc = { git = "https://github.com/pendulum-chain/spacewalk", rev = "d1188915d10735f5ff32f0301705f0aa091cd905"}
module-replace-rpc = { git = "https://github.com/pendulum-chain/spacewalk", rev = "d1188915d10735f5ff32f0301705f0aa091cd905"}
module-vault-registry-rpc = { git = "https://github.com/pendulum-chain/spacewalk", rev = "d1188915d10735f5ff32f0301705f0aa091cd905"}

spacewalk-primitives = { git = "https://github.com/pendulum-chain/spacewalk", rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae"}
spacewalk-primitives = { git = "https://github.com/pendulum-chain/spacewalk", rev = "d1188915d10735f5ff32f0301705f0aa091cd905"}

# Local
amplitude-runtime = {path = "../runtime/amplitude"}
Expand Down
36 changes: 18 additions & 18 deletions runtime/amplitude/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ smallvec = "1.9.0"
runtime-common = { path = "../common", default-features = false }

# Custom libraries for Spacewalk
currency = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
security = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
staking = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
oracle = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
stellar-relay = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
reward = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
fee = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
vault-registry = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
redeem = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
issue = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
nomination = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
replace = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
spacewalk-primitives = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
module-issue-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
module-oracle-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
module-redeem-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
module-replace-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
module-vault-registry-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae" }
currency = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
security = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
staking = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
oracle = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
stellar-relay = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
reward = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
fee = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
vault-registry = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
redeem = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
issue = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
nomination = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
replace = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
spacewalk-primitives = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
module-issue-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
module-oracle-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
module-redeem-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
module-replace-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }
module-vault-registry-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63" }

# Substrate
frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true, branch = "polkadot-v0.9.40" }
Expand Down
5 changes: 4 additions & 1 deletion runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ scale-info = {version = "2.1.1", default-features = false, features = ["derive"]
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.40" }
sp-consensus-aura = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.40" }
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.40" }
spacewalk-primitives = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae"}

spacewalk-primitives = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "daf395f6201a834629f6abad77e062d8aa554f63"}
dia-oracle = { git = "https://github.com/pendulum-chain/oracle-pallet", default-features = false, branch = "polkadot-v0.9.40" }
zenlink-protocol = { git = "https://github.com/pendulum-chain/Zenlink-DEX-Module", default-features = false, branch = "polkadot-v0.9.40-protocol" }

[features]
default = [
Expand All @@ -33,6 +35,7 @@ std = [
"parity-scale-codec/std",
"scale-info/std",
"dia-oracle/std",
"zenlink-protocol/std",
]

runtime-benchmarks = [
Expand Down
2 changes: 2 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use sp_runtime::{
};

pub mod chain_ext;
pub mod stellar;
pub mod zenlink;

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = MultiSignature;
Expand Down
14 changes: 14 additions & 0 deletions runtime/common/src/stellar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub const USDC_ISSUER: [u8; 32] = [
adelarja marked this conversation as resolved.
Show resolved Hide resolved
59, 153, 17, 56, 14, 254, 152, 139, 160, 168, 144, 14, 177, 207, 228, 79, 54, 111, 125, 190,
148, 107, 237, 7, 114, 64, 247, 246, 36, 223, 21, 197,
];

pub const BRL_ISSUER: [u8; 32] = [
adelarja marked this conversation as resolved.
Show resolved Hide resolved
234, 172, 104, 212, 208, 227, 123, 76, 36, 194, 83, 105, 22, 232, 48, 115, 95, 3, 45, 13, 107,
42, 28, 143, 202, 59, 197, 162, 94, 8, 62, 58,
];

pub const TZS_ISSUER: [u8; 32] = [
adelarja marked this conversation as resolved.
Show resolved Hide resolved
52, 201, 75, 42, 75, 169, 232, 181, 123, 34, 84, 125, 203, 179, 15, 68, 60, 76, 176, 45, 163,
130, 154, 137, 170, 27, 212, 120, 14, 68, 102, 186,
];
267 changes: 267 additions & 0 deletions runtime/common/src/zenlink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
use spacewalk_primitives::{Asset, CurrencyId};
use crate::stellar::{USDC_ISSUER, TZS_ISSUER, BRL_ISSUER};

use zenlink_protocol::{LOCAL, NATIVE};
pub type ZenlinkAssetId = zenlink_protocol::AssetId;

fn discriminant(currency: &CurrencyId) -> u8 {
match currency {
CurrencyId::Native => 0,
CurrencyId::XCM(_) => 1,
CurrencyId::Stellar(_) => 2,
CurrencyId::ZenlinkLPToken(_, _, _, _) => 6,
adelarja marked this conversation as resolved.
Show resolved Hide resolved
}
}

pub fn zenlink_id_to_currency_id(asset_id: ZenlinkAssetId, parachain_id: u32) -> Result<CurrencyId, ()> {
adelarja marked this conversation as resolved.
Show resolved Hide resolved
if asset_id.chain_id != parachain_id {
return Err(())
}

let index = asset_id.asset_index;
let asset_type = asset_id.asset_type;
let disc = ((index & 0x0000_0000_0000_ff00) >> 8) as u8;
let symbol = (index & 0x0000_0000_0000_00ff) as u8;
match (disc, asset_type) {
(0, NATIVE) => Ok(CurrencyId::Native),
(1, LOCAL) => Ok(CurrencyId::XCM(symbol)),
(2, LOCAL) => match symbol {
0 => Ok(CurrencyId::Stellar(Asset::StellarNative)),
1 => Ok(CurrencyId::Stellar(Asset::AlphaNum4 { code: *b"USDC", issuer: USDC_ISSUER })),
2 => Ok(CurrencyId::Stellar(Asset::AlphaNum4 { code: *b"TZS\0", issuer: TZS_ISSUER })),
3 => Ok(CurrencyId::Stellar(Asset::AlphaNum4 { code: *b"BRL\0", issuer: BRL_ISSUER })),
_ => return Err(()),
},
(6, LOCAL) => {
let token1_id = ((index & 0x0000_0000_00FF_0000) >> 16) as u8;
let token1_type = ((index & 0x0000_0000_FF00_0000) >> 24) as u8;

let token2_id = ((index & 0x0000_00FF_0000_0000) >> 32) as u8;
let token2_type = ((index & 0x0000_FF00_0000_0000) >> 40) as u8;

Ok(CurrencyId::ZenlinkLPToken(token1_id, token1_type, token2_id, token2_type))
},
_ => Err(()),
}
}

pub fn currency_id_to_zenlink_id(
currency_id: CurrencyId,
parachain_id: u32,
) -> Result<ZenlinkAssetId, ()> {
let disc = discriminant(&currency_id) as u64;
adelarja marked this conversation as resolved.
Show resolved Hide resolved
match currency_id {
CurrencyId::Native =>
Ok(ZenlinkAssetId { chain_id: parachain_id, asset_type: NATIVE, asset_index: 0 as u64 }),
adelarja marked this conversation as resolved.
Show resolved Hide resolved
CurrencyId::XCM(token_id) => Ok(ZenlinkAssetId {
chain_id: parachain_id,
asset_type: LOCAL,
asset_index: ((disc << 8) + token_id as u64) as u64,
}),
CurrencyId::Stellar(asset) => {
let _id = match asset {
Asset::StellarNative => 0u64,
Asset::AlphaNum4 { code, issuer } => match (&code, &issuer) {
(b"USDC", &USDC_ISSUER) => 1u64,
(b"TZS\0", &TZS_ISSUER) => 2u64,
(b"BRL\0", &BRL_ISSUER) => 3u64,
_ => return Err(()),
},
_ => return Err(()),
};
Ok(ZenlinkAssetId {
chain_id: parachain_id,
asset_type: LOCAL,
asset_index: ((disc << 8) + _id) as u64,
})
},
CurrencyId::ZenlinkLPToken(token1_id, token1_type, token2_id, token2_type) => {
let index = ((disc as u64) << 8) +
((token1_id as u64) << 16) +
((token1_type as u64) << 24) +
((token2_id as u64) << 32) +
((token2_type as u64) << 40);
Ok(ZenlinkAssetId { chain_id: parachain_id, asset_type: LOCAL, asset_index: index })
},
}
}

#[cfg(test)]
mod zenlink_tests {
use super::*;

#[test]
fn convert_zenlink_native_to_native_currency() {
// Native ZenlinkAsset index = 0x0000_0000_0000_0000
let fake_zenlink_asset =
ZenlinkAssetId { chain_id: 1000u32, asset_type: NATIVE, asset_index: 0 as u64 };
let currency: Result<CurrencyId, _> = zenlink_id_to_currency_id(fake_zenlink_asset, 1000u32);
assert_eq!(currency, Ok(CurrencyId::Native));
}

#[test]
fn convert_zenlink_xcm_to_xcm_currency() {
// XCM(0) ZenlinkAsset index = 0x0000_0000_0000_0100
let index = 0x0100 as u64;
let fake_zenlink_asset =
ZenlinkAssetId { chain_id: 1000u32, asset_type: LOCAL, asset_index: index };
let currency: Result<CurrencyId, _> = zenlink_id_to_currency_id(fake_zenlink_asset, 1000u32);
assert_eq!(currency, Ok(CurrencyId::XCM(0)));
}

fn get_stellar_asset(selector: u8) -> spacewalk_primitives::CurrencyId {
match selector {
0 => CurrencyId::Stellar(Asset::StellarNative),
1 => CurrencyId::Stellar(Asset::AlphaNum4 { code: *b"USDC", issuer: USDC_ISSUER }),
2 => CurrencyId::Stellar(Asset::AlphaNum4 { code: *b"TZS\0", issuer: TZS_ISSUER }),
3 => CurrencyId::Stellar(Asset::AlphaNum4 { code: *b"BRL\0", issuer: BRL_ISSUER }),
_ => CurrencyId::Stellar(Asset::StellarNative),
}
}

#[test]
fn convert_zenlink_stellar_to_stellar_currency() {
// Stellar Native ZenlinkAsset index = 0x0000_0000_0000_0200
let stellar_native_index = 0x0200 as u64;
let fake_zenlink_asset = ZenlinkAssetId {
chain_id: 1000u32,
asset_type: LOCAL,
asset_index: stellar_native_index,
};
let currency: Result<CurrencyId, _> = zenlink_id_to_currency_id(fake_zenlink_asset, 1000u32);
assert_eq!(currency, Ok(get_stellar_asset(0u8)));

// Stellar USDC ZenlinkAsset index = 0x0000_0000_0000_0201
let usdc_index = 0x0201 as u64;
let fake_zenlink_asset =
ZenlinkAssetId { chain_id: 1000u32, asset_type: LOCAL, asset_index: usdc_index };
let currency: Result<CurrencyId, _> = zenlink_id_to_currency_id(fake_zenlink_asset, 1000u32);
assert_eq!(currency, Ok(get_stellar_asset(1u8)));

// Stellar TZS ZenlinkAsset index = 0x0000_0000_0000_0202
let tzs_index = 0x0202 as u64;
let fake_zenlink_asset =
ZenlinkAssetId { chain_id: 1000u32, asset_type: LOCAL, asset_index: tzs_index };
let currency: Result<CurrencyId, _> = zenlink_id_to_currency_id(fake_zenlink_asset, 1000u32);
assert_eq!(currency, Ok(get_stellar_asset(2u8)));

// Stellar BRL ZenlinkAsset index = 0x0000_0000_0000_0203
let brl_index = 0x0203 as u64;
let fake_zenlink_asset =
ZenlinkAssetId { chain_id: 1000u32, asset_type: LOCAL, asset_index: brl_index };
let currency: Result<CurrencyId, _> = zenlink_id_to_currency_id(fake_zenlink_asset, 1000u32);
assert_eq!(currency, Ok(get_stellar_asset(3u8)));
}

#[test]
fn convert_zenlink_lp_token_to_lp_token_currency() {
// Native and XCM(0) LP token Zenlink index = 0x0000_0100_0000_0600
let native_xcm_lp_token_index = 0x0000_0100_0000_0600 as u64;
let fake_zenlink_asset = ZenlinkAssetId {
chain_id: 1000u32,
asset_type: LOCAL,
asset_index: native_xcm_lp_token_index,
};
let currency: Result<CurrencyId, _> = zenlink_id_to_currency_id(fake_zenlink_asset, 1000u32);
let expected_currency: CurrencyId = CurrencyId::ZenlinkLPToken(0, 0, 0, 1);
assert_eq!(currency, Ok(expected_currency));

// XCM(0) and XCM(1) LP token Zenlink index = 0x0000_0101_0100_0600
let xcm0_xcm1_lp_token_index = 0x0000_0101_0100_0600 as u64;
let fake_zenlink_asset = ZenlinkAssetId {
chain_id: 1000u32,
asset_type: LOCAL,
asset_index: xcm0_xcm1_lp_token_index,
};
let currency: Result<CurrencyId, _> = zenlink_id_to_currency_id(fake_zenlink_asset, 1000u32);
let expected_currency: CurrencyId = CurrencyId::ZenlinkLPToken(0, 1, 1, 1);
assert_eq!(currency, Ok(expected_currency));

// XCM(0) and Stellar Native LP Token Zenlink index = 0x0000_0200_0100_0600
let xcm0_stellar_native_lp_token_index = 0x0000_0200_0100_0600 as u64;
let fake_zenlink_asset = ZenlinkAssetId {
chain_id: 1000u32,
asset_type: LOCAL,
asset_index: xcm0_stellar_native_lp_token_index,
};
let currency: Result<CurrencyId, _> = zenlink_id_to_currency_id(fake_zenlink_asset, 1000u32);
let expected_currency: CurrencyId = CurrencyId::ZenlinkLPToken(0, 1, 0, 2);
assert_eq!(currency, Ok(expected_currency));

// XCM(0) and Stellar USDC LP Token Zenlink index = 0x0000_0201_0100_0600
let xcm0_stellar_usdc_lp_token_index = 0x0000_0201_0100_0600 as u64;
let fake_zenlink_asset = ZenlinkAssetId {
chain_id: 1000u32,
asset_type: LOCAL,
asset_index: xcm0_stellar_usdc_lp_token_index,
};
let currency: Result<CurrencyId, _> = zenlink_id_to_currency_id(fake_zenlink_asset, 1000u32);
let expected_currency: CurrencyId = CurrencyId::ZenlinkLPToken(0, 1, 1, 2);
assert_eq!(currency, Ok(expected_currency));

// Stellar Native and Stellar USDC LP Token Zenlink index = 0x0000_0201_0200_0600
let stellar_native_stellar_usdc_lp_token_index = 0x0000_0201_0200_0600 as u64;
let fake_zenlink_asset = ZenlinkAssetId {
chain_id: 1000u32,
asset_type: LOCAL,
asset_index: stellar_native_stellar_usdc_lp_token_index,
};
let currency: Result<CurrencyId, _> = zenlink_id_to_currency_id(fake_zenlink_asset, 1000u32);
let expected_currency: CurrencyId = CurrencyId::ZenlinkLPToken(0, 2, 1, 2);
assert_eq!(currency, Ok(expected_currency));
}

#[test]
fn convert_fake_zenlink_native_to_currency_id_error() {
// Native ZenlinkAsset index = 0x0000_0000_0000_0000
let fake_zenlink_asset =
ZenlinkAssetId { chain_id: 1000u32, asset_type: LOCAL, asset_index: 0 as u64 };
let currency: Result<CurrencyId, _> = zenlink_id_to_currency_id(fake_zenlink_asset, 1000u32);
assert_eq!(currency, Err(()));
}

#[test]
fn convert_zenlink_id_to_currency_id_error() {
let fake_zenlink_asset = ZenlinkAssetId {
chain_id: 1000u32,
asset_type: LOCAL,
asset_index: 0u64,
};
// We pass a parachain_id different than the asset chain_id
assert_eq!(zenlink_id_to_currency_id(fake_zenlink_asset, 1001u32), Err(()));
}

#[test]
fn convert_native_currency_to_zenlink_native() {
let fake_currency_id = CurrencyId::Native;
let expected_zenlink_asset =
ZenlinkAssetId { chain_id: 1000u32, asset_type: NATIVE, asset_index: 0u64 };
assert_eq!(currency_id_to_zenlink_id(fake_currency_id, 1000), Ok(expected_zenlink_asset));
}

#[test]
fn convert_xcm_currency_to_zenlink_xcm() {
let fake_currency_id = CurrencyId::XCM(0);
let expected_zenlink_asset =
ZenlinkAssetId { chain_id: 1000u32, asset_type: LOCAL, asset_index: 0x0100 as u64 };
assert_eq!(currency_id_to_zenlink_id(fake_currency_id, 1000), Ok(expected_zenlink_asset));
}

#[test]
fn convert_stellar_currency_to_stellar_zenlink() {
let stellar_assets_indexes: [u64; 4] = [0x0200u64, 0x0201u64, 0x0202u64, 0x0203u64];

for i in 0..=3usize {
let fake_currency_id = get_stellar_asset(i as u8);
let expected_zenlink_asset = ZenlinkAssetId {
chain_id: 1000u32,
asset_type: LOCAL,
asset_index: stellar_assets_indexes[i],
};
assert_eq!(
currency_id_to_zenlink_id(fake_currency_id, 1000),
Ok(expected_zenlink_asset)
);
}
}
}
Loading
Loading