Skip to content

Commit

Permalink
✨ IndexCoop Fetcher (#97)
Browse files Browse the repository at this point in the history
* index coop fetcher (code)

* feat: adding tests

* fix:remove unecessary code

* fix: adding usd as base

* DPI/USD

* feat: gecko assets

* test code for dpi

* format and more assets

* feat: fetch_weights method + cleanup

* feat: indexes addresses

* fix: weights fetching

* fix: stagecoach error

* fix: decimals

* fix: weth support

* fix: clean

* feat: dpi + mvi support

* fix: quantitites

* fix: propeller

* feat: mvi support

* ⬆️ 1.3.0

* format

---------

Co-authored-by: 0xevolve <Artevolve@yahoo.com>
  • Loading branch information
JordyRo1 and EvolveArt authored Apr 15, 2024
1 parent e9c0999 commit d5836ba
Show file tree
Hide file tree
Showing 19 changed files with 366 additions and 116 deletions.
15 changes: 15 additions & 0 deletions pragma/core/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class PragmaOnchainAsset(TypedDict):
{"type": "SPOT", "pair": ("ZEND", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("ZEND", "USDC"), "decimals": 8},
{"type": "SPOT", "pair": ("LDO", "USDT"), "decimals": 8},
{"type": "SPOT", "pair": ("LDO", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("MKR", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("AAVE", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("SNX", "USD"), "decimals": 8},
Expand All @@ -78,6 +79,20 @@ class PragmaOnchainAsset(TypedDict):
{"type": "SPOT", "pair": ("YFI", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("BAL", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("ETH", "ZEND"), "decimals": 8},
{"type": "SPOT", "pair": ("MC", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("RNDR", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("FET", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("IMX", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("GALA", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("ILV", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("APE", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("SAND", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("AXS", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("MANA", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("ENS", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("BLUR", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("DPI", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("MVI", "USD"), "decimals": 8},
]


Expand Down
6 changes: 3 additions & 3 deletions pragma/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class PragmaClient(

def __init__(
self,
network: str = "testnet",
network: str = "sepolia",
account_private_key: Optional[int] = None,
account_contract_address: Optional[int] = None,
contract_addresses_config: Optional[ContractAddresses] = None,
Expand All @@ -57,14 +57,14 @@ def __init__(
Client for interacting with Pragma on Starknet.
:param network: Target network for the client.
Can be a URL string, or one of
``"mainnet"``, ``"testnet"``, ``"pragma_testnet"``, ``"sharingan"`` or ``"devnet"``
``"mainnet"``, ``"sepolia"``, ``"pragma_testnet"``, ``"sharingan"`` or ``"devnet"``
:param account_private_key: Optional private key for requests. Not necessary if not making network updates
:param account_contract_address: Optional account contract address. Not necessary if not making network updates
:param contract_addresses_config: Optional Contract Addresses for Pragma.
Will default to the provided network but must be set if using non standard contracts.
:param port: Optional port to interact with local node. Will default to 5050.
:param chain_name: A str-representation of the chain if a URL string is given for `network`.
Must be one of ``"mainnet"``, ``"testnet"``, ``"pragma_testnet"``, ``"sharingan"`` or ``"devnet"``.
Must be one of ``"mainnet"``, ``"sepolia"``, ``"pragma_testnet"``, ``"sharingan"`` or ``"devnet"``.
:param api_url: Optional URL for the Pragma API. Defaults to http://localhost:8080
:param api_key: Optional API key for the Pragma API.
"""
Expand Down
2 changes: 1 addition & 1 deletion pragma/core/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __init__(
self.pair_id = pair_id
self.price = price

if autoscale_volume:
if volume > 0 and autoscale_volume:
asset = get_asset_spec_for_pair_id_by_type(felt_to_str(pair_id), "SPOT")
decimals = asset["decimals"] or 0
volume = volume or 0
Expand Down
46 changes: 31 additions & 15 deletions pragma/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

# Network Types
DEVNET = "devnet"
TESTNET = "testnet"
SEPOLIA = "sepolia"
MAINNET = "mainnet"
SHARINGAN = "sharingan"
Expand All @@ -27,7 +26,6 @@

Network = Literal[
"devnet",
"testnet",
"mainnet",
"sharingan",
"pragma_testnet",
Expand All @@ -38,7 +36,6 @@
CHAIN_IDS = {
DEVNET: 1536727068981429685321,
SHARINGAN: 1536727068981429685321,
TESTNET: 1536727068981429685321,
MAINNET: 23448594291968334,
PRAGMA_TESTNET: 8908953246943201047421899664489,
FORK_DEVNET: 1536727068981429685321,
Expand All @@ -47,6 +44,7 @@

ASSET_MAPPING: Dict[str, str] = {
"ETH": "ethereum",
"WETH": "weth",
"BTC": "bitcoin",
"WBTC": "wrapped-bitcoin",
"SOL": "solana",
Expand Down Expand Up @@ -78,13 +76,41 @@
"RPL": "rocket-pool",
"YFI": "yearn-finance",
"COMP": "compound-governance-token",
"DPI": "defipulse-index",
"MVI": "metaverse-index",
}

DPI_ASSETS = [
{"type": "SPOT", "pair": ("YFI", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("COMP", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("SNX", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("MKR", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("BAL", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("UNI", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("AAVE", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("LDO", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("ETH", "USD"), "decimals": 8},
]

MVI_ASSETS = [
{"type": "SPOT", "pair": ("MC", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("RNDR", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("FET", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("IMX", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("GALA", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("ILV", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("APE", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("SAND", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("AXS", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("MANA", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("ENS", "USD"), "decimals": 8},
{"type": "SPOT", "pair": ("BLUR", "USD"), "decimals": 8},
]

CHAIN_ID_TO_NETWORK = {v: k for k, v in CHAIN_IDS.items()}

STARKSCAN_URLS = {
MAINNET: "https://starkscan.co",
TESTNET: "https://testnet.starkscan.co",
SEPOLIA: "https://sepolia.starkscan.co",
DEVNET: "https://devnet.starkscan.co",
SHARINGAN: "https://sharingan-explorer.madara.zone",
Expand All @@ -98,21 +124,15 @@
MAINNET: [
"https://starknet-mainnet.public.blastapi.io/rpc/v0_6",
],
TESTNET: [
"https://starknet-testnet.public.blastapi.io/rpc/v0_6",
],
SEPOLIA: [
"https://starknet-sepolia.public.blastapi.io/rpc/v0_6",
],
}


def get_rpc_url(network=TESTNET, port=5050):
def get_rpc_url(network=SEPOLIA, port=5050):
if network.startswith("http"):
return network
if network == TESTNET:
random_index = random.randint(0, len(RPC_URLS[TESTNET]) - 1)
return RPC_URLS[TESTNET][random_index]
if network == SEPOLIA:
random_index = random.randint(0, len(RPC_URLS[SEPOLIA]) - 1)
return RPC_URLS[SEPOLIA][random_index]
Expand Down Expand Up @@ -143,10 +163,6 @@ class ContractAddresses:

CONTRACT_ADDRESSES = {
DEVNET: ContractAddresses(0, 0),
TESTNET: ContractAddresses(
2408056700008799988274832007944460979526684291270693941276336026156441738630,
3108238389225984732543655444430831893780207443780498125530192910262931411303,
),
MAINNET: ContractAddresses(
1035964020232444284030697086969999610062982650901949616270651804992179237909,
1202089834814778579992154020333959781277480478747022471664051891421849487195,
Expand Down
4 changes: 0 additions & 4 deletions pragma/publisher/client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import asyncio
import http.client
import json
import os
import time
from typing import Dict, List

import aiohttp
import requests
from dotenv import load_dotenv

from pragma.core.client import PragmaClient
Expand Down
1 change: 1 addition & 0 deletions pragma/publisher/fetchers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .gemini import GeminiFetcher
from .huobi import HuobiFetcher
from .index import IndexFetcher
from .indexcoop import IndexCoopFetcher
from .kaiko import KaikoFetcher
from .kucoin import KucoinFetcher
from .okx import OkxFetcher
Expand Down
2 changes: 2 additions & 0 deletions pragma/publisher/fetchers/binance.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ async def _fetch_pair(
# For now still leaving this line,
if pair[1] == "USD":
pair = (pair[0], "USDT")
if pair[0] == "WETH":
pair = ("ETH", pair[1])
else:
usdt_price = 1
url = self.format_url(pair[0], pair[1])
Expand Down
8 changes: 4 additions & 4 deletions pragma/publisher/fetchers/defillama.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class DefillamaFetcher(PublisherInterfaceT):
BASE_URL: str = (
"https://coins.llama.fi/prices/current/coingecko:{pair_id}" "?searchWidth=5m"
"https://coins.llama.fi/prices/current/coingecko:{pair_id}" "?searchWidth=15m"
)

SOURCE: str = "DEFILLAMA"
Expand Down Expand Up @@ -56,8 +56,7 @@ async def _fetch_pair(
return PublisherFetchError(
f"No data found for {'/'.join(pair)} from Defillama"
)

return self._construct(asset=asset, result=result)
return self._construct(asset, result)

async def fetch(self, session: ClientSession) -> List[SpotEntry]:
entries = []
Expand Down Expand Up @@ -121,10 +120,11 @@ def _construct(self, asset, result, hop_result=None) -> SpotEntry:

logger.info("Fetched price %d for %s from Coingecko", price, pair_id)

return SpotEntry(
entry = SpotEntry(
pair_id=pair_id,
price=price_int,
timestamp=timestamp,
source=self.SOURCE,
publisher=self.publisher,
)
return entry
27 changes: 26 additions & 1 deletion pragma/publisher/fetchers/gecko.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@
"starknet-alpha",
"0x00585c32b625999e6e5e78645ff8df7a9001cf5cf3eb6b80ccdd16cb64bd3a34",
),
"YFI": (
"eth",
"0x0bc529c00C6401aEF6D220BE8C6Ea1667F6Ad93e",
),
"COMP": ("eth", "0xc00e94Cb662C3520282E6f5717214004A7f26888"),
"SNX": ("eth", "0xC011a73ee8576Fb46F5E1c5751cA3B9Fe0af2a6F"),
"MKR": ("eth", "0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2"),
"BAL": ("eth", "0xba100000625a3754423978a60c9317c58a424e3D"),
"UNI": ("eth", "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984"),
"AAVE": ("eth", "0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9"),
"LDO": ("eth", "0x5A98FcBEA516Cf06857215779Fd812CA3beF1B32"),
"RPL": ("eth", "0xD33526068D116cE69F19A9ee46F0bd304F21A51f"),
"WETH": ("eth", "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
"MC": ("eth", "0x949d48eca67b17269629c7194f4b727d4ef9e5d6"),
"RNDR": ("eth", "0x6de037ef9ad2725eb40118bb1702ebb27e4aeb24"),
"FET": ("eth", "0xaea46A60368A7bD060eec7DF8CBa43b7EF41Ad85"),
"IMX": ("eth", "0xf57e7e7c23978c3caec3c3548e3d615c346e79ff"),
"GALA": ("eth", "0xd1d2Eb1B1e90B638588728b4130137D262C87cae"),
"ILV": ("eth", "0x767fe9edc9e0df98e07454847909b5e959d7ca0e"),
"SAND": ("eth", "0x3845badAde8e6dFF049820680d1F14bD3903a5d0"),
"AXS": ("eth", "0xbb0e17ef65f82ab018d8edd776e8dd940327b28b"),
"MANA": ("eth", "0x0f5d2fb29fb7d3cfee444a200298f468908cc942"),
"ENS": ("eth", "0xC18360217D8F7Ab5e7c516566761Ea12Ce7F9D72"),
"BLUR": ("eth", "0x5283d291dbcf85356a21ba090e6db59121208b44"),
"DPI": ("eth", "0x1494ca1f11d487c2bbe4543e90080aeba4ba3c2b"),
"MVI": ("eth", "0x72e364f2abdc788b7e918bc238b21f109cd634d7"),
}


Expand Down Expand Up @@ -64,7 +90,6 @@ async def _fetch_pair(
return PublisherFetchError(
f"Unknown price pair, do not know how to query GeckoTerminal for {pair[0]}"
)

url = self.BASE_URL.format(network=pool[0], token_address=pool[1])
async with session.get(url, headers=self.headers) as resp:
if resp.status == 404:
Expand Down
Loading

0 comments on commit d5836ba

Please sign in to comment.