Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExecutionError on the change_greeting function #2

Open
mashharuki opened this issue Oct 22, 2022 · 1 comment
Open

ExecutionError on the change_greeting function #2

mashharuki opened this issue Oct 22, 2022 · 1 comment

Comments

@mashharuki
Copy link

ExecutionError on the change_greeting function.

I execute the below command

near call dev-1666366301558-59429896796674 change_greeting '{"new_greeting":"Hello Near!!"}' --accountId dev-1666366301558-59429896796674

At that time I got the following error.

cheduling a call: dev-1666366301558-59429896796674.change_greeting({"new_greeting":"Hello Near!!"})
Doing account.functionCall()
Receipt: 14dev8FcD2vT2XGY8JrP5B13nBsS8SdSpNAaRtPTuQZ3
        Failure [dev-1666366301558-59429896796674]: Error: {"index":0,"kind":{"ExecutionError":"Smart contract panicked: panicked at 'Failed to deserialize input from JSON.: Error(\"missing field `message`\", line: 1, column: 27)', src/lib.rs:22:1"}}
Receipt: 8bamkrj5B6D63ve3pnxcyrJY1FsSF8rJu2bijaFcwmAr
        Log [dev-1666366301558-59429896796674]: set_greeting failed...
Transaction Id 7SpQJQywzvZpGoCaWDvNtjMgCpfjFRb3uc4DtaABFHmD
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.testnet.near.org/transactions/7SpQJQywzvZpGoCaWDvNtjMgCpfjFRb3uc4DtaABFHmD
false

Why ??

I followed the tutorial.

use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{env, log, near_bindgen, AccountId, Gas, Promise, PromiseError, PanicOnDefault};

pub mod external;
pub use crate::external::*;

/**
 * Contract struct
 */
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct Contract {
  // hello contract address
  pub hello_account: AccountId
}

#[near_bindgen]
impl Contract {
  /**
   * init function
   */
  #[init]
  #[private] 
  pub fn init(hello_account: AccountId) -> Self {
    assert!(!env::state_exists(), "Already initialized");
    Self {
      hello_account,
    }
  }

  /**
   * query greeting function
   */
  pub fn query_greeting(&self) -> Promise {
    // Create a promise to call HelloNEAR.get_greeting()
    let promise = hello_near::ext(self.hello_account.clone())
      .with_static_gas(Gas(5*TGAS))
      .get_greeting();
    
    // Create a promise to callback query_greeting_callback
    return promise.then( 
      Self::ext(env::current_account_id())
      .with_static_gas(Gas(5*TGAS))
      .query_greeting_callback()
    )
  }

  /**
   * callback function
   */
  #[private]
  pub fn query_greeting_callback(&self, #[callback_result] call_result: Result<String, PromiseError>) -> String {
    // Check if the promise succeeded by calling the method outlined in external.rs
    if call_result.is_err() {
      log!("There was an error contacting Hello NEAR");
      return "".to_string();
    }

    // Return the greeting
    let greeting: String = call_result.unwrap();
    greeting
  }

  /**
   * change greeting function
   */
  pub fn change_greeting(&mut self, new_greeting: String) -> Promise {
    // Create a promise to call HelloNEAR.set_greeting(message:string)
    hello_near::ext(self.hello_account.clone())
      .with_static_gas(Gas(5*TGAS))
      .set_greeting(new_greeting)
    .then( // Create a callback change_greeting_callback
        Self::ext(env::current_account_id())
        .with_static_gas(Gas(5*TGAS))
        .change_greeting_callback()
      )
  }

  /**
   * callback function
   */
  #[private]
  pub fn change_greeting_callback(&mut self, #[callback_result] call_result: Result<(), PromiseError>) -> bool {
    // Return whether or not the promise succeeded using the method outlined in external.rs
    if call_result.is_err() {
      env::log_str("set_greeting failed...");
      return false;
    } else {
      env::log_str("set_greeting was successful!");
      return true;
    }
  }
}


#[cfg(test)]
mod tests {
    use super::*;

    const HELLO_NEAR: &str = "beneficiary";

    #[test]
    fn initializes() {
        let beneficiary: AccountId = HELLO_NEAR.parse().unwrap();
        let contract = Contract::init(beneficiary);
        assert_eq!(contract.hello_account, HELLO_NEAR.parse().unwrap())
    }
}
@mashharuki
Copy link
Author

スクリーンショット 2022-10-22 22 50 41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant