Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Xtokens precompile #2489

Merged
merged 13 commits into from
Mar 24, 2023
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
2 changes: 2 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 modules/transaction-pause/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors = ["Acala Developers"]
edition = "2021"

[dependencies]
hex-literal = "0.3.1"
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
scale-info = { version = "2.2.0", default-features = false, features = ["derive"] }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions modules/transaction-pause/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use sp_core::H160;
use sp_runtime::DispatchResult;
use sp_std::{prelude::*, vec::Vec};

pub mod migrations;
mod mock;
mod tests;
pub mod weights;
Expand Down
54 changes: 54 additions & 0 deletions modules/transaction-pause/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// This file is part of Acala.

// Copyright (C) 2020-2023 Acala Foundation.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program 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.

// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{Config, PausedEvmPrecompiles, Weight, H160};
use frame_support::{log, traits::OnRuntimeUpgrade};
use hex_literal::hex;
use sp_core::Get;
use sp_std::{marker::PhantomData, vec};

pub struct MigrateEvmPrecompile<T>(PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateEvmPrecompile<T> {
fn on_runtime_upgrade() -> Weight {
let mut weight: Weight = Weight::zero();

let address_list = vec![
H160(hex!("0000000000000000000000000000000000000406")), // STABLE_ASSET
H160(hex!("0000000000000000000000000000000000000407")), // HOMA
H160(hex!("0000000000000000000000000000000000000409")), // HONZON
H160(hex!("000000000000000000000000000000000000040a")), // INCENTIVES
H160(hex!("000000000000000000000000000000000000040b")), // XTOKENS
];

log::info!(
target: "transaction-pause",
"MigrateEvmPrecompile::on_runtime_upgrade execute, will pause the address {:?}", address_list
);

for addr in address_list.iter() {
PausedEvmPrecompiles::<T>::mutate_exists(addr, |maybe_paused| {
if maybe_paused.is_none() {
*maybe_paused = Some(());
}
});
}

weight.saturating_accrue(T::DbWeight::get().writes(address_list.len().try_into().unwrap()));
weight
}
}
1 change: 1 addition & 0 deletions modules/xcm-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub mod module {
T::SovereignSubAccountLocationConvert::convert(sub_account_index),
WeightLimit::Limited(Self::xcm_dest_weight_and_fee(XcmInterfaceOperation::XtokensTransfer).0),
)
.map(|_| ())
}

/// Send XCM message to the relaychain for sub account to withdraw_unbonded staking currency
Expand Down
40 changes: 36 additions & 4 deletions modules/xcm-interface/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use frame_support::{
traits::{ConstU128, ConstU32, ConstU64, Everything, Nothing},
};
use frame_system::EnsureSignedBy;
use orml_traits::xcm_transfer::Transferred;
use primitives::{CurrencyId, TokenSymbol};
use sp_core::H256;
use sp_runtime::{testing::Header, traits::IdentityLookup, AccountId32};
Expand Down Expand Up @@ -147,17 +148,28 @@ impl XcmTransfer<AccountId, Balance, CurrencyId> for MockXcmTransfer {
_amount: Balance,
_dest: MultiLocation,
_dest_weight_limit: WeightLimit,
) -> DispatchResult {
) -> Result<Transferred<AccountId32>, DispatchError> {
unimplemented!()
}

