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
• 27,620 points

retagged Nov 23, 2018 by Kalgi 14 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,040 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
• 27,620 points
21 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 5, 2018 in Blockchain by Perry
• 16,450 points
234 views
0 votes
1 answer

How to connect two different nodes in private ethereum network?

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

answered Jan 9 in Blockchain by Omkar
• 65,840 points
56 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 26, 2018 in Blockchain by Johnathon
• 9,050 points
33 views
0 votes
1 answer
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
• 27,620 points
37 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,040 points
24 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.