将帐户传递给 near-js-api 函数调用

Passing account to near-js-api function call

我正在尝试从 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;
    }
}

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(),就像你所做的那样。