mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 23:21:02 +00:00
Token-swap-over-bridge pallet (#944)
* token swap pallet * token swap type (replay protection) * post-merge fixes * post-merge fix * Update modules/token-swap/src/lib.rs Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * Update modules/token-swap/src/lib.rs Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * add missing comment part * Update modules/token-swap/src/lib.rs Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * starting claim after lock period is over is forbidden * move spec_version and weight to arguments Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
This commit is contained in:
committed by
Bastian Köcher
parent
f86d101d7c
commit
eba80f42a9
@@ -0,0 +1,51 @@
|
|||||||
|
[package]
|
||||||
|
name = "pallet-bridge-token-swap"
|
||||||
|
description = "An Substrate pallet that allows parties on different chains (bridged using messages pallet) to swap their tokens"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Parity Technologies <admin@parity.io>"]
|
||||||
|
edition = "2018"
|
||||||
|
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
|
||||||
|
log = { version = "0.4.14", default-features = false }
|
||||||
|
serde = { version = "1.0", optional = true }
|
||||||
|
|
||||||
|
# Bridge dependencies
|
||||||
|
|
||||||
|
bp-message-dispatch = { path = "../../primitives/message-dispatch", default-features = false }
|
||||||
|
bp-messages = { path = "../../primitives/messages", default-features = false }
|
||||||
|
bp-runtime = { path = "../../primitives/runtime", default-features = false }
|
||||||
|
bp-token-swap = { path = "../../primitives/token-swap", default-features = false }
|
||||||
|
pallet-bridge-dispatch = { path = "../dispatch", default-features = false }
|
||||||
|
|
||||||
|
# Substrate Dependencies
|
||||||
|
|
||||||
|
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||||
|
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||||
|
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||||
|
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||||
|
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||||
|
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false, optional = true }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["std"]
|
||||||
|
std = [
|
||||||
|
"codec/std",
|
||||||
|
"bp-message-dispatch/std",
|
||||||
|
"bp-messages/std",
|
||||||
|
"bp-runtime/std",
|
||||||
|
"bp-token-swap/std",
|
||||||
|
"frame-support/std",
|
||||||
|
"frame-system/std",
|
||||||
|
"log/std",
|
||||||
|
"pallet-bridge-dispatch/std",
|
||||||
|
"serde",
|
||||||
|
"sp-core/std",
|
||||||
|
"sp-io/std",
|
||||||
|
"sp-runtime/std",
|
||||||
|
"sp-std/std",
|
||||||
|
]
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,174 @@
|
|||||||
|
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||||
|
// This file is part of Parity Bridges Common.
|
||||||
|
|
||||||
|
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
|
||||||
|
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
use crate as pallet_bridge_token_swap;
|
||||||
|
use crate::MessagePayloadOf;
|
||||||
|
|
||||||
|
use bp_messages::{source_chain::MessagesBridge, LaneId, MessageNonce};
|
||||||
|
use bp_runtime::ChainId;
|
||||||
|
use frame_support::weights::Weight;
|
||||||
|
use sp_core::H256;
|
||||||
|
use sp_runtime::{
|
||||||
|
testing::Header as SubstrateHeader,
|
||||||
|
traits::{BlakeTwo256, IdentityLookup},
|
||||||
|
Perbill,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub type AccountId = u64;
|
||||||
|
pub type Balance = u64;
|
||||||
|
pub type Block = frame_system::mocking::MockBlock<TestRuntime>;
|
||||||
|
pub type BridgedAccountId = u64;
|
||||||
|
pub type BridgedAccountPublic = u64;
|
||||||
|
pub type BridgedAccountSignature = u64;
|
||||||
|
pub type BridgedBalance = u64;
|
||||||
|
pub type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<TestRuntime>;
|
||||||
|
|
||||||
|
pub const OK_TRANSFER_CALL: u8 = 1;
|
||||||
|
pub const BAD_TRANSFER_CALL: u8 = 2;
|
||||||
|
pub const MESSAGE_NONCE: MessageNonce = 3;
|
||||||
|
|
||||||
|
pub const THIS_CHAIN_ACCOUNT: AccountId = 1;
|
||||||
|
pub const THIS_CHAIN_ACCOUNT_BALANCE: Balance = 100_000;
|
||||||
|
|
||||||
|
frame_support::construct_runtime! {
|
||||||
|
pub enum TestRuntime where
|
||||||
|
Block = Block,
|
||||||
|
NodeBlock = Block,
|
||||||
|
UncheckedExtrinsic = UncheckedExtrinsic,
|
||||||
|
{
|
||||||
|
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
|
||||||
|
Balances: pallet_balances::{Pallet, Call, Event<T>},
|
||||||
|
TokenSwap: pallet_bridge_token_swap::{Pallet, Call, Event<T>},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
frame_support::parameter_types! {
|
||||||
|
pub const BlockHashCount: u64 = 250;
|
||||||
|
pub const MaximumBlockWeight: Weight = 1024;
|
||||||
|
pub const MaximumBlockLength: u32 = 2 * 1024;
|
||||||
|
pub const AvailableBlockRatio: Perbill = Perbill::one();
|
||||||
|
}
|
||||||
|
|
||||||
|
impl frame_system::Config for TestRuntime {
|
||||||
|
type Origin = Origin;
|
||||||
|
type Index = u64;
|
||||||
|
type Call = Call;
|
||||||
|
type BlockNumber = u64;
|
||||||
|
type Hash = H256;
|
||||||
|
type Hashing = BlakeTwo256;
|
||||||
|
type AccountId = AccountId;
|
||||||
|
type Lookup = IdentityLookup<Self::AccountId>;
|
||||||
|
type Header = SubstrateHeader;
|
||||||
|
type Event = Event;
|
||||||
|
type BlockHashCount = BlockHashCount;
|
||||||
|
type Version = ();
|
||||||
|
type PalletInfo = PalletInfo;
|
||||||
|
type AccountData = pallet_balances::AccountData<Balance>;
|
||||||
|
type OnNewAccount = ();
|
||||||
|
type OnKilledAccount = ();
|
||||||
|
type BaseCallFilter = ();
|
||||||
|
type SystemWeightInfo = ();
|
||||||
|
type BlockWeights = ();
|
||||||
|
type BlockLength = ();
|
||||||
|
type DbWeight = ();
|
||||||
|
type SS58Prefix = ();
|
||||||
|
type OnSetCode = ();
|
||||||
|
}
|
||||||
|
|
||||||
|
frame_support::parameter_types! {
|
||||||
|
pub const ExistentialDeposit: u64 = 10;
|
||||||
|
pub const MaxReserves: u32 = 50;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl pallet_balances::Config for TestRuntime {
|
||||||
|
type MaxLocks = ();
|
||||||
|
type Balance = Balance;
|
||||||
|
type DustRemoval = ();
|
||||||
|
type Event = Event;
|
||||||
|
type ExistentialDeposit = ExistentialDeposit;
|
||||||
|
type AccountStore = frame_system::Pallet<TestRuntime>;
|
||||||
|
type WeightInfo = ();
|
||||||
|
type MaxReserves = MaxReserves;
|
||||||
|
type ReserveIdentifier = [u8; 8];
|
||||||
|
}
|
||||||
|
|
||||||
|
frame_support::parameter_types! {
|
||||||
|
pub const BridgeChainId: ChainId = *b"inst";
|
||||||
|
pub const OutboundMessageLaneId: LaneId = *b"lane";
|
||||||
|
pub const MessageDeliveryAndDispatchFee: Balance = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl pallet_bridge_token_swap::Config for TestRuntime {
|
||||||
|
type Event = Event;
|
||||||
|
|
||||||
|
type BridgeChainId = BridgeChainId;
|
||||||
|
type OutboundMessageLaneId = OutboundMessageLaneId;
|
||||||
|
type MessagesBridge = TestMessagesBridge;
|
||||||
|
type MessageDeliveryAndDispatchFee = MessageDeliveryAndDispatchFee;
|
||||||
|
|
||||||
|
type ThisCurrency = pallet_balances::Pallet<TestRuntime>;
|
||||||
|
type FromSwapToThisAccountIdConverter = TestAccountConverter;
|
||||||
|
|
||||||
|
type BridgedBalance = BridgedBalance;
|
||||||
|
type BridgedAccountId = BridgedAccountId;
|
||||||
|
type BridgedAccountPublic = BridgedAccountPublic;
|
||||||
|
type BridgedAccountSignature = BridgedAccountSignature;
|
||||||
|
type FromBridgedToThisAccountIdConverter = TestAccountConverter;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TestMessagesBridge;
|
||||||
|
|
||||||
|
impl MessagesBridge<AccountId, Balance, MessagePayloadOf<TestRuntime, ()>> for TestMessagesBridge {
|
||||||
|
type Error = ();
|
||||||
|
|
||||||
|
fn send_message(
|
||||||
|
sender: AccountId,
|
||||||
|
lane: LaneId,
|
||||||
|
message: MessagePayloadOf<TestRuntime, ()>,
|
||||||
|
delivery_and_dispatch_fee: Balance,
|
||||||
|
) -> Result<MessageNonce, Self::Error> {
|
||||||
|
assert_ne!(sender, THIS_CHAIN_ACCOUNT);
|
||||||
|
assert_eq!(lane, OutboundMessageLaneId::get());
|
||||||
|
assert_eq!(delivery_and_dispatch_fee, MessageDeliveryAndDispatchFee::get());
|
||||||
|
match message.call[0] {
|
||||||
|
OK_TRANSFER_CALL => Ok(MESSAGE_NONCE),
|
||||||
|
BAD_TRANSFER_CALL => Err(()),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TestAccountConverter;
|
||||||
|
|
||||||
|
impl sp_runtime::traits::Convert<H256, AccountId> for TestAccountConverter {
|
||||||
|
fn convert(hash: H256) -> AccountId {
|
||||||
|
hash.to_low_u64_ne()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Run pallet test.
|
||||||
|
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
|
||||||
|
let mut t = frame_system::GenesisConfig::default()
|
||||||
|
.build_storage::<TestRuntime>()
|
||||||
|
.unwrap();
|
||||||
|
pallet_balances::GenesisConfig::<TestRuntime> {
|
||||||
|
balances: vec![(THIS_CHAIN_ACCOUNT, THIS_CHAIN_ACCOUNT_BALANCE)],
|
||||||
|
}
|
||||||
|
.assimilate_storage(&mut t)
|
||||||
|
.unwrap();
|
||||||
|
let mut ext = sp_io::TestExternalities::new(t);
|
||||||
|
ext.execute_with(test)
|
||||||
|
}
|
||||||
@@ -135,6 +135,22 @@ pub trait MessageDeliveryAndDispatchPayment<AccountId, Balance> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Messages bridge API to be used from other pallets.
|
||||||
|
pub trait MessagesBridge<AccountId, Balance, Payload> {
|
||||||
|
/// Error type.
|
||||||
|
type Error: Debug;
|
||||||
|
|
||||||
|
/// Send message over the bridge.
|
||||||
|
///
|
||||||
|
/// Returns unique message nonce or error if send has failed.
|
||||||
|
fn send_message(
|
||||||
|
sender: AccountId,
|
||||||
|
lane: LaneId,
|
||||||
|
message: Payload,
|
||||||
|
delivery_and_dispatch_fee: Balance,
|
||||||
|
) -> Result<MessageNonce, Self::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
/// Handler for messages delivery confirmation.
|
/// Handler for messages delivery confirmation.
|
||||||
pub trait OnDeliveryConfirmed {
|
pub trait OnDeliveryConfirmed {
|
||||||
/// Called when we receive confirmation that our messages have been delivered to the
|
/// Called when we receive confirmation that our messages have been delivered to the
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
[package]
|
||||||
|
name = "bp-token-swap"
|
||||||
|
description = "Primitives of the pallet-bridge-token-swap pallet"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Parity Technologies <admin@parity.io>"]
|
||||||
|
edition = "2018"
|
||||||
|
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
|
||||||
|
|
||||||
|
# Substrate Dependencies
|
||||||
|
|
||||||
|
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||||
|
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["std"]
|
||||||
|
std = [
|
||||||
|
"codec/std",
|
||||||
|
"frame-support/std",
|
||||||
|
"sp-core/std",
|
||||||
|
]
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||||
|
// This file is part of Parity Bridges Common.
|
||||||
|
|
||||||
|
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
|
||||||
|
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
use codec::{Decode, Encode};
|
||||||
|
use frame_support::RuntimeDebug;
|
||||||
|
use sp_core::U256;
|
||||||
|
|
||||||
|
/// Token swap type.
|
||||||
|
///
|
||||||
|
/// Different swap types give a different guarantees regarding possible swap
|
||||||
|
/// replay protection.
|
||||||
|
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq)]
|
||||||
|
pub enum TokenSwapType<ThisBlockNumber> {
|
||||||
|
/// The `target_account_at_bridged_chain` is temporary and only have funds for single swap.
|
||||||
|
///
|
||||||
|
/// ***WARNING**: if `target_account_at_bridged_chain` still exists after the swap has been
|
||||||
|
/// completed (either by claiming or cancelling), the `source_account_at_this_chain` will be able
|
||||||
|
/// to restart the swap again and repeat the swap until `target_account_at_bridged_chain` depletes.
|
||||||
|
TemporaryTargetAccountAtBridgedChain,
|
||||||
|
/// This swap type prevents `source_account_at_this_chain` from restarting the swap after it has
|
||||||
|
/// been completed. There are two consequences:
|
||||||
|
///
|
||||||
|
/// 1) the `source_account_at_this_chain` won't be able to call `start_swap` after given <ThisBlockNumber>;
|
||||||
|
/// 2) the `target_account_at_bridged_chain` won't be able to call `claim_swap` (over the bridge) before
|
||||||
|
/// block `<ThisBlockNumber + 1>`.
|
||||||
|
///
|
||||||
|
/// The second element is the nonce of the swap. You must care about its uniqueness if you're
|
||||||
|
/// planning to perform another swap with exactly the same parameters (i.e. same amount, same accounts,
|
||||||
|
/// same `ThisBlockNumber`) to avoid collisions.
|
||||||
|
LockClaimUntilBlock(ThisBlockNumber, U256),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An intention to swap `source_balance_at_this_chain` owned by `source_account_at_this_chain`
|
||||||
|
/// to `target_balance_at_bridged_chain` owned by `target_account_at_bridged_chain`.
|
||||||
|
///
|
||||||
|
/// **IMPORTANT NOTE**: this structure is always the same during single token swap. So even
|
||||||
|
/// when chain changes, the meaning of This and Bridged are still used to point to the same chains.
|
||||||
|
/// This chain is always the chain where swap has been started. And the Bridged chain is the other chain.
|
||||||
|
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq)]
|
||||||
|
pub struct TokenSwap<ThisBlockNumber, ThisBalance, ThisAccountId, BridgedBalance, BridgedAccountId> {
|
||||||
|
/// The type of the swap.
|
||||||
|
pub swap_type: TokenSwapType<ThisBlockNumber>,
|
||||||
|
/// This chain balance to be swapped with `target_balance_at_bridged_chain`.
|
||||||
|
pub source_balance_at_this_chain: ThisBalance,
|
||||||
|
/// Account id of the party acting at This chain and owning the `source_account_at_this_chain`.
|
||||||
|
pub source_account_at_this_chain: ThisAccountId,
|
||||||
|
/// Bridged chain balance to be swapped with `source_balance_at_this_chain`.
|
||||||
|
pub target_balance_at_bridged_chain: BridgedBalance,
|
||||||
|
/// Account id of the party acting at the Bridged chain and owning the `target_balance_at_bridged_chain`.
|
||||||
|
pub target_account_at_bridged_chain: BridgedAccountId,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user