Home > OS >  Cannot Perform ViewFunction from near-api-js in Macos zsh but it works fine in Powershell
Cannot Perform ViewFunction from near-api-js in Macos zsh but it works fine in Powershell

Time:03-15

I've run into some error while trying to perform near-api-js viewFunction. I've made a script to check for storage balance of an accountId parse from the API body. When i receive the accountId parse from the API i parse into this ftGetStorageBalance function, from there the args: { account_id: accountId } is parse to accountViewFunction.

Here are the functions:

    async ftGetStorageBalance(
    tokenId: string,
    accountId: string,
  ): Promise<FTStorageBalance | null> {
    try {
      const config = await this.getDefaultConfig();
      const connection = await this.getConnect(config);
      const account = await this.getAccount(this.nearCfg.accountId, connection);
      return this.accountViewFunction(
        {
          methodName: 'storage_balance_of',
          args: { account_id: accountId },
        },
        account,
        tokenId,
      );
    } catch (e) {
      throw new Error(e);
    }
  }

Here is the function when the error hits:

async accountViewFunction(
    { methodName, args }: ViewFunctionOptions,
    account: nearAPI.Account,
    contractId: string,
  ): Promise<any> {
    // retrieve account_id from args

    //access the first key in the args
    // const key = Object.keys(args)[0];
    // retrieve the value of the key
    // const accountId = args[key];
    // const jsonArgs = { account_id: accountId };
    // const test = `{ "account_id" : "${accountId}" }`;
    // const jsontest = JSON.parse(test);
    // console.log(jsontest);
    // const bufferArgs = Buffer.from(JSON.stringify(jsonArgs));

    return account.viewFunction(contractId, methodName, args);
  }

I've tried console.log the args and here's what i get:

{ account_id: 'phuocsrp3.testnet' }

In the near-api-js library, it said that the args should be wrapped in JSON.

 * Invoke a contract view function using the RPC API.
     * @see {@link https://docs.near.org/docs/develop/front-end/rpc#call-a-contract-function}
     *
     * @param contractId NEAR account where the contract is deployed
     * @param methodName The view-only method (no state mutations) name on the contract as it is written in the contract code
     * @param args Any arguments to the view contract method, wrapped in JSON
     * @param options.parse Parse the result of the call. Receives a Buffer (bytes array) and converts it to any object. By default result will be treated as json.
     * @param options.stringify Convert input arguments into a bytes array. By default the input is treated as a JSON.
     * @returns {Promise<any>}
     */
    viewFunction(contractId: string, methodName: string, args?: any, { parse, stringify }?: {
        parse?: typeof parseJsonFromRawResponse;
        stringify?: typeof bytesJsonStringify;
    }): Promise<any>;

So i've tried parse into the accountViewFunction the json format with JSON.stringify(jsonArgs) stuff in the above script or even the Buffer.from(JSON.stringify(jsonArgs)) but it runs into the error stacks:

TypedError: [-32700] Parse error: Failed parsing args: missing field account_id
    at /Users/phuocha/Documents/phuoc_dev/work/starpunk-crosschain-starpad/node_modules/near-api-js/lib/providers/json-rpc-provider.js:329:31
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at Object.exponentialBackoff [as default] (/Users/phuocha/Documents/phuoc_dev/work/starpunk-crosschain-starpad/node_modules/near-api-js/lib/utils/exponential-backoff.js:7:24)
    at JsonRpcProvider.sendJsonRpc (/Users/phuocha/Documents/phuoc_dev/work/starpunk-crosschain-starpad/node_modules/near-api-js/lib/providers/json-rpc-provider.js:304:26)
    at JsonRpcProvider.query (/Users/phuocha/Documents/phuoc_dev/work/starpunk-crosschain-starpad/node_modules/near-api-js/lib/providers/json-rpc-provider.js:116:22)
    at Account.viewFunction (/Users/phuocha/Documents/phuoc_dev/work/starpunk-crosschain-starpad/node_modules/near-api-js/lib/account.js:366:24)
    at NearUtilsService.singleCheckStorageAndSendToken (/Users/phuocha/Documents/phuoc_dev/work/starpunk-crosschain-starpad/src/application/near/utils/near.utils.service.ts:478:28)
    at NearController.testsend (/Users/phuocha/Documents/phuoc_dev/work/starpunk-crosschain-starpad/src/application/near/near.controller.ts:58:20)
    at /Users/phuocha/Documents/phuoc_dev/work/starpunk-crosschain-starpad/node_modules/@nestjs/core/router/router-execution-context.js:46:28
    at /Users/phuocha/Documents/phuoc_dev/work/starpunk-crosschain-starpad/node_modules/@nestjs/core/router/router-proxy.js:9:17 {
  type: 'UntypedError',
  context: undefined
}

The above functions works well in Powershell but somehow it fails in macos environment.

Here is info about my env: Nodejs version: 14.18.3 Near-api-js: 0.44.2 Nestjs: 8.0.0

The above scripts I've taken a reference from: https://github.com/ref-finance/ref-ui/blob/main/src/services/near.ts

Please help!

CodePudding user response:

There are no problems with the arguments (args). You are using the token_id as the contractId when you call your viewFunction(). Maybe you can pass the correct contractId instead?

// Signature is fine. It expects a contractId, but you pass a tokenId when you call it.
accountViewFunction({ methodName, args },account,contractId){
  return account.viewFunction(contractId, methodName, args);
}

this.accountViewFunction({
  methodName: 'storage_balance_of',
  args: { account_id: accountId },
 },
 account,
 tokenId, // <-- you use this as contractId. 
);

Try to pass the contractId instead

this.accountViewFunction({
  methodName: 'storage_balance_of',
  args: { account_id: accountId },
 },
 account,
 this.contract.contractId, // <-- Use the contract's contractId
);

CodePudding user response:

We debugged this in office hours, the error was arising from using an undefined value in the contractId variable.

  • Related