Skip to content

Commit

Permalink
set evm origin when settle erc20 type CDP (#2685)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjj9219 authored Jan 8, 2024
1 parent 6559c64 commit 0fbef9d
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions modules/cdp-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
20 changes: 17 additions & 3 deletions modules/cdp-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -208,6 +208,12 @@ pub mod module {

type EvmAddressMapping: AddressMapping<Self::AccountId>;

/// Evm Bridge for getting info of contracts from the EVM.
type EVMBridge: EVMBridge<Self::AccountId, Balance>;

/// Evm Origin account when settle erc20 type CDP
type SettleErc20EvmOrigin: Get<Self::AccountId>;

/// Weight information for the extrinsics in this module.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -1153,9 +1159,17 @@ impl<T: Config> Pallet<T> {
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
<LoansOf<T>>::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,
Expand Down
52 changes: 49 additions & 3 deletions modules/cdp-engine/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand All @@ -111,7 +111,7 @@ impl pallet_balances::Config for Runtime {
type AccountStore = frame_system::Pallet<Runtime>;
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type ReserveIdentifier = ReserveIdentifier;
type WeightInfo = ();
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
Expand Down Expand Up @@ -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<Runtime>;
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<PalletBalances>;
type NetworkContractOrigin = EnsureSignedBy<NetworkContractAccount, AccountId>;
type NetworkContractSource = NetworkContractSource;

type DeveloperDeposit = ConstU128<1000>;
type PublicationFee = ConstU128<200>;
type TreasuryAccount = TreasuryAccount;
type FreePublicationOrigin = EnsureSignedBy<CouncilAccount, AccountId>;

type Runner = module_evm::runner::stack::Runner<Self>;
type FindAuthor = ();
type Task = ();
type IdleScheduler = ();
type WeightInfo = ();
}

impl module_evm_bridge::Config for Runtime {
type EVM = EVM;
}

thread_local! {
static IS_SHUTDOWN: RefCell<bool> = RefCell::new(false);
}
Expand Down Expand Up @@ -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 {
Expand All @@ -405,6 +447,8 @@ impl Config for Runtime {
type PalletId = CDPEnginePalletId;
type EvmAddressMapping = module_evm_accounts::EvmAddressMapping<Runtime>;
type Swap = SpecificJointsSwap<DEXModule, AlternativeSwapPathJointList>;
type EVMBridge = module_evm_bridge::EVMBridge<Runtime>;
type SettleErc20EvmOrigin = SettleErc20EvmOrigin;
type WeightInfo = ();
}

Expand All @@ -422,6 +466,8 @@ construct_runtime!(
DEXModule: module_dex,
Timestamp: pallet_timestamp,
EvmAccounts: module_evm_accounts,
EVM: module_evm,
EVMBridge: module_evm_bridge,
}
);

Expand Down
2 changes: 2 additions & 0 deletions modules/honzon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
53 changes: 51 additions & 2 deletions modules/honzon/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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<Runtime>;
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<PalletBalances>;
type NetworkContractOrigin = EnsureSignedBy<NetworkContractAccount, AccountId>;
type NetworkContractSource = NetworkContractSource;

type DeveloperDeposit = ConstU128<1000>;
type PublicationFee = ConstU128<200>;
type TreasuryAccount = TreasuryAccount;
type FreePublicationOrigin = EnsureSignedBy<CouncilAccount, AccountId>;

type Runner = module_evm::runner::stack::Runner<Self>;
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
Expand All @@ -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 {
Expand All @@ -283,6 +328,8 @@ impl module_cdp_engine::Config for Runtime {
type PalletId = CDPEnginePalletId;
type EvmAddressMapping = module_evm_accounts::EvmAddressMapping<Runtime>;
type Swap = SpecificJointsSwap<(), AlternativeSwapPathJointList>;
type EVMBridge = module_evm_bridge::EVMBridge<Runtime>;
type SettleErc20EvmOrigin = SettleErc20EvmOrigin;
type WeightInfo = ();
}

Expand All @@ -308,6 +355,8 @@ construct_runtime!(
CDPEngineModule: module_cdp_engine,
Timestamp: pallet_timestamp,
EvmAccounts: module_evm_accounts,
EVM: module_evm,
EVMBridge: module_evm_bridge,
}
);

Expand Down
3 changes: 3 additions & 0 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1112,6 +1113,8 @@ impl module_cdp_engine::Config for Runtime {
type PalletId = CDPEnginePalletId;
type EvmAddressMapping = module_evm_accounts::EvmAddressMapping<Runtime>;
type Swap = AcalaSwap;
type EVMBridge = module_evm_bridge::EVMBridge<Runtime>;
type SettleErc20EvmOrigin = SettleErc20EvmOrigin;
type WeightInfo = weights::module_cdp_engine::WeightInfo<Runtime>;
}

Expand Down
3 changes: 3 additions & 0 deletions runtime/common/src/precompile/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -456,6 +457,8 @@ impl module_cdp_engine::Config for Test {
type PalletId = CDPEnginePalletId;
type EvmAddressMapping = module_evm_accounts::EvmAddressMapping<Test>;
type Swap = SpecificJointsSwap<DexModule, AlternativeSwapPathJointList>;
type EVMBridge = module_evm_bridge::EVMBridge<Test>;
type SettleErc20EvmOrigin = SettleErc20EvmOrigin;
type WeightInfo = ();
}

Expand Down
3 changes: 3 additions & 0 deletions runtime/karura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1119,6 +1120,8 @@ impl module_cdp_engine::Config for Runtime {
type PalletId = CDPEnginePalletId;
type EvmAddressMapping = module_evm_accounts::EvmAddressMapping<Runtime>;
type Swap = AcalaSwap;
type EVMBridge = module_evm_bridge::EVMBridge<Runtime>;
type SettleErc20EvmOrigin = SettleErc20EvmOrigin;
type WeightInfo = weights::module_cdp_engine::WeightInfo<Runtime>;
}

Expand Down
3 changes: 3 additions & 0 deletions runtime/mandala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1156,6 +1157,8 @@ impl module_cdp_engine::Config for Runtime {
type PalletId = CDPEnginePalletId;
type EvmAddressMapping = module_evm_accounts::EvmAddressMapping<Runtime>;
type Swap = AcalaSwap;
type EVMBridge = module_evm_bridge::EVMBridge<Runtime>;
type SettleErc20EvmOrigin = SettleErc20EvmOrigin;
type WeightInfo = weights::module_cdp_engine::WeightInfo<Runtime>;
}

Expand Down

0 comments on commit 0fbef9d

Please sign in to comment.