defuse/contract/
upgrade.rs

1use defuse_controller::ControllerUpgradable;
2use near_plugins::{AccessControllable, access_control_any};
3use near_sdk::{Gas, Promise, assert_one_yocto, env, near};
4
5use super::{Contract, ContractExt, Role};
6
7const STATE_MIGRATE_DEFAULT_GAS: Gas = Gas::from_tgas(5);
8
9#[near]
10impl ControllerUpgradable for Contract {
11    #[access_control_any(roles(Role::DAO, Role::Upgrader))]
12    #[payable]
13    fn upgrade(
14        &mut self,
15        #[serializer(borsh)] code: Vec<u8>,
16        #[serializer(borsh)] state_migration_gas: Option<Gas>,
17    ) -> Promise {
18        assert_one_yocto();
19
20        let p = Promise::new(env::current_account_id()).deploy_contract(code);
21
22        Self::ext_on(p)
23            .with_static_gas(state_migration_gas.unwrap_or(STATE_MIGRATE_DEFAULT_GAS))
24            .state_migrate()
25    }
26
27    #[private]
28    fn state_migrate(&mut self) {}
29}