From 01c72ed09fd35f9836e0b0bc23ee5c4a51bbd51a Mon Sep 17 00:00:00 2001 From: Mrsirdev <111534516+Mrsirdev@users.noreply.github.com> Date: Thu, 19 Oct 2023 14:16:50 +0100 Subject: [PATCH] Add price change and bundle price calls (#74) --- CHANGELOG.md | 3 +++ api/handler/v1/erc20.go | 11 +++++--- api/handler/v1/helpers.go | 9 +++++++ cronjobs/price.py | 53 ++++++++++++++++++++----------------- cronjobs/redis_functions.py | 3 +++ internal/v1/db/price.go | 5 ++++ 6 files changed, 57 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94922be..11912f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,9 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## Unreleased +- (chore) [fse-710] Bundle all price cron API calls into a single API call for all tokens +- (chore) [fse-710] Fetch evmos 24h price change and return it on the ERC20ModuleBalance endpoint + ### Improvements - (chore) [fse-536] Adding dependabot diff --git a/api/handler/v1/erc20.go b/api/handler/v1/erc20.go index db6c890..1431293 100644 --- a/api/handler/v1/erc20.go +++ b/api/handler/v1/erc20.go @@ -52,9 +52,10 @@ type ERC20Entry struct { URL string `json:"url"` HandlingAction string `json:"handlingAction"` } `json:"handledByExternalUI"` - ERC20Address string `json:"erc20Address"` - PngSrc string `json:"pngSrc"` - Prefix string `json:"prefix"` + ERC20Address string `json:"erc20Address"` + PngSrc string `json:"pngSrc"` + Prefix string `json:"prefix"` + Price24HChange string `json:"price24HChange"` } type ModuleBalanceContainer struct { @@ -100,6 +101,7 @@ func ERC20ModuleEmptyBalance(ctx *fasthttp.RequestCtx) { networkConfig := networkConfigs[configIdx] mainnetConfig := resources.GetMainnetConfig(networkConfig) coingeckoPrice := GetCoingeckoPrice(v.CoingeckoID) + coin24hChnage := GetCoingecko24HChange(v.CoingeckoID) container.values[k] = ERC20Entry{ Name: v.Name, Symbol: v.Symbol, @@ -116,6 +118,7 @@ func ERC20ModuleEmptyBalance(ctx *fasthttp.RequestCtx) { ERC20Address: v.Erc20, PngSrc: v.PngSrc, Prefix: v.Prefix, + Price24HChange: coin24hChnage, } index++ } @@ -195,6 +198,7 @@ func ERC20ModuleBalance(ctx *fasthttp.RequestCtx) { networkConfig := networkConfigs[configIdx] mainnetConfig := resources.GetMainnetConfig(networkConfig) coingeckoPrice := GetCoingeckoPrice(v.CoingeckoID) + coin24hChnage := GetCoingecko24HChange(v.CoingeckoID) container.values[k] = ERC20Entry{ Name: v.Name, @@ -212,6 +216,7 @@ func ERC20ModuleBalance(ctx *fasthttp.RequestCtx) { ERC20Address: v.Erc20, PngSrc: v.PngSrc, Prefix: v.Prefix, + Price24HChange: coin24hChnage, } index++ } diff --git a/api/handler/v1/helpers.go b/api/handler/v1/helpers.go index 6492c85..66bbdd1 100644 --- a/api/handler/v1/helpers.go +++ b/api/handler/v1/helpers.go @@ -74,6 +74,15 @@ func GetCoingeckoPrice(coingeckoID string) string { return price } +func GetCoingecko24HChange(coingeckoID string) string { + change := "0" + val, err := db.RedisGet24HChange(coingeckoID) + if err == nil { + change = val + } + return change +} + func paramToString(param string, ctx *fasthttp.RequestCtx) string { return fmt.Sprint(ctx.UserValue(param)) } diff --git a/cronjobs/price.py b/cronjobs/price.py index 216b6e4..427bcd2 100644 --- a/cronjobs/price.py +++ b/cronjobs/price.py @@ -8,39 +8,42 @@ from github import get_tokens from helpers import get_erc20_coins -from redis_functions import redisSetPrice +from redis_functions import redisSetPrice, redisSetEvmosChange -def get_dex_screener_price(): - # Used only for evmos at the moment - url = "https://api.dexscreener.com/latest/dex/pairs/evmos/0xaea12f0b3b609811a0dc28dc02716c60d534f972" - resp = requests.get(url) - return float(resp.json().get("pair", {}).get("priceUsd", "0.0")) - -def get_price(asset: str, vs_currency: str): +def get_evmos_change(): try: - if asset == "evmos": - price = get_dex_screener_price() - return price - else: - url = 'https://api.coingecko.com/api/v3/simple/price?' - resp = requests.get(f'{url}ids={asset}&vs_currencies={vs_currency}') - print(resp) - return float(resp.json()[asset][vs_currency]) + url = 'https://api.coingecko.com/api/v3/coins/evmos' + resp = requests.get(f'{url}') + json_resp = resp.json() + redisSetEvmosChange(json_resp["market_data"]["price_change_percentage_24h"]) + return except Exception: return None +def get_prices(vs_currency: str, erc20_module_coins): + asset_ids = [] -def process_assets(erc20_module_coins): for coin in erc20_module_coins: - print(f'Getting price for {coin["tokenName"]}') - price = get_price(coin['coingeckoId'], 'usd') - if price is not None: - redisSetPrice(coin['coingeckoId'], 'usd', price) - print(f'Price {price} for {coin["tokenName"]}') - time.sleep(10) + if (coin["coingeckoId"] and coin["coingeckoId"] != ""): + asset_ids.append(coin["coingeckoId"]) + + delim = "," + try: + url = 'https://api.coingecko.com/api/v3/simple/price?' + resp = requests.get(f'{url}ids={delim.join(asset_ids)}&vs_currencies={vs_currency}') + return resp.json() + except Exception: + return None +def process_assets(prices): + for asset in prices: + price = prices[asset].get('usd', None) + if price is not None: + redisSetPrice(asset, 'usd', price) + print(f'Price {price} for {asset}') + running = True @@ -50,7 +53,9 @@ def main(): tracked_tokens = get_tokens() erc20_module_coins = get_erc20_coins(tracked_tokens) print('Getting prices...') - process_assets(erc20_module_coins) + prices = get_prices("usd", erc20_module_coins) + get_evmos_change() + process_assets(prices) time.sleep(300) diff --git a/cronjobs/redis_functions.py b/cronjobs/redis_functions.py index 7473659..80290e0 100644 --- a/cronjobs/redis_functions.py +++ b/cronjobs/redis_functions.py @@ -32,6 +32,9 @@ def redisSetPrice(asset: str, vs_currency: str, price: float): key = f'{asset}|{vs_currency}|price' r.mset({key: price}) +def redisSetEvmosChange(change: float): + key = f'evmos|24h|change' + r.mset({key: change}) def redisGetPrice(asset: str, vs_currency: str) -> float | None: key = f'{asset}|{vs_currency}|price' diff --git a/internal/v1/db/price.go b/internal/v1/db/price.go index 32e72f8..7105d9b 100644 --- a/internal/v1/db/price.go +++ b/internal/v1/db/price.go @@ -22,6 +22,11 @@ func RedisGetPrice(asset string, vsCurrency string) (string, error) { return formatRedisResponse(val, err) } +func RedisGet24HChange(asset string) (string, error) { + val, err := rdb.Get(ctxRedis, asset+"|24h|change").Result() + return formatRedisResponse(val, err) +} + func RedisSetPrice(asset string, vsCurrency string, price string) { key := buildKeyPrice(asset, vsCurrency) err := rdb.Set(ctxRedis, key, price, 0).Err()