Skip to content

Commit

Permalink
Merge pull request #10 from 8ball030/feat/fetch_tickers
Browse files Browse the repository at this point in the history
Feat/fetch subaccount
  • Loading branch information
8ball030 authored Dec 25, 2023
2 parents a82a1e8 + b914d89 commit 8ecc7ca
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 20 deletions.
47 changes: 47 additions & 0 deletions lyra/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ def instruments():
"""Interact with markets."""


@cli.group("tickers")
def tickers():
"""Interact with tickers."""


@cli.group("subaccounts")
def subaccounts():
"""Interact with subaccounts."""


@instruments.command("fetch")
@click.pass_context
@click.option(
Expand All @@ -80,5 +90,42 @@ def fetch_instruments(ctx, instrument_type):
print(markets)


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


@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
42 changes: 24 additions & 18 deletions lyra/lyra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
):
Expand All @@ -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,
Expand Down Expand Up @@ -261,18 +272,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
28 changes: 26 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,40 @@ 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


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

0 comments on commit 8ecc7ca

Please sign in to comment.