How to use two different APIs to get combined results

0 votes

Hi… I was going throught a code somewhere of jQuery/Ajax and saw there are two APIs. What if I want to get the combined results of these to APIs? How do I do it? The code is follows:

html

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="row" id="stats">

  <div class="col-4 col-12-large">

    <h4><strong>Ether contributed</strong></h4>

    <span id="eth_balance" style="font-size: 2.5em;">&mdash;</span>

    <!--<p id="total-ether-message" style="font-size:11px;"></p>-->

  </div>

  <div class="col-4 col-12-large">

    <h4><strong>Contributions in USD</strong></h4>

    <span id="token_usd" style="font-size: 2.5em;">&mdash;</span>

    <!--<p id="total-usd-message" style="font-size:11px;"></p>-->

  </div>

  <div class="col-4 col-12-large">

    <h4><strong>Tokens issued</strong></h4>

    <span id="token_amount" style="font-size: 2.5em;">&mdash;</span>

    <!-- <p id="total-tokens-message" style="font-size:11px;"></p> -->

  </div>

</div>

js

var token = '0xa74476443119A942dE498590Fe1f2454d7D4aC0d';

var address = '0xda0aed568d9a2dbdcbafc1576fedc633d28eee9a';

$.get("https://api.tokenbalance.com/token/" + token + "/" + address +'', function(data) {

    $("#eth_balance").html(Math.round(data.eth_balance).toFixed(2) + " ETH");

    $("#token_amount").html(Math.round(data.balance).toFixed(2) + " " + data.symbol);

});

$.get("https://api.etherscan.io/api?module=stats&action=ethprice", function(data) {

    $("#token_usd").html("$ " + Math.round(data.result.ethusd).toFixed(2));

    // Ideally I'd like to get [ data.result.ethusd x data.eth_balance ] to replace #token_usd, all wrapped in one function

    alert(data.result.ethusd)

});

Jul 31, 2018 in Blockchain by digger
• 26,740 points

retagged Nov 23, 2018 by Kalgi 363 views

1 answer to this question.

0 votes

You can use nesting of function to do. Use one function inside the other function. Store the values of the first function in a variable which will be available for the inner function to use. Look at the following code:

   var token = '0xa74476443119A942dE498590Fe1f2454d7D4aC0d';

    var address = '0xda0aed568d9a2dbdcbafc1576fedc633d28eee9a';

    $.get("https://api.tokenbalance.com/token/" + token + "/" + address + '', function (data) {

        var eth_balance = data.eth_balance;

        $("#eth_balance").html(Math.round(data.eth_balance).toFixed(2) + " ETH");

        $("#token_amount").html(Math.round(data.balance).toFixed(2) + " " + data.symbol);

        $.get("https://api.etherscan.io/api?module=stats&action=ethprice", function (data) {

            $("#token_usd").html("$ " + Math.round(data.result.ethusd * eth_balance).toFixed(2));

            // Ideally I'd like to get [ data.result.ethusd x data.eth_balance ] to replace #token_usd, all wrapped in one function

            alert(data.result.ethusd)

        });

    });

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="row" id="stats">

    <div class="col-4 col-12-large">

        <h4>

            <strong>Ether contributed</strong>

        </h4>

        <span id="eth_balance" style="font-size: 2.5em;">&mdash;</span>

        <!--<p id="total-ether-message" style="font-size:11px;"></p>-->

    </div>

    <div class="col-4 col-12-large">

        <h4>

            <strong>Contributions in USD</strong>

        </h4>

        <span id="token_usd" style="font-size: 2.5em;">&mdash;</span>

        <!--<p id="total-usd-message" style="font-size:11px;"></p>-->

    </div>

    <div class="col-4 col-12-large">

        <h4>

            <strong>Tokens issued</strong>

        </h4>

        <span id="token_amount" style="font-size: 2.5em;">&mdash;</span>

        <!-- <p id="total-tokens-message" style="font-size:11px;"></p> -->

    </div>

</div>

You should get a clear idea from this.

answered Jul 31, 2018 by slayer
• 29,350 points

Related Questions In Blockchain

0 votes
1 answer

How to get results by running the voting code on Ethereum?

In mist go to your contract and ...READ MORE

answered Sep 18, 2018 in Blockchain by digger
• 26,740 points
447 views
0 votes
1 answer

How to use different network cards for multiple composer rest server users?

You are getting the same network card in ...READ MORE

answered Dec 6, 2018 in Blockchain by Perry
• 17,100 points
1,006 views
–1 vote
1 answer

How to connect two different nodes in private ethereum network?

To connect two nodes, you can use ...READ MORE

answered Jan 10, 2019 in Blockchain by Omkar
• 69,210 points
2,644 views
+1 vote
1 answer

How does a miner get to know that a transaction is verified by all the nodes?

Contrary to the popular belief, it is ...READ MORE

answered Mar 27, 2018 in Blockchain by Johnathon
• 9,090 points
2,510 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,232 views
+1 vote
2 answers
0 votes
1 answer

Invalid Batch or signature in Savtooth

This will solve your problem import org.apache.commons.codec.binary.Hex; Transaction txn ...READ MORE

answered Aug 1, 2018 in Blockchain by digger
• 26,740 points
724 views
0 votes
1 answer

How to write a program to get details of ERC tokens?

You can use Javascript with the Metamask ...READ MORE

answered Jul 13, 2018 in Blockchain by slayer
• 29,350 points
725 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP