Skip to content

Commit

Permalink
Add price change and bundle price calls (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrsirdev authored Oct 19, 2023
1 parent 5c0014e commit 01c72ed
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions api/handler/v1/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -116,6 +118,7 @@ func ERC20ModuleEmptyBalance(ctx *fasthttp.RequestCtx) {
ERC20Address: v.Erc20,
PngSrc: v.PngSrc,
Prefix: v.Prefix,
Price24HChange: coin24hChnage,
}
index++
}
Expand Down Expand Up @@ -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,
Expand All @@ -212,6 +216,7 @@ func ERC20ModuleBalance(ctx *fasthttp.RequestCtx) {
ERC20Address: v.Erc20,
PngSrc: v.PngSrc,
Prefix: v.Prefix,
Price24HChange: coin24hChnage,
}
index++
}
Expand Down
9 changes: 9 additions & 0 deletions api/handler/v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
53 changes: 29 additions & 24 deletions cronjobs/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)


Expand Down
3 changes: 3 additions & 0 deletions cronjobs/redis_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 5 additions & 0 deletions internal/v1/db/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 01c72ed

Please sign in to comment.