Skip to content

Commit

Permalink
Merge pull request #7 from octopus-network/v1.0.4
Browse files Browse the repository at this point in the history
Upgrade to v1.0.4
  • Loading branch information
riversyang authored Jan 4, 2022
2 parents 7016e7e + 42638ef commit ec17b38
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion appchain-anchor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "appchain-anchor"
version = "1.0.3"
version = "1.0.4"
authors = ["Octopus Network"]
edition = "2018"

Expand Down
4 changes: 4 additions & 0 deletions appchain-anchor/src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ pub trait SudoActions {
fn resume_asset_transfer(&mut self);
///
fn remove_staking_history_at(&mut self, index: U64);
///
fn pause_rewards_withdrawal(&mut self);
///
fn resume_rewards_withdrawal(&mut self);
}

pub trait ValidatorActions {
Expand Down
10 changes: 10 additions & 0 deletions appchain-anchor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ pub struct AppchainAnchor {
asset_transfer_is_paused: bool,
/// The staking histories organized by account id
user_staking_histories: LazyOption<UserStakingHistories>,
/// Whether the rewards withdrawal is paused
rewards_withdrawal_is_paused: bool,
}

#[near_bindgen]
Expand Down Expand Up @@ -263,6 +265,7 @@ impl AppchainAnchor {
StorageKey::UserStakingHistories.into_bytes(),
Some(&UserStakingHistories::new()),
),
rewards_withdrawal_is_paused: false,
}
}
// Assert that the contract called by the owner.
Expand Down Expand Up @@ -338,6 +341,13 @@ impl AppchainAnchor {
"Asset transfer is now paused."
);
}
///
fn assert_rewards_withdrawal_is_not_paused(&self) {
assert!(
!self.rewards_withdrawal_is_paused,
"Rewards withdrawal is now paused."
);
}
/// Set the price (in USD) of OCT token
pub fn set_price_of_oct_token(&mut self, price: U128) {
let anchor_settings = self.anchor_settings.get().unwrap();
Expand Down
2 changes: 2 additions & 0 deletions appchain-anchor/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ impl StakingManager for AppchainAnchor {
//
fn withdraw_validator_rewards(&mut self, validator_id: AccountId) {
self.assert_asset_transfer_is_not_paused();
self.assert_rewards_withdrawal_is_not_paused();
let end_era = self
.validator_set_histories
.get()
Expand Down Expand Up @@ -552,6 +553,7 @@ impl StakingManager for AppchainAnchor {
//
fn withdraw_delegator_rewards(&mut self, delegator_id: AccountId, validator_id: AccountId) {
self.assert_asset_transfer_is_not_paused();
self.assert_rewards_withdrawal_is_not_paused();
let end_era = self
.validator_set_histories
.get()
Expand Down
25 changes: 5 additions & 20 deletions appchain-anchor/src/storage_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub struct OldAppchainAnchor {
reward_distribution_records: LazyOption<RewardDistributionRecords>,
/// Whether the asset transfer is paused
asset_transfer_is_paused: bool,
/// The staking histories organized by account id
user_staking_histories: LazyOption<UserStakingHistories>,
}

#[near_bindgen]
Expand All @@ -72,7 +74,7 @@ impl AppchainAnchor {
);
//
// Create the new contract using the data from the old contract.
let mut new_contract = AppchainAnchor {
let new_contract = AppchainAnchor {
appchain_id: old_contract.appchain_id,
appchain_registry: old_contract.appchain_registry,
owner: old_contract.owner,
Expand All @@ -96,28 +98,11 @@ impl AppchainAnchor {
beefy_light_client_state: old_contract.beefy_light_client_state,
reward_distribution_records: old_contract.reward_distribution_records,
asset_transfer_is_paused: old_contract.asset_transfer_is_paused,
user_staking_histories: LazyOption::new(
StorageKey::UserStakingHistories.into_bytes(),
Some(&UserStakingHistories::new()),
),
user_staking_histories: old_contract.user_staking_histories,
rewards_withdrawal_is_paused: false,
};
//
new_contract.initialize_user_staking_histories();
//
new_contract
}
}

impl AppchainAnchor {
pub fn initialize_user_staking_histories(&mut self) {
let staking_histories = self.staking_histories.get().unwrap();
let index_range = staking_histories.index_range();
let mut user_staking_histories = self.user_staking_histories.get().unwrap();
for index in index_range.start_index.0..index_range.end_index.0 + 1 {
if let Some(staking_history) = staking_histories.get(&index) {
user_staking_histories.add_staking_history(&staking_history);
}
}
self.user_staking_histories.set(&user_staking_histories);
}
}
26 changes: 26 additions & 0 deletions appchain-anchor/src/sudo_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,19 @@ impl SudoActions for AppchainAnchor {
//
fn pause_asset_transfer(&mut self) {
self.assert_owner();
assert!(
!self.asset_transfer_is_paused,
"Asset transfer is already paused."
);
self.asset_transfer_is_paused = true;
}
//
fn resume_asset_transfer(&mut self) {
self.assert_owner();
assert!(
self.asset_transfer_is_paused,
"Asset transfer is already resumed."
);
self.asset_transfer_is_paused = false;
}
//
Expand All @@ -181,4 +189,22 @@ impl SudoActions for AppchainAnchor {
let mut staking_histories = self.staking_histories.get().unwrap();
staking_histories.remove_at(&index.0);
}
//
fn pause_rewards_withdrawal(&mut self) {
self.assert_owner();
assert!(
!self.rewards_withdrawal_is_paused,
"Rewards withdrawal is already paused."
);
self.rewards_withdrawal_is_paused = true;
}
//
fn resume_rewards_withdrawal(&mut self) {
self.assert_owner();
assert!(
self.rewards_withdrawal_is_paused,
"Rewards withdrawal is already resumed."
);
self.rewards_withdrawal_is_paused = false;
}
}

0 comments on commit ec17b38

Please sign in to comment.