0
0
Blockchain / Solidityprogramming~5 mins

Ethereum networks (mainnet, testnet) in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Ethereum networks let you run and test blockchain programs safely. The mainnet is the real network with real money. Testnets are practice networks where you can try things without risk.

When you want to deploy a real Ethereum app that users will use with real money.
When you want to test your smart contract code without spending real Ether.
When you want to learn how Ethereum works without risking funds.
When you want to simulate transactions and see how your app behaves before going live.
Syntax
Blockchain / Solidity
network = 'mainnet'  # or 'testnet' like 'goerli', 'sepolia', 'rinkeby'

The mainnet is the live Ethereum network with real value.

Testnets like goerli or sepolia are copies of Ethereum for testing.

Examples
This sets the network to mainnet for real transactions.
Blockchain / Solidity
network = 'mainnet'
print(f"Connected to {network} - real Ethereum network")
This sets the network to Goerli testnet to test without real money.
Blockchain / Solidity
network = 'goerli'
print(f"Connected to {network} - test network for safe testing")
Sample Program

This program shows how to connect to different Ethereum networks and explains their purpose.

Blockchain / Solidity
def connect_to_network(network):
    if network == 'mainnet':
        return 'Connected to Ethereum mainnet with real Ether'
    elif network in ['goerli', 'sepolia', 'rinkeby']:
        return f'Connected to {network} testnet for safe testing'
    else:
        return 'Unknown network'

# Example usage
print(connect_to_network('mainnet'))
print(connect_to_network('goerli'))
print(connect_to_network('unknown'))
OutputSuccess
Important Notes

Always test your smart contracts on testnets before deploying to mainnet to avoid losing real money.

Testnets use fake Ether that you can get for free from faucets.

Mainnet transactions cost real Ether and are permanent.

Summary

Mainnet is the real Ethereum network with real value.

Testnets are safe places to practice and test your code.

Use testnets first, then deploy to mainnet when ready.