|
@ -247,24 +247,37 @@ class Abstract_Wallet(AddressSynchronizer): |
|
|
self.storage.put('labels', self.labels) |
|
|
self.storage.put('labels', self.labels) |
|
|
return changed |
|
|
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: |
|
|
if txid not in self.transactions: |
|
|
return |
|
|
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, {}) |
|
|
d = self.fiat_value.get(ccy, {}) |
|
|
if d and txid in d: |
|
|
if d and txid in d: |
|
|
d.pop(txid) |
|
|
d.pop(txid) |
|
|
else: |
|
|
else: |
|
|
return |
|
|
# avoid saving empty dict |
|
|
else: |
|
|
return True |
|
|
try: |
|
|
|
|
|
Decimal(text) |
|
|
|
|
|
except: |
|
|
|
|
|
return |
|
|
|
|
|
if ccy not in self.fiat_value: |
|
|
if ccy not in self.fiat_value: |
|
|
self.fiat_value[ccy] = {} |
|
|
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) |
|
|
self.storage.put('fiat_value', self.fiat_value) |
|
|
|
|
|
return reset |
|
|
|
|
|
|
|
|
def get_fiat_value(self, txid, ccy): |
|
|
def get_fiat_value(self, txid, ccy): |
|
|
fiat_value = self.fiat_value.get(ccy, {}).get(txid) |
|
|
fiat_value = self.fiat_value.get(ccy, {}).get(txid) |
|
@ -423,21 +436,11 @@ class Abstract_Wallet(AddressSynchronizer): |
|
|
income += value |
|
|
income += value |
|
|
# fiat computations |
|
|
# fiat computations |
|
|
if fx and fx.is_enabled() and fx.get_history_config(): |
|
|
if fx and fx.is_enabled() and fx.get_history_config(): |
|
|
fiat_value = self.get_fiat_value(tx_hash, fx.ccy) |
|
|
fiat_fields = self.get_tx_item_fiat(tx_hash, value, fx, tx_fee) |
|
|
fiat_default = fiat_value is None |
|
|
fiat_value = fiat_fields['fiat_value'].value |
|
|
fiat_rate = self.price_at_timestamp(tx_hash, fx.timestamp_rate) |
|
|
item.update(fiat_fields) |
|
|
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 |
|
|
|
|
|
if value < 0: |
|
|
if value < 0: |
|
|
acquisition_price = - value / Decimal(COIN) * self.average_price(tx_hash, fx.timestamp_rate, fx.ccy) |
|
|
capital_gains += fiat_fields['capital_gain'].value |
|
|
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 |
|
|
|
|
|
fiat_expenditures += -fiat_value |
|
|
fiat_expenditures += -fiat_value |
|
|
else: |
|
|
else: |
|
|
fiat_income += fiat_value |
|
|
fiat_income += fiat_value |
|
@ -478,6 +481,27 @@ class Abstract_Wallet(AddressSynchronizer): |
|
|
'summary': summary |
|
|
'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): |
|
|
def get_label(self, tx_hash): |
|
|
label = self.labels.get(tx_hash, '') |
|
|
label = self.labels.get(tx_hash, '') |
|
|
if label is '': |
|
|
if label is '': |
|
|