-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
P 1275 omni bridge precompile (#3254)
* feat: initial impl without logic * feat: impl * chore: small fix * chore: lock file * chore: update member * chore: fix * feat: ts-test seems tricky * chore: lock file * feat: add ts-test * chore: clean up * chore: small fix ts * chore: fix * chore: fix abi file * chore: small fix * chore: more hint * chore: fix * chore: fix * bump runtime versions * chore: fix * chore: fix * chore: fix * chore: fix --------- Co-authored-by: Kai <[email protected]> Co-authored-by: Kailai-Wang <[email protected]>
- Loading branch information
1 parent
f86d72d
commit 4a5e812
Showing
17 changed files
with
299 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
[package] | ||
authors = ["Trust Computing GmbH <[email protected]>"] | ||
edition = '2021' | ||
name = "pallet-evm-precompile-omni-bridge" | ||
version = '0.1.0' | ||
|
||
[dependencies] | ||
precompile-utils = { workspace = true } | ||
|
||
frame-support = { workspace = true } | ||
frame-system = { workspace = true } | ||
pallet-omni-bridge = { workspace = true } | ||
parity-scale-codec = { workspace = true } | ||
scale-info = { workspace = true, features = ["derive"] } | ||
sp-core = { workspace = true } | ||
sp-runtime = { workspace = true } | ||
sp-std = { workspace = true } | ||
|
||
fp-evm = { workspace = true } | ||
pallet-evm = { workspace = true } | ||
|
||
core-primitives = { workspace = true } | ||
|
||
[dev-dependencies] | ||
derive_more = { workspace = true } | ||
hex-literal = { workspace = true } | ||
libsecp256k1 = { workspace = true, features = ["std"] } | ||
serde = { workspace = true } | ||
sha3 = { workspace = true } | ||
precompile-utils = { workspace = true, features = ["std", "testing"] } | ||
pallet-timestamp = { workspace = true, features = ["std"] } | ||
parity-scale-codec = { workspace = true, features = ["std"] } | ||
sp-runtime = { workspace = true, features = ["std"] } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"core-primitives/std", | ||
"fp-evm/std", | ||
"frame-support/std", | ||
"frame-system/std", | ||
"libsecp256k1/std", | ||
"pallet-omni-bridge/std", | ||
"pallet-evm/std", | ||
"pallet-timestamp/std", | ||
"parity-scale-codec/std", | ||
"precompile-utils/std", | ||
"scale-info/std", | ||
"serde/std", | ||
"sha3/std", | ||
"sp-core/std", | ||
"sp-runtime/std", | ||
"sp-std/std", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
pragma solidity >=0.8.3; | ||
|
||
interface IOmniBridge { | ||
/// @notice Used to transfer assets through token bridge. | ||
/// @param amount: The amount of tokens to be transferred. | ||
/// @param dest_id: The destination chain id indicator | ||
/// @param native: Indicator of if asset is native. If true, asset_id will be ignored | ||
/// @param asset_id: Resource indicator of type of assets transferred (In substrate runtime it is u128) | ||
/// @param recipient: Recipient address, typically H160/H256 | ||
/// @custom:selector 0xef185624 | ||
/// payIn(uint256,uint8,bool,uint256,bytes) | ||
function payIn(uint256 amount, uint8 dest_id, bool native, uint256 asset_id, bytes calldata recipient) external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2020-2024 Trust Computing GmbH. | ||
// This file is part of Litentry. | ||
// | ||
// Litentry 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. | ||
// | ||
// Litentry 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 Litentry. If not, see <https://www.gnu.org/licenses/>. | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use fp_evm::{PrecompileFailure, PrecompileHandle}; | ||
|
||
use core_primitives::AssetId; | ||
use frame_support::{ | ||
dispatch::{GetDispatchInfo, PostDispatchInfo}, | ||
traits::fungible::NativeOrWithId, | ||
}; | ||
use pallet_evm::AddressMapping; | ||
use precompile_utils::prelude::*; | ||
use sp_runtime::traits::Dispatchable; | ||
|
||
use sp_core::U256; | ||
use sp_std::{marker::PhantomData, vec::Vec}; | ||
|
||
use pallet_omni_bridge::{ChainType, PayInRequest}; | ||
|
||
pub struct OmniBridgePrecompile<Runtime>(PhantomData<Runtime>); | ||
|
||
type BridgeBalanceOf<Runtime> = <Runtime as pallet_omni_bridge::Config>::Balance; | ||
|
||
#[precompile_utils::precompile] | ||
impl<Runtime> OmniBridgePrecompile<Runtime> | ||
where | ||
Runtime: pallet_omni_bridge::Config<AssetKind = NativeOrWithId<AssetId>> + pallet_evm::Config, | ||
Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo, | ||
Runtime::RuntimeCall: From<pallet_omni_bridge::Call<Runtime>>, | ||
<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>, | ||
BridgeBalanceOf<Runtime>: TryFrom<U256> + Into<U256>, | ||
{ | ||
#[precompile::public("payIn(uint256,uint8,bool,uint256,bytes)")] | ||
fn pay_in( | ||
handle: &mut impl PrecompileHandle, | ||
amount: U256, | ||
dest_id: u8, | ||
native: bool, | ||
asset_id: U256, | ||
recipient: UnboundedBytes, | ||
) -> EvmResult { | ||
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller); | ||
|
||
let amount: BridgeBalanceOf<Runtime> = amount.try_into().map_err(|_| { | ||
Into::<PrecompileFailure>::into(RevertReason::value_is_too_large("balance type")) | ||
})?; | ||
let recipient: Vec<u8> = recipient.into(); | ||
let asset_id: AssetId = asset_id.try_into().map_err(|_| { | ||
Into::<PrecompileFailure>::into(RevertReason::value_is_too_large("asset id type")) | ||
})?; | ||
|
||
let pay_in_request: PayInRequest<NativeOrWithId<AssetId>, BridgeBalanceOf<Runtime>> = | ||
match native { | ||
true => PayInRequest { | ||
asset: NativeOrWithId::Native, | ||
// This is substrate parachain precompile | ||
// So always be non native chain | ||
dest_chain: ChainType::Ethereum(dest_id.into()), | ||
dest_account: recipient, | ||
amount, | ||
}, | ||
false => PayInRequest { | ||
asset: NativeOrWithId::WithId(asset_id), | ||
// This is substrate parachain precompile | ||
// So always be non native chain | ||
dest_chain: ChainType::Ethereum(dest_id.into()), | ||
dest_account: recipient, | ||
amount, | ||
}, | ||
}; | ||
|
||
let call = pallet_omni_bridge::Call::<Runtime>::pay_in { req: pay_in_request }; | ||
RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.