def test_json_appbase(self): asset = Asset("SBD", steem_instance=self.bts) amount = Amount("1", asset, new_appbase_format=False, steem_instance=self.bts) if self.bts.rpc.get_use_appbase(): self.assertEqual( amount.json(), [str(1 * 10**asset.precision), asset.precision, asset.asset]) else: self.assertEqual(amount.json(), "1.000 SBD")
def test_json_appbase2(self): asset = Asset("SBD", steem_instance=self.appbase) amount = Amount("1", asset, new_appbase_format=True, steem_instance=self.appbase) self.assertEqual( amount.json(), { 'amount': str(1 * 10**asset.precision), 'nai': asset.asset, 'precision': asset.precision })
def __init__(self, symbol: str): super().__init__(symbol) settings = self.coin.settings['json'] rpcs = settings.get('rpcs') # If you've specified custom RPC nodes in the custom JSON, make a new instance with those # Otherwise, use the global shared_steem_instance. self.rpc = shared_steem_instance() if empty(rpcs, itr=True) else Steem( rpcs) # type: Steem self.rpc.set_password_storage(settings.get('pass_store', 'environment')) # For easy reference, the Beem asset object, and precision self.asset = asset = Asset(self.symbol) self.precision = int(asset.precision)
def test_json_appbase2(self): asset = Asset("SBD", steem_instance=self.bts) amount = Amount("1", asset, new_appbase_format=True, steem_instance=self.bts) if self.bts.rpc.get_use_appbase(): self.assertEqual( amount.json(), { 'amount': str(1 * 10**asset.precision), 'nai': asset.asset, 'precision': asset.precision }) else: self.assertEqual(amount.json(), "1.000 SBD")
def test_init(self): stm = self.bts # String init asset = Asset("HBD", blockchain_instance=stm) symbol = asset["symbol"] precision = asset["precision"] amount = Amount("1 {}".format(symbol), blockchain_instance=stm) self.dotest(amount, 1, symbol) # Amount init amount = Amount(amount, blockchain_instance=stm) self.dotest(amount, 1, symbol) # blockchain dict init amount = Amount({ "amount": 1 * 10 ** precision, "asset_id": asset["id"] }, blockchain_instance=stm) self.dotest(amount, 1, symbol) # API dict init amount = Amount({ "amount": 1.3 * 10 ** precision, "asset": asset["id"] }, blockchain_instance=stm) self.dotest(amount, 1.3, symbol) # Asset as symbol amount = Amount(1.3, Asset("HBD"), blockchain_instance=stm) self.dotest(amount, 1.3, symbol) # Asset as symbol amount = Amount(1.3, symbol, blockchain_instance=stm) self.dotest(amount, 1.3, symbol) # keyword inits amount = Amount(amount=1.3, asset=Asset("HBD", blockchain_instance=stm), blockchain_instance=stm) self.dotest(amount, 1.3, symbol) amount = Amount(amount=1.3001, asset=Asset("HBD", blockchain_instance=stm), blockchain_instance=stm) self.dotest(amount, 1.3001, symbol) amount = Amount(amount=1.3001, asset=Asset("HBD", blockchain_instance=stm), fixed_point_arithmetic=True, blockchain_instance=stm) self.dotest(amount, 1.3, symbol) # keyword inits amount = Amount(amount=1.3, asset=dict(Asset("HBD", blockchain_instance=stm)), blockchain_instance=stm) self.dotest(amount, 1.3, symbol) # keyword inits amount = Amount(amount=1.3, asset=symbol, blockchain_instance=stm) self.dotest(amount, 1.3, symbol) amount = Amount(amount=8.190, asset=symbol, blockchain_instance=stm) self.dotest(amount, 8.190, symbol)
def clean_tx(tx: dict, symbol: str, account: str, memo: str = None, memo_case: bool = False) -> Union[dict, None]: """Filters an individual transaction. See :meth:`.clean_txs` for info""" # log.debug(tx) if tx.get('type', 'NOT SET') != 'transfer': log.debug('Steem TX is not transfer. Type is: %s', tx.get('type', 'NOT SET')) return None txid = tx.get('trx_id', None) _am = tx['amount'] # Transfer ops contain a dict 'amount', containing amount:int, nai:str, precision:int amt_sym = str(Asset(_am['nai']).symbol).upper() # Conv asset ID (e.g. @@000000021) to symbol, i.e. "STEEM" if amt_sym != symbol: # If the symbol doesn't match the symbol we were passed, skip this TX return None # Convert integer amount/precision to Decimal's, preventing floating point issues amt_int = Decimal(_am['amount']) amt_prec = Decimal(_am['precision']) amt = amt_int / (Decimal(10) ** amt_prec) # Use precision value to convert from integer amt to decimal amt tx_memo = tx.get('memo') log.debug('Filtering/cleaning steem transaction, Amt: %f, TXID: %s', amt, txid) if tx['to'] != account or tx['from'] == account: return None # If the transaction isn't to us (account), or it's from ourselves, ignore it. if not empty(memo) and (tx_memo != memo or (not memo_case and tx_memo.lower() != memo.lower())): return None d = parse(tx['timestamp']) d = timezone.make_aware(d, pytz.UTC) return dict( txid=txid, coin=symbol, vout=int(tx.get('op_in_trx', 0)), tx_timestamp=d, from_account=tx.get('from', None), to_account=tx.get('to', None), memo=tx_memo, amount=Decimal(amt) )
def test_int(self): self.assertEqual( int(Amount("0.9999", self.symbol)), 999) self.assertEqual( int(Amount(0.151, self.symbol)), 151) self.assertEqual( int(Amount(8.190, self.symbol)), 8190) self.assertEqual( int(Amount(round(0.1509,3), self.symbol)), 151) self.assertEqual( int(Amount(round(0.1509,3), self.asset)), 151) self.assertEqual( int(Amount(int(1), self.symbol)), 1000) self.assertEqual( int(Amount(amount=round(0.1509,3), asset=Asset("HBD"))), 151)
def test_json_appbase(self): asset = Asset("SBD", steem_instance=self.appbase) amount = Amount("1", asset, steem_instance=self.appbase) self.assertEqual( amount.json(), [str(1 * 10**asset.precision), asset.precision, asset.asset])
def asset(self) -> Asset: """Easy reference to the Beem Asset object for our current symbol""" if not self._asset: self._asset = Asset(self.symbol, steem_instance=self.rpc) return self._asset
def __init__(self, amount, asset=None, fixed_point_arithmetic=False, new_appbase_format=True, blockchain_instance=None, **kwargs): self["asset"] = {} self.new_appbase_format = new_appbase_format self.fixed_point_arithmetic = fixed_point_arithmetic if blockchain_instance is None: if kwargs.get("steem_instance"): blockchain_instance = kwargs["steem_instance"] elif kwargs.get("hive_instance"): blockchain_instance = kwargs["hive_instance"] self.blockchain = blockchain_instance or shared_blockchain_instance() if amount and asset is None and isinstance(amount, Amount): # Copy Asset object self["amount"] = amount["amount"] self["symbol"] = amount["symbol"] self["asset"] = amount["asset"] elif amount and asset is None and isinstance(amount, list) and len(amount) == 3: # Copy Asset object self["amount"] = Decimal(amount[0]) / Decimal(10 ** amount[1]) self["asset"] = Asset(amount[2], blockchain_instance=self.blockchain) self["symbol"] = self["asset"]["symbol"] elif amount and asset is None and isinstance(amount, dict) and "amount" in amount and "nai" in amount and "precision" in amount: # Copy Asset object self.new_appbase_format = True self["amount"] = Decimal(amount["amount"]) / Decimal(10 ** amount["precision"]) self["asset"] = Asset(amount["nai"], blockchain_instance=self.blockchain) self["symbol"] = self["asset"]["symbol"] elif amount is not None and asset is None and isinstance(amount, string_types): self["amount"], self["symbol"] = amount.split(" ") self["asset"] = Asset(self["symbol"], blockchain_instance=self.blockchain) elif (amount and asset is None and isinstance(amount, dict) and "amount" in amount and "asset_id" in amount): self["asset"] = Asset(amount["asset_id"], blockchain_instance=self.blockchain) self["symbol"] = self["asset"]["symbol"] self["amount"] = Decimal(amount["amount"]) / Decimal(10 ** self["asset"]["precision"]) elif (amount and asset is None and isinstance(amount, dict) and "amount" in amount and "asset" in amount): self["asset"] = Asset(amount["asset"], blockchain_instance=self.blockchain) self["symbol"] = self["asset"]["symbol"] self["amount"] = Decimal(amount["amount"]) / Decimal(10 ** self["asset"]["precision"]) elif isinstance(amount, (float)) and asset and isinstance(asset, Asset): self["amount"] = str(amount) self["asset"] = asset self["symbol"] = self["asset"]["symbol"] elif isinstance(amount, (integer_types, Decimal)) and asset and isinstance(asset, Asset): self["amount"] = amount self["asset"] = asset self["symbol"] = self["asset"]["symbol"] elif isinstance(amount, (float)) and asset and isinstance(asset, dict): self["amount"] = str(amount) self["asset"] = asset self["symbol"] = self["asset"]["symbol"] elif isinstance(amount, (integer_types, Decimal)) and asset and isinstance(asset, dict): self["amount"] = amount self["asset"] = asset self["symbol"] = self["asset"]["symbol"] elif isinstance(amount, (float)) and asset and isinstance(asset, string_types): self["amount"] = str(amount) self["asset"] = Asset(asset, blockchain_instance=self.blockchain) self["symbol"] = asset elif isinstance(amount, (integer_types, Decimal)) and asset and isinstance(asset, string_types): self["amount"] = amount self["asset"] = Asset(asset, blockchain_instance=self.blockchain) self["symbol"] = asset elif amount and asset and isinstance(asset, Asset): self["amount"] = amount self["symbol"] = asset["symbol"] self["asset"] = asset elif amount and asset and isinstance(asset, string_types): self["amount"] = amount self["asset"] = Asset(asset, blockchain_instance=self.blockchain) self["symbol"] = self["asset"]["symbol"] else: raise ValueError if self.fixed_point_arithmetic: self["amount"] = quantize(self["amount"], self["asset"]["precision"]) else: self["amount"] = Decimal(self["amount"])
def asset(self): """ Returns the asset as instance of :class:`steem.asset.Asset` """ if not self["asset"]: self["asset"] = Asset(self["symbol"], blockchain_instance=self.blockchain) return self["asset"]
def __init__(self, amount, asset=None, new_appbase_format=True, steem_instance=None): self["asset"] = {} self.new_appbase_format = new_appbase_format self.steem = steem_instance or shared_steem_instance() if amount and asset is None and isinstance(amount, Amount): # Copy Asset object self["amount"] = amount["amount"] self["symbol"] = amount["symbol"] self["asset"] = amount["asset"] elif amount and asset is None and isinstance(amount, list) and len(amount) == 3: # Copy Asset object self["amount"] = int(amount[0]) / (10 ** amount[1]) self["asset"] = Asset(amount[2], steem_instance=self.steem) self["symbol"] = self["asset"]["symbol"] elif amount and asset is None and isinstance(amount, dict) and "amount" in amount and "nai" in amount and "precision" in amount: # Copy Asset object self.new_appbase_format = True self["amount"] = int(amount["amount"]) / (10 ** amount["precision"]) self["asset"] = Asset(amount["nai"], steem_instance=self.steem) self["symbol"] = self["asset"]["symbol"] elif amount is not None and asset is None and isinstance(amount, string_types): self["amount"], self["symbol"] = amount.split(" ") self["asset"] = Asset(self["symbol"], steem_instance=self.steem) elif (amount and asset is None and isinstance(amount, dict) and "amount" in amount and "asset_id" in amount): self["asset"] = Asset(amount["asset_id"], steem_instance=self.steem) self["symbol"] = self["asset"]["symbol"] self["amount"] = int(amount["amount"]) / 10 ** self["asset"]["precision"] elif (amount and asset is None and isinstance(amount, dict) and "amount" in amount and "asset" in amount): self["asset"] = Asset(amount["asset"], steem_instance=self.steem) self["symbol"] = self["asset"]["symbol"] self["amount"] = int(amount["amount"]) / 10 ** self["asset"]["precision"] elif amount and asset and isinstance(asset, Asset): self["amount"] = amount self["symbol"] = asset["symbol"] self["asset"] = asset elif amount and asset and isinstance(asset, string_types): self["amount"] = amount self["asset"] = Asset(asset, steem_instance=self.steem) self["symbol"] = self["asset"]["symbol"] elif isinstance(amount, (integer_types, float)) and asset and isinstance(asset, Asset): self["amount"] = amount self["asset"] = asset self["symbol"] = self["asset"]["symbol"] elif isinstance(amount, (integer_types, float)) and asset and isinstance(asset, dict): self["amount"] = amount self["asset"] = asset self["symbol"] = self["asset"]["symbol"] elif isinstance(amount, (integer_types, float)) and asset and isinstance(asset, string_types): self["amount"] = amount self["asset"] = Asset(asset, steem_instance=self.steem) self["symbol"] = asset else: raise ValueError # make sure amount is a float self["amount"] = float(self["amount"])