-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtax-master.py
62 lines (48 loc) · 1.72 KB
/
tax-master.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sys
from open_tax.collectors.default import AlchemyCollector
from open_tax.atomizers.default import AlchemyAtomizer
from open_tax.contextualizers.default import naive_asset_flow
from open_tax.contextualizers.base import Remainder
from open_tax.realizers.default import BasicRealizer
from dotenv import load_dotenv
import os
import json
from beancount.parser import printer
def main():
load_dotenv()
inp = json.loads(sys.argv[1])
collector = AlchemyCollector(
os.environ.get('ALCHEMY_KEY'),
'https://eth-mainnet.g.alchemy.com/v2/',
inp['wallets']
)
atomizer = AlchemyAtomizer()
raw_alchemy_entries = collector.collect()
print(json.dumps(raw_alchemy_entries, indent=2, default=repr))
alchemy_atoms = atomizer.atomize(
raw_alchemy_entries, {'chain': 'ethereum'})
print(f'total atoms: {len(alchemy_atoms)}')
leftover_atoms, effects = naive_asset_flow(alchemy_atoms, [])
assert not leftover_atoms, 'Leftover Atoms'
print(f'total effects: {len(effects)}')
for e in effects:
print(e)
CURRENCY = inp['currency'].upper()
bob = BasicRealizer('Assets:Cash', 'Assets:Crypto', {
Remainder.AssetChange: 'Income:Asset-PnL',
Remainder.BaseIncome: 'Income:Base',
Remainder.TxFee: 'Expenses:Tx-Fees'
}, CURRENCY)
bean_txs = bob.realize(effects)
fp = f'{inp["id"]}.beancount'
with open(fp, 'w') as f:
f.write('')
with open(fp, 'a') as f:
f.write(f'option "operating_currency" "{CURRENCY}"\n')
printer.print_entries(bean_txs, file=f)
if __name__ == '__main__':
try:
main()
except Exception as err:
with open('error.txt', 'w') as f:
f.write(repr(err) + '\n')