I have a function which can rate ETH in USD. How can I find out the rate of ETH in USD in automatic mode?

Can this be done in Solidty? Or do I have to use a third-party language, for example, Javascript?

function setExchangeRate(uint value) onlyOwned {
    require(value > 0);

    ExchangeRateChanged(exchangeRate, value);
    exchangeRate = value;
  • Hello Blanry, Could you please edit your post to make it clearer? When you say "This is can solidity...?" do you mean "Can this be done with Solidity? Or do I need a third-party language like Javascript?" I am confused by the fact that you say "I have a function which can rate ETH in USD" but then you ask how you can find out that rate... can you please clarify what you have already done, and what you are actually needing / asking? And what do you mean by "in automatic mode?"
    – Tesa
    Jul 30, 2017 at 15:17

1 Answer 1


How find out the rate of ETH in USD in the contract?

To automatically get this data, rather than manually feeding it in by calling a function in the contract, you would need to use an oracle to get the real-world information into your contract. You could use:

Check out this blog post for a more thorough explanation and example of how to fetch ETH/USD price data using Chainlink Price Feeds

First interpretation: This is can solidty?

Yes, that code is Solidity, which is the language smart contracts are written in. You can't (currently) use native Javascript as a smart contract language. (There's no compiler that creates EVM bytecode from Javascript.)

Second interpretation: This can be done in Solidity?

Yes, see the threads I link to. Note that using an oracle isn't free. You will need to pay for the service.

  • Why not just check value of a pegged token like DAI?
    – User
    Nov 14, 2020 at 19:46
  • You mean the ETH/DAI value? You'd still need to use an oracle to get that value. And what would happen when DAI or other stable token loses its peg? (DAI is often +/- 1-2% off peg.) Nov 15, 2020 at 9:28

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.