Skip to content

Commit

Permalink
feat: set destroy params on_runtimeupgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
kaigedong committed Mar 16, 2022
1 parent bf211fc commit 37b4b80
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 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 pallets/generic-func/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ name = "generic-func"
version = "0.1.0"

[dependencies]
bs58 = { package = "bs58", version = "0.4.0", default-features = false }
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = [
"derive"
] }
Expand Down
31 changes: 30 additions & 1 deletion pallets/generic-func/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use sp_runtime::{
traits::{BlakeTwo256, Saturating},
RandomNumberGenerator,
};
use sp_std::prelude::*;
use sp_std::{convert::TryInto, prelude::*};

pub use pallet::*;
pub use rpc_types::*;
Expand Down Expand Up @@ -115,6 +115,18 @@ pub mod pallet {
}
0
}

fn on_runtime_upgrade() -> Weight {
let rent_fee_pot: Vec<u8> = b"5GR31fgcHdrJ14eFW1xJmHhZJ56eQS7KynLKeXmDtERZTiw2".to_vec();

let account_id32: [u8; 32] = Self::get_accountid32(&rent_fee_pot).unwrap_or_default();
let account = T::AccountId::decode(&mut &account_id32[..]).ok().unwrap_or_default();

let destroy_frequency: T::BlockNumber = (2880 * 7u32).into();
DestroyHook::<T>::put((account, destroy_frequency));

0
}
}

#[pallet::call]
Expand Down Expand Up @@ -233,4 +245,21 @@ impl<T: Config> Pallet<T> {
TotalDestroy::<T>::insert(&who, total_destroy);
Self::deposit_event(Event::DestroyDBC(who, burn_amount));
}

pub fn get_accountid32(addr: &Vec<u8>) -> Option<[u8; 32]> {
let mut data: [u8; 35] = [0; 35];

let length = bs58::decode(addr).into(&mut data).ok()?;
if length != 35 {
return None;
}

let (_prefix_len, _ident) = match data[0] {
0..=63 => (1, data[0] as u16),
_ => return None,
};

let account_id32: [u8; 32] = data[1..33].try_into().ok()?;
Some(account_id32)
}
}

0 comments on commit 37b4b80

Please sign in to comment.