async def test_send_raw_transaction_and_get_balance(async_stubbed_sender, async_stubbed_receiver, test_http_client_async): """Test sending a raw transaction to localnet.""" # Get a recent blockhash resp = await test_http_client_async.get_recent_blockhash(Finalized) assert_valid_response(resp) recent_blockhash = resp["result"]["value"]["blockhash"] # Create transfer tx transfer lamports from stubbed sender to async_stubbed_receiver transfer_tx = Transaction(recent_blockhash=recent_blockhash).add( sp.transfer( sp.TransferParams(from_pubkey=async_stubbed_sender.public_key, to_pubkey=async_stubbed_receiver, lamports=1000))) # Sign transaction transfer_tx.sign(async_stubbed_sender) # Send raw transaction resp = await test_http_client_async.send_raw_transaction( transfer_tx.serialize()) assert_valid_response(resp) # Confirm transaction resp = await test_http_client_async.confirm_transaction(resp["result"]) # Check balances resp = await test_http_client_async.get_balance( async_stubbed_sender.public_key) assert_valid_response(resp) assert resp["result"]["value"] == 9999988000 resp = await test_http_client_async.get_balance(async_stubbed_receiver) assert_valid_response(resp) assert resp["result"]["value"] == 10000002000
def send_transaction(self, txn: Transaction, *signers: Account) -> RPCResponse: """Send a transaction. :param txn: Transaction object. :param signers: Signers to sign the transaction >>> from solana.account import Account >>> from solana.system_program import TransferParams, transfer >>> sender, reciever = Account(1), Account(2) >>> tx = transfer(TransferParams( ... from_pubkey=sender.public_key(), to_pubkey=reciever.public_key(), lamports=1000)) >>> solana_client = Client("http://localhost:8899") >>> solana_client.send_transaction(tx, sender) # doctest: +SKIP {'jsonrpc': '2.0', 'result': '236zSA5w4NaVuLXXHK1mqiBuBxkNBu84X6cfLBh1v6zjPrLfyECz4zdedofBaZFhs4gdwzSmij9VkaSo2tR5LTgG', 'id': 12} """ try: # TODO: Cache recent blockhash blockhash_resp = self.get_recent_blockhash() if not blockhash_resp["result"]: raise RuntimeError("failed to get recent blockhash") txn.recent_blockhash = Blockhash( blockhash_resp["result"]["value"]["blockhash"]) except Exception as err: raise RuntimeError("failed to get recent blockhash") from err txn.sign(*signers) wire_format = b58encode(txn.serialize()).decode("utf-8") return self._provider.make_request(RPCMethod("sendTransaction"), wire_format)
def send_transaction( self, txn: Transaction, *signers: Account, opts: types.TxOpts = types.TxOpts() ) -> types.RPCResponse: """Send a transaction. :param txn: Transaction object. :param signers: Signers to sign the transaction. :param opts: (optional) Transaction options. >>> from solana.account import Account >>> from solana.system_program import TransferParams, transfer >>> from solana.transaction import Transaction >>> sender, reciever = Account(1), Account(2) >>> txn = Transaction().add(transfer(TransferParams( ... from_pubkey=sender.public_key(), to_pubkey=reciever.public_key(), lamports=1000))) >>> solana_client = Client("http://localhost:8899") >>> solana_client.send_transaction(txn, sender) # doctest: +SKIP {'jsonrpc': '2.0', 'result': '236zSA5w4NaVuLXXHK1mqiBuBxkNBu84X6cfLBh1v6zjPrLfyECz4zdedofBaZFhs4gdwzSmij9VkaSo2tR5LTgG', 'id': 12} """ try: # TODO: Cache recent blockhash blockhash_resp = self.get_recent_blockhash() if not blockhash_resp["result"]: raise RuntimeError("failed to get recent blockhash") txn.recent_blockhash = Blockhash(blockhash_resp["result"]["value"]["blockhash"]) except Exception as err: raise RuntimeError("failed to get recent blockhash") from err txn.sign(*signers) return self.send_raw_transaction(txn.serialize(), opts=opts)
async def test_send_raw_transaction_and_get_balance(alt_stubbed_sender, alt_stubbed_receiver, test_http_client_async): """Test sending a raw transaction to localnet.""" # Get a recent blockhash resp = await test_http_client_async.get_recent_blockhash() assert_valid_response(resp) recent_blockhash = resp["result"]["value"]["blockhash"] # Create transfer tx transfer lamports from stubbed sender to alt_stubbed_receiver transfer_tx = Transaction(recent_blockhash=recent_blockhash).add( sp.transfer( sp.TransferParams(from_pubkey=alt_stubbed_sender.public_key(), to_pubkey=alt_stubbed_receiver, lamports=1000))) # Sign transaction transfer_tx.sign(alt_stubbed_sender) # Send raw transaction resp = await test_http_client_async.send_raw_transaction( transfer_tx.serialize()) assert_valid_response(resp) # Confirm transaction resp = await aconfirm_transaction(test_http_client_async, resp["result"]) assert_valid_response(resp) expected_meta = { "err": None, "fee": 5000, "innerInstructions": [], "logMessages": [ "Program 11111111111111111111111111111111 invoke [1]", "Program 11111111111111111111111111111111 success", ], "postBalances": [9999988000, 1954, 1], "postTokenBalances": [], "preBalances": [9999994000, 954, 1], "preTokenBalances": [], "rewards": [], "status": { "Ok": None }, } assert resp["result"]["meta"] == expected_meta # Check balances resp = await test_http_client_async.get_balance( alt_stubbed_sender.public_key()) assert_valid_response(resp) assert resp["result"]["value"] == 9999988000 resp = await test_http_client_async.get_balance(alt_stubbed_receiver) assert_valid_response(resp) assert resp["result"]["value"] == 1954