0

I'm studying solidity development with FreeCodeCamp.org.

Within the course there is a contract called fund.sol where you can finance a contract with donations in dollars, the following question occurred to me:

I have a metamask wallet in BSC-20, and I want the funds to be transferred to this wallet, however when doing the test the fund wallet was changed and I lost the funds, I wrote down the wallet, but when inserting it into a new compilation and trying to withdraw the funds an error is displayed:

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?Returned error: Internal JSON-RPC error.[object Object]

Below is my code:

//SPDX-License-Identifier: MIT


pragma solidity ^0.8.8;



import "./PriceConverter.sol";

error NotOwner();

contract FundMe{

    using PriceConverter for uint256;


    address[] public funders;
    mapping(address => uint256) public addressToAmountFunded; // mapeamento para saber quem doou quanto em valor

    address public immutable i_owner;

    constructor(){
        i_owner = msg.sender;
    }

    function fund() public payable{

        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); 
        (bool callSuccess, ) = payable(msg.sender).call{value: address(this).balance}(""); 
        require(callSuccess, "Call failed");

    }

    modifier onlyOwner{

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


    receive() external payable {   
        fund();    
    }

    fallback() external payable { 
        fund();
    }

    
}

This is the PriceConverter.sol:


import {AggregatorV3Interface} from "@chainlink/[email protected]/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";


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

library PriceConverter{
    
    function getPrice() internal view returns(uint256){
       
        AggregatorV3Interface priceFeed = AggregatorV3Interface(0x14e613AC84a31f709eadbdF89C6CC390fDc9540A);
       
        (, int256 answer,,,) = priceFeed.latestRoundData(); 
       
        return uint256 (answer * 1e10); 

    }

    function getVersion () internal view returns (uint256){
        AggregatorV3Interface priceFeed = AggregatorV3Interface(0x14e613AC84a31f709eadbdF89C6CC390fDc9540A);
        return priceFeed.version();
    }


    function getConversionRate(uint256 ethAmount) internal view returns(uint256){
        uint256 ethPrice = getPrice();
        
        uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1e18; 

        return ethAmountInUsd;
    }

}

Thx for ur help

New contributor
user2238658 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

2 Answers 2

1

Since, withdraw() function is having onlyOwner modifier attached to it, so it can only be called using the wallet using which you've deployed the contract, otherwise it would just revert with NotOwner() custom error.

0
0

I remove onlyOwner and the error continue. The code:


//SPDX-License-Identifier: MIT


pragma solidity ^0.8.8;


import "./PriceConverter.sol";


contract FundMe{

    using PriceConverter for uint256;


    address[] public funders;
    mapping(address => uint256) public addressToAmountFunded;

    
    function fund() public payable{

        funders.push(msg.sender);
        addressToAmountFunded[msg.sender] = msg.value;

    }

    
    function withdraw() public {

        for (uint256 funderIndex = 0;funderIndex < funders.length; funderIndex++){ 
            address funder = funders[funderIndex];
            addressToAmountFunded[funder] = 0;
        }

        funders  = new address[](0); 
        (bool callSuccess, ) = payable(msg.sender).call{value: address(this).balance}(""); 
        require(callSuccess, "Call failed");

    }

    receive() external payable {   
        fund();    
    }

    fallback() external payable { 
        fund();
    }
    
}


The error:

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
Returned error: Internal JSON-RPC error.
[object Object]
New contributor
user2238658 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.