Is it possible to get the current price of ETH in USD in a buyTokens function of a deployed solidity smart contract?

For instance, every time GETH starts up is determines the price of ETH during initialization.

Or, is the real way to do it with pre-calculation on a site using web3 to interact with the smart contract once it determines the USD value?

Thank you in advance!


You can use any external APIs for this.

Inorder to call external APIs in contract, you have to use oraclize service.

Sample code for your reference:

    pragma solidity ^0.4.16;
    import "./usingOraclize.sol";  //Importing Oraclize
    contract TestOraclizeCall is usingOraclize {
            uint public price;
           event Log(string text);
           function TestOraclizeCall() {
                 OAR = OraclizeAddrResolverI(0x5049063e4a7704ac155e4f1f42a4954bbef5bbde);
      function __callback(bytes32 _myid, string _result) {
                 require (msg.sender == oraclize_cbAddress());
                  price = parseInt(_result, 2);
        function update() payable {


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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