/// Transfer `MultiAsset`
fn transfer_multi_asset(
fn transfer_multiasset(
_who: AccountId,
_asset: MultiAsset,
_dest: MultiLocation,
_dest_weight_limit: WeightLimit,
) -> DispatchResult {
) -> Result<Transferred<AccountId32>, DispatchError> {
unimplemented!()
}

fn transfer_with_fee(
_who: AccountId,
_currency_id: CurrencyId,
_amount: Balance,
_fee: Balance,
_dest: MultiLocation,
_dest_weight_limit: WeightLimit,
) -> Result<Transferred<AccountId>, DispatchError> {
unimplemented!()
}

Expand All @@ -168,7 +180,27 @@ impl XcmTransfer<AccountId, Balance, CurrencyId> for MockXcmTransfer {
_fee: MultiAsset,
_dest: MultiLocation,
_dest_weight_limit: WeightLimit,
) -> DispatchResult {
) -> Result<Transferred<AccountId32>, DispatchError> {
unimplemented!()
}

fn transfer_multicurrencies(
_who: AccountId,
_currencies: Vec<(CurrencyId, Balance)>,
_fee_item: u32,
_dest: MultiLocation,
_dest_weight_limit: WeightLimit,
) -> Result<Transferred<AccountId32>, DispatchError> {
unimplemented!()
}

fn transfer_multiassets(
_who: AccountId,
_assets: MultiAssets,
_fee: MultiAsset,
_dest: MultiLocation,
_dest_weight_limit: WeightLimit,
) -> Result<Transferred<AccountId32>, DispatchError> {
unimplemented!()
}
}
Expand Down
2 changes: 1 addition & 1 deletion orml
2 changes: 1 addition & 1 deletion predeploy-contracts
Submodule predeploy-contracts updated 64 files
+24 −292 README.md
+13 −32 contracts/dex/DEX.sol
+66 −16 contracts/dex/IDEX.sol
+269 −0 contracts/docs/dex/DEX.md
+269 −0 contracts/docs/dex/IDEX.md
+186 −0 contracts/docs/elin/contracts/token/ERC20/IERC20.md
+12 −0 contracts/docs/elin/contracts/utils/math/SafeMath.md
+102 −0 contracts/docs/evm-accounts/EVMAccounts.md
+102 −0 contracts/docs/evm-accounts/IEVMAccounts.md
+274 −0 contracts/docs/evm/EVM.md
+274 −0 contracts/docs/evm/IEVM.md
+166 −0 contracts/docs/homa/Homa.md
+166 −0 contracts/docs/homa/IHoma.md
+192 −0 contracts/docs/honzon/Honzon.md
+192 −0 contracts/docs/honzon/IHonzon.md
+213 −0 contracts/docs/incentives/Incentives.md
+213 −0 contracts/docs/incentives/InterfaceIncentives.md
+60 −0 contracts/docs/nft/NFT.md
+37 −0 contracts/docs/oracle/IOracle.md
+37 −0 contracts/docs/oracle/Oracle.md
+142 −0 contracts/docs/schedule/ISchedule.md
+142 −0 contracts/docs/schedule/Schedule.md
+381 −0 contracts/docs/stable-asset/IStableAsset.md
+381 −0 contracts/docs/stable-asset/StableAsset.md
+12 −0 contracts/docs/token/MultiCurrency.md
+283 −0 contracts/docs/token/Token.md
+525 −0 contracts/docs/utils/ADDRESS.md
+187 −0 contracts/docs/xtokens/IXtokens.md
+187 −0 contracts/docs/xtokens/Xtokens.md
+8 −12 contracts/evm-accounts/EVMAccounts.sol
+18 −6 contracts/evm-accounts/IEVMAccounts.sol
+15 −34 contracts/evm/EVM.sol
+42 −14 contracts/evm/IEVM.sol
+11 −28 contracts/homa/Homa.sol
+29 −12 contracts/homa/IHoma.sol
+11 −34 contracts/honzon/Honzon.sol
+43 −19 contracts/honzon/IHonzon.sol
+11 −24 contracts/incentives/Incentives.sol
+51 −12 contracts/incentives/InterfaceIncentives.sol
+7 −2 contracts/oracle/IOracle.sol
+6 −4 contracts/oracle/Oracle.sol
+1 −1 contracts/package.json
+42 −16 contracts/schedule/ISchedule.sol
+8 −12 contracts/schedule/Schedule.sol
+97 −23 contracts/stable-asset/IStableAsset.sol
+16 −77 contracts/stable-asset/StableAsset.sol
+5 −0 contracts/token/MultiCurrency.sol
+63 −114 contracts/token/Token.sol
+1 −0 contracts/utils/AcalaAddress.d.ts
+2 −0 contracts/utils/AcalaAddress.js
+1 −0 contracts/utils/AcalaAddress.sol
+1 −0 contracts/utils/KaruraAddress.d.ts
+2 −0 contracts/utils/KaruraAddress.js
+1 −0 contracts/utils/KaruraAddress.sol
+1 −0 contracts/utils/MandalaAddress.d.ts
+2 −0 contracts/utils/MandalaAddress.js
+1 −0 contracts/utils/MandalaAddress.sol
+113 −0 contracts/xtokens/IXtokens.sol
+180 −0 contracts/xtokens/Xtokens.sol
+4 −0 generate/generate.js
+6 −2 hardhat.config.ts
+1 −0 package.json
+5 −0 resources/bytecodes.json
+28 −16 yarn.lock
1 change: 1 addition & 0 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,7 @@ pub type Executive = frame_executive::Executive<
// Note: The following Migrations do not use the StorageVersion feature, must to be removed after the upgrade
module_asset_registry::migrations::MigrateV1MultiLocationToV3<Runtime>,
module_xcm_interface::migrations::MigrateXcmDestWeightAndFee<Runtime>,
module_transaction_pause::migrations::MigrateEvmPrecompile<Runtime>,
),
>;

