-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlib.rs
72 lines (64 loc) · 2.56 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! A simple contract to be deployed via `Upgradable`. It requires [state migration].
//!
//! [state migration]: https://docs.near.org/develop/upgrade#migrating-the-state
use near_plugins::{access_control, AccessControlRole, AccessControllable, Upgradable};
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{env, near_bindgen, PanicOnDefault};
/// Roles correspond to those defined in the initial contract `../upgradable`, to make permissions
/// granted before the upgrade remain valid.
#[derive(AccessControlRole, Deserialize, Serialize, Copy, Clone)]
#[serde(crate = "near_sdk::serde")]
pub enum Role {
DAO,
CodeStager,
CodeDeployer,
DurationManager,
}
/// The struct differs from the one defined in the initial contract `../upgradable`, hence [state
/// migration] is required.
///
/// [state migration]: https://docs.near.org/develop/upgrade#migrating-the-state
#[access_control(role_type(Role))]
#[near_bindgen]
#[derive(Upgradable, PanicOnDefault, BorshDeserialize, BorshSerialize)]
#[upgradable(access_control_roles(
code_stagers(Role::CodeStager, Role::DAO),
code_deployers(Role::CodeDeployer, Role::DAO),
duration_initializers(Role::DurationManager, Role::DAO),
duration_update_stagers(Role::DurationManager, Role::DAO),
duration_update_appliers(Role::DurationManager, Role::DAO),
))]
pub struct Contract {
is_migrated: bool,
}
#[near_bindgen]
impl Contract {
/// Migrates state from [`OldContract`] to [`Contract`].
///
/// It follows the state migration pattern described [here].
///
/// [here]: https://docs.near.org/develop/upgrade#migrating-the-state
#[private]
#[init(ignore_state)]
pub fn migrate() -> Self {
// Ensure old state can be read and deserialized.
let _: OldContract = env::state_read().expect("Should be able to load old state");
Self { is_migrated: true }
}
/// A migration method that fails on purpose to test the rollback mechanism of
/// `Upgradable::up_deploy_code`.
#[private]
#[init(ignore_state)]
pub fn migrate_with_failure() -> Self {
env::panic_str("Failing migration on purpose");
}
/// This method is _not_ defined in the initial contract, so calling it successfully proves the
/// contract defined in this file was deployed and the old state was migrated.
pub fn is_migrated(&self) -> bool {
self.is_migrated
}
}
/// Corresponds to the state defined in the initial `../upgradable` contract.
#[derive(BorshDeserialize)]
pub struct OldContract;