-
Notifications
You must be signed in to change notification settings - Fork 14
241 update spacewalk dependencies and configure the lp token for use with the zenlink pallet #246
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
Merged
ebma
merged 36 commits into
main
from
241-update-spacewalk-dependencies-and-configure-the-lp-token-for-use-with-the-zenlink-pallet
Jul 17, 2023
Merged
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 9988ad4
added PARA_CHAIN_ID constant
adelarja d9f5b03
Fixed shifting issue
adelarja c003c2b
Improved format and added hard-coded values for Stellar assets
adelarja fe5b292
Added BRL, TZS and USDC issuers
adelarja 94282f4
Added StellarNative
adelarja 81fa633
Added Err(()) return to match statement
adelarja 6f1ff80
Fixed type issues
adelarja b63df99
Added TryFrom for WrappedCurrency to CurrencyId. Improved format
adelarja 82cb978
changed 'zenlink_id_to_currency_id' function
adelarja aaa621d
Added unit tests for the conversion functions
adelarja 73c7e81
Abstracted methods into separate file
adelarja 5bf9a43
Changed module
adelarja a5564d3
Refactored runtime configuration of zenlink
adelarja 19abbde
Improved code format. Added unit tests
adelarja e573c15
Update Cargo.toml
adelarja d8c0655
Update Cargo.toml
adelarja 901d3c4
Extended statement to check also the issuer in the match of conversio…
adelarja 2a53682
Merge branch '241-update-spacewalk-dependencies-and-configure-the-lp-…
adelarja db97794
Renamed test varible and test name.
adelarja 1239e98
Refactored zenlink_id_to_currency_id function. Now it checks that the…
adelarja c8ce574
Added unit test for Err result when converting between zenlink asset …
adelarja c34634e
Asset type is checked now. renamed variables
adelarja 7f45510
Added stellar module with issuer definitions.
adelarja ee098ab
Changed spacewalk dependency commit hash
adelarja b4733f5
Fixed spacewalk dependencies in node
adelarja 6d3df88
Implemented GenerateLpAssetId to change the generate_lp_asset_id func…
adelarja b58616f
Refactored zenlink stuffs in runtime common. Added zenlink configurat…
adelarja 8aa58dd
Merge branch 'main' of github.com:pendulum-chain/pendulum into 241-up…
adelarja 190f2e8
Added zenlink protocol runtime api configuration for pendulum.
adelarja 51ff2d6
Fixed primite issue
adelarja 6817a12
Deleted unnecessary brackets
adelarja c57c37c
Updated spacewalk dependencies hash
adelarja 6cf7d2e
Added comment with USDC issuer address.
adelarja e0717ef
Added comment with BRL issuer address.
adelarja 55a8a15
Added comment with TZS issuer address.
adelarja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| pub const USDC_ISSUER: [u8; 32] = [ | ||
| 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, | ||
| ]; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
Outdated
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.
Outdated
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(¤cy_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) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.