diff --git a/pallet/staking/src/lib.rs b/pallet/staking/src/lib.rs index 9c94242f9..2ef26f6ac 100644 --- a/pallet/staking/src/lib.rs +++ b/pallet/staking/src/lib.rs @@ -47,7 +47,7 @@ pub use darwinia_staking_traits::*; // crates.io use codec::FullCodec; -use ethabi::{Function, Param, ParamType, StateMutability, Token}; +use ethabi::{Function, Param, ParamType, StateMutability}; use ethereum::TransactionAction; // darwinia use darwinia_ethtx_forwarder::{ForwardEthOrigin, ForwardRequest, TxType}; @@ -138,7 +138,7 @@ pub mod pallet { #[pallet::constant] type MaxDeposits: Get; - /// The address of KTON reward distribution contract. + /// Address of the KTON reward distribution contract. #[pallet::constant] type KtonRewardDistributionContract: Get; } @@ -971,26 +971,31 @@ where PalletId(*b"da/staki").into_account_truncating() } -/// The address of the RewardsDistribution. -/// 0x000000000Ae5DB7BDAf8D071e680452e33d91Dd5. +/// The address of the `StakingRewardDistribution` contract. +/// 0x0DBFbb1Ab6e42F89661B4f98d5d0acdBE21d1ffC. pub struct KtonRewardDistributionContract; impl Get for KtonRewardDistributionContract where T: From<[u8; 20]>, { fn get() -> T { - [0, 0, 0, 0, 10, 229, 219, 123, 218, 248, 208, 113, 230, 128, 69, 46, 51, 217, 29, 213] - .into() + [ + 13, 191, 187, 26, 182, 228, 47, 137, 102, 27, 79, 152, 213, 208, 172, 219, 226, 29, 31, + 252, + ] + .into() } } -/// KTON staker contact notification interface. +/// `StakingRewardDistribution` contact notification interface. pub trait KtonStakerNotification { /// Notify the KTON staker contract. fn notify(_: Balance) {} } impl KtonStakerNotification for () {} -/// KTON staker contact notifier. +/// `StakingRewardDistribution` contact notifier. +/// +/// https://github.com/darwinia-network/KtonDAO/blob/2de20674f2ef90b749ade746d0768c7bda356402/src/staking/KtonDAOVault.sol#L40. pub struct KtonStakerNotifier(PhantomData); impl KtonStakerNotification for KtonStakerNotifier where @@ -999,24 +1004,11 @@ where ::AccountId: Into, { fn notify(amount: Balance) { - // KTONStakingRewards - // 0x000000000419683a1a03AbC21FC9da25fd2B4dD7 - let staking_reward = - H160([0, 0, 0, 0, 4, 25, 104, 58, 26, 3, 171, 194, 31, 201, 218, 37, 253, 43, 77, 215]); - - let reward_distr = T::KtonRewardDistributionContract::get().into(); - // https://github.com/darwinia-network/kton-staker/blob/175f0ec131d4aef3bf64cfb2fce1d262e7ce9140/src/RewardsDistribution.sol#L11 + let krd_contract = T::KtonRewardDistributionContract::get().into(); #[allow(deprecated)] let function = Function { name: "distributeRewards".into(), - inputs: vec![ - Param { - name: "ktonStakingRewards".into(), - kind: ParamType::Address, - internal_type: None, - }, - Param { name: "reward".into(), kind: ParamType::Uint(256), internal_type: None }, - ], + inputs: Vec::new(), outputs: vec![Param { name: "success or not".into(), kind: ParamType::Bool, @@ -1025,26 +1017,31 @@ where constant: None, state_mutability: StateMutability::Payable, }; + let input = match function.encode_input(&[]) { + Ok(i) => i, + Err(e) => { + log::error!("failed to encode input due to {e:?}"); - let request = ForwardRequest { + return; + }, + }; + let req = ForwardRequest { tx_type: TxType::LegacyTransaction, - action: TransactionAction::Call(reward_distr), - value: U256::zero(), - input: function - .encode_input(&[Token::Address(staking_reward), Token::Uint(amount.into())]) - .unwrap_or_default(), + action: TransactionAction::Call(krd_contract), + value: U256::from(amount), + input, gas_limit: U256::from(1_000_000), }; - // Treasury account. + // Treasury pallet account. let sender = H160([ 109, 111, 100, 108, 100, 97, 47, 116, 114, 115, 114, 121, 0, 0, 0, 0, 0, 0, 0, 0, ]); if let Err(e) = >::forward_transact( ForwardEthOrigin::ForwardEth(sender).into(), - request, + req, ) { - log::error!("[pallet::staking] failed to notify KTON staker contract due to {e:?}"); + log::error!("failed to call `distributeRewards` on `KtonRewardDistributionContract` contract due to {e:?}"); } } } diff --git a/pallet/staking/src/migration.rs b/pallet/staking/src/migration.rs index 33af6418d..adbaa104f 100644 --- a/pallet/staking/src/migration.rs +++ b/pallet/staking/src/migration.rs @@ -4,3 +4,97 @@ pub mod v1; /// Migration version 2. pub mod v2; + +// crates.io +use ethabi::Token; +// self +use crate::*; + +/// Migrate `StakingRewardDistribution` contract. +/// +/// https://github.com/darwinia-network/KtonDAO/blob/722bdf62942868de2eeaf19bc70d7a165fc031af/src/Owned.sol#L5. +/// https://github.com/darwinia-network/KtonDAO/blob/045b5b59d56b426cb8b06b9da912d0a3ad0a636d/src/staking/KtonDAOVault.sol#L36. +pub fn migrate_staking_reward_distribution_contract() +where + T: Config + darwinia_ethtx_forwarder::Config, + T::RuntimeOrigin: Into> + From, + ::AccountId: Into, +{ + // Treasury pallet account. + let sender = + H160([109, 111, 100, 108, 100, 97, 47, 116, 114, 115, 114, 121, 0, 0, 0, 0, 0, 0, 0, 0]); + let krd_contract = T::KtonRewardDistributionContract::get().into(); + // 0x000000000Ae5DB7BDAf8D071e680452e33d91Dd5. + let krd_contract_old = H160([ + 0, 0, 0, 0, 10, 229, 219, 123, 218, 248, 208, 113, 230, 128, 69, 46, 51, 217, 29, 213, + ]); + + { + #[allow(deprecated)] + let func = Function { + name: "nominateNewOwner".into(), + inputs: vec![Param { + name: "_owner".into(), + kind: ParamType::Address, + internal_type: None, + }], + outputs: Vec::new(), + constant: None, + state_mutability: StateMutability::Payable, + }; + let input = match func.encode_input(&[Token::Address(krd_contract)]) { + Ok(i) => i, + Err(e) => { + log::error!("failed to encode input due to {e:?}"); + + return; + }, + }; + let req = ForwardRequest { + tx_type: TxType::LegacyTransaction, + action: TransactionAction::Call(krd_contract_old), + value: U256::zero(), + input, + gas_limit: U256::from(1_000_000), + }; + + if let Err(e) = >::forward_transact( + ForwardEthOrigin::ForwardEth(sender).into(), + req, + ) { + log::error!("failed to call `nominateNewOwner` due to {e:?}"); + } + } + { + #[allow(deprecated)] + let func = Function { + name: "acceptOwnershipFromOldDistribution".into(), + inputs: Vec::new(), + outputs: Vec::new(), + constant: None, + state_mutability: StateMutability::Payable, + }; + let input = match func.encode_input(&[]) { + Ok(i) => i, + Err(e) => { + log::error!("failed to encode input due to {e:?}"); + + return; + }, + }; + let req = ForwardRequest { + tx_type: TxType::LegacyTransaction, + action: TransactionAction::Call(krd_contract), + value: U256::zero(), + input, + gas_limit: U256::from(1_000_000), + }; + + if let Err(e) = >::forward_transact( + ForwardEthOrigin::ForwardEth(sender).into(), + req, + ) { + log::error!("failed to call `acceptOwnershipFromOldDistribution` due to {e:?}"); + } + } +} diff --git a/runtime/koi/src/lib.rs b/runtime/koi/src/lib.rs index 62cd0a398..028828162 100644 --- a/runtime/koi/src/lib.rs +++ b/runtime/koi/src/lib.rs @@ -71,11 +71,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsWithSystem, - ( - migration::CustomOnRuntimeUpgrade, - cumulus_pallet_xcmp_queue::migration::v4::MigrationToV4, - pallet_xcm::migration::MigrateToLatestXcmVersion, - ), + migration::CustomOnRuntimeUpgrade, >; /// Runtime version. diff --git a/runtime/koi/src/migration.rs b/runtime/koi/src/migration.rs index 37a5440f7..60755eafa 100644 --- a/runtime/koi/src/migration.rs +++ b/runtime/koi/src/migration.rs @@ -45,27 +45,8 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { } fn migrate() -> frame_support::weights::Weight { - // core - use core::str::FromStr; - - const REVERT_BYTECODE: [u8; 5] = [0x60, 0x00, 0x60, 0x00, 0xFD]; - // DOT equals to the 0x405 in the pallet-evm runtime. - const ADDRESS: &str = "0x0000000000000000000000000000000000000405"; - - if let Ok(addr) = H160::from_str(ADDRESS) { - EVM::create_account(addr, REVERT_BYTECODE.to_vec()); - } - - let mut w = 3; - - w += migration_helper::PalletCleaner { - name: b"Identity", - values: &[b"Registrars"], - maps: &[b"SuperOf", b"SubsOf", b"Registrars"], - } - .remove_all(); - w += migration_helper::migrate_identity::>(); + darwinia_staking::migration::migrate_staking_reward_distribution_contract::(); // frame_support::weights::Weight::zero() - ::DbWeight::get().reads_writes(2, w) + ::DbWeight::get().reads_writes(10, 10) }