在 init 方法中设置的字符串属性总是 returns 空字符串
String attribute set in init method always returns empty string
我有以下带有 impl 的结构:
#[near_bindgen]
#[derive(Default, Serialize, Deserialize, BorshDeserialize, BorshSerialize, Debug)]
pub struct MyStruct {
owner: String
}
#[near_bindgen(init => new)]
impl MyStruct {
fn new() -> Self {
Self {
owner: "bob".to_string()
}
}
fn get_owner(&self) -> String {
return self.owner;
}
}
然后我使用 near deploy my_contract --masterAccount myAccount
部署合约
如果我使用 near-shell 调用 get_owner:near call my_contract get_owner --accountId=myAccount
它总是 returns ""
而不是预期的 "bob"
。
似乎新方法可能不会在部署时被调用。
初始化程序不会在部署时自动调用。 deploy
只部署代码,不调用合约上的任何内容。我们可能应该向 shell 添加一个新方法,它执行 deploy_and_call
。但现在只需手动调用 new
。
我们不自动初始化的原因是 initializer
可能需要额外的参数。您可以将所有者传递给 new
方法。下面是一个示例,如何使用带有自定义参数的初始化程序,以及如何确保在没有初始化的情况下无法调用合约:
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct FunToken {
/// AccountID -> Account details.
pub accounts: Map<AccountId, Account>,
/// Total supply of the all token.
pub total_supply: Balance,
}
impl Default for FunToken {
fn default() -> Self {
env::panic(b"Not initialized");
unreachable!();
}
}
#[near_bindgen(init => new)]
impl FunToken {
pub fn new(owner_id: AccountId, total_supply: Balance) -> Self {
let mut ft = Self { accounts: Map::new(b"a".to_vec()), total_supply };
let mut account = ft.get_account(&owner_id);
account.balance = total_supply;
ft.accounts.insert(&owner_id, &account);
ft
}
}
从这里开始:https://github.com/nearprotocol/near-bindgen/blob/master/examples/fun-token/src/lib.rs#L52-L77
基本上它在Default调用时会panic,所以无法调用未初始化的合约。
初始化函数通常在需要参数化合约初始化时使用。如果没有参数那么就实现 Default
trait:
impl Default for MyStruct {
fn default() -> Self {
Self {
owner: "bob".to_string()
}
}}
我有以下带有 impl 的结构:
#[near_bindgen]
#[derive(Default, Serialize, Deserialize, BorshDeserialize, BorshSerialize, Debug)]
pub struct MyStruct {
owner: String
}
#[near_bindgen(init => new)]
impl MyStruct {
fn new() -> Self {
Self {
owner: "bob".to_string()
}
}
fn get_owner(&self) -> String {
return self.owner;
}
}
然后我使用 near deploy my_contract --masterAccount myAccount
如果我使用 near-shell 调用 get_owner:near call my_contract get_owner --accountId=myAccount
它总是 returns ""
而不是预期的 "bob"
。
似乎新方法可能不会在部署时被调用。
初始化程序不会在部署时自动调用。 deploy
只部署代码,不调用合约上的任何内容。我们可能应该向 shell 添加一个新方法,它执行 deploy_and_call
。但现在只需手动调用 new
。
我们不自动初始化的原因是 initializer
可能需要额外的参数。您可以将所有者传递给 new
方法。下面是一个示例,如何使用带有自定义参数的初始化程序,以及如何确保在没有初始化的情况下无法调用合约:
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct FunToken {
/// AccountID -> Account details.
pub accounts: Map<AccountId, Account>,
/// Total supply of the all token.
pub total_supply: Balance,
}
impl Default for FunToken {
fn default() -> Self {
env::panic(b"Not initialized");
unreachable!();
}
}
#[near_bindgen(init => new)]
impl FunToken {
pub fn new(owner_id: AccountId, total_supply: Balance) -> Self {
let mut ft = Self { accounts: Map::new(b"a".to_vec()), total_supply };
let mut account = ft.get_account(&owner_id);
account.balance = total_supply;
ft.accounts.insert(&owner_id, &account);
ft
}
}
从这里开始:https://github.com/nearprotocol/near-bindgen/blob/master/examples/fun-token/src/lib.rs#L52-L77
基本上它在Default调用时会panic,所以无法调用未初始化的合约。
初始化函数通常在需要参数化合约初始化时使用。如果没有参数那么就实现 Default
trait:
impl Default for MyStruct {
fn default() -> Self {
Self {
owner: "bob".to_string()
}
}}