Giter VIP home page Giter VIP logo

cross-contract-hello-rust-deprecated's People

Contributors

gagdiez avatar idea404 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

cross-contract-hello-rust-deprecated's Issues

ExecutionError on the change_greeting function

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())
    }
}

adding an example of how to get the value of a cross-contract call in a contract

Hi, while studying the NEAR Protocol, I encountered the following question: how to get the value of an cross-contract call not in a command line request or a JavaScript request, but in one of the methods of the contract to further interact with the received value. That is, something like this:

    pub fn method(&self, greeting_2: String) -> String {
          ....
          let greeting = self.query_greeting().value;
          greeting + greeting_2
    }

I propose to add an example of how to solve this issue, because it is quite useful to handle the value of the cross-contract call in the contract itself, and it will also help beginners in learning the Near Protocol.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.