Skip to content

Commit

Permalink
Migrate KTON DAO stuffs (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
aurexav authored Jul 11, 2024
1 parent c1cc484 commit 239a502
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 58 deletions.
61 changes: 29 additions & 32 deletions pallet/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -138,7 +138,7 @@ pub mod pallet {
#[pallet::constant]
type MaxDeposits: Get<u32>;

/// The address of KTON reward distribution contract.
/// Address of the KTON reward distribution contract.
#[pallet::constant]
type KtonRewardDistributionContract: Get<Self::AccountId>;
}
Expand Down Expand Up @@ -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<T> Get<T> 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<T>(PhantomData<T>);
impl<T> KtonStakerNotification for KtonStakerNotifier<T>
where
Expand All @@ -999,24 +1004,11 @@ where
<T as frame_system::Config>::AccountId: Into<H160>,
{
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,
Expand All @@ -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) = <darwinia_ethtx_forwarder::Pallet<T>>::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:?}");
}
}
}
94 changes: 94 additions & 0 deletions pallet/staking/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>()
where
T: Config + darwinia_ethtx_forwarder::Config,
T::RuntimeOrigin: Into<Result<ForwardEthOrigin, T::RuntimeOrigin>> + From<ForwardEthOrigin>,
<T as frame_system::Config>::AccountId: Into<H160>,
{
// 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) = <darwinia_ethtx_forwarder::Pallet<T>>::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) = <darwinia_ethtx_forwarder::Pallet<T>>::forward_transact(
ForwardEthOrigin::ForwardEth(sender).into(),
req,
) {
log::error!("failed to call `acceptOwnershipFromOldDistribution` due to {e:?}");
}
}
}
6 changes: 1 addition & 5 deletions runtime/koi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
(
migration::CustomOnRuntimeUpgrade,
cumulus_pallet_xcmp_queue::migration::v4::MigrationToV4<Runtime>,
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
),
migration::CustomOnRuntimeUpgrade,
>;

/// Runtime version.
Expand Down
23 changes: 2 additions & 21 deletions runtime/koi/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<pallet_balances::Pallet<Runtime>>();
darwinia_staking::migration::migrate_staking_reward_distribution_contract::<Runtime>();

// frame_support::weights::Weight::zero()
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(2, w)
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(10, 10)
}

0 comments on commit 239a502

Please sign in to comment.