Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
44 lines (31 sloc) 1.35 KB

Calling Contracts

Wiring smart contract functions to the window

You need to do two things in order to access your smart contract calls on the frontend.

  1. Defining the methods you intend to call in your contract, and making sure they are public. (You're probably good on this one)
  2. Declaring the methods that you want to call during the initialization of the contract on the frontend. (You probably forgot this one.)
// Initializing our contract APIs by contract name and configuration.
window.contract = await near.loadContract(config.contractName, {
...  
  // View methods are read only. They don't modify the state, but usually return some value. 
  viewMethods: ["hello"],
  // Change methods can modify the state. But you don't receive the returned value when called.
  changeMethods: [],
...
});

Calling smart contract functions

When calling your functions on the front end, instead of calling:

contract.someMethod("YOUR DATA");

You need to send the object with the variable name that's going to be used in the backend, just like when calling a REST API.

contract.someMethod({
    myData: "YOUR DATA"
})

If you're not passing the params as a JSON, you'll often see an error in the encoder that looks similar to this:

"ABORT: unexpected string field null : 'YOUR DATA'".
You can’t perform that action at this time.