【发布时间】:2021-03-27 10:50:21
【问题描述】:
首先,让我先说我无论如何都不是开发人员。我有一个很小的编码背景,我试图在这里依赖它,但它让我失望了。
我的问题如下:
我有一些代码,它基本上是一个在以太坊网络上执行智能合约工作的机器人。代码完全在 javascript 中,所以我试图做的区块链方面是无关紧要的。但基本上我正在尝试执行一个 API 请求,该请求接收一个数字(称为“gas”),这个数字决定了我的机器人愿意支付多少费用才能在以太坊网络上执行工作。但是,我了解到 API 发送的默认数字太低。所以我决定尝试通过乘以从 API 请求中获得的数字来增加 gas。但是,当我这样做时,似乎代码在收到 API 请求之前尝试将数字相乘。因此,我收到一个 NaN 错误。原始代码如下所示:
async work() {
this.txPending = true;
try {
const gas = await this.getGas();
const tx = await this.callWork(gas);
this.log.info(`Transaction hash: ${tx.hash}`);
const receipt = await tx.wait();
this.log.info(`Transaction confirmed in block ${receipt.blockNumber}`);
this.log.info(`Gas used: ${receipt.gasUsed.toString()}`);
} catch (error) {
this.log.error("While working:" + error);
}
this.txPending = false;
}
我所做的更改如下所示:
async work() {
this.txPending = true;
try {
let temp_gas = await this.getGas();
const gas = temp_gas * 1.5;
const tx = await this.callWork(gas);
console.log(gas);
console.log('something');
const receipt = await tx.wait();
this.log.info(`Transaction confirmed in block ${receipt.blockNumber}`);
this.log.info(`Gas used: ${receipt.gasUsed.toString()}`);
} catch (error) {
this.log.error("While working:" + error);
}
this.txPending = false;
}
根据我的研究,似乎我需要做的是创建一个 Promise 函数,但我完全不知道如何创建它。如果有人可以帮助我,我愿意付给他们一点以太坊,也许 0.2(大约 150 美元)。
【问题讨论】:
-
对不起,这2段代码有什么区别?
标签: javascript ethereum solidity