From 0fbef9d8bc35eedd1ca9bce855b61fd19b285c27 Mon Sep 17 00:00:00 2001 From: wangjj9219 <183318287@qq.com> Date: Mon, 8 Jan 2024 08:46:07 +0800 Subject: [PATCH] set evm origin when settle erc20 type CDP (#2685) --- Cargo.lock | 4 ++ modules/cdp-engine/Cargo.toml | 2 + modules/cdp-engine/src/lib.rs | 20 ++++++++-- modules/cdp-engine/src/mock.rs | 52 ++++++++++++++++++++++++-- modules/honzon/Cargo.toml | 2 + modules/honzon/src/mock.rs | 53 ++++++++++++++++++++++++++- runtime/acala/src/lib.rs | 3 ++ runtime/common/src/precompile/mock.rs | 3 ++ runtime/karura/src/lib.rs | 3 ++ runtime/mandala/src/lib.rs | 3 ++ 10 files changed, 137 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 418ebf8471..c61d2ac2a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6677,7 +6677,9 @@ dependencies = [ "log", "module-cdp-treasury", "module-dex", + "module-evm", "module-evm-accounts", + "module-evm-bridge", "module-loans", "module-support", "nutsfinance-stable-asset", @@ -7034,7 +7036,9 @@ dependencies = [ "frame-system", "module-cdp-engine", "module-cdp-treasury", + "module-evm", "module-evm-accounts", + "module-evm-bridge", "module-loans", "module-support", "orml-currencies", diff --git a/modules/cdp-engine/Cargo.toml b/modules/cdp-engine/Cargo.toml index 2ef3a00b54..2cabd3ede1 100644 --- a/modules/cdp-engine/Cargo.toml +++ b/modules/cdp-engine/Cargo.toml @@ -32,6 +32,8 @@ module-dex = { workspace = true, features = ["std"] } module-cdp-treasury = { workspace = true, features = ["std"] } module-evm-accounts = { workspace = true, features = ["std"] } orml-tokens = { workspace = true, features = ["std"] } +module-evm = { workspace = true, features = ["std"] } +module-evm-bridge = { workspace = true, features = ["std"] } [features] default = ["std"] diff --git a/modules/cdp-engine/src/lib.rs b/modules/cdp-engine/src/lib.rs index 406cf76506..ad84cff6be 100644 --- a/modules/cdp-engine/src/lib.rs +++ b/modules/cdp-engine/src/lib.rs @@ -34,9 +34,9 @@ use frame_system::{ pallet_prelude::*, }; use module_support::{ - AddressMapping, CDPTreasury, CDPTreasuryExtended, DEXManager, EmergencyShutdown, ExchangeRate, FractionalRate, - InvokeContext, LiquidateCollateral, LiquidationEvmBridge, Price, PriceProvider, Rate, Ratio, RiskManager, Swap, - SwapLimit, + AddressMapping, CDPTreasury, CDPTreasuryExtended, DEXManager, EVMBridge, EmergencyShutdown, ExchangeRate, + FractionalRate, InvokeContext, LiquidateCollateral, LiquidationEvmBridge, Price, PriceProvider, Rate, Ratio, + RiskManager, Swap, SwapLimit, }; use orml_traits::{Change, GetByKey, MultiCurrency}; use orml_utilities::OffchainErr; @@ -208,6 +208,12 @@ pub mod module { type EvmAddressMapping: AddressMapping; + /// Evm Bridge for getting info of contracts from the EVM. + type EVMBridge: EVMBridge; + + /// Evm Origin account when settle erc20 type CDP + type SettleErc20EvmOrigin: Get; + /// Weight information for the extrinsics in this module. type WeightInfo: WeightInfo; } @@ -1153,9 +1159,17 @@ impl Pallet { let confiscate_collateral_amount = sp_std::cmp::min(settle_price.saturating_mul_int(bad_debt_value), collateral); + if let CurrencyId::Erc20(_) = currency_id { + T::EVMBridge::set_origin(T::SettleErc20EvmOrigin::get()); + } + // confiscate collateral and all debit >::confiscate_collateral_and_debit(&who, currency_id, confiscate_collateral_amount, debit)?; + if let CurrencyId::Erc20(_) = currency_id { + T::EVMBridge::kill_origin(); + } + Self::deposit_event(Event::SettleCDPInDebit { collateral_type: currency_id, owner: who, diff --git a/modules/cdp-engine/src/mock.rs b/modules/cdp-engine/src/mock.rs index c22eeda698..d1bc1ae374 100644 --- a/modules/cdp-engine/src/mock.rs +++ b/modules/cdp-engine/src/mock.rs @@ -29,7 +29,7 @@ use frame_support::{ use frame_system::EnsureSignedBy; use module_support::{mocks::MockStableAsset, AuctionManager, EmergencyShutdown, SpecificJointsSwap}; use orml_traits::parameter_type_with_key; -use primitives::{DexShare, Moment, TokenSymbol, TradingPair}; +use primitives::{evm::convert_decimals_to_evm, DexShare, Moment, ReserveIdentifier, TokenSymbol, TradingPair}; use sp_core::{crypto::AccountId32, H256}; use sp_runtime::{ testing::TestXt, @@ -99,7 +99,7 @@ impl orml_tokens::Config for Runtime { type CurrencyHooks = (); type MaxLocks = (); type MaxReserves = (); - type ReserveIdentifier = [u8; 8]; + type ReserveIdentifier = ReserveIdentifier; type DustRemovalWhitelist = Nothing; } @@ -111,7 +111,7 @@ impl pallet_balances::Config for Runtime { type AccountStore = frame_system::Pallet; type MaxLocks = (); type MaxReserves = (); - type ReserveIdentifier = [u8; 8]; + type ReserveIdentifier = ReserveIdentifier; type WeightInfo = (); type RuntimeHoldReason = RuntimeHoldReason; type RuntimeFreezeReason = RuntimeFreezeReason; @@ -285,6 +285,47 @@ impl module_evm_accounts::Config for Runtime { type WeightInfo = (); } +parameter_types! { + pub NetworkContractSource: EvmAddress = EvmAddress::from_str("1000000000000000000000000000000000000001").unwrap(); +} + +ord_parameter_types! { + pub const CouncilAccount: AccountId = AccountId::from([1u8; 32]); + pub const NetworkContractAccount: AccountId = AccountId::from([0u8; 32]); + pub const StorageDepositPerByte: u128 = convert_decimals_to_evm(10); +} + +impl module_evm::Config for Runtime { + type AddressMapping = module_evm_accounts::EvmAddressMapping; + type Currency = PalletBalances; + type TransferAll = (); + type NewContractExtraBytes = ConstU32<1>; + type StorageDepositPerByte = StorageDepositPerByte; + type TxFeePerGas = ConstU128<10>; + type RuntimeEvent = RuntimeEvent; + type PrecompilesType = (); + type PrecompilesValue = (); + type GasToWeight = (); + type ChargeTransactionPayment = module_support::mocks::MockReservedTransactionPayment; + type NetworkContractOrigin = EnsureSignedBy; + type NetworkContractSource = NetworkContractSource; + + type DeveloperDeposit = ConstU128<1000>; + type PublicationFee = ConstU128<200>; + type TreasuryAccount = TreasuryAccount; + type FreePublicationOrigin = EnsureSignedBy; + + type Runner = module_evm::runner::stack::Runner; + type FindAuthor = (); + type Task = (); + type IdleScheduler = (); + type WeightInfo = (); +} + +impl module_evm_bridge::Config for Runtime { + type EVM = EVM; +} + thread_local! { static IS_SHUTDOWN: RefCell = RefCell::new(false); } @@ -379,6 +420,7 @@ parameter_types! { pub MaxSwapSlippageCompareToOracle: Ratio = Ratio::saturating_from_rational(50, 100); pub MaxLiquidationContractSlippage: Ratio = Ratio::saturating_from_rational(80, 100); pub const CDPEnginePalletId: PalletId = PalletId(*b"aca/cdpe"); + pub const SettleErc20EvmOrigin: AccountId = AccountId32::new([255u8; 32]); } impl Config for Runtime { @@ -405,6 +447,8 @@ impl Config for Runtime { type PalletId = CDPEnginePalletId; type EvmAddressMapping = module_evm_accounts::EvmAddressMapping; type Swap = SpecificJointsSwap; + type EVMBridge = module_evm_bridge::EVMBridge; + type SettleErc20EvmOrigin = SettleErc20EvmOrigin; type WeightInfo = (); } @@ -422,6 +466,8 @@ construct_runtime!( DEXModule: module_dex, Timestamp: pallet_timestamp, EvmAccounts: module_evm_accounts, + EVM: module_evm, + EVMBridge: module_evm_bridge, } ); diff --git a/modules/honzon/Cargo.toml b/modules/honzon/Cargo.toml index a7b2daecc8..eefd15e5f7 100644 --- a/modules/honzon/Cargo.toml +++ b/modules/honzon/Cargo.toml @@ -27,6 +27,8 @@ orml-traits = { workspace = true, features = ["std"] } orml-tokens = { workspace = true, features = ["std"] } module-cdp-treasury = { workspace = true, features = ["std"] } module-evm-accounts = { workspace = true, features = ["std"] } +module-evm = { workspace = true, features = ["std"] } +module-evm-bridge = { workspace = true, features = ["std"] } [features] default = ["std"] diff --git a/modules/honzon/src/mock.rs b/modules/honzon/src/mock.rs index 3d467df16b..e7d5ddaab8 100644 --- a/modules/honzon/src/mock.rs +++ b/modules/honzon/src/mock.rs @@ -33,14 +33,17 @@ use module_support::{ SpecificJointsSwap, }; use orml_traits::parameter_type_with_key; -use primitives::{Balance, Moment, ReserveIdentifier, TokenSymbol}; +use primitives::{ + evm::{convert_decimals_to_evm, EvmAddress}, + Balance, Moment, ReserveIdentifier, TokenSymbol, +}; use sp_core::{crypto::AccountId32, H256}; use sp_runtime::{ testing::TestXt, traits::{AccountIdConversion, IdentityLookup, One as OneT}, BuildStorage, FixedPointNumber, }; -use sp_std::cell::RefCell; +use sp_std::{cell::RefCell, str::FromStr}; mod honzon { pub use super::super::*; @@ -244,6 +247,47 @@ impl module_evm_accounts::Config for Runtime { type WeightInfo = (); } +parameter_types! { + pub NetworkContractSource: EvmAddress = EvmAddress::from_str("1000000000000000000000000000000000000001").unwrap(); +} + +ord_parameter_types! { + pub const CouncilAccount: AccountId = AccountId::from([1u8; 32]); + pub const NetworkContractAccount: AccountId = AccountId::from([0u8; 32]); + pub const StorageDepositPerByte: u128 = convert_decimals_to_evm(10); +} + +impl module_evm::Config for Runtime { + type AddressMapping = module_evm_accounts::EvmAddressMapping; + type Currency = PalletBalances; + type TransferAll = (); + type NewContractExtraBytes = ConstU32<1>; + type StorageDepositPerByte = StorageDepositPerByte; + type TxFeePerGas = ConstU128<10>; + type RuntimeEvent = RuntimeEvent; + type PrecompilesType = (); + type PrecompilesValue = (); + type GasToWeight = (); + type ChargeTransactionPayment = module_support::mocks::MockReservedTransactionPayment; + type NetworkContractOrigin = EnsureSignedBy; + type NetworkContractSource = NetworkContractSource; + + type DeveloperDeposit = ConstU128<1000>; + type PublicationFee = ConstU128<200>; + type TreasuryAccount = TreasuryAccount; + type FreePublicationOrigin = EnsureSignedBy; + + type Runner = module_evm::runner::stack::Runner; + type FindAuthor = (); + type Task = (); + type IdleScheduler = (); + type WeightInfo = (); +} + +impl module_evm_bridge::Config for Runtime { + type EVM = EVM; +} + parameter_type_with_key! { pub MinimumCollateralAmount: |_currency_id: CurrencyId| -> Balance { 10 @@ -257,6 +301,7 @@ parameter_types! { pub MaxSwapSlippageCompareToOracle: Ratio = Ratio::saturating_from_rational(50, 100); pub MaxLiquidationContractSlippage: Ratio = Ratio::saturating_from_rational(80, 100); pub const CDPEnginePalletId: PalletId = PalletId(*b"aca/cdpe"); + pub const SettleErc20EvmOrigin: AccountId = AccountId32::new([255u8; 32]); } impl module_cdp_engine::Config for Runtime { @@ -283,6 +328,8 @@ impl module_cdp_engine::Config for Runtime { type PalletId = CDPEnginePalletId; type EvmAddressMapping = module_evm_accounts::EvmAddressMapping; type Swap = SpecificJointsSwap<(), AlternativeSwapPathJointList>; + type EVMBridge = module_evm_bridge::EVMBridge; + type SettleErc20EvmOrigin = SettleErc20EvmOrigin; type WeightInfo = (); } @@ -308,6 +355,8 @@ construct_runtime!( CDPEngineModule: module_cdp_engine, Timestamp: pallet_timestamp, EvmAccounts: module_evm_accounts, + EVM: module_evm, + EVMBridge: module_evm_bridge, } ); diff --git a/runtime/acala/src/lib.rs b/runtime/acala/src/lib.rs index 7f26d406fd..0652d22b68 100644 --- a/runtime/acala/src/lib.rs +++ b/runtime/acala/src/lib.rs @@ -1085,6 +1085,7 @@ parameter_types! { pub MinimumDebitValue: Balance = 50 * dollar(AUSD); pub MaxSwapSlippageCompareToOracle: Ratio = Ratio::saturating_from_rational(10, 100); pub MaxLiquidationContractSlippage: Ratio = Ratio::saturating_from_rational(15, 100); + pub SettleErc20EvmOrigin: AccountId = AccountId::from(hex_literal::hex!("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); // `26fFquxSECczieT6xrgG9uvg7LaEc1vj5M6SmX5K6QYN6TGZ` } impl module_cdp_engine::Config for Runtime { @@ -1112,6 +1113,8 @@ impl module_cdp_engine::Config for Runtime { type PalletId = CDPEnginePalletId; type EvmAddressMapping = module_evm_accounts::EvmAddressMapping; type Swap = AcalaSwap; + type EVMBridge = module_evm_bridge::EVMBridge; + type SettleErc20EvmOrigin = SettleErc20EvmOrigin; type WeightInfo = weights::module_cdp_engine::WeightInfo; } diff --git a/runtime/common/src/precompile/mock.rs b/runtime/common/src/precompile/mock.rs index a4ee24c929..8946e8c15f 100644 --- a/runtime/common/src/precompile/mock.rs +++ b/runtime/common/src/precompile/mock.rs @@ -430,6 +430,7 @@ parameter_types! { pub DefaultLiquidationPenalty: FractionalRate = FractionalRate::try_from(Rate::saturating_from_rational(10, 100)).unwrap(); pub MaxLiquidationContractSlippage: Ratio = Ratio::saturating_from_rational(15, 100); pub CDPEnginePalletId: PalletId = PalletId(*b"aca/cdpe"); + pub SettleErc20EvmOrigin: AccountId = AccountId::from(hex_literal::hex!("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); } impl module_cdp_engine::Config for Test { @@ -456,6 +457,8 @@ impl module_cdp_engine::Config for Test { type PalletId = CDPEnginePalletId; type EvmAddressMapping = module_evm_accounts::EvmAddressMapping; type Swap = SpecificJointsSwap; + type EVMBridge = module_evm_bridge::EVMBridge; + type SettleErc20EvmOrigin = SettleErc20EvmOrigin; type WeightInfo = (); } diff --git a/runtime/karura/src/lib.rs b/runtime/karura/src/lib.rs index 134dadae71..e4b78f262c 100644 --- a/runtime/karura/src/lib.rs +++ b/runtime/karura/src/lib.rs @@ -1092,6 +1092,7 @@ parameter_types! { pub MinimumDebitValue: Balance = 50 * dollar(KUSD); pub MaxSwapSlippageCompareToOracle: Ratio = Ratio::saturating_from_rational(10, 100); pub MaxLiquidationContractSlippage: Ratio = Ratio::saturating_from_rational(15, 100); + pub SettleErc20EvmOrigin: AccountId = AccountId::from(hex_literal::hex!("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); // `u5wKvsdTcsYQXeB9nvDQ7PppNHeVefghTzBY9niAhMPXpyo` } impl module_cdp_engine::Config for Runtime { @@ -1119,6 +1120,8 @@ impl module_cdp_engine::Config for Runtime { type PalletId = CDPEnginePalletId; type EvmAddressMapping = module_evm_accounts::EvmAddressMapping; type Swap = AcalaSwap; + type EVMBridge = module_evm_bridge::EVMBridge; + type SettleErc20EvmOrigin = SettleErc20EvmOrigin; type WeightInfo = weights::module_cdp_engine::WeightInfo; } diff --git a/runtime/mandala/src/lib.rs b/runtime/mandala/src/lib.rs index aefdef98d1..4c937156a9 100644 --- a/runtime/mandala/src/lib.rs +++ b/runtime/mandala/src/lib.rs @@ -1129,6 +1129,7 @@ parameter_types! { pub MinimumDebitValue: Balance = dollar(AUSD); pub MaxSwapSlippageCompareToOracle: Ratio = Ratio::saturating_from_rational(10, 100); pub MaxLiquidationContractSlippage: Ratio = Ratio::saturating_from_rational(15, 100); + pub SettleErc20EvmOrigin: AccountId = AccountId::from(hex_literal::hex!("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); // `5HrN7fHLXWcFiXPwwtq2EkSGns9eMt5P7SpeTPewumZy6ftb` } impl module_cdp_engine::Config for Runtime { @@ -1156,6 +1157,8 @@ impl module_cdp_engine::Config for Runtime { type PalletId = CDPEnginePalletId; type EvmAddressMapping = module_evm_accounts::EvmAddressMapping; type Swap = AcalaSwap; + type EVMBridge = module_evm_bridge::EVMBridge; + type SettleErc20EvmOrigin = SettleErc20EvmOrigin; type WeightInfo = weights::module_cdp_engine::WeightInfo; }