This scripts compiles a contract and gets the bytecode
and abi
bytecode
is the compiled machine code that will be deployed to the Ethereum blockchain.abi
is the Application Binary Interface, which defines the functions and events in the smart contract, allowing interaction with it after deployment.from solcx import compile_standard, install_solc
import json
with open("./SimpleWallet.sol", "r") as file:
simple_storage_file = file.read()
# Compile
install_solc("0.8.0")
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"SimpleWallet.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {
"*": ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"]
}
}
},
},
solc_version="0.8.0",
)
with open("compiled_code.json", "w") as file:
json.dump(compiled_sol, file)
# bytecode
# ABI
bytecode = compiled_sol["contracts"]["SimpleWallet.sol"]["SimpleWallet"]["evm"][
"bytecode"
]["object"]
abi = json.loads(
compiled_sol["contracts"]["SimpleWallet.sol"]["SimpleWallet"]["metadata"]
)["output"]["abi"]
SimpleWallet.sol
into the variable simple_storage_file
.compile_standard
function compiles the solidity code and output is stored in the compiled_sol
variable.bytecode
and ABI
are extracted from the compiled Solidity codeGanache is a personal blockchain for rapid Ethereum and Filecoin distributed application development. You can use Ganache across the entire development cycle; enabling you to develop, deploy, and test your dApps
in a safe and deterministic environment.
Ganache UI
Here we can get an address and a private key to deploy a contract with
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:7545"))
# Debug: Check connection and network ID
if w3.is_connected():
print("Connected to Web3")
print(f"Network ID: {w3.eth.chain_id}")
else:
print("Failed to connect to Web3")
chain_id = 1337
my_address = "0xc7677e5e27D02AA7ea6Bd721d51D570058552154"
private_key = os.getenv("PRIVATE_KEY")
# Create the contract in Python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
# Get the latest transaction
nonce = w3.eth.get_transaction_count(my_address)
# Submit the transaction that deploys the contract
transaction = SimpleStorage.constructor().build_transaction(
{
"chainId": chain_id,
"gasPrice": w3.eth.gas_price,
"from": my_address,
"nonce": nonce,
}
)
# Sign the transaction
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)
print("Deploying Contract!")
# Send it!
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
# Wait for the transaction to be mined, and get the transaction receipt
print("Waiting for transaction to finish...")
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print(f"Done! Contract deployed to {tx_receipt.contractAddress}")
chain_id
: The chain ID of the Ganache network you are working with.my_address
: The Ethereum address from which transactions will be sent.private_key
: The private key associated with my_address
, retrieved from environment variables for security.getBalance
function from the Simple Wallet contract we deployed0
wei
to the contract, we can do this by calling the function deposit
and sending wei
getBalance
again and we should see that now the contract returns 1 ethsimple_wallet = w3.eth.contract(address=tx_receipt.contractAddress, abi=abi)
print(simple_wallet.functions.getBalance().call())
tx = simple_wallet.functions.deposit().build_transaction(
{
"chainId": chain_id,
"gasPrice": w3.eth.gas_price,
"from": my_address,
"nonce": nonce + 1,
"value": w3.to_wei(1, "ether"), # Sending 1 ETH
}
)
signed_tx = w3.eth.account.sign_transaction(tx, private_key=private_key)
hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
recipt = w3.eth.wait_for_transaction_receipt(hash)
print(simple_wallet.functions.getBalance().call())
Install ganache
cli using npm
npm install -g ganache
You can use the deterministic
parameter to always use the same addresses and private keys
ganache --deterministic
ganache v7.9.2 (@ganache/cli: 0.10.2, @ganache/core: 0.10.2)
Starting RPC server
Available Accounts
==================
(0) 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1 (1000 ETH)