首页 > 解决方案 > 消息:调用方法时“实例化过程中发生错误”

问题描述

我在 Rust 中构建了一个简单的合约脚本,构建没有错误,并使用我的帐户 id 成功部署到测试网,但是当我尝试调用该函数get_weight()时出现这样的错误,我不知道为什么会这样。

Scheduling a call: neargochi.testnet.get_weight()
Receipt: BwvRTnKdjmB5wzkNcZ1qgJyLrWEbUDDJ1UoMynmeVHvc
        Failure [neargochi.testnet]: Error: Error happened during instantiation
An error occured
Error: Error happened during instantiation
    at Object.parseResultError (C:\Users\AppData\Roaming\npm\node_modules\near-cli\node_modules\near-api-js\lib\utils\rpc_errors.js:31:29)
    at Account.signAndSendTransactionV2 (C:\Users\AppData\Roaming\npm\node_modules\near-cli\node_modules\near-api-js\lib\account.js:149:36)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async scheduleFunctionCall (C:\Users\AppData\Roaming\npm\node_modules\near-cli\commands\call.js:54:34)
    at async Object.handler (C:\Users\AppData\Roaming\npm\node_modules\near-cli\utils\exit-on-error.js:52:9)
ServerTransactionError: Error happened during instantiation
    at Object.parseResultError (C:\Users\AppData\Roaming\npm\node_modules\near-cli\node_modules\near-api-js\lib\utils\rpc_errors.js:31:29)
    at Account.signAndSendTransactionV2 (C:\Users\AppData\Roaming\npm\node_modules\near-cli\node_modules\near-api-js\lib\account.js:149:36)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async scheduleFunctionCall (C:\Users\\AppData\Roaming\npm\node_modules\near-cli\commands\call.js:54:34)
    at async Object.handler (C:\Users\AppData\Roaming\npm\node_modules\near-cli\utils\exit-on-error.js:52:9) {
  type: 'Instantiate',
  context: undefined,
  index: 0,
  message: 'Error happened during instantiation',
  transaction_outcome: {
    block_hash: '6jUCtjQhM9Bu3CLVEAfUVR8FFUHqNF3q7zGqTkEPPKTy',
    id: 'HLoLz4ydmFjBCDb2kMUMp7ckaPJuKawhfeNgcuwZxWb8',
    outcome: {
      executor_id: 'edowahdana.testnet',
      gas_burnt: 2427947831208,
      logs: [],
      receipt_ids: [ 'gDv1hnAdHS1BDu9X963V3r9xKwogmyCfi4xdL7f2jai' ],
      status: {
        SuccessReceiptId: 'gDv1hnAdHS1BDu9X963V3r9xKwogmyCfi4xdL7f2jai'
      },
      tokens_burnt: '242794783120800000000'
    },
    proof: []
  }
} 

这是我lib.rs包含合同的文件:

use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{env, near_bindgen, setup_alloc};

use rand::Rng; // 0.8.0

setup_alloc!();

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct Status {
    id: i16,
    weight: i16,
    hunger: i16,
    happines: i16,
    is_sick: bool,
    //Facing: FaceTo
}

// Default value for every key pair
impl Default for Status {
    fn default() -> Self {
        env::panic(b"The contract is not initialized")
    }
}

#[near_bindgen]
impl Status {

    pub fn health_check(&self) -> bool {
        if !self.is_sick { return true; }
        else { return false; }
    }
    
    pub fn get_weight(&self) -> i16 {
        self.weight
    }
    
    pub fn get_hunger(&self) -> i16 {
        self.hunger
    }
    
    pub fn get_happines(&self) -> i16 {
        self.happines
    }
    
    pub fn medicine(&mut self) {
        if self.is_sick { self.is_sick = false; }
    }
    
    pub fn meal(&mut self) {
        if self.health_check() {
            self.weight = i16::wrapping_add(self.weight, 1);
            self.hunger = 4;
        }
    }
    
    pub fn play(&mut self, value: i16) -> bool {
        self.weight = i16::wrapping_sub(self.weight, 1);
        self.hunger = i16::wrapping_sub(self.weight, 1);
        
        let random_value = rand::thread_rng().gen_range(0..2);
        if random_value == value {
            self.happines = i16::wrapping_add(self.happines, 1);
            true
        } else { false }
    }
    
    pub fn snack(&mut self) {
        if self.health_check() {
            if self.hunger > 4 { self.is_sick = true; }
            
            self.weight = i16::wrapping_add(self.weight, 2);
            self.hunger = i16::wrapping_add(self.hunger, 1);
        }
    }
    
}

我已经用谷歌搜索了这个错误,但我没有找到任何线索,我不知道我做错了什么,但希望有人可以帮助我解决这个问题。

标签: rustnear

解决方案


推荐阅读