Contract

To Share and +4 nLEARNs

When you instantiate an instance of Contract you need to specify the names of the functions you have on your smart contract.
Then the new instance of Contract will have methods with the same names as your smart contract functions.
For example if you deployed a contract with my_smart_contract_function function on it, then this will work:

const contract = new Contract(account, {
  changeMethods: ["my_smart_contract_function"], // your smart-contract has a function `my_smart_contract_function`
  sender: account
});
// `contract` object has `my_smart_contract_function` function on it: 
contract.my_smart_contract_function()

Load Contract


“`js
const { Contract } = nearAPI;

const contract = new Contract(
account, // the account object that is connecting
“example-contract.testnet”,
{
// name of contract you’re connecting to
viewMethods: [“getMessages”], // view methods do not change state but usually return a value
changeMethods: [“addMessage”], // change methods modify state
}
);
“`

[ Class `Contract`](https://near.github.io/near-api-js/classes/contract.contract-1.html)


“`js
const { Contract } = nearAPI;

const contract = new Contract(
wallet.account(), // the account object that is connecting
“example-contract.testnet”,
{
// name of contract you’re connecting to
viewMethods: [“getMessages”], // view methods do not change state but usually return a value
changeMethods: [“addMessage”], // change methods modify state
}
);
“`

[ Class `Contract`](https://near.github.io/near-api-js/classes/contract.contract-1.html)


Call Contract


“`js
const contract = new Contract(account, {
changeMethods: [“method_name”],
});
await contract.method_name(
{
arg_name: “value”, // argument name and value – pass empty object if no args required
},
“300000000000000”, // attached GAS (optional)
“1000000000000000000000000” // attached deposit in yoctoNEAR (optional)
);
“`


“`js
const contract = new Contract(account, {
changeMethods: [“method_name”],
});
await contract.method_name(
{
callbackUrl: ‘https://example.com/callback’, // callbackUrl after the transaction approved (optional)
meta: ‘some info’, // meta information NEAR Wallet will send back to the application. `meta` will be attached to the `callbackUrl` as a url param
args: {
arg_name: “value” // argument name and value – pass empty object if no args required
},
gas: 300000000000000, // attached GAS (optional)
amount: 1000000000000000000000000 // attached deposit in yoctoNEAR (optional)
}
);
“`


“`js
const contract = new Contract(account, {
viewMethods: [“view_method_name”],
});
const response = await contract.view_method_name();
“`


“`js
const contract = new Contract(account, {
viewMethods: [“view_method_name”],
});
const response = await contract.view_method_name({ arg_name: “arg_value” });
“`


Class Contract

Generate comment with AI 2 nL
Scroll to Top
Report a bug👀