Browse Source

compute capital gains using wallet.txi and txo

3.1
ThomasV 7 years ago
parent
commit
51f04d4e7b
  1. 61
      lib/wallet.py

61
lib/wallet.py

@ -503,6 +503,17 @@ class Abstract_Wallet(PrintError):
delta += v delta += v
return delta return delta
def get_tx_value(self, txid):
" effect of tx on the entire domain"
delta = 0
for addr, d in self.txi.get(txid, {}).items():
for n, v in d:
delta -= v
for addr, d in self.txo.get(txid, {}).items():
for n, v, cb in d:
delta += v
return delta
def get_wallet_delta(self, tx): def get_wallet_delta(self, tx):
""" effect of tx on wallet """ """ effect of tx on wallet """
addresses = self.get_addresses() addresses = self.get_addresses()
@ -1694,7 +1705,7 @@ class Abstract_Wallet(PrintError):
coins = self.get_utxos(domain) coins = self.get_utxos(domain)
now = time.time() now = time.time()
p = price_func(now) p = price_func(now)
ap = sum(self.coin_price(coin, price_func, ccy, self.txin_value(coin)) for coin in coins) ap = sum(self.coin_price(coin['prevout_hash'], price_func, ccy, self.txin_value(coin)) for coin in coins)
lp = sum([coin['value'] for coin in coins]) * p / Decimal(COIN) lp = sum([coin['value'] for coin in coins]) * p / Decimal(COIN)
return lp - ap return lp - ap
@ -1704,42 +1715,36 @@ class Abstract_Wallet(PrintError):
and the price of these coins when they entered the wallet. and the price of these coins when they entered the wallet.
price_func: function that returns the fiat price given a timestamp price_func: function that returns the fiat price given a timestamp
""" """
tx = self.transactions[txid] out_value = - self.get_tx_value(txid)/Decimal(COIN)
ir, im, v, fee = self.get_wallet_delta(tx)
out_value = -v
fiat_value = self.get_fiat_value(txid, ccy) fiat_value = self.get_fiat_value(txid, ccy)
if fiat_value is None: liquidation_price = - fiat_value if fiat_value else out_value * self.price_at_timestamp(txid, price_func)
p = self.price_at_timestamp(txid, price_func) acquisition_price = out_value * self.average_price(txid, price_func, ccy)
liquidation_price = out_value/Decimal(COIN) * p
else:
liquidation_price = - fiat_value
acquisition_price = out_value/Decimal(COIN) * self.average_price(tx, price_func, ccy)
return acquisition_price, liquidation_price return acquisition_price, liquidation_price
def average_price(self, tx, price_func, ccy): def average_price(self, txid, price_func, ccy):
""" average price of the inputs of a transaction """ """ Average acquisition price of the inputs of a transaction """
input_value = sum(self.txin_value(txin) for txin in tx.inputs()) / Decimal(COIN) input_value = 0
total_price = sum(self.coin_price(txin, price_func, ccy, self.txin_value(txin)) for txin in tx.inputs()) total_price = 0
return total_price / input_value for addr, d in self.txi.get(txid, {}).items():
for ser, v in d:
def coin_price(self, coin, price_func, ccy, txin_value): input_value += v
""" fiat price of acquisition of coin """ total_price += self.coin_price(ser.split(':')[0], price_func, ccy, v)
txid = coin['prevout_hash'] return total_price / (input_value/Decimal(COIN))
tx = self.transactions[txid]
if all([self.is_mine(txin['address']) for txin in tx.inputs()]): def coin_price(self, txid, price_func, ccy, txin_value):
return self.average_price(tx, price_func, ccy) * txin_value/Decimal(COIN) """
elif all([ not self.is_mine(txin['address']) for txin in tx.inputs()]): Acquisition price of a coin.
This assumes that either all inputs are mine, or no input is mine.
"""
if self.txi.get(txid, {}) != {}:
return self.average_price(txid, price_func, ccy) * txin_value/Decimal(COIN)
else:
fiat_value = self.get_fiat_value(txid, ccy) fiat_value = self.get_fiat_value(txid, ccy)
if fiat_value is not None: if fiat_value is not None:
return fiat_value return fiat_value
else: else:
p = self.price_at_timestamp(txid, price_func) p = self.price_at_timestamp(txid, price_func)
return p * txin_value/Decimal(COIN) return p * txin_value/Decimal(COIN)
else:
# could be some coinjoin transaction..
return Decimal('NaN')
class Simple_Wallet(Abstract_Wallet): class Simple_Wallet(Abstract_Wallet):
# wallet with a single keystore # wallet with a single keystore

Loading…
Cancel
Save