javascriptsoliditysmartcontractsmetamaskdecentralized-applications

"Internal JSON-RPC error" when calling Solidity smart contract function


I'm building a DApp and i'm trying to use a function in my smart contract to get the actual ethereum value, and then use it to convert a certain amount of dollars into its Ethereum value. When trying to do so, i'm getting "Internal JSON-RPC error" and i can't understand why. The contract has been correctly compiled and migrated.

This is the js code to call the function:

App.contracts.TravelCoin.deployed().then(function(instance) {
                                flightsInstance = instance;
                                return flightsInstance.GetValueInEth(dollarPrice);
                            }).then(function(value) {
                                console.log("inside function");
                                cell2.innerHTML = value;
                            }).catch(function(err) {
                                console.log(err.message);
                            });

This is the Solidity smart contract code:

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

contract TravelCoin{

    AggregatorV3Interface internal priceFeed;

    constructor() {
        priceFeed = AggregatorV3Interface(0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e);
    }

    function getLatestPrice() public view returns (uint) {
        (, int price,,,) = priceFeed.latestRoundData();
        return uint (price*1e18);
    }

    function GetValueInEth(uint dollarsAmount) public view returns (uint) {
        uint valuePrice = uint(getLatestPrice());
        uint ethAmount = (dollarsAmount*1e18)/valuePrice;
        return ethAmount;
    }
}

If you want to reproduce the issue, here it is the repository link: https://github.com/CicaMatt/TravelCoin.git

I really don't know what causes this issues, as i call the other function the same way but i'm not getting any problem.


Solution

  • Looks like the contract address and function parameters are correct. You could try increasing the gas limit to see if that resolves the issue. You could also try checking the error message returned by the Ethereum node to see if it provides any additional information about the problem.

    If you're having trouble with node stuff, I wouldn't recommend trying to tackle it on your own. It's probably better to use a node RPC provider, like GetBlock, because it's easier and their support team can help you sort out any issues.