首页 > 解决方案 > Calculate balances (in database) of different currencies (rates in API) - using javascript

问题描述

I would appreciate some help.

I have a database and API. The database has the balance for each crypto_currency and the API has the current rate for the cryptocurrency in euros.

#The data sources

API Data

data:
0: {id: "bitcoin", symbol: "btc", current_price: 47000,…}
1: {id: "ethereum", symbol: "eth", current_price: 1500,…}
2: {id: "monero", symbol: "xmr", current_price: 200,…}

Database

#Notes:

#What I want to achieve: I want to make the following calculation:

  1. Calculate the balance for all currencies balance(from database)*current_rate

  2. sum (balance*current_rate)

Where do I start? How do I filter the API data? One way would be to query the date from the API with query parameters, but then I would go over the API call limit if there are more users on the site, as there are on average 10 balances, so that would count as 10 API calls.

If you could guide me on where to start, how would you do it, in theory, would be appreciated.

#One solution that worked

I have tried this: I sort the balances desc by api_id in the database and sort the API query based on id aswell....that way the order of the currencies is the same in the API with the database, that way i can match the two things and make the calculation.

However i am thinking there is probably another way, that is more advanced.

标签: javascriptmysqlapi

解决方案


推荐阅读