Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 9 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions crates/bridge-pot-currency-swap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "bridge-pot-currency-swap"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
primitives-currency-swap = { version = "0.1", path = "../primitives-currency-swap", default-features = false }

frame-support = { default-features = false, git = "https://github.com/humanode-network/substrate", branch = "locked/polkadot-v0.9.38" }

[features]
default = ["std"]
std = ["frame-support/std", "primitives-currency-swap/std"]
try-runtime = ["frame-support/try-runtime", "primitives-currency-swap/try-runtime"]
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ use frame_support::{
traits::{Currency, ExistenceRequirement, Get, Imbalance, WithdrawReasons},
};

use super::{Config, CurrencySwap, Pallet};
use super::{Config, CurrencySwap};

/// A marker type for the implementation that does not require pot accounts existence.
pub enum Marker {}

impl<T: Config<I>, I: 'static>
primitives_currency_swap::CurrencySwap<T::AccountIdFrom, T::AccountIdTo>
for CurrencySwap<Pallet<T, I>, Marker>
impl<T: Config> primitives_currency_swap::CurrencySwap<T::AccountIdFrom, T::AccountIdTo>
for CurrencySwap<T, Marker>
{
type From = T::CurrencyFrom;
type To = T::CurrencyTo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use frame_support::{
traits::{Currency, ExistenceRequirement, Get, Imbalance, WithdrawReasons},
};

use super::{Config, CurrencySwap, Pallet};
use super::{Config, CurrencySwap};

/// A marker type for the implementation that requires pot accounts existence.
pub enum Marker {}
Expand All @@ -30,9 +30,8 @@ impl From<Error> for DispatchError {
}
}

