Merge commit 'e5bed7ac380b6adb54b60a2a72a2a8f07f50d6c1' as 'bridges'

This commit is contained in:
Hernando Castano
2021-04-21 11:56:23 -04:00
339 changed files with 71658 additions and 0 deletions
@@ -0,0 +1,25 @@
[package]
name = "bp-currency-exchange"
description = "Primitives of currency exchange module."
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-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"sp-api/std",
"sp-std/std",
]
@@ -0,0 +1,150 @@
// 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/>.
#![cfg_attr(not(feature = "std"), no_std)]
// RuntimeApi generated functions
#![allow(clippy::too_many_arguments)]
// Generated by `DecodeLimit::decode_with_depth_limit`
#![allow(clippy::unnecessary_mut_passed)]
use codec::{Decode, Encode, EncodeLike};
use frame_support::{Parameter, RuntimeDebug};
use sp_api::decl_runtime_apis;
use sp_std::marker::PhantomData;
/// All errors that may happen during exchange.
#[derive(RuntimeDebug, PartialEq)]
pub enum Error {
/// Invalid peer blockchain transaction provided.
InvalidTransaction,
/// Peer transaction has invalid amount.
InvalidAmount,
/// Peer transaction has invalid recipient.
InvalidRecipient,
/// Cannot map from peer recipient to this blockchain recipient.
FailedToMapRecipients,
/// Failed to convert from peer blockchain currency to this blockhain currency.
FailedToConvertCurrency,
/// Deposit has failed.
DepositFailed,
/// Deposit has partially failed (changes to recipient account were made).
DepositPartiallyFailed,
}
/// Result of all exchange operations.
pub type Result<T> = sp_std::result::Result<T, Error>;
/// Peer blockchain lock funds transaction.
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq)]
pub struct LockFundsTransaction<TransferId, Recipient, Amount> {
/// Something that uniquely identifies this transfer.
pub id: TransferId,
/// Funds recipient on the peer chain.
pub recipient: Recipient,
/// Amount of the locked funds.
pub amount: Amount,
}
/// Peer blockchain transaction that may represent lock funds transaction.
pub trait MaybeLockFundsTransaction {
/// Transaction type.
type Transaction;
/// Identifier that uniquely identifies this transfer.
type Id: Decode + Encode + EncodeLike + sp_std::fmt::Debug;
/// Peer recipient type.
type Recipient;
/// Peer currency amount type.
type Amount;
/// Parse lock funds transaction of the peer blockchain. Returns None if
/// transaction format is unknown, or it isn't a lock funds transaction.
fn parse(tx: &Self::Transaction) -> Result<LockFundsTransaction<Self::Id, Self::Recipient, Self::Amount>>;
}
/// Map that maps recipients from peer blockchain to this blockchain recipients.
pub trait RecipientsMap {
/// Peer blockchain recipient type.
type PeerRecipient;
/// Current blockchain recipient type.
type Recipient;
/// Lookup current blockchain recipient by peer blockchain recipient.
fn map(peer_recipient: Self::PeerRecipient) -> Result<Self::Recipient>;
}
/// Conversion between two currencies.
pub trait CurrencyConverter {
/// Type of the source currency amount.
type SourceAmount;
/// Type of the target currency amount.
type TargetAmount;
/// Covert from source to target currency.
fn convert(amount: Self::SourceAmount) -> Result<Self::TargetAmount>;
}
/// Currency deposit.
pub trait DepositInto {
/// Recipient type.
type Recipient;
/// Currency amount type.
type Amount;
/// Grant some money to given account.
fn deposit_into(recipient: Self::Recipient, amount: Self::Amount) -> Result<()>;
}
/// Recipients map which is used when accounts ids are the same on both chains.
#[derive(Debug)]
pub struct IdentityRecipients<AccountId>(PhantomData<AccountId>);
impl<AccountId> RecipientsMap for IdentityRecipients<AccountId> {
type PeerRecipient = AccountId;
type Recipient = AccountId;
fn map(peer_recipient: Self::PeerRecipient) -> Result<Self::Recipient> {
Ok(peer_recipient)
}
}
/// Currency converter which is used when currency is the same on both chains.
#[derive(Debug)]
pub struct IdentityCurrencyConverter<Amount>(PhantomData<Amount>);
impl<Amount> CurrencyConverter for IdentityCurrencyConverter<Amount> {
type SourceAmount = Amount;
type TargetAmount = Amount;
fn convert(currency: Self::SourceAmount) -> Result<Self::TargetAmount> {
Ok(currency)
}
}
decl_runtime_apis! {
/// API for Rialto exchange transactions submitters.
pub trait RialtoCurrencyExchangeApi<Proof: Parameter> {
/// Returns true if currency exchange module is able to import transaction proof in
/// its current state.
fn filter_transaction_proof(proof: Proof) -> bool;
}
/// API for Kovan exchange transactions submitters.
pub trait KovanCurrencyExchangeApi<Proof: Parameter> {
/// Returns true if currency exchange module is able to import transaction proof in
/// its current state.
fn filter_transaction_proof(proof: Proof) -> bool;
}
}