Skip to content

Commit

Permalink
Add protocol settings subaccount_for_council_keeper_contract.
Browse files Browse the repository at this point in the history
  • Loading branch information
riversyang committed Dec 1, 2022
1 parent 58fafd0 commit 10e3fff
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 12 deletions.
2 changes: 2 additions & 0 deletions appchain-anchor/src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ pub trait ProtocolSettingsManager {
fn change_validator_commission_percent(&mut self, value: u16);
///
fn change_maximum_allowed_unprofitable_era_count(&mut self, value: u16);
///
fn change_subaccount_for_council_keeper_contract(&mut self, subaccount_name: String);
}

pub trait AppchainSettingsManager {
Expand Down
10 changes: 9 additions & 1 deletion appchain-anchor/src/permissionless_actions/switching_era.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,15 @@ impl AppchainAnchor {
let args = near_sdk::serde_json::to_vec(&args)
.expect("Failed to serialize the cross contract args using JSON.");
let contract_account = AccountId::from_str(
format!("council-keeper.{}", self.appchain_registry).as_str(),
format!(
"{}.{}",
self.protocol_settings
.get()
.unwrap()
.subaccount_for_council_keeper_contract,
self.appchain_registry
)
.as_str(),
)
.unwrap();
Promise::new(contract_account).function_call(
Expand Down
94 changes: 92 additions & 2 deletions appchain-anchor/src/storage_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,58 @@ pub enum OldAppchainState {
Dead,
}

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone)]
#[serde(crate = "near_sdk::serde")]
pub struct OldProtocolSettings {
/// A validator has to deposit a certain amount of OCT token to this contract for
/// being validator of the appchain.
pub minimum_validator_deposit: U128,
/// The minimum amount for a validator to increase or decrease his/her deposit.
pub minimum_validator_deposit_changing_amount: U128,
/// The maximum percent value that the deposit of a validator in total stake
pub maximum_validator_stake_percent: u16,
/// The minimum deposit amount for a delegator to delegate his voting weight to
/// a certain validator.
pub minimum_delegator_deposit: U128,
/// The minimum amount for a delegator to increase or decrease his/her delegation
/// to a validator.
pub minimum_delegator_deposit_changing_amount: U128,
/// The minimum price (in USD) of total stake in this contract for
/// booting corresponding appchain
pub minimum_total_stake_price_for_booting: U128,
/// The maximum percentage of the total market value of all NEP-141 tokens to the total
/// market value of OCT token staked in this contract
pub maximum_market_value_percent_of_near_fungible_tokens: u16,
/// The maximum percentage of the total market value of wrapped appchain token to the total
/// market value of OCT token staked in this contract
pub maximum_market_value_percent_of_wrapped_appchain_token: u16,
/// The minimum number of validator(s) registered in this contract for
/// booting the corresponding appchain and keep it alive.
pub minimum_validator_count: U64,
/// The maximum number of validator(s) registered in this contract for
/// the corresponding appchain.
pub maximum_validator_count: U64,
/// The maximum number of validator(s) which a delegator can delegate to.
pub maximum_validators_per_delegator: U64,
/// The unlock period (in days) for validator(s) can withdraw their deposit after
/// they are removed from the corresponding appchain.
pub unlock_period_of_validator_deposit: U64,
/// The unlock period (in days) for delegator(s) can withdraw their deposit after
/// they no longer delegates their stake to a certain validator on the corresponding appchain.
pub unlock_period_of_delegator_deposit: U64,
/// The maximum number of historical eras that the validators or delegators are allowed to
/// withdraw their reward
pub maximum_era_count_of_unwithdrawn_reward: U64,
/// The maximum number of valid appchain message.
/// If the era number of appchain message is smaller than the latest era number minus
/// this value, the message will be considered as `invalid`.
pub maximum_era_count_of_valid_appchain_message: U64,
/// The percent of commission fees of a validator's reward in an era
pub validator_commission_percent: u16,
/// The maximum unprofitable era count for auto-unbonding a validator
pub maximum_allowed_unprofitable_era_count: u16,
}

#[derive(BorshDeserialize, BorshSerialize)]
pub struct OldAppchainAnchor {
/// The id of corresponding appchain.
Expand Down Expand Up @@ -69,7 +121,7 @@ pub struct OldAppchainAnchor {
/// The anchor settings for appchain.
anchor_settings: LazyOption<AnchorSettings>,
/// The protocol settings for appchain anchor.
protocol_settings: LazyOption<ProtocolSettings>,
protocol_settings: LazyOption<OldProtocolSettings>,
/// The state of the corresponding appchain.
appchain_state: OldAppchainState,
/// The staking history data happened in this contract.
Expand Down Expand Up @@ -125,7 +177,12 @@ impl AppchainAnchor {
validator_profiles: old_contract.validator_profiles,
appchain_settings: old_contract.appchain_settings,
anchor_settings: old_contract.anchor_settings,
protocol_settings: old_contract.protocol_settings,
protocol_settings: LazyOption::new(
StorageKey::ProtocolSettings.into_bytes(),
Some(&ProtocolSettings::from_old_version(
old_contract.protocol_settings.get().unwrap(),
)),
),
appchain_state: AppchainState::from_old_version(old_contract.appchain_state),
staking_histories: old_contract.staking_histories,
appchain_notification_histories: old_contract.appchain_notification_histories,
Expand Down Expand Up @@ -161,3 +218,36 @@ impl AppchainState {
}
}
}

impl ProtocolSettings {
pub fn from_old_version(old_version: OldProtocolSettings) -> Self {
ProtocolSettings {
minimum_validator_deposit: old_version.minimum_validator_deposit,
minimum_validator_deposit_changing_amount: old_version
.minimum_validator_deposit_changing_amount,
maximum_validator_stake_percent: old_version.maximum_validator_stake_percent,
minimum_delegator_deposit: old_version.minimum_delegator_deposit,
minimum_delegator_deposit_changing_amount: old_version
.minimum_delegator_deposit_changing_amount,
minimum_total_stake_price_for_booting: old_version
.minimum_total_stake_price_for_booting,
maximum_market_value_percent_of_near_fungible_tokens: old_version
.maximum_market_value_percent_of_near_fungible_tokens,
maximum_market_value_percent_of_wrapped_appchain_token: old_version
.maximum_market_value_percent_of_wrapped_appchain_token,
minimum_validator_count: old_version.minimum_validator_count,
maximum_validator_count: old_version.maximum_validator_count,
maximum_validators_per_delegator: old_version.maximum_validators_per_delegator,
unlock_period_of_validator_deposit: old_version.unlock_period_of_validator_deposit,
unlock_period_of_delegator_deposit: old_version.unlock_period_of_delegator_deposit,
maximum_era_count_of_unwithdrawn_reward: old_version
.maximum_era_count_of_unwithdrawn_reward,
maximum_era_count_of_valid_appchain_message: old_version
.maximum_era_count_of_valid_appchain_message,
validator_commission_percent: old_version.validator_commission_percent,
maximum_allowed_unprofitable_era_count: old_version
.maximum_allowed_unprofitable_era_count,
subaccount_for_council_keeper_contract: "octopus-council".to_string(),
}
}
}
16 changes: 9 additions & 7 deletions appchain-anchor/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub struct ProtocolSettings {
pub minimum_validator_deposit: U128,
/// The minimum amount for a validator to increase or decrease his/her deposit.
pub minimum_validator_deposit_changing_amount: U128,
/// The maximum percent value that the deposit of a validator in total stake
/// The maximum percent value that the deposit of a validator in total stake.
pub maximum_validator_stake_percent: u16,
/// The minimum deposit amount for a delegator to delegate his voting weight to
/// a certain validator.
Expand All @@ -130,13 +130,13 @@ pub struct ProtocolSettings {
/// to a validator.
pub minimum_delegator_deposit_changing_amount: U128,
/// The minimum price (in USD) of total stake in this contract for
/// booting corresponding appchain
/// booting corresponding appchain.
pub minimum_total_stake_price_for_booting: U128,
/// The maximum percentage of the total market value of all NEP-141 tokens to the total
/// market value of OCT token staked in this contract
/// market value of OCT token staked in this contract.
pub maximum_market_value_percent_of_near_fungible_tokens: u16,
/// The maximum percentage of the total market value of wrapped appchain token to the total
/// market value of OCT token staked in this contract
/// market value of OCT token staked in this contract.
pub maximum_market_value_percent_of_wrapped_appchain_token: u16,
/// The minimum number of validator(s) registered in this contract for
/// booting the corresponding appchain and keep it alive.
Expand All @@ -153,16 +153,18 @@ pub struct ProtocolSettings {
/// they no longer delegates their stake to a certain validator on the corresponding appchain.
pub unlock_period_of_delegator_deposit: U64,
/// The maximum number of historical eras that the validators or delegators are allowed to
/// withdraw their reward
/// withdraw their reward.
pub maximum_era_count_of_unwithdrawn_reward: U64,
/// The maximum number of valid appchain message.
/// If the era number of appchain message is smaller than the latest era number minus
/// this value, the message will be considered as `invalid`.
pub maximum_era_count_of_valid_appchain_message: U64,
/// The percent of commission fees of a validator's reward in an era
/// The percent of commission fees of a validator's reward in an era.
pub validator_commission_percent: u16,
/// The maximum unprofitable era count for auto-unbonding a validator
/// The maximum unprofitable era count for auto-unbonding a validator.
pub maximum_allowed_unprofitable_era_count: u16,
/// The subaccount name for council keeper contract.
pub subaccount_for_council_keeper_contract: String,
}

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone)]
Expand Down
16 changes: 16 additions & 0 deletions appchain-anchor/src/user_actions/settings_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl Default for ProtocolSettings {
maximum_era_count_of_valid_appchain_message: U64::from(7),
validator_commission_percent: 20,
maximum_allowed_unprofitable_era_count: 3,
subaccount_for_council_keeper_contract: "octopus-council".to_string(),
}
}
}
Expand Down Expand Up @@ -283,6 +284,21 @@ impl ProtocolSettingsManager for AppchainAnchor {
protocol_settings.maximum_allowed_unprofitable_era_count = value;
self.protocol_settings.set(&protocol_settings);
}
//
fn change_subaccount_for_council_keeper_contract(&mut self, subaccount_name: String) {
self.assert_owner();
assert!(
!subaccount_name.trim().is_empty(),
"The subaccount name can not be empty."
);
let mut protocol_settings = self.protocol_settings.get().unwrap();
assert!(
!subaccount_name.eq(&protocol_settings.subaccount_for_council_keeper_contract),
"The value is not changed."
);
protocol_settings.subaccount_for_council_keeper_contract = subaccount_name;
self.protocol_settings.set(&protocol_settings);
}
}

#[near_bindgen]
Expand Down
4 changes: 2 additions & 2 deletions tests/simulator/common/basic_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub async fn initialize_contracts_and_users(
//
let council_keeper = appchain_registry
.as_account()
.create_subaccount(worker, "council-keeper")
.create_subaccount(worker, "octopus-council")
.initial_balance(parse_near!("10 N"))
.transact()
.await?
Expand Down Expand Up @@ -117,7 +117,7 @@ pub async fn initialize_contracts_and_users(
true => appchain_anchor
.deploy(
worker,
&std::fs::read(format!("res/appchain_anchor_v2.3.0.wasm"))?,
&std::fs::read(format!("res/appchain_anchor_v2.3.1.wasm"))?,
)
.await?
.unwrap(),
Expand Down

0 comments on commit 10e3fff

Please sign in to comment.