impl<T: Config<I>, I: 'static>
primitives_currency_swap::CurrencySwap<T::AccountIdFrom, T::AccountIdTo>
for CurrencySwap<Pallet<T, I>, Marker>
impl<T: Config> primitives_currency_swap::CurrencySwap<T::AccountIdFrom, T::AccountIdTo>
for CurrencySwap<T, Marker>
{
type From = T::CurrencyFrom;
type To = T::CurrencyTo;
Expand Down
56 changes: 56 additions & 0 deletions crates/bridge-pot-currency-swap/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! Bridge pot currency swap implementation.

// Either generate code at stadard mode, or `no_std`, based on the `std` feature presence.
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{
sp_runtime::traits::Convert,
sp_std::marker::PhantomData,
traits::{fungible::Inspect, Currency, Get},
};

pub mod existence_optional;
pub mod existence_required;

pub use existence_optional::Marker as ExistenceOptional;
pub use existence_required::Marker as ExistenceRequired;

/// The config for the generic bridge pot currency swap logic.
pub trait Config {
/// The type representing the account key for the currency to swap from.
type AccountIdFrom;

/// The type representing the account key for the currency to swap to.
type AccountIdTo;

/// The currency to swap from.
type CurrencyFrom: Currency<Self::AccountIdFrom>
+ Inspect<
Self::AccountIdFrom,
Balance = <Self::CurrencyFrom as Currency<Self::AccountIdFrom>>::Balance,
>;

/// The currency to swap to.
type CurrencyTo: Currency<Self::AccountIdTo>
+ Inspect<
Self::AccountIdTo,
Balance = <Self::CurrencyTo as Currency<Self::AccountIdTo>>::Balance,
>;

/// The converter to determine how the balance amount should be converted from one currency to
/// another.
type BalanceConverter: Convert<
<Self::CurrencyFrom as Currency<Self::AccountIdFrom>>::Balance,
<Self::CurrencyTo as Currency<Self::AccountIdTo>>::Balance,
>;

/// The account to land the balances to when receiving the funds as part of the swap operation.
type PotFrom: Get<Self::AccountIdFrom>;

/// The account to take the balances from when sending the funds as part of the swap operation.
type PotTo: Get<Self::AccountIdTo>;
}

/// A [`primitives_currency_swap::CurrencySwap`] implementation that does the swap using two
/// "pot" accounts for each of end swapped currencies.
pub struct CurrencySwap<T: Config, ExistenceRequirement>(PhantomData<(T, ExistenceRequirement)>);
2 changes: 0 additions & 2 deletions crates/humanode-peer/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,6 @@ fn testnet_genesis(
total_claimable: Some(DEV_ACCOUNT_BALANCE),
},
balanced_currency_swap_bridges_initializer: Default::default(),
evm_to_native_swap_bridge: Default::default(),
native_to_evm_swap_bridge: Default::default(),
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/humanode-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ substrate-wasm-builder = { git = "https://github.com/humanode-network/substrate"
[dependencies]
author-ext-api = { version = "0.1", path = "../author-ext-api", default-features = false }
bioauth-flow-api = { version = "0.1", path = "../bioauth-flow-api", default-features = false }
bridge-pot-currency-swap = { version = "0.1", path = "../bridge-pot-currency-swap", default-features = false }
eip191-crypto = { version = "0.1", path = "../eip191-crypto", default-features = false }
eip191-token-claim = { version = "0.1", path = "../eip191-token-claim", default-features = false }
eip712-account-claim = { version = "0.1", path = "../eip712-account-claim", default-features = false }
Expand All @@ -19,7 +20,6 @@ keystore-bioauth-account-id = { version = "0.1", path = "../keystore-bioauth-acc
pallet-balanced-currency-swap-bridges-initializer = { version = "0.1", path = "../pallet-balanced-currency-swap-bridges-initializer", default-features = false }
pallet-bioauth = { version = "0.1", path = "../pallet-bioauth", default-features = false }
pallet-bootnodes = { version = "0.1", path = "../pallet-bootnodes", default-features = false }
pallet-bridge-pot-currency-swap = { version = "0.1", path = "../pallet-bridge-pot-currency-swap", default-features = false }
pallet-chain-properties = { version = "0.1", path = "../pallet-chain-properties", default-features = false }
pallet-chain-start-moment = { version = "0.1", path = "../pallet-chain-start-moment", default-features = false }
pallet-currency-swap = { version = "0.1", path = "../pallet-currency-swap", default-features = false }
Expand Down Expand Up @@ -139,6 +139,7 @@ runtime-benchmarks = [
std = [
"author-ext-api/std",
"bioauth-flow-api/std",
"bridge-pot-currency-swap/std",
"chrono/std",
"codec/std",
"eip191-crypto/std",
Expand All @@ -163,7 +164,6 @@ std = [
"pallet-base-fee/std",
"pallet-bioauth/std",
"pallet-bootnodes/std",
"pallet-bridge-pot-currency-swap/std",
"pallet-chain-properties/std",
"pallet-chain-start-moment/std",
"pallet-currency-swap/std",
Expand Down Expand Up @@ -220,6 +220,7 @@ std = [
"vesting-scheduling-timestamp/std",
]
try-runtime = [
"bridge-pot-currency-swap/try-runtime",
"fp-self-contained/try-runtime",
"frame-executive/try-runtime",
"frame-support/try-runtime",
Expand All @@ -232,7 +233,6 @@ try-runtime = [
"pallet-base-fee/try-runtime",
"pallet-bioauth/try-runtime",
"pallet-bootnodes/try-runtime",
"pallet-bridge-pot-currency-swap/try-runtime",
"pallet-chain-properties/try-runtime",
"pallet-chain-start-moment/try-runtime",
"pallet-currency-swap/try-runtime",
Expand Down
42 changes: 32 additions & 10 deletions crates/humanode-runtime/src/currency_swap.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
use pallet_bridge_pot_currency_swap::ExistenceRequired;
use bridge_pot_currency_swap::ExistenceRequired;
use sp_runtime::traits::Identity;

use crate::{
AccountId, EvmAccountId, EvmToNativeSwapBridge, EvmToNativeSwapBridgePot, FeesPot,
NativeToEvmSwapBridge, TreasuryPot,
parameter_types, AccountId, Balances, EvmAccountId, EvmBalances, EvmToNativeSwapBridgePot,
FeesPot, NativeToEvmSwapBridgePot, TreasuryPot,
};

parameter_types! {
pub NativeToEvmSwapBridgePotAccountId: AccountId = NativeToEvmSwapBridgePot::account_id();
pub EvmToNativeSwapBridgePotAccountId: EvmAccountId = EvmToNativeSwapBridgePot::account_id();
}

pub type NativeToEvmOneToOne =
pallet_bridge_pot_currency_swap::CurrencySwap<NativeToEvmSwapBridge, ExistenceRequired>;
bridge_pot_currency_swap::CurrencySwap<NativeToEvmOneToOneConfig, ExistenceRequired>;

pub struct NativeToEvmOneToOneConfig;

impl bridge_pot_currency_swap::Config for NativeToEvmOneToOneConfig {
type AccountIdFrom = AccountId;
type AccountIdTo = EvmAccountId;
type CurrencyFrom = Balances;
type CurrencyTo = EvmBalances;
type BalanceConverter = Identity;
type PotFrom = NativeToEvmSwapBridgePotAccountId;
type PotTo = EvmToNativeSwapBridgePotAccountId;
}

pub type EvmToNativeOneToOne =
pallet_bridge_pot_currency_swap::CurrencySwap<EvmToNativeSwapBridge, ExistenceRequired>;
bridge_pot_currency_swap::CurrencySwap<EvmToNativeOneToOneConfig, ExistenceRequired>;

pub struct GenesisVerifier;
pub struct EvmToNativeOneToOneConfig;

impl pallet_bridge_pot_currency_swap::GenesisVerifier for GenesisVerifier {
fn verify() -> bool {
true
}
impl bridge_pot_currency_swap::Config for EvmToNativeOneToOneConfig {
type AccountIdFrom = EvmAccountId;
type AccountIdTo = AccountId;
type CurrencyFrom = EvmBalances;
type CurrencyTo = Balances;
type BalanceConverter = Identity;
type PotFrom = EvmToNativeSwapBridgePotAccountId;
type PotTo = NativeToEvmSwapBridgePotAccountId;
}

pub struct EvmToNativeProxyConfig;
Expand Down
32 changes: 0 additions & 32 deletions crates/humanode-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,41 +779,11 @@ impl pallet_utility::Config for Runtime {
type WeightInfo = weights::pallet_utility::WeightInfo<Runtime>;
}

parameter_types! {
pub const NativeToEvmSwapBridgePalletId: PalletId = PalletId(*b"hmsb/ne1");
pub const EvmToNativeSwapBridgePalletId: PalletId = PalletId(*b"hmsb/en1");
}

parameter_types! {
pub NativeToEvmSwapBridgePotAccountId: AccountId = NativeToEvmSwapBridgePot::account_id();
pub EvmToNativeSwapBridgePotAccountId: EvmAccountId = EvmToNativeSwapBridgePot::account_id();
}

type BridgeInstanceNativeToEvmSwap = pallet_bridge_pot_currency_swap::Instance1;
type BridgeInstanceEvmToNativeSwap = pallet_bridge_pot_currency_swap::Instance2;

impl pallet_bridge_pot_currency_swap::Config<BridgeInstanceNativeToEvmSwap> for Runtime {
type AccountIdFrom = AccountId;
type AccountIdTo = EvmAccountId;
type CurrencyFrom = Balances;
type CurrencyTo = EvmBalances;
type BalanceConverter = Identity;
type PotFrom = NativeToEvmSwapBridgePotAccountId;
type PotTo = EvmToNativeSwapBridgePotAccountId;
type GenesisVerifier = currency_swap::GenesisVerifier;
}

impl pallet_bridge_pot_currency_swap::Config<BridgeInstanceEvmToNativeSwap> for Runtime {
type AccountIdFrom = EvmAccountId;
type AccountIdTo = AccountId;
type CurrencyFrom = EvmBalances;
type CurrencyTo = Balances;
type BalanceConverter = Identity;
type PotFrom = EvmToNativeSwapBridgePotAccountId;
type PotTo = NativeToEvmSwapBridgePotAccountId;
type GenesisVerifier = currency_swap::GenesisVerifier;
}

parameter_types! {
pub TreasuryPotAccountId: AccountId = TreasuryPot::account_id();
}
Expand Down Expand Up @@ -876,8 +846,6 @@ construct_runtime!(
EvmToNativeSwapBridgePot: pallet_pot::<Instance5> = 34,
CurrencySwap: pallet_currency_swap = 35,
BalancedCurrencySwapBridgesInitializer: pallet_balanced_currency_swap_bridges_initializer = 36,
NativeToEvmSwapBridge: pallet_bridge_pot_currency_swap::<Instance1> = 37,
EvmToNativeSwapBridge: pallet_bridge_pot_currency_swap::<Instance2> = 38,
}
);

Expand Down
4 changes: 1 addition & 3 deletions crates/humanode-runtime/src/tests/claims_and_vesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,7 @@ fn prepare_genesis_json(token_claims: &str, token_claim_pot_balance: u128) -> St
"evmToNativeSwapBridgePot": {{
"initialState": "Initialized"
}},
"balancedCurrencySwapBridgesInitializer": null,
"nativeToEvmSwapBridge": null,
"evmToNativeSwapBridge": null
"balancedCurrencySwapBridgesInitializer": null
}}"#
)
}
Expand Down
6 changes: 2 additions & 4 deletions crates/humanode-runtime/src/tests/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ fn works() {
"evmToNativeSwapBridgePot": {
"initialState": "Initialized"
},
"balancedCurrencySwapBridgesInitializer": null,
"nativeToEvmSwapBridge": null,
"evmToNativeSwapBridge": null
"balancedCurrencySwapBridgesInitializer": null
}"#;
let config: GenesisConfig = serde_json::from_str(json_input).unwrap();
assert_ok!(config.build_storage());
Expand All @@ -140,7 +138,7 @@ fn unknown_field() {
`feesPot`, `tokenClaimsPot`, `transactionPayment`, `session`, `chainProperties`, \
`ethereumChainId`, `sudo`, `grandpa`, `ethereum`, `evm`, `dynamicFee`, `baseFee`, \
`imOnline`, `evmAccountsMapping`, `tokenClaims`, `nativeToEvmSwapBridgePot`, \
`evmToNativeSwapBridgePot`, `balancedCurrencySwapBridgesInitializer`, `nativeToEvmSwapBridge`, `evmToNativeSwapBridge` at line 1 column 6"
`evmToNativeSwapBridgePot`, `balancedCurrencySwapBridgesInitializer` at line 1 column 6"
);
}

Expand Down
Loading