Nhảy đến nội dung chính

Contract

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, "example-contract.testnet", {
changeMethods: ["my_smart_contract_function"], // your smart-contract has a function `my_smart_contract_function`
});
// `contract` object has `my_smart_contract_function` function on it:
contract.my_smart_contract_function();

Load Contract

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

Call Contract

const contract = new Contract(account, "example-contract.testnet", {
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)
);

Class Contract

Was this page helpful?