Lesson 8: TypeError: AbiCoder is not a constructor #5683
-
Hi, const { assert, expect } = require("chai")
const { deployments, ethers, getNamedAccounts } = require("hardhat")
describe("FundME", function () {
let fundme, mock
let deployer
let sendEther = ethers.utils.parseEther("1")
beforeEach(async () => {
await deployments.fixture(["all"])
deployer = (await getNamedAccounts()).deployer
const myContract = await deployments.get("FundMe")
const myMockContract = await deployments.get("MockV3Aggregator")
fundme = await ethers.getContractAt(myContract.abi, myContract.address)
mock = await ethers.getContractAt(
myMockContract.abi,
myMockContract.address
)
})
describe("Constructor", function () {
it("MockAggregator address should be the priceFeed address", async () => {
const response = await fundme.priceFeed()
assert.equal(response, mock.address)
})
})
describe("fund", function () {
it("Send enough ether to contract", async () => {
expect(fundme.fund()).to.be.revertedWith("Didn't send enough")
})
it("funder is deployer", async () => {
await fundme.fund({ value: sendEther })
const response = fundme.funders(0)
assert(response, deployer)
})
it("msg.sender could represent a value", async () => {
await fundme.fund({ value: sendEther })
const response = fundme.funderToAmount(deployer)
assert(response.toString(), sendEther.toString())
})
})
describe("withdraw", function () {
it("work with single account", async () => {
const startingFundBalance = await ethers.provider.getBalance(
fundme.address
)
const startingDeployerBalance = await ethers.provider.getBalance(
deployer
)
//act
const transactionResponse = await fundme.withdraw()
const transactionReceipt = await transactionResponse.wait(1)
const { gasUsed, effectiveGasPrice } = transactionReceipt
const gasCost = gasUsed.mul(effectiveGasPrice)
//ending
const endingFundBalance = await ethers.provider.getBalance(
fundme.address
)
const endingDeployerBalance = await ethers.provider.getBalance(
deployer
)
assert(endingFundBalance, 0)
assert.equal(
startingFundBalance.add(startingDeployerBalance).toString(),
endingDeployerBalance.add(gasCost).toString()
)
})
it("woke by multiple accounts", async () => {
const siners = await ethers.getSigners()
for (let i = 1; i < 6; i++) {
const connectedFundMeContract = await fundme.connect(siners[i])
await connectedFundMeContract.fund({ value: sendEther })
}
const startingFundBalance = await ethers.provider.getBalance(
fundme.address
)
const startingDeployerBalance = await ethers.provider.getBalance(
deployer
)
//act
const transactionResponse = await fundme.withdraw()
const transactionReceipt = await transactionResponse.wait(1)
const { gasUsed, effectiveGasPrice } = transactionReceipt
const gasCost = gasUsed.mul(effectiveGasPrice)
//ending
const endingFundBalance = await ethers.provider.getBalance(
fundme.address
)
const endingDeployerBalance = await ethers.provider.getBalance(
deployer
)
assert(endingFundBalance, 0)
assert.equal(
startingFundBalance.add(startingDeployerBalance).toString(),
endingDeployerBalance.add(gasCost).toString()
)
//after ending withdraw all of funders and addressToFunders reset
await expect(fundme.funders(0)).to.be.reverted
})
})
}) this is my hardhat.config require("@nomicfoundation/hardhat-toolbox")
require("@nomiclabs/hardhat-ethers")
require("hardhat-deploy")
require("dotenv").config()
const SEPOLIA_RPC_URL = process.env.SEPOLIA_RPC_URL
const PRAIVATE_KEY_0 = process.env.PRAIVATE_KEY_0
const ETHERSCAN_API = process.env.ETHERSCAN_API
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
// solidity: "0.8.18",
solidity: {
compilers: [{ version: "0.8.8" }, { version: "0.6.6" }],
},
namedAccounts: {
deployer: {
default: 0,
},
user: {
default: 1,
},
},
networks: {
sepolia: {
url: SEPOLIA_RPC_URL,
accounts: [PRAIVATE_KEY_0],
chainId: 11155111,
blockConfirmations: 6,
},
},
etherscan: {
apiKey: ETHERSCAN_API,
},
gasReporter: {
enabled: false,
noColors: true,
currency: "USD",
// coinmarketcap: COINMARKETCAP_API,
token: "MATIC",
},
} my contract: // SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
//we want fund form some users
//we want withdraw all Funded amount
//we want modify a value
import "./PriceConvertor.sol";
contract FundMe {
using PriceConvertor for uint256;
uint256 MINIMUM_AMOUNT = 10 * 1e18; //10000000000000000000 wei = 10 $
address public i_owner;
address[] public funders;
mapping(address => uint256) public funderToAmount;
AggregatorV3Interface public priceFeed;
constructor(address priceFeedAddress) {
i_owner = msg.sender;
priceFeed = AggregatorV3Interface(priceFeedAddress);
}
//fund
function fund() public payable {
require(
msg.value.getConversion(priceFeed) > MINIMUM_AMOUNT,
"Didn't send enough"
);
funders.push(msg.sender);
funderToAmount[msg.sender] += msg.value;
}
function withdraw() public onlyOwner {
for (uint256 funderIndex; funderIndex < funders.length; funderIndex++) {
address funder = funders[funderIndex];
funderToAmount[funder] += 0;
}
funders = new address[](0);
//Use call to transfer amount
(bool callSuccess, ) = payable(msg.sender).call{
value: address(this).balance
}("");
require(callSuccess, "call fail");
}
modifier onlyOwner() {
require(msg.sender == i_owner, "function sender is not owner");
_;
}
} Also my terminal: FundME
withdraw
1) woke by multiple accounts
0 passing (1s)
1 failing
1) FundME
withdraw
woke by multiple accounts:
TypeError: AbiCoder is not a constructor
at decodeReturnData (node_modules\@nomicfoundation\hardhat-chai-matchers\src\internal\reverted\utils.ts:65:15)
at onError (node_modules\@nomicfoundation\hardhat-chai-matchers\src\internal\reverted\reverted.ts:62:49)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at runNextTicks (node:internal/process/task_queues:64:3)
at listOnTimeout (node:internal/timers:533:9)
at processTimers (node:internal/timers:507:7)
at Context.<anonymous> (test\unit\FundMe.test.js:116:13)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
@mahziarwoas You are the second person facing this issue and I am not sure about this error, please leave your repository link so I can test it locally, I think it is an update somewhere. |
Beta Was this translation helpful? Give feedback.
-
The issue is with You can test like this |
Beta Was this translation helpful? Give feedback.
@mahziarwoas The error is in the
chai matchers
, it is using ethers version 6 and insidenode_modules/@nomicfoundation/hardhat-chai-matchers/src/internal/reverted/utils.ts:65:15
it is using this code which imports theAbiCoder
and initiate it.And we are using ethers version 5 because
hardhat-deploy
is dependent on it, now I think from this point onward we need to leavehardhat-deploy
and use the hardhat way to deploy and test our contracts untilhardhat-deploy
get updated to ethers version 6.