From 60cbed5ba148ee6bf01bf2c9e835834fe440dcd5 Mon Sep 17 00:00:00 2001 From: 8baller Date: Sat, 23 Dec 2023 14:28:11 +0000 Subject: [PATCH 1/2] [feat] added to cli group --- lyra/cli.py | 19 +++++++++++++++++++ lyra/lyra.py | 27 +++++++++++---------------- tests/test_main.py | 19 +++++++++++++++++-- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/lyra/cli.py b/lyra/cli.py index fcae92f..ab1dc31 100644 --- a/lyra/cli.py +++ b/lyra/cli.py @@ -65,6 +65,11 @@ def instruments(): """Interact with markets.""" +@cli.group("tickers") +def tickers(): + """Interact with tickers.""" + + @instruments.command("fetch") @click.pass_context @click.option( @@ -80,5 +85,19 @@ def fetch_instruments(ctx, instrument_type): print(markets) +# we pass the instrument_name as a parameter +@tickers.command("fetch") +@click.pass_context +@click.argument( + "instrument_name", + type=str, +) +def fetch_tickers(ctx, instrument_name): + """Fetch tickers.""" + client = ctx.obj["client"] + ticker = client.fetch_ticker(instrument_name=instrument_name) + print(ticker) + + if __name__ == "__main__": cli() # pylint: disable=no-value-for-parameter diff --git a/lyra/lyra.py b/lyra/lyra.py index 7f61d5d..7ff929e 100644 --- a/lyra/lyra.py +++ b/lyra/lyra.py @@ -82,7 +82,7 @@ def create_account(self, wallet): def fetch_instruments( self, - expired=True, + expired=False, instrument_type: InstrumentType = InstrumentType.PERP, currency: UnderlyingCurrency = UnderlyingCurrency.BTC, ): @@ -261,18 +261,13 @@ def _sign_order(self, order): order['signature'] = self.wallet.signHash(typed_data_hash).signature.hex() return order - -def main(): - """Execute the main function.""" - from tests.test_main import TEST_PRIVATE_KEY - - client = LyraClient(TEST_PRIVATE_KEY) - client.create_order( - instrument_name="ETH-PERP", - price=1310, - amount=100, - ) - - -if __name__ == "__main__": - main() + def fetch_ticker(self, instrument_name): + """ + Fetch the ticker for a given instrument name. + """ + url = f"{BASE_URL}/public/get_ticker" + payload = {"instrument_name": instrument_name} + headers = {"accept": "application/json", "content-type": "application/json"} + response = requests.post(url, json=payload, headers=headers) + results = json.loads(response.content)["result"] + return results diff --git a/tests/test_main.py b/tests/test_main.py index 8f5f4ba..93ecfca 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -58,16 +58,31 @@ def test_create_order(lyra_client): """ Test the LyraClient class. """ - assert "error" not in lyra_client.create_order( + result = lyra_client.create_order( price=200, amount=1, instrument_name="ETH-PERP", side=OrderSide.BUY, order_type=OrderType.LIMIT, ) + assert "error" not in result -def test_define_order(lyra_client): +def test_fetch_ticker(lyra_client): """ Test the LyraClient class. """ + instruments = lyra_client.fetch_instruments(instrument_type=InstrumentType.PERP) + instrument_name = instruments[0]['instrument_name'] + ticker = lyra_client.fetch_ticker(instrument_name=instrument_name) + assert ticker['instrument_name'] == instrument_name + + +def test_fetch_option_tickers(lyra_client): + """ + Test the LyraClient class. + """ + instruments = lyra_client.fetch_instruments(instrument_type=InstrumentType.OPTION, expired=False) + instrument_name = instruments[0]['instrument_name'] + ticker = lyra_client.fetch_ticker(instrument_name=instrument_name) + assert ticker['instrument_name'] == instrument_name From b914d8948060dc7b61672bce64e3825079017f9c Mon Sep 17 00:00:00 2001 From: 8baller Date: Mon, 25 Dec 2023 17:07:26 +0000 Subject: [PATCH 2/2] [feat] added fetch subaccounts to the cli and appropriate endpoint --- lyra/cli.py | 30 +++++++++++++++++++++++++++++- lyra/lyra.py | 15 +++++++++++++-- tests/test_main.py | 9 +++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/lyra/cli.py b/lyra/cli.py index ab1dc31..026afe1 100644 --- a/lyra/cli.py +++ b/lyra/cli.py @@ -70,6 +70,11 @@ def tickers(): """Interact with tickers.""" +@cli.group("subaccounts") +def subaccounts(): + """Interact with subaccounts.""" + + @instruments.command("fetch") @click.pass_context @click.option( @@ -85,7 +90,6 @@ def fetch_instruments(ctx, instrument_type): print(markets) -# we pass the instrument_name as a parameter @tickers.command("fetch") @click.pass_context @click.argument( @@ -99,5 +103,29 @@ def fetch_tickers(ctx, instrument_name): print(ticker) +@subaccounts.command("fetch") +@click.pass_context +def fetch_subaccounts(ctx): + """Fetch subaccounts.""" + print("Fetching subaccounts") + client = ctx.obj["client"] + subaccounts = client.fetch_subaccounts() + print(subaccounts) + + +@subaccounts.command("info") +@click.pass_context +@click.argument( + "subaccount_id", + type=int, +) +def fetch_subaccount(ctx, subaccount_id): + """Fetch subaccount.""" + print("Fetching subaccount") + client = ctx.obj["client"] + subaccount = client.fetch_subaccount(subaccount_id=subaccount_id) + print(subaccount) + + if __name__ == "__main__": cli() # pylint: disable=no-value-for-parameter diff --git a/lyra/lyra.py b/lyra/lyra.py index 7ff929e..811d7c6 100644 --- a/lyra/lyra.py +++ b/lyra/lyra.py @@ -102,18 +102,29 @@ def fetch_instruments( results = response.json()["result"] return results - def fetch_subaccounts(self, wallet): + def fetch_subaccounts(self, wallet=None): """ Returns the subaccounts for a given wallet """ endpoint = "get_subaccounts" url = f"{BASE_URL}/private/{endpoint}" - payload = {"wallet": wallet} + payload = {"wallet": wallet if wallet else self.wallet.address} headers = self._create_signature_headers() response = requests.post(url, json=payload, headers=headers) results = json.loads(response.content)["result"] return results + def fetch_subaccount(self, subaccount_id): + """ + Returns information for a given subaccount + """ + url = f"{BASE_URL}/private/get_subaccount" + payload = {"subaccount_id": subaccount_id} + headers = self._create_signature_headers() + response = requests.post(url, json=payload, headers=headers) + results = response.json()["result"] + return results + def create_order( self, price, diff --git a/tests/test_main.py b/tests/test_main.py index 93ecfca..db0a40e 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -86,3 +86,12 @@ def test_fetch_option_tickers(lyra_client): instrument_name = instruments[0]['instrument_name'] ticker = lyra_client.fetch_ticker(instrument_name=instrument_name) assert ticker['instrument_name'] == instrument_name + + +def test_fetch_subaccount(lyra_client): + """ + Test the LyraClient class. + """ + subaccount_id = lyra_client.fetch_subaccounts()['subaccount_ids'][0] + subaccount = lyra_client.fetch_subaccount(subaccount_id) + assert subaccount['subaccount_id'] == subaccount_id