async def assertStreamPurchased(self, stream: Transaction, operation): await self.account.release_all_outputs() buyer_balance = await self.account.get_balance() merchant_balance = lbc_to_dewies(str(await self.blockchain.get_balance())) pre_purchase_count = (await self.daemon.jsonrpc_purchase_list())['total_items'] purchase = await operation() stream_txo, purchase_txo = stream.outputs[0], purchase.outputs[0] stream_fee = stream_txo.claim.stream.fee self.assertEqual(stream_fee.dewies, purchase_txo.amount) self.assertEqual(stream_fee.address, purchase_txo.get_address(self.ledger)) await self.ledger.wait(purchase) await self.generate(1) merchant_balance += lbc_to_dewies('1.0') # block reward await self.ledger.wait(purchase) self.assertEqual( await self.account.get_balance(), buyer_balance - (purchase.input_sum-purchase.outputs[2].amount)) self.assertEqual( str(await self.blockchain.get_balance()), dewies_to_lbc(merchant_balance + purchase_txo.amount)) purchases = await self.daemon.jsonrpc_purchase_list() self.assertEqual(purchases['total_items'], pre_purchase_count+1) tx = purchases['items'][0].tx_ref.tx self.assertEqual(len(tx.outputs), 3) # purchase txo, purchase data, change txo0 = tx.outputs[0] txo1 = tx.outputs[1] self.assertEqual(txo0.purchase, txo1) # purchase txo has reference to purchase data self.assertTrue(txo1.is_purchase_data) self.assertTrue(txo1.can_decode_purchase_data) self.assertIsInstance(txo1.purchase_data, Purchase) self.assertEqual(txo1.purchase_data.claim_id, stream_txo.claim_id)
async def start(self, ledger=None, wallet=None): if lbc_to_dewies(self.max_fee) < 1: return self.ledger = ledger self.wallet = wallet self.running = True self.task = asyncio.ensure_future(self.pay()) self.task.add_done_callback( lambda _: log.info("Stopping wallet server payments."))
def _save_support(transaction): bind = "({})".format(','.join(['?'] * len(claim_id_to_supports))) transaction.execute(f"delete from support where claim_id in {bind}", list(claim_id_to_supports.keys())) for claim_id, supports in claim_id_to_supports.items(): for support in supports: transaction.execute( "insert into support values (?, ?, ?, ?)", ("%s:%i" % (support['txid'], support['nout']), claim_id, lbc_to_dewies(support['amount']), support.get('address', "")) )
async def pay(self): while self.running: await asyncio.sleep(self.payment_period) features = await self.ledger.network.retriable_call( self.ledger.network.get_server_features) address = features['payment_address'] amount = str(features['daily_fee']) if not address or not amount: continue if not self.ledger.is_valid_address(address): self._on_payment_controller.add_error( ServerPaymentInvalidAddressError(address)) continue if self.wallet.is_locked: self._on_payment_controller.add_error( ServerPaymentWalletLockedError()) continue amount = lbc_to_dewies( features['daily_fee'] ) # check that this is in lbc and not dewies limit = lbc_to_dewies(self.max_fee) if amount > limit: self._on_payment_controller.add_error( ServerPaymentFeeAboveMaxAllowedError( features['daily_fee'], self.max_fee)) continue tx = await Transaction.create( [], [ Output.pay_pubkey_hash( amount, self.ledger.address_to_hash160(address)) ], self.wallet.get_accounts_or_all(None), self.wallet.get_account_or_default(None)) await self.ledger.broadcast(tx) if self.analytics_manager: await self.analytics_manager.send_credits_sent() self._on_payment_controller.add(tx)
async def create_nondeterministic_channel(self, name, price, pubkey_bytes, daemon=None, blocking=False): account = (daemon or self.daemon).wallet_manager.default_account claim_address = await account.receiving.get_or_create_usable_address() claim = Claim() claim.channel.public_key_bytes = pubkey_bytes tx = await Transaction.claim_create( name, claim, lbc_to_dewies(price), claim_address, [self.account], self.account ) await tx.sign([self.account]) await (daemon or self.daemon).broadcast_or_release(tx, blocking) return self.sout(tx)
def _save_claims(transaction): content_claims_to_update = [] for claim_info in claim_infos: outpoint = "%s:%i" % (claim_info['txid'], claim_info['nout']) claim_id = claim_info['claim_id'] name = claim_info['name'] amount = lbc_to_dewies(claim_info['amount']) height = claim_info['height'] address = claim_info['address'] sequence = claim_info['claim_sequence'] certificate_id = claim_info['value'].signing_channel_id try: source_hash = claim_info['value'].stream.source.sd_hash except (AttributeError, ValueError): source_hash = None serialized = binascii.hexlify(claim_info['value'].to_bytes()) transaction.execute( "insert or replace into claim values (?, ?, ?, ?, ?, ?, ?, ?, ?)", (outpoint, claim_id, name, amount, height, serialized, certificate_id, address, sequence) ) # if this response doesn't have support info don't overwrite the existing # support info if 'supports' in claim_info: claim_id_to_supports[claim_id] = claim_info['supports'] if not source_hash: continue stream_hash = transaction.execute( "select file.stream_hash from stream " "inner join file on file.stream_hash=stream.stream_hash where sd_hash=?", (source_hash,) ).fetchone() if not stream_hash: continue stream_hash = stream_hash[0] known_outpoint = transaction.execute( "select claim_outpoint from content_claim where stream_hash=?", (stream_hash,) ).fetchone() known_claim_id = transaction.execute( "select claim_id from claim " "inner join content_claim c3 ON claim.claim_outpoint=c3.claim_outpoint " "where c3.stream_hash=?", (stream_hash,) ).fetchone() if not known_claim_id: content_claims_to_update.append((stream_hash, outpoint)) elif known_outpoint != outpoint: content_claims_to_update.append((stream_hash, outpoint)) for stream_hash, outpoint in content_claims_to_update: self._save_content_claim(transaction, outpoint, stream_hash) if stream_hash in self.content_claim_callbacks: update_file_callbacks.append(self.content_claim_callbacks[stream_hash]())
def to_dewies(self, currency, amount) -> int: converted = self.convert_currency(currency, "LBC", amount) return lbc_to_dewies(str(converted))
def calculate_effective_amount(amount: str, supports: typing.Optional[typing.List[typing.Dict]] = None) -> str: return dewies_to_lbc( lbc_to_dewies(amount) + sum([lbc_to_dewies(support['amount']) for support in supports]) )