Skip to content

Commit

Permalink
Merge pull request #7 from sndmndss/dev
Browse files Browse the repository at this point in the history
Add from and to timestamp params to get_deposits method
  • Loading branch information
sndmndss authored May 15, 2024
2 parents f31dce9 + fc8eb58 commit b86ceac
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 17 deletions.
4 changes: 2 additions & 2 deletions bpx/__async/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ async def get_balances(self, window: int = None):
url, headers = super().get_balances(window)
return await self.http_client.get(url, headers=headers)

async def deposits(self, limit: int = 100, offset: int = 0, window: int = None):
url, headers, params = super().deposits(limit, offset, window)
async def get_deposits(self, limit: int = 100, offset: int = 0, window: int = None):
url, headers, params = super().get_deposits(limit, offset, window)
return await self.http_client.get(url, headers=headers, params=params)

async def get_deposit_address(self, blockchain: str, window: int = None):
Expand Down
6 changes: 4 additions & 2 deletions bpx/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ def get_balances(self, window: int = None):
url, headers = super().get_balances(window)
return self.http_client.get(url, headers=headers)

def deposits(self, limit: int = 100, offset: int = 0, window: int = None):
url, headers, params = super().deposits(limit, offset, window)
def get_deposits(self, limit: int = 100, offset: int = 0,
__from: int = None, to: int = None, window: int = None):

url, headers, params = super().get_deposits(limit, offset, window, __from, to)
return self.http_client.get(url, headers=headers, params=params)

def get_deposit_address(self, blockchain: str, window: int = None):
Expand Down
6 changes: 5 additions & 1 deletion bpx/base/base_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_balances(self, window: int):
url = self.BPX_API_URL + 'api/v1/capital'
return url, headers

def deposits(self, limit: int, offset: int, window: int):
def get_deposits(self, limit: int, offset: int, window: int, __from=None, to=None):
if limit > 1000 or limit < 0:
raise LimitValueError
if offset < 0:
Expand All @@ -35,6 +35,10 @@ def deposits(self, limit: int, offset: int, window: int):
'limit': limit,
'offset': offset,
}
if __from:
params['from'] = __from
if to:
params['to'] = to
headers = self._headers(params, 'depositQueryAll', window=window)
url = self.BPX_API_URL + 'wapi/v1/capital/deposits'
return url, headers, params
Expand Down
2 changes: 1 addition & 1 deletion bpx/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_ticker(self, symbol: str):
def get_depth(self, symbol: str):
return self.http_client.get(self.get_depth_url(symbol))

def get_klines(self, symbol: str, interval: str, start_time=0, end_time=0):
def get_klines(self, symbol: str, interval: str, start_time: int, end_time: int = 0):
return self.http_client.get(self.get_klines_url(symbol, interval, start_time, end_time))

def get_status(self):
Expand Down
13 changes: 13 additions & 0 deletions bpx/simulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@


def get_volume(filled_orders: list[dict]) -> float | int:
"""
Return the volume of the filled orders.
filled_orders is bpx.account.Account.get_fill_history_query() response
"""
prices = [float(item['price']) * float(item["quantity"]) for item in filled_orders]
return sum(prices)


