|
|
@ -247,24 +247,37 @@ class Abstract_Wallet(AddressSynchronizer): |
|
|
|
self.storage.put('labels', self.labels) |
|
|
|
return changed |
|
|
|
|
|
|
|
def set_fiat_value(self, txid, ccy, text): |
|
|
|
def set_fiat_value(self, txid, ccy, text, fx, value): |
|
|
|
if txid not in self.transactions: |
|
|
|
return |
|
|
|
if not text: |
|
|
|
# since fx is inserting the thousands separator, |
|
|
|
# and not util, also have fx remove it |
|
|
|
text = fx.remove_thousands_separator(text) |
|
|
|
def_fiat = self.default_fiat_value(txid, fx, value) |
|
|
|
formatted = fx.ccy_amount_str(def_fiat, commas=False) |
|
|
|
def_fiat_rounded = Decimal(formatted) |
|
|
|
reset = not text |
|
|
|
if not reset: |
|
|
|
try: |
|
|
|
text_dec = Decimal(text) |
|
|
|
text_dec_rounded = Decimal(fx.ccy_amount_str(text_dec, commas=False)) |
|
|
|
reset = text_dec_rounded == def_fiat_rounded |
|
|
|
except: |
|
|
|
# garbage. not resetting, but not saving either |
|
|
|
return False |
|
|
|
if reset: |
|
|
|
d = self.fiat_value.get(ccy, {}) |
|
|
|
if d and txid in d: |
|
|
|
d.pop(txid) |
|
|
|
else: |
|
|
|
return |
|
|
|
else: |
|
|
|
try: |
|
|
|
Decimal(text) |
|
|
|
except: |
|
|
|
return |
|
|
|
# avoid saving empty dict |
|
|
|
return True |
|
|
|
if ccy not in self.fiat_value: |
|
|
|
self.fiat_value[ccy] = {} |
|
|
|
self.fiat_value[ccy][txid] = text |
|
|
|
if not reset: |
|
|
|
self.fiat_value[ccy][txid] = text |
|
|
|
self.storage.put('fiat_value', self.fiat_value) |
|
|
|
return reset |
|
|
|
|
|
|
|
def get_fiat_value(self, txid, ccy): |
|
|
|
fiat_value = self.fiat_value.get(ccy, {}).get(txid) |
|
|
@ -423,21 +436,11 @@ class Abstract_Wallet(AddressSynchronizer): |
|
|
|
income += value |
|
|
|
# fiat computations |
|
|
|
if fx and fx.is_enabled() and fx.get_history_config(): |
|
|
|
fiat_value = self.get_fiat_value(tx_hash, fx.ccy) |
|
|
|
fiat_default = fiat_value is None |
|
|
|
fiat_rate = self.price_at_timestamp(tx_hash, fx.timestamp_rate) |
|
|
|
fiat_value = fiat_value if fiat_value is not None else value / Decimal(COIN) * fiat_rate |
|
|
|
fiat_fee = tx_fee / Decimal(COIN) * fiat_rate if tx_fee is not None else None |
|
|
|
item['fiat_value'] = Fiat(fiat_value, fx.ccy) |
|
|
|
item['fiat_fee'] = Fiat(fiat_fee, fx.ccy) if fiat_fee else None |
|
|
|
item['fiat_default'] = fiat_default |
|
|
|
fiat_fields = self.get_tx_item_fiat(tx_hash, value, fx, tx_fee) |
|
|
|
fiat_value = fiat_fields['fiat_value'].value |
|
|
|
item.update(fiat_fields) |
|
|
|
if value < 0: |
|
|
|
acquisition_price = - value / Decimal(COIN) * self.average_price(tx_hash, fx.timestamp_rate, fx.ccy) |
|
|
|
liquidation_price = - fiat_value |
|
|
|
item['acquisition_price'] = Fiat(acquisition_price, fx.ccy) |
|
|
|
cg = liquidation_price - acquisition_price |
|
|
|
item['capital_gain'] = Fiat(cg, fx.ccy) |
|
|
|
capital_gains += cg |
|
|
|
capital_gains += fiat_fields['capital_gain'].value |
|
|
|
fiat_expenditures += -fiat_value |
|
|
|
else: |
|
|
|
fiat_income += fiat_value |
|
|
@ -478,6 +481,27 @@ class Abstract_Wallet(AddressSynchronizer): |
|
|
|
'summary': summary |
|
|
|
} |
|
|
|
|
|
|
|
def default_fiat_value(self, tx_hash, fx, value): |
|
|
|
return value / Decimal(COIN) * self.price_at_timestamp(tx_hash, fx.timestamp_rate) |
|
|
|
|
|
|
|
def get_tx_item_fiat(self, tx_hash, value, fx, tx_fee): |
|
|
|
item = {} |
|
|
|
fiat_value = self.get_fiat_value(tx_hash, fx.ccy) |
|
|
|
fiat_default = fiat_value is None |
|
|
|
fiat_rate = self.price_at_timestamp(tx_hash, fx.timestamp_rate) |
|
|
|
fiat_value = fiat_value if fiat_value is not None else self.default_fiat_value(tx_hash, fx, value) |
|
|
|
fiat_fee = tx_fee / Decimal(COIN) * fiat_rate if tx_fee is not None else None |
|
|
|
item['fiat_value'] = Fiat(fiat_value, fx.ccy) |
|
|
|
item['fiat_fee'] = Fiat(fiat_fee, fx.ccy) if fiat_fee else None |
|
|
|
item['fiat_default'] = fiat_default |
|
|
|
if value < 0: |
|
|
|
acquisition_price = - value / Decimal(COIN) * self.average_price(tx_hash, fx.timestamp_rate, fx.ccy) |
|
|
|
liquidation_price = - fiat_value |
|
|
|
item['acquisition_price'] = Fiat(acquisition_price, fx.ccy) |
|
|
|
cg = liquidation_price - acquisition_price |
|
|
|
item['capital_gain'] = Fiat(cg, fx.ccy) |
|
|
|
return item |
|
|
|
|
|
|
|
def get_label(self, tx_hash): |
|
|
|
label = self.labels.get(tx_hash, '') |
|
|
|
if label is '': |
|
|
|