Skip to content

Commit

Permalink
add blockchain importer
Browse files Browse the repository at this point in the history
  • Loading branch information
tarioch committed Nov 19, 2020
1 parent 3994271 commit dbba233
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,26 @@ Requires the dependencies for camelot to be installed. See https://camelot-py.re

from tariochbctools.importers.cembrastatement import importer as cembrastatementimp
CONFIG = [cembrastatementimp.Importer('\d+.pdf', 'Liabilities:Cembra:Mastercard')]


**blockchain**

Import transactions from Blockchain

Create a file called blockchain.yaml in your import location (e.g. downloads folder).

::
base_ccy: CHF
addresses:
- address: 'SOMEADDRESS'
currency: 'BTC'
narration: 'Some Narration'
asset_account: 'Assets:MyCrypto:BTC'
- address: 'SOMEOTHERADDRESS'
currency: 'LTC'
narration: 'Some Narration'
asset_account: 'Assets:MyCrypto:LTC'
::

from tariochbctools.importers.blockchain import importer as bcimp
CONFIG = [bcimp.Importer()]
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ install_requires =
ibflex
requests
camelot-py[cv]
blockcypher
# The usage of test_requires is discouraged, see `Dependency Management` docs
# tests_require = pytest; pytest-cov
# Require a specific Python version, e.g. Python 2.7 or >= 3.4
Expand Down
71 changes: 71 additions & 0 deletions src/tariochbctools/importers/blockchain/importer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import yaml
from os import path


from beancount.core import prices
from beancount.ingest import importer
from beancount.core import data
from beancount.core import amount
from beancount.core.number import D

import blockcypher


class Importer(importer.ImporterProtocol):
"""An importer for Blockchain data."""

def identify(self, file):
return 'blockchain.yaml' == path.basename(file.name)

def file_account(self, file):
return ''

def extract(self, file, existing_entries):
config = yaml.safe_load(file.contents())
self.config = config
self.priceMap = prices.build_price_map(existing_entries)
baseCcy = config['base_ccy']

entries = []
for address in self.config['addresses']:
currency = address['currency']
addressDetails = blockcypher.get_address_details(address['address'], coin_symbol=currency.lower())
for trx in addressDetails['txrefs']:
metakv = {
'ref': trx['tx_hash'],
}
meta = data.new_metadata(file.name, 0, metakv)

date = trx['confirmed'].date()
price = prices.get_price(self.priceMap, tuple([currency, baseCcy]), date)
cost = data.Cost(
price[1],
baseCcy,
None,
None
)

outputType = 'eth' if currency.lower() == 'eth' else 'btc'
amt = blockcypher.from_base_unit(trx['value'], outputType)

entry = data.Transaction(
meta,
date,
'*',
'',
address['narration'],
data.EMPTY_SET,
data.EMPTY_SET,
[
data.Posting(
address['asset_account'],
amount.Amount(D(str(amt)), currency),
cost,
None,
None,
None),
]
)
entries.append(entry)

return entries

0 comments on commit dbba233

Please sign in to comment.