Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add restricted balances to AccountInfoV2 #4048

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions state-chain/custom-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ use state_chain_runtime::{
constants::common::TX_FEE_MULTIPLIER,
runtime_apis::{CustomRuntimeApi, Environment, LiquidityProviderInfo, RuntimeApiAccountInfoV2},
};
use std::{collections::HashMap, marker::PhantomData, sync::Arc};
use std::{
collections::{BTreeMap, HashMap},
marker::PhantomData,
sync::Arc,
};

#[derive(Serialize, Deserialize)]
#[serde(tag = "role", rename_all = "snake_case")]
Expand All @@ -50,6 +54,7 @@ pub enum RpcAccountInfo {
is_online: bool,
is_bidding: bool,
bound_redeem_address: Option<EthereumAddress>,
restricted_balances: BTreeMap<EthereumAddress, NumberOrHex>,
},
}

Expand Down Expand Up @@ -103,6 +108,11 @@ impl RpcAccountInfo {
is_online: info.is_online,
is_bidding: info.is_bidding,
bound_redeem_address: info.bound_redeem_address,
restricted_balances: info
.restricted_balances
.into_iter()
.map(|(address, balance)| (address, balance.into()))
.collect(),
}
}
}
Expand All @@ -121,6 +131,7 @@ pub struct RpcAccountInfoV2 {
pub is_online: bool,
pub is_bidding: bool,
pub bound_redeem_address: Option<EthereumAddress>,
pub restricted_balances: BTreeMap<EthereumAddress, u128>,
}

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -569,6 +580,7 @@ where
is_online: account_info.is_online,
is_bidding: account_info.is_bidding,
bound_redeem_address: account_info.bound_redeem_address,
restricted_balances: account_info.restricted_balances,
})
}

Expand Down Expand Up @@ -982,6 +994,7 @@ mod test {
is_online: true,
is_qualified: true,
bound_redeem_address: Some(H160::from([1; 20])),
restricted_balances: BTreeMap::from_iter(vec![(H160::from([1; 20]), 10u128.pow(18))]),
});
assert_eq!(
serde_json::to_value(validator).unwrap(),
Expand All @@ -998,7 +1011,10 @@ mod test {
"last_heartbeat": 0,
"online_credits": 0,
"reputation_points": 0,
"role": "validator"
"role": "validator",
"restricted_balances": {
"0x0101010101010101010101010101010101010101": "0xde0b6b3a7640000"
}
})
);
}
Expand Down
2 changes: 1 addition & 1 deletion state-chain/pallets/cf-funding/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ A validator can have at most one open redemption at any given time. They must ei

### Redemption Restrictions

Redemptions can be subject to certain rules and restrictions. The following categories apply simulataneously, that is, all of the following restrictions are checked on every redemption request.
Redemptions can be subject to certain rules and restrictions. The following categories apply simultaneously, that is, all of the following restrictions are checked on every redemption request.

#### Bonded Funds

Expand Down
2 changes: 2 additions & 0 deletions state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ impl_runtime_apis! {
let bound_redeem_address = pallet_cf_funding::BoundRedeemAddress::<Runtime>::get(&account_id);
let reputation_info = pallet_cf_reputation::Reputations::<Runtime>::get(&account_id);
let account_info = pallet_cf_flip::Account::<Runtime>::get(&account_id);
let restricted_balances = pallet_cf_funding::RestrictedBalances::<Runtime>::get(&account_id);
RuntimeApiAccountInfoV2 {
balance: account_info.total(),
bond: account_info.bond(),
Expand All @@ -935,6 +936,7 @@ impl_runtime_apis! {
is_online: Reputation::is_qualified(&account_id),
is_bidding,
bound_redeem_address,
restricted_balances,
}
}

Expand Down
3 changes: 2 additions & 1 deletion state-chain/runtime/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sp_api::decl_runtime_apis;
use sp_runtime::DispatchError;
use sp_std::vec::Vec;
use sp_std::{collections::btree_map::BTreeMap, vec::Vec};

type VanityName = Vec<u8>;

Expand Down Expand Up @@ -49,6 +49,7 @@ pub struct RuntimeApiAccountInfoV2 {
pub is_online: bool,
pub is_bidding: bool,
pub bound_redeem_address: Option<EthereumAddress>,
pub restricted_balances: BTreeMap<EthereumAddress, u128>,
}

#[derive(Encode, Decode, Eq, PartialEq, TypeInfo)]
Expand Down
4 changes: 2 additions & 2 deletions state-chain/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ pub trait OnAccountFunded {
type ValidatorId;
type Amount;

/// A callback that is triggered after some validator's balance has changed signigicantly,
/// either by funding it with more Flip, or by executing a redemption.
/// A callback that is triggered after some validator's balance has changed significantly,
/// either by funding it with more Flip, or by initiating/reverting a redemption.
///
/// Note this does not trigger on small changes like transaction fees.
///
Expand Down