【问题标题】:Passing account to near-js-api function call将帐户传递给 near-js-api 函数调用
【发布时间】:2021-01-15 13:14:39
【问题描述】:

我正在尝试从near-js-api 为我的合同调用以下方法。它将 Rsut AccountId 作为参数。

序列化Account 并将其传递给合约的正确方法是什么?

另外,调用合约初始化器时有什么特别的注意事项吗?

#[near_bindgen]
impl BurnerPool {

    #[init]
    fn new(token_id: AccountId) -> Self {

        assert!(!env::state_exists(), "Already initialized");

        let pool = Self {
            token_id: token_id,
            total_received: 0,
        };

        return pool;
    }
}

【问题讨论】:

    标签: javascript nearprotocol


    【解决方案1】:

    AccountId 是一个字符串。所以只需粘贴一个字符串值。

    注意事项:

    1. 最好先验证一个帐户,然后再对它进行任何操作:

      #[inline]
      pub fn assert_account_is_valid(a: &AccountId) {
          assert!(
              env::is_valid_account_id(a.as_bytes()),
              format!("{} account ID is invalid", a)
          );
      }
      
    2. 如果在使用前严格要求合约初始化,如果其他人代替你初始化它会发生坏事(例如设置所有者地址),那么您可以添加一些保护方法(例如在智能合约中硬编码哈希并在new 方法中进行原像检查)。

    3. 如果一个合约不应该被初始化两次,那么总是用assert调用!env::state_exists(),就像你做的那样。

    【讨论】:

      猜你喜欢
      • 2022-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 2014-08-16
      • 1970-01-01
      • 2020-10-10
      相关资源
      最近更新 更多