Skip to content

Commit

Permalink
Process staking (#133)
Browse files Browse the repository at this point in the history
* Process staking

* Correct type

* Refactor

* Introduce `Adjust` trait

* Refactor

* Format

* Update links

* Doc

* Fix

* Doc
  • Loading branch information
AurevoirXavier authored Dec 20, 2022
1 parent ba6a50c commit ced7f23
Show file tree
Hide file tree
Showing 22 changed files with 706 additions and 2,640 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

10 changes: 7 additions & 3 deletions pallet/account-migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ version = "6.0.0"

[dependencies]
# crates.io
codec = { default-features = false, package = "parity-scale-codec", version = "3.2.1", features = ["derive"] }
scale-info = { default-features = false, version = "2.3.0", features = ["derive"] }
codec = { default-features = false, package = "parity-scale-codec", version = "3.2", features = ["derive"] }
scale-info = { default-features = false, version = "2.3", features = ["derive"] }

# darwinia
dc-primitives = { default-features = false, path = "../../core/primitives" }
darwinia-deposit = { default-features = false, path = "../deposit" }
darwinia-staking = { default-features = false, path = "../staking" }
dc-primitives = { default-features = false, path = "../../core/primitives" }

# substrate
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
Expand All @@ -38,6 +40,8 @@ std = [
"scale-info/std",

# darwinia
"darwinia-deposit/std",
"darwinia-staking/std",
"dc-primitives/std",

# paritytech
Expand Down
56 changes: 51 additions & 5 deletions pallet/account-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@
mod tests;

// darwinia
use darwinia_deposit::Deposit;
use darwinia_staking::Ledger;
use dc_primitives::{AccountId as AccountId20, Balance, BlockNumber, Index};
// substrate
use frame_support::{
log,
pallet_prelude::*,
traits::{LockableCurrency, WithdrawReasons},
traits::{Currency, ExistenceRequirement::KeepAlive, LockableCurrency, WithdrawReasons},
};
use frame_system::{pallet_prelude::*, AccountInfo};
use pallet_balances::AccountData;
Expand Down Expand Up @@ -82,6 +84,8 @@ pub mod pallet {
AccountData = AccountData<Balance>,
> + pallet_balances::Config<Balance = Balance>
+ pallet_vesting::Config<Currency = pallet_balances::Pallet<Self>>
+ darwinia_deposit::Config
+ darwinia_staking::Config
{
/// Override the [`frame_system::Config::RuntimeEvent`].
type RuntimeEvent: From<Event> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
Expand All @@ -102,6 +106,8 @@ pub mod pallet {
pub enum Error<T> {
/// Exceed maximum vesting count.
ExceedMaxVestings,
/// Exceed maximum deposit count.
ExceedMaxDeposits,
}

/// [`frame_system::Account`] data.
Expand All @@ -121,9 +127,26 @@ pub mod pallet {
pub type Vestings<T: Config> =
StorageMap<_, Blake2_128Concat, AccountId32, Vec<VestingInfo<Balance, BlockNumber>>>;

/// [`darwinia_deposit::Deposits`] data.
#[pallet::storage]
#[pallet::unbounded]
#[pallet::getter(fn deposit_of)]
pub type Deposits<T: Config> = StorageMap<_, Blake2_128Concat, AccountId32, Vec<Deposit>>;

/// [`darwinia_staking::Bonded`] data.
///
/// <https://github.dev/darwinia-network/darwinia-common/blob/6a9392cfb9fe2c99b1c2b47d0c36125d61991bb7/frame/staking/src/lib.rs#L592>
#[pallet::storage]
#[pallet::getter(fn bonded)]
pub type Bonded<T: Config> = StorageMap<_, Twox64Concat, AccountId32, AccountId32>;

/// [`darwinia_staking::Ledgers`] data.
#[pallet::storage]
#[pallet::getter(fn ledger_of)]
pub type Ledgers<T: Config> = StorageMap<_, Blake2_128Concat, AccountId32, Ledger<T>>;

// TODO: identity storages
// TODO: proxy storages
// TODO: staking storages

#[pallet::call]
impl<T: Config> Pallet<T> {
Expand All @@ -142,12 +165,12 @@ pub mod pallet {

<frame_system::Account<T>>::insert(to, account);

if let Some(vs) = <Vestings<T>>::get(&from) {
let locked = vs.iter().map(|v| v.locked()).sum();
if let Some(v) = <Vestings<T>>::get(&from) {
let locked = v.iter().map(|v| v.locked()).sum();

<pallet_vesting::Vesting<T>>::insert(
to,
BoundedVec::try_from(vs).map_err(|_| <Error<T>>::ExceedMaxVestings)?,
BoundedVec::try_from(v).map_err(|_| <Error<T>>::ExceedMaxVestings)?,
);

// https://github.dev/paritytech/substrate/blob/19162e43be45817b44c7d48e50d03f074f60fbf4/frame/vesting/src/lib.rs#L248
Expand All @@ -156,6 +179,29 @@ pub mod pallet {
// https://github.dev/paritytech/substrate/blob/19162e43be45817b44c7d48e50d03f074f60fbf4/frame/vesting/src/lib.rs#L86
<pallet_balances::Pallet<T>>::set_lock(*b"vesting ", &to, locked, reasons);
}
if let Some(l) = <Bonded<T>>::get(&from).and_then(<Ledgers<T>>::get) {
if let Some(ds) = <Deposits<T>>::get(&from) {
<pallet_balances::Pallet<T> as Currency<_>>::transfer(
&to,
&darwinia_deposit::account_id(),
ds.iter().map(|d| d.value).sum(),
KeepAlive,
)?;
<darwinia_deposit::Deposits<T>>::insert(
to,
BoundedVec::try_from(ds).map_err(|_| <Error<T>>::ExceedMaxDeposits)?,
);
}

<pallet_balances::Pallet<T> as Currency<_>>::transfer(
&to,
&darwinia_staking::account_id(),
l.staked_ring + l.unstaking_ring.iter().map(|(r, _)| r).sum::<Balance>(),
KeepAlive,
)?;
// TODO: kton transfer
<darwinia_staking::Ledgers<T>>::insert(to, l);
}

Self::deposit_event(Event::Migrated { from, to });

Expand Down
23 changes: 12 additions & 11 deletions pallet/deposit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ use core::{
cmp::Ordering::{Equal, Greater, Less},
ops::ControlFlow::{Break, Continue},
};
// crates.io
use codec::FullCodec;
// darwinia
use dc_inflation::MILLISECS_PER_YEAR;
use dc_types::{Balance, Moment};
Expand Down Expand Up @@ -201,7 +203,7 @@ pub mod pallet {

DispatchResult::Ok(())
})?;
T::Ring::transfer(&who, &Self::account_id(), amount, KeepAlive)?;
T::Ring::transfer(&who, &account_id(), amount, KeepAlive)?;
T::Kton::mint(&who, dc_inflation::deposit_interest(amount, months))?;

// TODO: event?
Expand Down Expand Up @@ -237,7 +239,7 @@ pub mod pallet {
<Result<(), ()>>::Ok(())
});

T::Ring::transfer(&Self::account_id(), &who, claimed, AllowDeath)?;
T::Ring::transfer(&account_id(), &who, claimed, AllowDeath)?;

// TODO: event?

Expand All @@ -246,15 +248,6 @@ pub mod pallet {

// TODO: claim_with_penalty
}
impl<T> Pallet<T>
where
T: Config,
{
/// The account of the deposit pot.
pub fn account_id() -> T::AccountId {
PalletId(*b"dar/depo").into_account_truncating()
}
}
}
pub use pallet::*;

Expand Down Expand Up @@ -309,3 +302,11 @@ where
.unwrap_or_default()
}
}

/// The account of the deposit pot.
pub fn account_id<A>() -> A
where
A: FullCodec,
{
PalletId(*b"dar/depo").into_account_truncating()
}
4 changes: 2 additions & 2 deletions pallet/deposit/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ use frame_support::{assert_noop, assert_ok, traits::Get};
fn lock_should_work() {
new_test_ext().execute_with(|| {
assert_eq!(System::account(&1).consumers, 0);
assert_eq!(Balances::free_balance(&Deposit::account_id()), 0);
assert_eq!(Balances::free_balance(&darwinia_deposit::account_id()), 0);
assert_eq!(Balances::free_balance(&1), 1_000 * UNIT);
assert_ok!(Deposit::lock(RuntimeOrigin::signed(1), 10 * UNIT, 1));
assert_eq!(System::account(&1).consumers, 1);
assert_eq!(Balances::free_balance(&Deposit::account_id()), 10 * UNIT);
assert_eq!(Balances::free_balance(&darwinia_deposit::account_id()), 10 * UNIT);
assert_eq!(Balances::free_balance(&1), 990 * UNIT);
});
}
Expand Down
21 changes: 21 additions & 0 deletions tool/state-processor/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
root = true

[*]
charset=utf-8
end_of_line=lf
indent_size=tab
indent_style=tab
insert_final_newline=true
max_line_length=100
tab_width=4
trim_trailing_whitespace=true

[*.py]
charset=utf-8
indent_size=4
indent_style=space

[*.{sh,yml,yaml}]
indent_size=2
indent_style=space
tab_width=8
15 changes: 15 additions & 0 deletions tool/state-processor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# System
.DS_Store

# Integrated Development Environment
.idea
.vscode

# Package Manager
## cargo
target
## npm
node_modules

# Test data
test-data
29 changes: 29 additions & 0 deletions tool/state-processor/.rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Basic
edition = "2021"
hard_tabs = true
max_width = 100
tab_spaces = 4

# Imports
imports_granularity = "Crate"
reorder_imports = true

# Format comments
comment_width = 100
wrap_comments = true

# Misc
match_arm_blocks = false
match_block_trailing_comma = true
newline_style = "Unix"
reorder_impl_items = true
reorder_modules = true
use_field_init_shorthand = true
use_small_heuristics = "Max"

# Wait for stablization
# format_code_in_doc_comments = true

# Could give it a try
# group_imports = "StdExternalCrate"
# inline_attribute_width = 100
Loading

0 comments on commit ced7f23

Please sign in to comment.