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
항상 ""
예상되는 "bob"
.
배포시 새 메서드가 호출되지 않는 것 같습니다.
이니셜 라이저는 배포시 자동으로 호출되지 않습니다. deploy
코드를 배포하고 계약에 대해 아무것도 호출하지 않습니다. 셸에 새 메서드를 추가해야합니다 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
기본적으로 기본 호출 중에 패닉이 발생하므로 초기화되지 않은 계약을 호출 할 수 없습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다