gpt4 book ai didi

nearprotocol - 在附近调用 changemethods 后尝试获取最终执行结果的交易详细信息

转载 作者:行者123 更新时间:2023-12-05 06:52:34 24 4
gpt4 key购买 nike

我确实有一个同时具有 viewMethods 和 changeMethods 的智能合约。

我能够部署合约,并且我们能够成功调用 View 方法和 chnagementhods。

根据附近的文档,无论何时调用函数,它都会返回 promise 。async functionCall(contractId: string, methodName: string, args: any, gas: number, amount?: BN): Promise<FinalExecutionOutcome> {}

但是当我尝试获取结果时,它始终为空。

想法是从响应中获取交易细节。

这是示例代码

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;


}

谢谢

最佳答案

您没有使用functionCall 方法,您是直接使用合约上的方法。如果你想要交易细节,你需要在账户上使用 functionCall 方法,如下例:

我假设你在 initNear() 中返回了 near,并且可以连接到钱包。

// 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
},
});

关于nearprotocol - 在附近调用 changemethods 后尝试获取最终执行结果的交易详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65928288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com