我正在尝试从 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
是一个字符串。所以只需粘贴一个字符串值。
注意事项:
最好先验证一个帐户,然后再对其进行任何操作:
#[inline] pub fn assert_account_is_valid(a: &AccountId) { assert!( env::is_valid_account_id(a.as_bytes()), format!("{} account ID is invalid", a) ); }
如果在使用前严格要求合约初始化,如果别人代替你初始化它会发生不好的事情(例如设置所有者地址),那么你可以添加一些保护方法(例如hardcoding hash in智能合约并在
new
方法中进行原像检查)。如果一个合约不应该被初始化两次,那么总是用
assert
调用!env::state_exists()
,就像你所做的那样。
关于javascript - 将帐户传递给 near-js-api 函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64127857/