smartcontractkit / full-blockchain-solidity-course-js

Learn Blockchain, Solidity, and Full Stack Web3 Development with Javascript
12.55k stars 3k forks source link

I keep getting error when I call fund function #1553

Closed benduongquangmobile closed 2 years ago

benduongquangmobile commented 2 years ago

Lesson

Lesson 4

Could you please leave a link to the timestamp in the video where this error occurs? (You can right click a video and "copy video URL at current time")

https://youtu.be/gyMwXuJrbJQ?t=17832

Operating System

macOS (Apple Silicon)

Describe the bug

I keep getting error when I call fund function

image

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15; // solidity 0.8.15

import "./PriceConverter.sol";
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "hardhat/console.sol";

// get funds from users
// wthdraw funds
// set a minimum amount of funds value in USD

// 1e18 == 10^18 == 1 ether

error NotOwner();

contract FundMe {
    using PriceConverter for uint256;
    uint256 public constant MINIMUM_USD = 1 * 1e18;
    mapping(address => uint256) public addressToAmountFunded;
    address[] public funders;
    address public immutable i_owner;

    constructor() {
        i_owner = msg.sender;
    }

    function fund() public payable {
        console.log(msg.value.getConversionRate());

        require(
            msg.value.getConversionRate() >= 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);

        // call
        (bool callSuccess, ) = payable(msg.sender).call{
            value: address(this).balance
        }("");
        console.log("callSuccess:", callSuccess);
        require(callSuccess, "Call failed");
    }

    modifier onlyOwner() {
        if (msg.sender != i_owner) {
            revert NotOwner();
        }
        _;
    }

    receive() external payable {
        fund();
    }

    fallback() external payable {
        fund();
    }
}
br0wnD3v commented 2 years ago

Its just remix telling you that the transaction is likely to fail, which I think is because you didn't send any ETH along when trying to use the fund(). Add an arbitrary value of ETH, say 0.1ETH with the transaction.