Nechi
Nechi

Reputation: 41

Javascript functioncall on Near Protocol

I want to do a functioncall on Nearprotocol with javascript, but there is always this error:

Account.viewFunction(contractId, methodName, args, options) deprecated use Account.viewFunction(ViewFunctionCallOptions) instead <anonymous>:null:null throw new errors_1.TypedError(errorMessage, response.error.name);

This is my js script:

const nearAPI = require("near-api-js");
async function contractFunctionCall() {
    const {keyStores, KeyPair, connect} = nearAPI;
    const myKeyStore = new keyStores.InMemoryKeyStore();
    const PRIVATE_KEY = "the private key of this dev account";


    const keyPair = KeyPair.fromString(PRIVATE_KEY);

    await myKeyStore.setKey("testnet", "dev-1673623139694-96382653233746", keyPair);

    const connectionConfig = {
        networkId: "testnet",
        keyStore: myKeyStore,
        nodeUrl: "https://rpc.testnet.near.org",
        walletUrl: "https://wallet.testnet.near.org",
        helperUrl: "https://helper.testnet.near.org",
        explorerUrl: "https://explorer.testnet.near.org",
    };
    // connect to NEAR
    const nearConnection = await connect(connectionConfig);
    const account = await nearConnection.account("dev-1673623139694-96382653233746");

    const contract = new nearAPI.Contract(account, "dev-1673623139694-96382653233746", {
        viewMethods: ["get_charge"],
        changeMethods: ["store_charge"]
    });

    const response = await account.viewFunctionV1("get_charge", JSON.stringify({key: "key1"}));
    console.log(response);

}
contractFunctionCall();

Upvotes: 1

Views: 124

Answers (1)

Nechi
Nechi

Reputation: 41

The fault was at my smart contract. My parameters at my view-method was (&mut self). The (&mut self) only makes it to my compiler public. I changed the parameters to (&self) and it did work!

Upvotes: 1

Related Questions