def funds_target_usd(self): from funding.bin.utils import Summary, coin_to_usd prices = Summary.fetch_prices() if not prices: return return coin_to_usd(amt=self.funds_target, btc_per_coin=prices['coin-btc'], usd_per_btc=prices['btc-usd'])
def balance(self): """This property retrieves the current funding status of this proposal. It uses Redis cache to not spam the daemon too much. Returns a nice dictionary containing all relevant proposal funding info""" from funding.bin.utils import Summary, coin_to_usd from funding.factory import cache, db_session rtn = {'sum': 0.0, 'txs': [], 'pct': 0.0, 'available': 0.0} cache_key = 'coin_balance_pid_%d' % self.id data = cache.get(cache_key) if not data: from funding.bin.daemon import Daemon try: data = Daemon().get_transfers_in(index=self.id, proposal_id=self.id) if not isinstance(data, dict): print('error; get_transfers_in; %d' % self.id) return rtn cache.set(cache_key, data=data, expiry=300) except: print('error; get_transfers_in; %d' % self.id) return rtn prices = Summary.fetch_prices() for tx in data['txs']: if prices: tx['amount_usd'] = coin_to_usd(amt=tx['amount_human'], btc_per_coin=prices['coin-btc'], usd_per_btc=prices['btc-usd']) tx['datetime'] = datetime.fromtimestamp(tx['timestamp']) if data.get('sum', 0.0): data['pct'] = 100 / float(self.funds_target / data.get('sum', 0.0)) data['available'] = data['sum'] else: data['pct'] = 0.0 data['available'] = 0.0 if data['pct'] != self.funds_progress: self.funds_progress = data['pct'] db_session.commit() db_session.flush() if data['available']: data['remaining_pct'] = 100 / float( data['sum'] / data['available']) else: data['remaining_pct'] = 0.0 return data
def spends(self): from funding.bin.utils import Summary, coin_to_usd from funding.factory import cache, db_session rtn = {'sum': 0.0, 'txs': [], 'pct': 0.0, 'spent': 0.0} cache_key = 'coin_spends_pid_%d' % self.id data = cache.get(cache_key) if not data: from funding.bin.daemon import Daemon try: data = Daemon().get_transfers_out(index=self.id, proposal_id=self.id) if not isinstance(data, dict): print('error; get_transfers_out; %d' % self.id) return rtn cache.set(cache_key, data=data, expiry=300) except: print('error; get_transfers_out; %d' % self.id) return rtn prices = Summary.fetch_prices() for tx in data['txs']: if prices: tx['amount_usd'] = coin_to_usd(amt=tx['amount_human'], btc_per_coin=prices['coin-btc'], usd_per_btc=prices['btc-usd']) tx['datetime'] = datetime.fromtimestamp(tx['timestamp']) if data.get('sum', 0.0): data['pct'] = 100 / float(self.funds_target / data.get('sum', 0.0)) data['spent'] = data['sum'] else: data['pct'] = 0.0 data['spent'] = 0.0 if data['spent']: data['remaining_pct'] = 100 / float(data['sum'] / data['spent']) else: data['remaining_pct'] = 0.0 return data
def api_coin_usd(amount): from funding.bin.utils import Summary, coin_to_usd prices = Summary.fetch_prices() return jsonify(usd=coin_to_usd(amt=amount, btc_per_coin=prices['coin-btc'], usd_per_btc=prices['btc-usd']))
def balance(self): """This property retrieves the current funding status of this proposal. It uses Redis cache to not spam the daemon too much. Returns a nice dictionary containing all relevant proposal funding info""" from funding.bin.utils import Summary, coin_to_usd from funding.factory import db rtn = {'sum': 0.0, 'txs': [], 'pct': 0.0, 'available': 0} if self.archived: return rtn try: r = requests.get( f'http://{settings.RPC_HOST}:{settings.RPC_PORT}/json_rpc', json={ "jsonrpc": "2.0", "id": "0", "method": "get_payments", "params": { "payment_id": self.payment_id } }) r.raise_for_status() blob = r.json() assert 'result' in blob assert 'payments' in blob['result'] assert isinstance(blob['result']['payments'], list) except Exception as ex: return rtn txs = blob['result']['payments'] for tx in txs: tx['amount_human'] = float(tx['amount']) / 1e11 tx['txid'] = tx['tx_hash'] tx['type'] = 'in' data = { 'sum': sum([float(z['amount']) / 1e11 for z in txs]), 'txs': txs } if not isinstance(data, dict): print('error; get_transfers_in; %d' % self.id) return rtn prices = Summary.fetch_prices() for tx in data['txs']: if prices: tx['amount_usd'] = coin_to_usd(amt=tx['amount_human'], btc_per_coin=prices['coin-btc'], usd_per_btc=prices['btc-usd']) if data.get('sum', 0.0): data['pct'] = 100 / float(self.funds_target / data.get('sum', 0.0)) data['available'] = data['sum'] else: data['pct'] = 0.0 data['available'] = 0.0 if data['pct'] != self.funds_progress: self.funds_progress = data['pct'] db.session.commit() db.session.flush() if data['available']: data['remaining_pct'] = 100 / float( data['sum'] / data['available']) else: data['remaining_pct'] = 0.0 return data