Skip to content

Commit

Permalink
add geckoterminal price
Browse files Browse the repository at this point in the history
  • Loading branch information
t0rbik committed Sep 23, 2023
1 parent af2f53f commit 75b7321
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions app/assets/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class Token(Model):
DEXSCREENER_ENDPOINT = "https://api.dexscreener.com/latest/dex/tokens/"
# See: https://defillama.com/docs/api#operations-tag-coins
DEFILLAMA_ENDPOINT = "https://coins.llama.fi/prices/current/"
# See: https://www.geckoterminal.com/dex-api
GECKOTERMINAL_ENDPOINT = "https://api.geckoterminal.com/api/v2/networks/"
# See: https://api.dev.dex.guru/docs#tag/Token-Finance
DEXGURU_ENDPOINT = "https://api.dev.dex.guru/v1/chain/10/tokens/%/market"
# See: https://docs.open.debank.com/en/reference/api-pro-reference/token
Expand Down Expand Up @@ -108,6 +110,30 @@ def defillama_price_in_stables(self):

return 0

def geckoterminal_price_in_stables(self):
"""Returns the price quoted from our llama defis."""
# Peg it forever.
if self.address == STABLE_TOKEN_ADDRESS:
return 1.0

if (
self.nativeChainAddress != ""
and self.nativeChainId != 0
and self.nativeChainAddress is not None
and self.nativeChainId is not None
):
chain_name = self.CHAIN_NAMES[str(self.nativeChainId)]
chain_token = chain_name + ":" + self.nativeChainAddress.lower()
else:
chain_token = "base/tokens/" + self.address.lower()

res = requests.get(self.GECKOTERMINAL_ENDPOINT + chain_token).json()
try:
data = res.get("data", {})
return float(data["attributes"]["price_usd"])
except:
return 0

def one_inch_price_in_stables(self):
"""Returns the price quoted from an aggregator in stables/USDC."""
# Peg it forever.
Expand Down Expand Up @@ -179,8 +205,12 @@ def aggregated_price_in_stables(self):
# price = self.defillama_price_in_stables()
price = 0

if price != 0:
return price
try:
price = self.geckoterminal_price_in_stables()
if price != 0:
return price
except:
pass

try:
return self.dexscreener_price_in_stables()
Expand Down

0 comments on commit 75b7321

Please sign in to comment.