'Trying to get transacation details for FinalExecutionOutcome after calling changemethods on near

I do have a smart contract that has both viewMethods as well as changeMethods.

I am able to deploy the contract and we are able to successfully call both the viewmethods as well as chnagementhods.

As per near documentation whenever a function is called it return the promise. async functionCall(contractId: string, methodName: string, args: any, gas: number, amount?: BN): Promise<FinalExecutionOutcome> {}

but when I try to get the result, it is always null.

The idea is to get transaction details from the response.

This is sample code

import * as nearAPI from "near-api-js"
import { getConfig } from './config'

async initContract() {

if (!this.contract) {
    //initialize near
    await this.initNear();

    this.contract = await this.near.loadContract(process.env.CONTRACT_NAME, { // eslint-disable-line require-atomic-updates
        // NOTE: This configuration only needed while NEAR is still in development
       
        viewMethods: [...],
        // Change methods can modify the state. But you don't receive the returned value when called.
        changeMethods: [ ...."set_comments"],
        // Sender is the account ID to initialize transactions.
        sender: ....
    });
}

return this;
}


    async setComments(
        id: String,
        comments: String
    ) {
        if (this.contract) {
           const respone=  await this.contract.set_comments(
                {
                    id: id,
                    comments: comments
                });
            return respone;
//response is always coming as null even though the comments are updated on near
        }
        return null;
 

       }

Thanks



Solution 1:[1]

You are not using the functionCall method, you are using the method on the contract directly. If you want the transaction details, you need to use the functionCall method on the account, like the below example:

I assume you return near inside initNear(), and can connect to the wallet.

// initialize near
const near = await this.initNear();

// connect to the wallet
const wallet = new WalletConnection(near,'APP_KEY_PREFIX');


// Inside setComments function
const response = await wallet.account().functionCall({
  contractId: this.contract.contractId,
  methodName: 'set_comments',
  args: {
   id: id,
   comments: comments
  },
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John