0

I'm trying to get getConversionRate() to return an ETH value for 100 by running this script:

from brownie import Donation, config, network, MockV3Aggregator

from scripts.helpful_scripts import (
    LOCAL_BLOCKCHAIN_ENVIRONMENTS,
    deploy_mocks,
    get_account,
)


def deploy_donation():
    account = get_account()

    if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
        price_feed_address = config["networks"][network.show_active()][
            "eth_usd_price_feed_address"
        ]
    else:
        deploy_mocks()
        price_feed_address = MockV3Aggregator[-1].address
    donation = Donation.deploy(
        price_feed_address,
        {"from": account},
    )
    print(f"Contract deployed to {donation.address}")
    get_price = donation.getPrice()
    print(f"The current price of ETH/USD is {get_price}")
    get_conversion_rate = donation.getConversionRate(100)
    print(get_conversion_rate)
    return donation


def main():
    deploy_donation()

for this smart contract:

// SPDX-LIcense-Identifier: MIT

pragma solidity 0.6.6;

import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
import "@chainlink/contracts/src/v0.6/vendor/SafeMathChainlink.sol";

contract Donation {
    using SafeMathChainlink for uint256;

    uint256 ethAmount;
    address payable owner;
    AggregatorV3Interface public priceFeed;

    constructor(address _priceFeed) public {
        priceFeed = AggregatorV3Interface(_priceFeed);
        owner = msg.sender;
    }

    function donate(uint256 _amount) public payable {
        ethAmount = getConversionRate(_amount);
        owner.transfer(ethAmount);
    }

    function getConversionRate(uint256 rawUSD) public view returns (uint256) {
        uint256 ethPrice = getPrice();
        uint256 ethValue = (rawUSD / ethPrice) * 1000000000000000000;
        return ethValue;
    }

    function getPrice() public view returns (uint256) {
        (, int256 answer, , , ) = priceFeed.latestRoundData();
        return uint256(answer * 10000000000);
    }
}

Please what is the problem with my script, how do i get it to return a converted value?

1 Answer 1

0

In Solidity there are no fractions. So for example: 1/2 will get rounded down to 0.

In your script, you calculate rawUSD / ethPrice. Since rawUSD is only 100, and ethPrice is probably larger, this gets rounded down to 0.

This is why in Solidity you should always do multiplication before division.

So if you change your line to:

uint256 ethValue = rawUSD * 1000000000000000000 / ethPrice;

You should be able to get a result.

*Note: I didn't check the correctness of the decimal calculation which might be a different issue. Also, you can write 1e18 or 1 ether instead of 1000000000000000000 to make it more readable.

1
  • 1
    Thanks so much! i had to changed my getPrice() to get it to work. function getPrice() public view returns (uint256) { (, int256 answer, , , ) = priceFeed.latestRoundData(); return uint256(answer / 100000000); } Commented Aug 28, 2022 at 14:04

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.