This repository provides a practical demonstration of how to deploy and interact with a simple smart contract on the Avalanche (AVAX) blockchain network. It includes two core components: a Solidity smart contract (Register.sol
) for storing and retrieving information and a Python script (BlockChain.py
) utilizing Web3 to interact with the deployed contract. This work exemplifies the synergy between blockchain development and client-side scripting for decentralized applications (dApps).
Blockchain technology enables the development of decentralized systems that operate transparently and securely. Smart contracts, programs deployed on blockchains, form the backbone of these systems, allowing users to automate and enforce agreements without intermediaries. This repository features:
- Register.sol: A Solidity smart contract designed to store and retrieve string data.
- BlockChain.py: A Python script leveraging the Web3 library to connect to the Avalanche Testnet and interact with the smart contract.
This Solidity smart contract contains two primary functions:
setInfo(string _info)
: Allows users to store a string value on the blockchain.getInfo()
: Retrieves the stored string value.
The contract demonstrates the use of:
- Public functions for interaction.
- The Ethereum ABI (Application Binary Interface) for encoding and decoding data.
pragma solidity ^0.8.0;
contract Register {
string private info;
function setInfo(string memory _info) public {
info = _info;
}
function getInfo() public view returns (string memory) {
return info;
}
}
This Python script is used to interact with the deployed Register.sol
contract. It demonstrates how to:
- Connect to the Avalanche Testnet using Infura as the blockchain node provider.
- Use the Web3 Python library to call smart contract functions.
- Connect to the Avalanche blockchain.
- Retrieve the latest block number.
- Invoke the
getInfo
function to fetch stored data. - Handle contract ABIs for interaction.
from web3 import Web3
infura_url = "https://api.avax-test.network/ext/bc/C/rpc"
w3 = Web3(Web3.HTTPProvider(infura_url))
if w3.is_connected():
print("Connected to AVAX Testnet Block Chain")
else:
print("Connection failed")
contractAddress = "0x25709e153c097d8C58d34B2810e8EaA09525dEDE"
contractABI = [
{
"inputs": [],
"name": "getInfo",
"outputs": [{"internalType": "string", "name": "", "type": "string"}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [{"internalType": "string", "name": "_info", "type": "string"}],
"name": "setInfo",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
contract = w3.eth.contract(contractAddress, abi=contractABI)
info = contract.functions.getInfo().call()
print(f"Contract Info: {info}")
-
Software and Tools
- Remix IDE for Solidity contract development.
- Python 3.x for running the interaction script.
-
Blockchain Network
- Avalanche Testnet.
-
Libraries
- Web3.py (
pip install web3
).
- Web3.py (
- Open
Register.sol
in Remix IDE. - Compile the contract using the Solidity compiler (version 0.8.x).
- Deploy the contract to the Avalanche Testnet.
- Copy the deployed contract address and ABI.
- Update
BlockChain.py
:- Replace
contractAddress
with the deployed contract address. - Use the ABI of the deployed contract.
- Replace
- Run the script:
python BlockChain.py
- Verify the output, including the latest block number and contract interactions.
The repository demonstrates how to integrate a basic smart contract with a Python-based client. Key takeaways include:
- Effective use of the Web3 library to interact with blockchain.
- A minimalistic yet functional smart contract for learning purposes.
This repository is a concise example for developers interested in exploring blockchain development. By combining Solidity and Python, it lays a foundation for more complex dApp ecosystems. Future enhancements could include:
- Integrating a frontend for user-friendly interactions.
- Expanding contract functionality.
- Web3.py Documentation
- Remix IDE
- Infura Platform
- Introduction to Blockchain and Web3 | Blockchain Beginner Bootcamp by Chainlink
This project is licensed under the MIT License - see the LICENSE file for details.