0
0
Blockchain / Solidityprogramming~7 mins

Sending transactions in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

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.

You want to send cryptocurrency to a friend.
You want to buy something using blockchain tokens.
You want to call a smart contract to do an action.
You want to transfer ownership of a digital asset.
You want to record data permanently on the blockchain.
Syntax
Blockchain / Solidity
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.

Examples
This example sends 1 Ether from one address to another with basic gas settings.
Blockchain / Solidity
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)
This example calls a smart contract function to transfer tokens, with encoded data.
Blockchain / Solidity
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)
Sample Program

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.

Blockchain / Solidity
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()}')
OutputSuccess
Important Notes

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.

Summary

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.