我在尝试执行近api-js viewFunction时遇到了一些错误。我已经编写了一个脚本来检查accountId解析的存储平衡。当我收到来自API的accountId解析时,我解析为这个ftGetStorageBalance函数,从那里args:{ account_id: accountId }解析为accountViewFunction。
以下是功能:
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);
}
}以下是错误发生时的函数:
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);
}我尝试过console.log的args,下面是我得到的:
{ account_id: 'phuocsrp3.testnet' }在近api-js库中,它说应该用JSON包装args。
* 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>;因此,我尝试将accountViewFunction解析为json格式,上面的脚本中包含JSON.stringify(jsonArgs)内容,甚至Buffer.from(JSON.stringify(jsonArgs)),但是它遇到了错误堆栈:
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
}上面的函数在Powershell中运行良好,但在macos环境中却失败了。
以下是关于我的env: Nodejs版本的信息: 14.18.3近api-js: 0.44.2 Nestjs: 8.0.0
上面的脚本我参考了:https://github.com/ref-finance/ref-ui/blob/main/src/services/near.ts
请帮帮我!
发布于 2022-03-15 12:40:32
我们在办公时间对此进行了调试,错误是由于在undefined变量中使用了contractId值而产生的。
发布于 2022-03-15 00:38:52
参数(args)没有问题。当您调用您的token_id时,您正在使用contractId作为viewFunction()。也许你可以通过正确的contractId来代替?
// 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.
);尝试传递contractId而不是
this.accountViewFunction({
methodName: 'storage_balance_of',
args: { account_id: accountId },
},
account,
this.contract.contractId, // <-- Use the contract's contractId
);https://stackoverflow.com/questions/71467897
复制相似问题