|
| 1 | +"""Imports transactions from Kraken crypto exchange ledger csv file. |
| 2 | +
|
| 3 | +The CSV header is the following: |
| 4 | +"txid","refid","time","type","subtype","aclass","asset","amount","fee","balance" |
| 5 | +""" |
| 6 | + |
| 7 | + |
| 8 | +import csv |
| 9 | + |
| 10 | +import beangulp |
| 11 | +import dateutil.parser |
| 12 | +from beancount.core import data, flags |
| 13 | +from beancount.core.amount import Amount |
| 14 | +from beancount.core.number import D |
| 15 | + |
| 16 | +from uabean.importers.mixins import IdentifyMixin |
| 17 | + |
| 18 | + |
| 19 | +class Importer(IdentifyMixin, beangulp.Importer): |
| 20 | + FLAG = flags.FLAG_OKAY |
| 21 | + matchers = [ |
| 22 | + ("content", __doc__.split("\n")[-2]), |
| 23 | + ("mime", "text/csv"), |
| 24 | + ] |
| 25 | + |
| 26 | + def __init__( |
| 27 | + self, |
| 28 | + spot_account="Assets:Kraken:Spot", |
| 29 | + staking_account="Assets:Kraken:Staking", |
| 30 | + fee_account="Expenses:Fees:Kraken", |
| 31 | + staking_income_account="Income:Staking:Kraken", |
| 32 | + ): |
| 33 | + self.spot_account = spot_account |
| 34 | + self.staking_account = staking_account |
| 35 | + self.fee_account = fee_account |
| 36 | + self.staking_income_account = staking_income_account |
| 37 | + super().__init__() |
| 38 | + |
| 39 | + def account(self, _): |
| 40 | + return "kraken" |
| 41 | + |
| 42 | + def parse_date(self, s): |
| 43 | + return dateutil.parser.parse(s) |
| 44 | + |
| 45 | + def extract(self, filename, existing_entries=None): |
| 46 | + entries = [] |
| 47 | + balances = {} |
| 48 | + for index, row in enumerate(csv.DictReader(open(filename)), 1): |
| 49 | + meta = data.new_metadata(filename, index) |
| 50 | + entry = self.get_entry_from_row(row, meta, balances) |
| 51 | + if entry is not None: |
| 52 | + entries.append(entry) |
| 53 | + for (account, currency), (date, balance) in balances.items(): |
| 54 | + entries.append( |
| 55 | + data.Balance( |
| 56 | + data.new_metadata(filename, -1), |
| 57 | + date.date() + dateutil.relativedelta.relativedelta(days=1), |
| 58 | + account, |
| 59 | + Amount(balance, currency), |
| 60 | + None, |
| 61 | + None, |
| 62 | + ) |
| 63 | + ) |
| 64 | + return entries |
| 65 | + |
| 66 | + def get_entry_from_row(self, row, meta, balances): |
| 67 | + if not row["txid"]: |
| 68 | + return None |
| 69 | + date = self.parse_date(row["time"]) |
| 70 | + meta["time"] = date.strftime("%H:%M:%S") |
| 71 | + postings = [] |
| 72 | + match (row["type"], row["subtype"]): |
| 73 | + case ("deposit", "") | ("transfer", "spottostaking"): |
| 74 | + postings.append( |
| 75 | + data.Posting( |
| 76 | + self.spot_account, |
| 77 | + self.ammount_from_row(row), |
| 78 | + None, |
| 79 | + None, |
| 80 | + None, |
| 81 | + None, |
| 82 | + ) |
| 83 | + ) |
| 84 | + self.update_balance( |
| 85 | + balances, |
| 86 | + date, |
| 87 | + self.spot_account, |
| 88 | + row["asset"], |
| 89 | + D(row["balance"]), |
| 90 | + ) |
| 91 | + case ("transfer", "stakingfromspot"): |
| 92 | + postings.append( |
| 93 | + data.Posting( |
| 94 | + self.staking_account, |
| 95 | + self.ammount_from_row(row), |
| 96 | + None, |
| 97 | + None, |
| 98 | + None, |
| 99 | + None, |
| 100 | + ) |
| 101 | + ) |
| 102 | + self.update_balance( |
| 103 | + balances, |
| 104 | + date, |
| 105 | + self.staking_account, |
| 106 | + row["asset"], |
| 107 | + D(row["balance"]), |
| 108 | + ) |
| 109 | + case ("staking", ""): |
| 110 | + postings.append( |
| 111 | + data.Posting( |
| 112 | + self.staking_account, |
| 113 | + self.ammount_from_row(row), |
| 114 | + None, |
| 115 | + None, |
| 116 | + None, |
| 117 | + None, |
| 118 | + ) |
| 119 | + ) |
| 120 | + postings.append( |
| 121 | + data.Posting( |
| 122 | + self.staking_income_account, |
| 123 | + None, |
| 124 | + None, |
| 125 | + None, |
| 126 | + None, |
| 127 | + None, |
| 128 | + ) |
| 129 | + ) |
| 130 | + self.update_balance( |
| 131 | + balances, |
| 132 | + date, |
| 133 | + self.staking_account, |
| 134 | + row["asset"], |
| 135 | + D(row["balance"]), |
| 136 | + ) |
| 137 | + case _: |
| 138 | + raise NotImplementedError( |
| 139 | + f"Unknown transaction type {row['type']} row['subtype']" |
| 140 | + ) |
| 141 | + if float(row["fee"]): |
| 142 | + postings.append( |
| 143 | + data.Posting( |
| 144 | + self.fee_account, |
| 145 | + self.ammount_from_row(row), |
| 146 | + None, |
| 147 | + None, |
| 148 | + None, |
| 149 | + None, |
| 150 | + ) |
| 151 | + ) |
| 152 | + return data.Transaction( |
| 153 | + meta, |
| 154 | + date.date(), |
| 155 | + self.FLAG, |
| 156 | + "", |
| 157 | + "", |
| 158 | + data.EMPTY_SET, |
| 159 | + data.EMPTY_SET, |
| 160 | + postings, |
| 161 | + ) |
| 162 | + |
| 163 | + def ammount_from_row(self, row): |
| 164 | + return Amount(D(row["amount"]), row["asset"]) |
| 165 | + |
| 166 | + def update_balance(self, balances, date, account, currency, balance): |
| 167 | + if (account, currency) not in balances: |
| 168 | + balances[(account, currency)] = (date, balance) |
| 169 | + else: |
| 170 | + balances[(account, currency)] = max( |
| 171 | + balances[(account, currency)], (date, balance) |
| 172 | + ) |
| 173 | + |
| 174 | + |
| 175 | +def get_test_importer(): |
| 176 | + return Importer() |
| 177 | + |
| 178 | + |
| 179 | +if __name__ == "__main__": |
| 180 | + from beangulp.testing import main |
| 181 | + |
| 182 | + main(get_test_importer()) |
0 commit comments