def test_transfers_with_invoices(self): keys = [priv.public_key for priv in generate_keys(5)] # Single memo memo_instruction, il = self._get_invoice_memo_instruction(TransactionType.SPEND, 10, 2) tx = solana.Transaction.new( keys[0], [ memo_instruction, token.transfer(keys[1], keys[2], keys[3], 10), token.transfer(keys[2], keys[3], keys[4], 20) ], ) creations, payments = parse_transaction(tx, il) assert len(creations) == 0 for i in range(2): assert payments[i].sender == keys[1 + i] assert payments[i].destination == keys[2 + i] assert payments[i].tx_type == TransactionType.SPEND assert payments[i].quarks == (1 + i) * 10 assert payments[i].invoice == Invoice.from_proto(il.invoices[i]) assert not payments[i].memo # Multiple memos memo_instruction_1, il1 = self._get_invoice_memo_instruction(TransactionType.SPEND, 10, 1) memo_instruction_2, il2 = self._get_invoice_memo_instruction(TransactionType.P2P, 10, 1) tx = solana.Transaction.new( keys[0], [ memo_instruction_1, token.transfer(keys[1], keys[2], keys[3], 10), memo_instruction_2, token.transfer(keys[2], keys[3], keys[4], 20), ], ) creations, payments = parse_transaction(tx, il1) assert len(creations) == 0 expected_invoices = [il1.invoices[0], None] expected_types = [TransactionType.SPEND, TransactionType.P2P] for i in range(2): assert payments[i].sender == keys[1 + i] assert payments[i].destination == keys[2 + i] assert payments[i].tx_type == expected_types[i] assert payments[i].quarks == (1 + i) * 10 if expected_invoices[i]: assert payments[i].invoice == Invoice.from_proto(expected_invoices[i]) else: assert not payments[i].invoice assert not payments[i].memo
f'{len(batch_result.succeeded)} succeeded, {len(batch_result.failed)} failed' ) for result in batch_result.succeeded: print( f'Sent 1 kin to {result.earn.destination.stellar_address} in transaction {result.tx_id.hex()}' ) for result in batch_result.failed: print( f'Failed to send 1 kin to {result.earn.destination.stellar_address} in transaction {result.tx_id.hex()} ' f'(error: {repr(result.error)})') # Send an earn batch of earns with 1 Kin each, with invoices earns = [ Earn(dest, kin_to_quarks('1'), invoice=Invoice([LineItem(f'Payment {idx}', kin_to_quarks('1'))])) for idx, dest in enumerate(destinations) ] batch_result = client.submit_earn_batch(source, earns) print( f'{len(batch_result.succeeded)} succeeded, {len(batch_result.failed)} failed' ) for result in batch_result.succeeded: print( f'Sent 1 kin to {result.earn.destination.stellar_address} in transaction {result.tx_id.hex()}', ) for result in batch_result.failed: print( f'Failed to send 1 kin to {result.earn.destination.stellar_address} in transaction {result.tx_id.hex()} ' f'(error: {repr(result.error)})')
tx_id = client.submit_payment(p) print( f'transaction successfully submitted with hash: {base58.b58encode(tx_id)}' ) except Error as e: print(f'transaction failed: {repr(e)}') if isinstance(e, TransactionErrors): print( f'tx_error={repr(e.tx_error)}, len(op_errors)={len(e.op_errors)}') for op_error in e.op_errors: print(f'op_error={repr(op_error)}') # Send payment of 1 Kin with an invoice invoice = Invoice([ LineItem('Test Payment', 100000, description='This is a description of the payment', sku=b'some sku') ]) payment = Payment(source, dest, TransactionType.EARN, kin_to_quarks('1'), invoice=invoice) try: tx_id = client.submit_payment(p) print( f'transaction successfully submitted with hash: {base58.b58encode(tx_id)}' ) except Error as e: print(f'transaction failed: {repr(e)}') if isinstance(e, TransactionErrors):
destinations = [PublicKey.from_base58(addr) for addr in args['destinations'].split(',')] # Send an earn batch with 1 Kin each earns = [Earn(dest, kin_to_quarks('1')) for idx, dest in enumerate(destinations)] batch_result = client.submit_earn_batch(EarnBatch(source, earns)) if batch_result.tx_error: print(f'{batch_result.tx_id} failed with error {repr(batch_result.tx_error)}') if batch_result.earn_errors: for e in batch_result.earn_errors: print(f'earn {e.earn_index} failed with error {repr(e.error)}') else: print(f'{batch_result.tx_id} submitted') # Send an earn batch of earns with 1 Kin each, with invoices earns = [Earn(dest, kin_to_quarks('1'), invoice=Invoice([LineItem(f'Payment {idx}', kin_to_quarks('1'))])) for idx, dest in enumerate(destinations)] batch_result = client.submit_earn_batch(EarnBatch(source, earns)) if batch_result.tx_error: print(f'{batch_result.tx_id} failed with error {repr(batch_result.tx_error)}') if batch_result.earn_errors: for e in batch_result.earn_errors: print(f'earn {e.earn_index} failed with error {repr(e.error)}') else: print(f'{batch_result.tx_id} submitted') # Send earn batch with dedupe_id batch = EarnBatch(source, earns, dedupe_id=uuid.uuid4().bytes) try: batch_result = client.submit_earn_batch(batch)
def _gen_invoice_list(): return InvoiceList([Invoice([ LineItem('Item1', 10), ])])
print("Sent 1 kin to {} in transaction {}".format( result.earn.destination.stellar_address, result.tx_hash.hex(), )) for result in batch_result.failed: print("Failed to send 1 kin to {} in transaction {} (error: {})".format( result.earn.destination.stellar_address, result.tx_hash.hex(), repr(result.error), )) # Send an earn batch of earns with 1 Kin each, with invoices earns = [ Earn(dest, kin_to_quarks("1"), invoice=Invoice( [LineItem("Payment {}".format(idx), kin_to_quarks("1"))])) for idx, dest in enumerate(destinations) ] batch_result = client.submit_earn_batch(source, earns) print("{} succeeded, {} failed".format(len(batch_result.succeeded), len(batch_result.failed))) for result in batch_result.succeeded: print( "Sent 1 kin to {} in transaction {}".format( result.earn.destination.stellar_address, result.tx_hash.hex()), ) for result in batch_result.failed: print("Failed to send 1 kin to {} in transaction {} (error: {})".format( result.earn.destination.stellar_address, result.tx_hash.hex(), repr(result.error), ))
source = PrivateKey.from_string(args['sender']) dest = PublicKey.from_string(args['destination']) # Send a payment of 1 Kin payment = Payment(source, dest, TransactionType.EARN, kin_to_quarks("1")) submit_payment(payment) # Send a payment of 1 Kin with a text memo payment = Payment(source, dest, TransactionType.EARN, kin_to_quarks("1"), memo='1-test') submit_payment(payment) # Send payment of 1 Kin with an invoice invoice = Invoice([ LineItem("Test Payment", 100000, description="This is a description of the payment", sku=b'some sku') ]) payment = Payment(source, dest, TransactionType.EARN, kin_to_quarks("1"), invoice=invoice) submit_payment(payment) client.close()