Skip to content

Commit

Permalink
241 update spacewalk dependencies and configure the lp token for use …
Browse files Browse the repository at this point in the history
…with the zenlink pallet (#246)

* Implemented initial draft code for the conversion between ZenlinkAssetId and Spacewalk assets.

* added PARA_CHAIN_ID constant

* Fixed shifting issue

* Improved format and added hard-coded values for Stellar assets

* Added BRL, TZS and USDC issuers

* Added StellarNative

* Added Err(()) return to match statement

* Fixed type issues

* Added TryFrom for WrappedCurrency to CurrencyId. Improved format

* changed 'zenlink_id_to_currency_id' function

* Added unit tests for the conversion functions

* Abstracted methods into separate file

* Changed module

* Refactored runtime configuration of zenlink

* Improved code format. Added unit tests

* Update Cargo.toml

deleted unnecessary dependencies.

* Update Cargo.toml

Updated spacewalk dependencies.

* Extended statement to check also the issuer in the match of conversion function for stellar assets.

* Renamed test varible and test name.

* Refactored zenlink_id_to_currency_id function. Now it checks that the asset chain_id and the parachain_id are the same.

* Added unit test for Err result when converting between zenlink asset and spacewalk asset.

* Added stellar module with issuer definitions.

* Changed spacewalk dependency commit hash

* Fixed spacewalk dependencies in node

* Implemented GenerateLpAssetId to change the generate_lp_asset_id function, allowing us to use a discriminant of 3 for the Zenlink LP token.

* Refactored zenlink stuffs in runtime common. Added zenlink configuration to amplitude.

* Added zenlink protocol runtime api configuration for pendulum.

Signed-off-by: adelarja <[email protected]>

* Fixed primite issue

* Deleted unnecessary brackets

* Updated spacewalk dependencies hash

* Added comment with USDC issuer address.

Co-authored-by: Marcel Ebert <[email protected]>

* Added comment with BRL issuer address.

Co-authored-by: Marcel Ebert <[email protected]>

* Added comment with TZS issuer address.

Co-authored-by: Marcel Ebert <[email protected]>

---------

Signed-off-by: adelarja <[email protected]>
Co-authored-by: Marcel Ebert <[email protected]>
  • Loading branch information
adelarja and ebma authored Jul 17, 2023
1 parent 86e95d8 commit f0ee380
Show file tree
Hide file tree
Showing 11 changed files with 455 additions and 129 deletions.
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 = "ae95aa32f3cff47c5ff26d27af81540569bf81d3"}
module-oracle-rpc = { git = "https://github.com/pendulum-chain/spacewalk", rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3"}
module-redeem-rpc = { git = "https://github.com/pendulum-chain/spacewalk", rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3"}
module-replace-rpc = { git = "https://github.com/pendulum-chain/spacewalk", rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3"}
module-vault-registry-rpc = { git = "https://github.com/pendulum-chain/spacewalk", rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3"}

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

# 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 = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
security = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
staking = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
oracle = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
stellar-relay = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
reward = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
fee = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
vault-registry = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
redeem = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
issue = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
nomination = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
replace = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
spacewalk-primitives = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
module-issue-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
module-oracle-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
module-redeem-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
module-replace-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }
module-vault-registry-rpc-runtime-api = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "ae95aa32f3cff47c5ff26d27af81540569bf81d3" }

# Substrate
frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true, branch = "polkadot-v0.9.40" }
Expand Down
68 changes: 33 additions & 35 deletions runtime/amplitude/src/zenlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,36 @@ use orml_traits::MultiCurrency;
use sp_runtime::{DispatchError, DispatchResult};
use sp_std::marker::PhantomData;

use spacewalk_primitives::CurrencyId;

use zenlink_protocol::{
AssetId, LocalAssetHandler, PairLpGenerate, ZenlinkMultiAssets, LOCAL, NATIVE,
AssetId, Config as ZenlinkConfig, GenerateLpAssetId, LocalAssetHandler, ZenlinkMultiAssets,
};
pub type ZenlinkAssetId = zenlink_protocol::AssetId;

pub type ZenlinkAssetId = AssetId;
use runtime_common::{zenlink, zenlink::*};

pub struct ZenlinkLPGenerate<T>(PhantomData<T>);
impl<T: ZenlinkConfig> GenerateLpAssetId<ZenlinkAssetId> for ZenlinkLPGenerate<T> {
fn generate_lp_asset_id(
asset_0: ZenlinkAssetId,
asset_1: ZenlinkAssetId,
) -> Option<ZenlinkAssetId> {
zenlink::generate_lp_asset_id(asset_0, asset_1, ParachainInfo::parachain_id().into())
}
}

parameter_types! {
pub SelfParaId: u32 = ParachainInfo::parachain_id().into();
pub const ZenlinkPalletId: PalletId = PalletId(*b"/zenlink");
pub ZenlinkRegisteredParaChains: Vec<(MultiLocation, u128)> = vec![];
}

impl zenlink_protocol::Config for Runtime {
impl ZenlinkConfig for Runtime {
type RuntimeEvent = super::RuntimeEvent;
type MultiAssetsHandler = MultiAssets;
type PalletId = ZenlinkPalletId;
type AssetId = AssetId;
type LpGenerate = PairLpGenerate<Self>;
type LpGenerate = ZenlinkLPGenerate<Self>;
type TargetChains = ZenlinkRegisteredParaChains;
type SelfParaId = SelfParaId;
type WeightInfo = ();
Expand All @@ -38,23 +50,27 @@ where
Local: MultiCurrency<AccountId, CurrencyId = CurrencyId>,
{
fn local_balance_of(asset_id: ZenlinkAssetId, who: &AccountId) -> AssetBalance {
if let Ok(currency_id) = zenlink_id_to_currency_id(asset_id) {
if let Ok(currency_id) =
zenlink_id_to_currency_id(asset_id, ParachainInfo::parachain_id().into())
{
return TryInto::<AssetBalance>::try_into(Local::free_balance(currency_id, &who))
.unwrap_or_default()
}
AssetBalance::default()
}

fn local_total_supply(asset_id: ZenlinkAssetId) -> AssetBalance {
if let Ok(currency_id) = zenlink_id_to_currency_id(asset_id) {
if let Ok(currency_id) =
zenlink_id_to_currency_id(asset_id, ParachainInfo::parachain_id().into())
{
return TryInto::<AssetBalance>::try_into(Local::total_issuance(currency_id))
.unwrap_or_default()
}
AssetBalance::default()
}

fn local_is_exists(asset_id: ZenlinkAssetId) -> bool {
match zenlink_id_to_currency_id(asset_id) {
match zenlink_id_to_currency_id(asset_id, ParachainInfo::parachain_id().into()) {
Ok(_) => true,
Err(_) => false,
}
Expand All @@ -66,7 +82,9 @@ where
target: &AccountId,
amount: AssetBalance,
) -> DispatchResult {
if let Ok(currency_id) = zenlink_id_to_currency_id(asset_id) {
if let Ok(currency_id) =
zenlink_id_to_currency_id(asset_id, ParachainInfo::parachain_id().into())
{
Local::transfer(
currency_id,
&origin,
Expand All @@ -85,7 +103,9 @@ where
origin: &AccountId,
amount: AssetBalance,
) -> Result<AssetBalance, DispatchError> {
if let Ok(currency_id) = zenlink_id_to_currency_id(asset_id) {
if let Ok(currency_id) =
zenlink_id_to_currency_id(asset_id, ParachainInfo::parachain_id().into())
{
Local::deposit(
currency_id,
&origin,
Expand All @@ -105,7 +125,9 @@ where
origin: &AccountId,
amount: AssetBalance,
) -> Result<AssetBalance, DispatchError> {
if let Ok(currency_id) = zenlink_id_to_currency_id(asset_id) {
if let Ok(currency_id) =
zenlink_id_to_currency_id(asset_id, ParachainInfo::parachain_id().into())
{
Local::withdraw(
currency_id,
&origin,
Expand All @@ -120,27 +142,3 @@ where
Ok(amount)
}
}

fn zenlink_id_to_currency_id(asset_id: ZenlinkAssetId) -> Result<CurrencyId, ()> {
let para_chain_id: u32 = ParachainInfo::parachain_id().into();
if asset_id.chain_id != para_chain_id {
log::error!("Asset Chain Id {} not compatibile with the Parachain Id.", asset_id.chain_id);
return Err(())
}

match asset_id.asset_type {
NATIVE => Ok(CurrencyId::Native),
LOCAL => {
let foreign_id = asset_id.asset_index.try_into().map_err(|_| {
log::error!("{} has no Foreign Currency Id match.", asset_id.asset_index);
()
})?;

Ok(XCM(foreign_id))
},
other => {
log::error!("Unsupported asset type index:{other}");
Err(())
},
}
}
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 = "ae95aa32f3cff47c5ff26d27af81540569bf81d3"}
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
17 changes: 17 additions & 0 deletions runtime/common/src/stellar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN
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,
];

// GDVKY2GU2DRXWTBEYJJWSFXIGBZV6AZNBVVSUHEPZI54LIS6BA7DVVSP
pub const BRL_ISSUER: [u8; 32] = [
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,
];

// GA2MSSZKJOU6RNL3EJKH3S5TB5CDYTFQFWRYFGUJVIN5I6AOIRTLUHTO
pub const TZS_ISSUER: [u8; 32] = [
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,
];
Loading

0 comments on commit f0ee380

Please sign in to comment.