Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Pallet: Atomic Swap #6349

Merged
merged 21 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Init atomic swap pallet
  • Loading branch information
sorpaas committed Jun 14, 2020
commit b0292653caed2d98472950757dc01f269813bc97
15 changes: 15 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ members = [
"utils/wasm-builder-runner",
"frame/assets",
"frame/aura",
"frame/atomic-swap",
"frame/authority-discovery",
"frame/authorship",
"frame/babe",
Expand Down
39 changes: 39 additions & 0 deletions frame/atomic-swap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "pallet-atomic-swap"
version = "2.0.0-rc3"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
homepage = "https://substrate.dev"
repository = "https://github.com/paritytech/substrate/"
description = "FRAME atomic swap pallet"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
serde = { version = "1.0.101", optional = true }
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false }
frame-support = { version = "2.0.0-rc3", default-features = false, path = "../support" }
frame-system = { version = "2.0.0-rc3", default-features = false, path = "../system" }
sp-runtime = { version = "2.0.0-rc3", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "2.0.0-rc3", default-features = false, path = "../../primitives/std" }
sp-io = { version = "2.0.0-rc3", default-features = false, path = "../../primitives/io" }
sp-core = { version = "2.0.0-rc3", default-features = false, path = "../../primitives/core" }

[dev-dependencies]
pallet-balances = { version = "2.0.0-rc3", default-features = false, path = "../balances" }

[features]
default = ["std"]
std = [
"serde",
"codec/std",
"frame-support/std",
"frame-system/std",
"sp-runtime/std",
"sp-std/std",
"sp-io/std",
"sp-core/std",
"pallet-balances/std",
]
127 changes: 127 additions & 0 deletions frame/atomic-swap/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// This file is part of Substrate.

// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! # Atomic swap support pallet

// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]

mod tests;

use sp_std::prelude::*;
use sp_core::H256;
use frame_support::{
dispatch::DispatchResult, decl_module, decl_storage, decl_event,
traits::{Currency, ReservableCurrency},
};
use frame_system::{self as system, ensure_signed};
use codec::{Encode, Decode};
use sp_runtime::RuntimeDebug;

/// Pending atomic swap operation.
#[derive(Clone, RuntimeDebug, Eq, PartialEq, Encode, Decode)]
pub struct PendingSwap<AccountId, Balance, BlockNumber> {
/// Source of the swap.
pub source: AccountId,
/// Balance value of the swap.
pub balance: Balance,
/// End block of the lock.
pub end_block: BlockNumber,
}

/// Balance type from the pallet's point of view.
pub type BalanceFor<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;

/// AccountId type from the pallet's point of view.
pub type AccountIdFor<T> = <T as frame_system::Trait>::AccountId;

/// BlockNumber type from the pallet's point of view.
pub type BlockNumberFor<T> = <T as frame_system::Trait>::BlockNumber;

/// PendingSwap type from the pallet's point of view.
pub type PendingSwapFor<T> = PendingSwap<AccountIdFor<T>, BalanceFor<T>, BlockNumberFor<T>>;

/// Atomic swap's pallet configuration trait.
pub trait Trait: frame_system::Trait {
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
/// The currency mechanism.
type Currency: ReservableCurrency<Self::AccountId>;
}

decl_storage! {
trait Store for Module<T: Trait> as Example {
pub PendingSwaps: double_map
hasher(twox_64_concat) T::AccountId, hasher(blake2_128_concat) H256
=> Option<PendingSwapFor<T>>;
}
}

decl_event!(
/// Event of atomic swap pallet.
pub enum Event<T> where
Balance = BalanceFor<T>,
AccountId = AccountIdFor<T>,
PendingSwap = PendingSwapFor<T>,
{
/// Swap created.
NewSwap(AccountId, H256, PendingSwap),
/// Swap claimed.
SwapClaimed(AccountId, H256, Balance),
/// Swap cancelled.
SwapCancelled(AccountId, H256, Balance),
}
);

decl_module! {
/// Module definition of atomic swap pallet.
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event() = default;

#[weight = 0]
fn create_swap(
origin,
_target: AccountIdFor<T>,
_hashed_proof: H256,
_balance: BalanceFor<T>,
_duration: BlockNumberFor<T>,
) -> DispatchResult {
ensure_signed(origin)?;
unimplemented!()
}

#[weight = 0]
fn claim_swap(
origin,
_hashed_proof: H256,
_proof: Vec<u8>,
) -> DispatchResult {
ensure_signed(origin)?;
unimplemented!()
}

#[weight = 0]
fn cancel_swap(
origin,
_target: AccountIdFor<T>,
_hashed_proof: H256,
) -> DispatchResult {
ensure_signed(origin)?;
unimplemented!()
}
}
}
90 changes: 90 additions & 0 deletions frame/atomic-swap/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#![cfg(test)]

use super::*;

use frame_support::{
assert_ok, impl_outer_origin, parameter_types, weights::{Weight, DispatchInfo, GetDispatchInfo},
traits::{OnInitialize, OnFinalize}
};
use sp_core::H256;
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required.
use sp_runtime::{
Perbill,
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};

impl_outer_origin! {
pub enum Origin for Test where system = frame_system {}
}

// For testing the pallet, we construct most of a mock runtime. This means
// first constructing a configuration type (`Test`) which `impl`s each of the
// configuration traits of pallets we want to use.
#[derive(Clone, Eq, PartialEq)]
pub struct Test;
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::Trait for Test {
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Call = ();
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = ();
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = ();
type BlockExecutionWeight = ();
type ExtrinsicBaseWeight = ();
type MaximumExtrinsicWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
type ModuleToIndex = ();
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
}
parameter_types! {
pub const ExistentialDeposit: u64 = 1;
}
impl pallet_balances::Trait for Test {
type Balance = u64;
type DustRemoval = ();
type Event = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
}
impl Trait for Test {
type Event = ();
type Currency = Balances;
}
type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;
type Example = Module<Test>;

// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
// We use default for brevity, but you can configure as desired if needed.
pallet_balances::GenesisConfig::<Test>::default().assimilate_storage(&mut t).unwrap();
t.into()
}

#[test]
fn two_party_swap() {
new_test_ext().execute_with(|| {
unimplemented!()
});
}