Sending transactions lets you move digital money or data on a blockchain. It is how you pay, trade, or interact with apps on the blockchain.
Sending transactions in Blockchain / Solidity
Start learning this pattern below
Jump into concepts and practice - no test required
transaction = {
'from': sender_address,
'to': receiver_address,
'value': amount_to_send,
'gas': gas_limit,
'gasPrice': gas_price,
'nonce': transaction_count,
'data': optional_data
}
signed_tx = sign_transaction(transaction, sender_private_key)
send_transaction(signed_tx)The transaction must be signed with the sender's private key to prove ownership.
Gas and gasPrice control the fee paid to process the transaction.
transaction = {
'from': '0xABC123...',
'to': '0xDEF456...',
'value': 1000000000000000000, # 1 Ether in wei
'gas': 21000,
'gasPrice': 20000000000, # 20 Gwei
'nonce': 0
}
signed_tx = sign_transaction(transaction, private_key)
send_transaction(signed_tx)transaction = {
'from': '0xABC123...',
'to': '0xContractAddress...',
'value': 0,
'gas': 100000,
'gasPrice': 25000000000,
'nonce': 1,
'data': '0xa9059cbb000000000000000000000000Recipient000000000000000000000000000000000000000000000000000000000000000a'
}
signed_tx = sign_transaction(transaction, private_key)
send_transaction(signed_tx)This program connects to a local Ethereum node, creates a transaction to send 1 Ether from one account to another, signs it with the private key, and sends it. It then prints the transaction hash as confirmation.
from web3 import Web3 # Connect to local Ethereum node w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545')) # Sender account and private key (example, do not use real keys) sender = w3.eth.accounts[0] private_key = '0x4c0883a69102937d6231471b5dbb6204fe512961708279a0e7f3b1a1a7b7e5f6' # Receiver address receiver = w3.eth.accounts[1] # Build transaction nonce = w3.eth.get_transaction_count(sender) tx = { 'nonce': nonce, 'to': receiver, 'value': w3.to_wei(1, 'ether'), 'gas': 21000, 'gasPrice': w3.to_wei('50', 'gwei') } # Sign transaction signed_tx = w3.eth.account.sign_transaction(tx, private_key) # Send transaction tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction) print(f'Transaction sent with hash: {tx_hash.hex()}')
Always keep your private key secret and never share it.
Gas price affects how fast your transaction is processed; higher gas price means faster processing.
Nonce must be correct to avoid transaction replacement or failure.
Sending transactions moves value or data on the blockchain.
Transactions must be signed with the sender's private key.
Gas and nonce are important to make transactions valid and processed.
Practice
What is the main purpose of sending a transaction on a blockchain?
Solution
Step 1: Understand what a transaction does
A transaction moves value or data between accounts on the blockchain.Step 2: Compare options to the definition
Only To move value or data from one account to another correctly describes sending a transaction.Final Answer:
To move value or data from one account to another -> Option CQuick Check:
Transaction purpose = move value/data [OK]
- Confusing transactions with mining
- Thinking transactions create blockchains
- Believing transactions delete blockchain data
Which of the following is the correct way to sign a transaction before sending it?
transaction.sign(____)Solution
Step 1: Identify the key needed for signing
Transactions must be signed with the sender's private key to prove ownership.Step 2: Match the correct key to the method
Only the sender's private key can sign the transaction securely.Final Answer:
sender's private key -> Option BQuick Check:
Sign with private key = sender's private key [OK]
- Using public key to sign
- Using receiver's key instead of sender's
- Confusing private and public keys
Consider this code snippet sending a transaction:
tx = {
'to': '0xabc123',
'value': 10,
'nonce': 5,
'gas': 21000
}
signed_tx = sign_transaction(tx, private_key)
result = send_transaction(signed_tx)
print(result)What will print(result) most likely output if the nonce is incorrect?
Solution
Step 1: Understand nonce role in transactions
Nonce must be correct and sequential to avoid replay or duplication errors.Step 2: Identify error caused by wrong nonce
An incorrect nonce causes a 'Nonce too low or too high' error during sending.Final Answer:
'Nonce too low or too high error' -> Option AQuick Check:
Wrong nonce = nonce error [OK]
- Confusing nonce error with gas error
- Assuming signature error for nonce issues
- Expecting success despite wrong nonce
Given this code snippet, what is the main error preventing the transaction from sending?
tx = {
'to': '0xdef456',
'value': 5,
'nonce': 3
}
signed_tx = sign_transaction(tx, private_key)
result = send_transaction(signed_tx)
print(result)Options:
Solution
Step 1: Check required transaction fields
Gas is required to pay for transaction processing; missing gas causes failure.Step 2: Verify other fields and keys
Nonce and receiver address look valid; no info about wrong private key.Final Answer:
Missing gas field in the transaction -> Option DQuick Check:
Missing gas = transaction fails [OK]
- Ignoring gas field requirement
- Assuming nonce or address is wrong without evidence
- Not checking transaction structure
You want to send multiple transactions quickly from the same account. Which approach ensures all transactions are accepted without nonce conflicts?
1. Use the same nonce for all transactions
2. Increment nonce by 1 for each transaction
3. Use random nonce values
4. Skip nonce and rely on networkSolution
Step 1: Understand nonce role in multiple transactions
Nonce must be unique and sequential per account to avoid conflicts.Step 2: Identify correct nonce handling method
Incrementing nonce by 1 for each transaction ensures proper ordering and acceptance.Final Answer:
Increment nonce by 1 for each transaction -> Option AQuick Check:
Sequential nonce = no conflicts [OK]
- Reusing same nonce causing rejection
- Using random nonce causing errors
- Assuming network assigns nonce automatically
