LESSON 7: Error: contract runner does not support sending transactions #6420
-
I am writing unit test for checking if only owner can withdraw from contract, my code is as below const { deployments, ethers, getNamedAccounts } = require("hardhat")
const { assert, expect } = require("chai")
describe("FundMe", async function () {
let fundMe
let deployer
let MockV3Aggregator
const sendValue = ethers.parseEther("1")
beforeEach(async function () {
//const accounts = ether.getSigners()
deployer = (await getNamedAccounts()).deployer
const deploymentResults = await deployments.fixture(["all"]) //will run all deploy scripts of whose tags are mentioned here
const fundMeAddress = deploymentResults['FundMe']?.address;
fundMe = await ethers.getContractAt("FundMe", fundMeAddress)
const mockV3AggregatorAddress =
deploymentResults['MockV3Aggregator']?.address;
MockV3Aggregator = await ethers.getContractAt("MockV3Aggregator", mockV3AggregatorAddress)
})
describe("withdarw", async function () {
beforeEach(async function () {
await fundMe.fund({ value: sendValue })
})
it("withdraw eth from a single founder", async function () {
//Arrange
const startingFundMeBalance = await ethers.provider.getBalance(fundMe.target)
const startingDeployerBalance = await ethers.provider.getBalance(deployer)
//Act
const transactionResponse = await fundMe.withdraw()
const transactionReciept = await transactionResponse.wait(1)
const { gasUsed, gasPrice } = transactionReciept
const gasCost = gasUsed * gasPrice
const endingFundMeBalance = await ethers.provider.getBalance(
fundMe.target
)
const endingDeployerBalance = await ethers.provider.getBalance(
deployer
)
//Assert
assert.equal(endingFundMeBalance, 0)
assert.equal((startingFundMeBalance + startingDeployerBalance).toString(), (endingDeployerBalance + gasCost).toString())
})
it("allows us to withdraw with multiple funders", async function () {
const accounts = await ethers.getSigners()
for (let i = 1; i < 6; i++) {
const fundMeConnected = await fundMe.connect(accounts[i])
await fundMeConnected.fund({ value: sendValue })
}
const startingFundMeBalance = await ethers.provider.getBalance(fundMe.target)
const startingDeployerBalance = await ethers.provider.getBalance(deployer)
const transactionResponse = await fundMe.withdraw()
const transactionReciept = await transactionResponse.wait(1)
const { gasUsed, gasPrice } = transactionReciept
const gasCost = gasUsed * gasPrice
const endingFundMeBalance = await ethers.provider.getBalance(
fundMe.target
)
const endingDeployerBalance = await ethers.provider.getBalance(
deployer
)
assert.equal(endingFundMeBalance, 0)
assert.equal((startingFundMeBalance + startingDeployerBalance).toString(), (endingDeployerBalance + gasCost).toString())
await expect(fundMe.funders(0)).to.be.reverted
for (let i = 1; i < 6; i++) {
assert.equal(await fundMe.addressToAmountFunded(accounts[i].address), 0)
}
})
it("only allows owner to withdraw", async function () {
const accounts = ethers.getSigners()
// const attacker = accounts[1]
const fundMeConnected = await fundMe.connect(accounts[1])
await expect(fundMeConnected.withdraw()).to.be.reverted
})
})
}) my fund me contract is- // SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "./PriceConverter.sol";
error FundMe_NotOwner();
/** @title a contract to crowdfunding
* @author Neelaksh Singh
* @notice A sample funding contract for practise
* @dev Implements price feed as our library
*/
contract FundMe {
using PriceConverter for uint256;
AggregatorV3Interface public priceFeed;
mapping(address => uint256) public addressToAmountFunded;
address[] public funders;
// Could we make this constant? /* hint: no! We should make it immutable! */
address public /* immutable */ iOwner;
uint256 public constant MINIMUM_USD = 50 * 10**18;
uint256 public sentValue;
modifier onlyOwner {
// require(msg.sender == owner);
if (msg.sender != iOwner) revert FundMe_NotOwner();
_;
}
constructor(address priceFeedAddress) {
iOwner = msg.sender;
priceFeed = AggregatorV3Interface(priceFeedAddress);
}
// fallback() external payable {
// fund();
// }
// receive() external payable {
// fund();
// }
function fund() public payable {
sentValue = msg.value.getConversionRate(priceFeed);
require(sentValue >= MINIMUM_USD, "You need to spend more ETH!");
// require(PriceConverter.getConversionRate(msg.value) >= MINIMUM_USD, "You need to spend more ETH!");
addressToAmountFunded[msg.sender] += msg.value;
funders.push(msg.sender);
}
function withdraw() public onlyOwner {
for (uint256 funderIndex=0; funderIndex < funders.length; funderIndex++){
address funder = funders[funderIndex];
addressToAmountFunded[funder] = 0;
}
funders = new address[](0);
// // transfer
// payable(msg.sender).transfer(address(this).balance);
// // send
// bool sendSuccess = payable(msg.sender).send(address(this).balance);
// require(sendSuccess, "Send failed");
// call
(bool callSuccess, ) = payable(msg.sender).call{value: address(this).balance}("");
require(callSuccess, "Call failed");
}
} on running -
please help me resolve this issue |
Beta Was this translation helpful? Give feedback.
Answered by
mari0-0
Apr 9, 2024
Replies: 1 comment
-
Hello 👋 Just add
to
this will solve the error. Mark this discussion as answered if it worked |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Neel10Singh
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello 👋
Just add
await
keyword in front ofgetSigners()
change your code from
const accounts = ethers.getSigners()
to
const accounts = await ethers.getSigners()
this will solve the error. Mark this discussion as answered if it worked