Here is my getLatestPrice( ) function within my smart contract:

    function getLatestPrice() public view returns (int256) {
        /*uint80 roundID*/
        int256 price,

    ) = priceFeed.latestRoundData(); /*uint80 answeredInRound*/ /*uint timeStamp*/ /*uint startedAt*/
    return price;

This function has been implemented strictly, as the Chainlink documentation suggests.

Now here are my questions:

  • Why do I run into errors whenever I want to add a small logic to this function? For instance, when I try to assign the retrieved response, named price, to one of my local variables, the assignation does not get completed, and my local variable stays zero, although they are the same type ( int256 )

  • On the other hand, after witnessing the above errors, I tried to implement the logic another way. However, it seems I cannot do something like the function below. Can someone clarify the situation for me?

Here is the function:

function calculateDong() public returns (int256) {
    myvariable = int256(getLatestPrice());
    return myvariable;

To make it more clear, here is a screenshot of the two mentioned functions within my script:

enter image description here

And here are the return values we see after calling them in order. Why the second one shows zero?

enter image description here


1 Answer 1


Assuming dong is a storage variable declared as int256 internal dong; you can get its value by creating a getter function like this:

function getDong() external view returns(int256) {
    return dong;

calculateDong() function modifies state, so you can implement it like this

function calculateDong() external {
    dong = getLatestPrice();

And then you can call the previously created getter: getDong()

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.