#near #ownable #multisig

无std multi-ownable

NEAR插件,用于在多个账户之间共享合约的所有权

3个版本

0.1.2 2022年1月22日
0.1.1 2022年1月22日
0.1.0 2022年1月22日

#9 in #ownable

MIT许可协议

14KB
157

multi-ownable

NEAR插件,允许多个账户共享合约的所有权。调用以散列形式存储,因此存储需求非常低。

测试

cargo test -- --nocapture

用法

multi-ownable可以通过宏添加到您的合约中

// Arguments:
// 1. name of your contract
// 2. name of the field where the multi ownable state is stored
// 3. enum type for possible multisig calls
// 4. callback function to process completed multisig calls
crate::impl_multi_ownable!(Contract, multi_ownable, MultiOwnableCall, on_call);

以下是如何使用multi-ownable的完整示例

// import "impl_multi_ownable" and "MultiOwnableData"
use multi_ownable::{impl_multi_ownable, MultiOwnableData};
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{env, near_bindgen, require, AccountId, BorshStorageKey, PanicOnDefault};

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct Contract {
  multi_ownable: MultiOwnableData, // add this to the Contract
  number: u64,
}
#[derive(BorshSerialize, BorshStorageKey)]
enum StorageKey {
  Owners,
  MultiOwnableCalls,
}

#[near_bindgen]
impl Contract {
  #[init]
  pub fn new(owner_id: AccountId) -> Self {
    let mut this = Self {
      number: 0,
      multi_ownable: MultiOwnableData::new(StorageKey::Owners, StorageKey::MultiOwnableCalls),
    };
    // initialize multi_ownable in the "new" func of your Contract
    this.init_multi_ownable(vec![owner_id.clone()], 1);
    this
  }

  pub fn get_number(&self) -> u64 {
    self.number
  }

  // arguments are received as a json string
  fn on_call(&mut self, call_name: MultiOwnableCall, arguments: &str) {
    match call_name {
      MultiOwnableCall::UpdateNumber => self._update_number(arguments),
      MultiOwnableCall::DoSomethingElse => self._do_something_else(arguments),
    }
  }

  #[private]
  fn _update_number(&mut self, args: &str) {
    // first, deserialize your arguments
    let UpdateNumberArgs { number } =
      near_sdk::serde_json::from_str(&args).expect("Invalid SetRewardRateArgs");
    self.number = number;
  }

  #[private]
  fn _do_something_else(&self, _args: &str) {
    // do something else
  }
}

// an argument struct for "update_number" call
#[derive(Serialize, Deserialize, Clone)]
#[serde(crate = "near_sdk::serde")]
pub struct UpdateNumberArgs {
  pub number: u64,
}

// create an enum to match possible multisig calls
// make sure to both "rename" and "alias" your fields to be snake_case
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone)]
#[serde(crate = "near_sdk::serde")]
pub enum MultiOwnableCall {
  #[serde(rename = "update_number", alias = "update_number")]
  UpdateNumber,
  #[serde(rename = "do_something_else", alias = "do_something_else")]
  DoSomethingElse,
}

crate::impl_multi_ownable!(Contract, multi_ownable, MultiOwnableCall, on_call);

依赖项

~5.5MB
~113K SLoC