From d64d04a255e5e6419008960710e3937e20c8afc3 Mon Sep 17 00:00:00 2001 From: hajdbo Date: Mon, 11 May 2020 21:41:18 +0800 Subject: [PATCH] adds get_ledgers --- CHANGELOG | 3 +++ bfxapi/models/__init__.py | 1 + bfxapi/models/ledger.py | 56 +++++++++++++++++++++++++++++++++++++++ bfxapi/rest/bfx_rest.py | 17 +++++++++++- bfxapi/version.py | 2 +- 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 bfxapi/models/ledger.py diff --git a/CHANGELOG b/CHANGELOG index 49db0bf..3b772ae 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +1.1.7 +- Adds rest.get_ledgers + 1.1.6 - Adds 'new_ticker' websocket event stream - Adds 'ws.stop' function to kill all websocket connections diff --git a/bfxapi/models/__init__.py b/bfxapi/models/__init__.py index a575991..87b07ea 100644 --- a/bfxapi/models/__init__.py +++ b/bfxapi/models/__init__.py @@ -18,5 +18,6 @@ from .deposit_address import DepositAddress from .withdraw import Withdraw from .ticker import Ticker from .funding_ticker import FundingTicker +from .ledger import Ledger NAME = 'models' diff --git a/bfxapi/models/ledger.py b/bfxapi/models/ledger.py new file mode 100644 index 0000000..d809775 --- /dev/null +++ b/bfxapi/models/ledger.py @@ -0,0 +1,56 @@ +""" +Module used to describe a ledger object +""" + +class LedgerModel: + """ + Enum used to index the location of each value in a raw array + """ + ID = 0 + CURRENCY=1 + MTS = 3 + AMOUNT = 5 + BALANCE = 6 + DESCRIPTION = 8 + +class Ledger: + """ + ID int + CURRENCY string Currency (BTC, etc) + PLACEHOLDER + MTS int Millisecond Time Stamp of the update + PLACEHOLDER + AMOUNT string Amount of funds to ledger + BALANCE string Amount of funds to ledger + PLACEHOLDER + DESCRIPTION + """ + + # [2794967447, 'USD', None, 1588004822000, None, -8.6166026, 4299.6846957, None, 'Trading fees for 4303.997301 UST (USTUSD) @ 1.001 on BFX (0.2%) on wallet exchange'], + + def __init__(self, currency, mts, amount, balance, description): + self.currency = currency + self.mts = mts + self.amount = amount + self.balance = balance + self.description = description + + @staticmethod + def from_raw_ledger(raw_ledger): + """ + Parse a raw ledger object into a Ledger object + + @return Ledger + """ + currency = raw_ledger[LedgerModel.CURRENCY] + mts = raw_ledger[LedgerModel.MTS] + amount = raw_ledger[LedgerModel.AMOUNT] + balance = raw_ledger[LedgerModel.BALANCE] + description = raw_ledger[LedgerModel.DESCRIPTION] + return Ledger(currency, mts, amount, balance, description) + + def __str__(self): + ''' Allow us to print the Ledger object in a pretty format ''' + text = "Ledger <{} {} balance:{} '{}' mts={}>" + return text.format(self.amount, self.currency, self.balance, + self.description, self.mts) diff --git a/bfxapi/rest/bfx_rest.py b/bfxapi/rest/bfx_rest.py index d6c4413..79f4f48 100644 --- a/bfxapi/rest/bfx_rest.py +++ b/bfxapi/rest/bfx_rest.py @@ -10,7 +10,7 @@ import json from ..utils.custom_logger import CustomLogger from ..utils.auth import generate_auth_headers, calculate_order_flags, gen_unique_cid from ..models import Wallet, Order, Position, Trade, FundingLoan, FundingOffer -from ..models import FundingCredit, Notification +from ..models import FundingCredit, Notification, Ledger class BfxRest: @@ -365,6 +365,21 @@ class BfxRest: credits = await self.post(endpoint, params=params) return [FundingCredit.from_raw_credit(c) for c in credits] + async def get_ledgers(self, symbol, start, end, limit=25, category=None): + """ + Get all ledgers on account associated with API_KEY - Requires authentication. + See category filters here: https://docs.bitfinex.com/reference#rest-auth-ledgers + @return Array + """ + endpoint = "auth/r/ledgers/{}/hist".format(symbol) + params = "?start={}&end={}&limit={}".format(start, end, limit) + if (category): + payload = { "category": category , } + raw_ledgers = await self.post(endpoint, payload, params=params) + else: + raw_ledgers = await self.post(endpoint, params=params) + return [Ledger.from_raw_ledger(rl) for rl in raw_ledgers] + async def submit_funding_offer(self, symbol, amount, rate, period, funding_type=FundingOffer.Type.LIMIT, hidden=False): """ diff --git a/bfxapi/version.py b/bfxapi/version.py index 60c98d6..6e0afc3 100644 --- a/bfxapi/version.py +++ b/bfxapi/version.py @@ -2,4 +2,4 @@ This module contains the current version of the bfxapi lib """ -__version__ = '1.1.6' +__version__ = '1.1.7'