我已经安装了硬帽并使用
分叉了主网npx hardhat node --fork https://eth-mainnet.g.alchemy.com/v2/my-api-key
现在,在开发一个非常简单的测试时,如:
const { expect, assert} = require("chai");
const { ethers, waffle } = require("hardhat")
describe("Read and write to the blockchain", () => {
it('should send a transaction (swap a token)', async function () {
const [ownerSigner] = await ethers.getSigners()
const mainNetForkUniswapRouter = new ethers.Contract(addressRouter, routerABI, ownerSigner);
const amountOut = await getAmountOut()
const myAddress = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
const txSwap = await mainNetForkUniswapRouter.swapExactTokensForTokens(
amountIn, // amountIn
amountOut, // amountOut
[addressFrom, addressTo], // path
myAddress, // address to
Date.now() + 1000 + 60 + 5, // deadline
{
gasLimit: 200000,
gasPrice: ethers.utils.parseUnits("5.5", "gwei")
}// gas
);
assert(txSwap.hash)
console.log("The waffle")
console.log(waffle)
console.log("--- --- --- --- --- --- ---")
const mainnetForkProvider = waffle.provider;
const txReceipt = await mainnetForkProvider.getTransactionReceipt(txSwap.hash)
console.log("")
console.log("Swap tx")
console.log(txSwap)
console.log("")
console.log("Tx Receipt")
console.log(txReceipt)
});
});
当运行这个测试with npx hardhat test
时,我得到以下错误消息:
TypeError: Cannot read properties of undefined (reading 'provider')
at Context.<anonymous> (test/sendSwapTx.js:84:44)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at runNextTicks (node:internal/process/task_queues:65:3)
at listOnTimeout (node:internal/timers:528:9)
at processTimers (node:internal/timers:502:7)
堆栈跟踪指向const mainnetForkProvider = waffle.provider;
似乎出于某种原因waffle
是未定义的,但我不明白为什么,因为我在代码的顶部导入它。
关于如何修复此错误的任何帮助?
@user3358107评论回复:
将这行添加到
hardhat.config.js
:require("@nomiclabs/hardhat-waffle");
试试这些版本:
npm install --save-dev hardhat@^2.9.3 @nomiclabs/hardhat-waffle@^2.0.0 ethereum-waffle@^3.0.0 chai@^4.2.0 @nomiclabs/hardhat-ethers@^2.0.0 ethers@^5.0.0
尝试将const mainnetForkProvider = waffle.provider;
更改为:
const mainnetForkProvider = network.provider;
很惊讶你不用把收据改成:
const txReceipt = txSwap.wait();