Expand Down
2 changes: 2 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ module-transaction-pause = { path = "../../modules/transaction-pause", default-f
orml-oracle = { path = "../../orml/oracle", default-features = false }
orml-traits = { path = "../../orml/traits", default-features = false }
orml-tokens = { path = "../../orml/tokens", default-features = false }
orml-xtokens = { path = "../../orml/xtokens", default-features = false }
orml-bencher = { path = "../../orml/bencher", default-features = false, optional = true }
orml-nft = { path = "../../orml/nft", default-features = false, optional = true }
orml-currencies = { path = "../../orml/currencies", default-features = false, optional = true }
Expand Down Expand Up @@ -120,6 +121,7 @@ std = [
"orml-rewards/std",
"orml-tokens/std",
"orml-traits/std",
"orml-xtokens/std",

"module-asset-registry/std",
"module-cdp-engine/std",
Expand Down
4 changes: 2 additions & 2 deletions runtime/common/src/precompile/evm_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ where
}
Action::GetEvmAddress => {
// bytes32
let input_data = input.bytes_at(1, 32)?;
let input_data = input.bytes32_at(1)?;

let mut buf = [0u8; 32];
buf.copy_from_slice(&input_data[..]);
Expand All @@ -107,7 +107,7 @@ where
}
Action::ClaimDefaultEvmAddress => {
// bytes32
let input_data = input.bytes_at(1, 32)?;
let input_data = input.bytes32_at(1)?;

let mut buf = [0u8; 32];
buf.copy_from_slice(&input_data[..]);
Expand Down
21 changes: 18 additions & 3 deletions runtime/common/src/precompile/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ pub trait InputT {
fn u64_at(&self, index: usize) -> Result<u64, Self::Error>;
fn u32_at(&self, index: usize) -> Result<u32, Self::Error>;

fn bytes_at(&self, start: usize, len: usize) -> Result<Vec<u8>, Self::Error>;
fn bytes_at(&self, start: usize) -> Result<Vec<u8>, Self::Error>;
fn bytes32_at(&self, start: usize) -> Result<Vec<u8>, Self::Error>;
fn bool_at(&self, index: usize) -> Result<bool, Self::Error>;
}

Expand Down Expand Up @@ -194,8 +195,18 @@ where
})
}

fn bytes_at(&self, index: usize, len: usize) -> Result<Vec<u8>, Self::Error> {
let bytes = self.nth_param(index, Some(len))?;
fn bytes_at(&self, index: usize) -> Result<Vec<u8>, Self::Error> {
let offset = self.u32_at(index)?;
let data_index = (offset as usize).saturating_div(PER_PARAM_BYTES).saturating_add(1);

let bytes_len = self.u32_at(data_index)?;
let bytes = self.nth_param(data_index.saturating_add(1), Some(bytes_len as usize))?;

Ok(bytes.to_vec())
}

fn bytes32_at(&self, index: usize) -> Result<Vec<u8>, Self::Error> {
let bytes = self.nth_param(index, Some(32))?;

Ok(bytes.to_vec())
}
Expand Down Expand Up @@ -249,6 +260,10 @@ impl Output {
ethabi::encode(&[Token::Bytes(b.to_vec())])
}

pub fn encode_bytes_tuple(b: Vec<&[u8]>) -> Vec<u8> {
ethabi::encode(&[Token::Tuple(b.into_iter().map(|v| Token::Bytes(v.to_vec())).collect())])
}

pub fn encode_fixed_bytes(b: &[u8]) -> Vec<u8> {
ethabi::encode(&[Token::FixedBytes(b.to_vec())])
}
Expand Down
Loading