Lesson 7: FundMe.test.js "Error: call revert exception" #1752
-
Hi another time. FundMe
constructor
1) sets the aggregator addresses correctly
fund
✔ Didn't send enough
1 passing (973ms)
1 failing
1) FundMe
constructor
sets the aggregator addresses correctly:
Error: call revert exception [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (method="priceFeed()", data="0x", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.6.4)
at Logger.makeError (node_modules/@ethersproject/logger/src.ts/index.ts:261:28)
at Logger.throwError (node_modules/@ethersproject/logger/src.ts/index.ts:273:20)
at Interface.decodeFunctionResult (node_modules/@ethersproject/abi/src.ts/interface.ts:427:23)
at Contract.<anonymous> (node_modules/@ethersproject/contracts/src.ts/index.ts:400:44)
at step (node_modules/@ethersproject/contracts/lib/index.js:48:23)
at Object.next (node_modules/@ethersproject/contracts/lib/index.js:29:53)
at fulfilled (node_modules/@ethersproject/contracts/lib/index.js:20:58)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. FundMe.test.js: const { assert, expect } = require("chai")
const { providers } = require("ethers")
const { deployments, ethers, getNamedAccounts } = require("hardhat")
describe("FundMe", async function () {
let fundMe
let deployer
let mockV3Aggregator
const sendValue = ethers.utils.parseEther("1") // that converts the 1 to the amount of ether
beforeEach(async () => {
// deploy our fundMe contract
// using Hardhat-deploy
// const accounts = await ethers.getSigners()
// const accountZero = accounts[0]
const {deployer} = await getNamedAccounts(); //this catch the deployer object and signed on deployer
await deployments.fixture(["all"]) //fixture from hardhat allows us to acces all tags of the folders and files
fundMe = await ethers.getContractAt("FundMe", deployer) //now, fundMe variable would be equal to the contract FundMe
mockV3Aggregator = await ethers.getContractAt(
"MockV3Aggregator",
deployer
)
providers.getCode
})
describe("constructor", async function () {
it("sets the aggregator addresses correctly", async function () {
const response = await fundMe.priceFeed() //this will run locally the priceFeed variable of the smartcontract
assert.equal(response, mockV3Aggregator.address)
})
})
describe("fund", async function (){
it("Didn't send enough", async function(){
await fundMe.fund()
})
})
}) FundMe.sol: // SPDX-License-Identifier: MIT
// Pragma
pragma solidity ^0.8.7;
// Imports
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "./PriceConverter.sol";
// Error Codes
error FundMe__NotOwner();
//Interfaces, Libraries, Contracts
/** @title A contract for crowd funding
* @author BeastDevMoney
* @notice This contract is to demo a samle funding contract
* @dev This implements price feeds as our library
*/
contract FundMe {
//Type Declarations
using PriceConverter for uint256;
//State Variables
mapping(address => uint256) public addressToAmountFunded;
address[] public funders;
address public immutable owner;
uint256 public constant minimumUSD = 50 * 1e18; //constant variables are cheaper when it is called than normal variables
AggregatorV3Interface public priceFeed;
modifier onlyOwner() {
//require(msg.sender == owner);
if (msg.sender != owner) revert FundMe__NotOwner();
_;
}
//Functions Order:
//// constructor
//// receive
//// fallback
//// external
//// public
//// internal
//// private
//// view / pure
constructor(address priceFeedAddress) {
owner = msg.sender;
priceFeed = AggregatorV3Interface(priceFeedAddress);
}
/**
* @notice This function funds this contract
* @dev This implements price feeds as our library
here you can put "paramaters" and "returns" if it has the function
*/
function fund() public payable {
require( //the first parameter it goint to be msg.value and the second paramater it's going to be priceFeed
msg.value.getConversionRate(priceFeed) >= minimumUSD,
"Didn't send enough!"
);
funders.push(msg.sender);
addressToAmountFunded[msg.sender] = msg.value;
}
function withdraw() public onlyOwner {
for (
uint256 funderIndex = 0;
funderIndex < funders.length;
funderIndex++
) {
address funder = funders[funderIndex];
addressToAmountFunded[funder] = 0;
}
funders = new address[](0);
//transfer
//msg.sender = address
//payable(msg.sender) = payable address
//call
(bool callSucces, ) = payable(msg.sender).call{
value: address(this).balance
}("");
require(callSucces, "Call failed");
//this it refers to the hole contract
}
}
I don't know if its something simple, but I don't understand whats going on. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
Following along with the following should fix your issue1. In FundMe.test.js, copy the following code:describe("constructor", async function () {
it("sets the aggregator addresses correctly", async function () {
const response = await fundMe.getPriceFeed() //this will run locally the priceFeed variable of the smartcontract
assert.equal(response, mockV3Aggregator.address)
})
}) Also, is the following code correct? I have not seen this being used, make sure it is — if not, please remove it. providers.getCode 2. In FundMe.sol, add the following code at last:function getPriceFeed() public view returns (AggregatorV3Interface) {
return priceFeed;
} |
Beta Was this translation helpful? Give feedback.
-
In test please replace this: mockV3Aggregator = await ethers.getContractAt(
"MockV3Aggregator",
deployer
) by this mockV3Aggregator = await ethers.getContract(
"MockV3Aggregator",
deployer
) and here describe("constructor", async function () {
it("sets the aggregator addresses correctly", async function () {
const response = await fundMe.priceFeed() //this will run locally the priceFeed variable of the smartcontract
assert.equal(response, mockV3Aggregator.address)
})
}) try logging response and mockV3Aggregator.address |
Beta Was this translation helpful? Give feedback.
-
@eerjuano Push the code to GitHub and leave link here, need to fix issues. |
Beta Was this translation helpful? Give feedback.
-
Hey @eerjuano write your beforeEach like this:
This might solve some of your errors |
Beta Was this translation helpful? Give feedback.
@eerjuano Push the code to GitHub and leave link here, need to fix issues.