def get_fees(filled_orders: list[dict]) -> float | int:
"""
Return the fees of the filled orders.
filled_orders is bpx.account.Account.get_fill_history_query() response
"""
fees = [float(item['price']) * float(item["fee"])
if item["feeSymbol"] != 'USDC' else float(item["fee"]) for item in filled_orders]
return sum(fees)
Expand All @@ -17,6 +25,11 @@ def get_fees(filled_orders: list[dict]) -> float | int:
def get_approximate_balance_in_usdc(balance: dict,
time_interval: str = "1m",
public: Public = default_public) -> float:
"""
Return the approximate account balance in USDC
balance is bpx.account.Account.get_balances() response
time_interval is approximate price in USDC in interval(see enums)
"""
usdc_symbol = "USDC"
balance_usdc = 0
close_price = 0
Expand Down
2 changes: 1 addition & 1 deletion examples/account_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def account_example():
print(account.get_fill_history_query("SOL_USDC", limit=999))
print(account.get_withdrawals())
print(account.execute_order("SOL_USDC", "Bid", "Limit", 0.01, time_in_force="IOC", price=1, window=10000))
print(account.deposits(limit=100, offset=0, window=5000))
print(account.get_deposits(limit=100, offset=0, window=5000))
print(account.get_balances())
print(account.get_deposit_address("Solana"))
print(account.get_order_history_query("SOL_USDC"))
Expand Down
2 changes: 1 addition & 1 deletion examples/async/account_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async def account_example():
fill_history = await account.get_fill_history_query("SOL_USDC", limit=999)
withdrawals = await account.get_withdrawals()
executed_order = await account.execute_order("SOL_USDC", "Bid", "Limit", 0.01, time_in_force="IOC", price=1, window=10000)
deposits = await account.deposits(limit=1, offset=0, window=5000)
deposits = await account.get_deposits(limit=1, offset=0, window=5000)
balances = await account.get_balances()
deposit_address = await account.get_deposit_address("Solana")
order_history = await account.get_order_history_query("SOL_USDC")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "bpx-py"
version = "1.1.2"
version = "1.1.3"
description = "Backpack API SDK tool"
authors = ["sndmndss <[email protected]>"]
readme = "README.md"
Expand Down
8 changes: 4 additions & 4 deletions tests/test_base_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ def test_get_balances(account):
assert "X-API-Key" in headers


def test_deposits(account):
def test_get_deposits(account):
with pytest.raises(LimitValueError):
account.deposits(limit=1001, offset=0, window=10000)
account.get_deposits(limit=1001, offset=0, window=10000)

with pytest.raises(NegativeValueError):
account.deposits(limit=100, offset=-1, window=10000)
account.get_deposits(limit=100, offset=-1, window=10000)

url, headers, params = account.deposits(limit=100, offset=0, window=10000)
url, headers, params = account.get_deposits(limit=100, offset=0, window=10000)
assert url == "https://api.backpack.exchange/wapi/v1/capital/deposits"
assert params['limit'] == 100
assert params['offset'] == 0
Expand Down
2 changes: 1 addition & 1 deletion tests/tests_api/test__async_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def test_execute_order(account_client: Account):

@pytest.mark.asyncio
async def test_deposits(account_client: Account):
deposits = await account_client.deposits(limit=3)
deposits = await account_client.get_deposits(limit=3)
assert isinstance(deposits, list)
assert len(deposits) == 3

Expand Down
4 changes: 3 additions & 1 deletion tests/tests_api/test__async_public.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ async def test_get_depth_returns_dict(public_client: Public):

@pytest.mark.asyncio
async def test_get_klines_returns_list(public_client: Public):
klines = await public_client.get_klines("BTC_USDC", "1d")
start_time = 1715692417
end_time = 1715778817
klines = await public_client.get_klines("BTC_USDC", "1d", start_time, end_time)
assert isinstance(klines, list)

@pytest.mark.asyncio
Expand Down
2 changes: 1 addition & 1 deletion tests/tests_api/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_execute_order(account_client: Account):


def test_deposits(account_client: Account):
deposits = account_client.deposits(limit=3)
deposits = account_client.get_deposits(limit=3)
assert isinstance(deposits, list)
assert len(deposits) == 3

Expand Down
4 changes: 3 additions & 1 deletion tests/tests_api/test_public.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def test_get_depth_returns_dict(public_client: Public):


def test_get_klines_returns_list(public_client: Public):
assert isinstance(public_client.get_klines("BTC_USDC", "1d"), list)
start_time = 1715692417
end_time = 1715778817
assert isinstance(public_client.get_klines("BTC_USDC", "1d", start_time, end_time), list)


def test_get_status_returns_dict(public_client: Public):
Expand Down

0 comments on commit b86ceac

Please sign in to comment.