我正试图从near-js-api
为我的合同调用以下方法。它以RsutAccountId
为自变量。
将账户序列化并将其传递给合同的正确方法是什么?
此外,在调用合同初始化程序时是否有任何特殊考虑?
#[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) ); }
-
如果在使用之前严格要求合约初始化,并且如果其他人代替你初始化它(例如设置所有者地址(会发生不好的事情,那么你可以添加一些保护方法(例如在智能合约中对哈希进行硬编码,并在
new
方法中进行预映像检查(。 -
如果一个契约不应该初始化两次,那么总是用
assert
调用!env::state_exists()
,就像您所做的那样。