Getting Started
Python
Connect to PulseChain using web3.py.
Python (web3.py)
Connect to PulseChain using web3.py.
Setup
pip install web3
Basic usage
from web3 import Web3
w3 = Web3(Web3.HTTPProvider("https://rpc.pulsechain.box"))
# Verify connection
print("Connected:", w3.is_connected())
print("Chain ID:", w3.eth.chain_id) # 369
# Get latest block number
block_number = w3.eth.block_number
print("Block:", block_number)
# Get native PLS balance
balance_wei = w3.eth.get_balance("0x3693693693693693693693693693693693693693")
balance_pls = w3.from_wei(balance_wei, "ether")
print(f"Balance: {balance_pls} PLS")
Read a contract
ERC20_ABI = [
{"name": "name", "type": "function", "inputs": [], "outputs": [{"type": "string"}], "stateMutability": "view"},
{"name": "symbol", "type": "function", "inputs": [], "outputs": [{"type": "string"}], "stateMutability": "view"},
{"name": "decimals", "type": "function", "inputs": [], "outputs": [{"type": "uint8"}], "stateMutability": "view"},
{"name": "balanceOf", "type": "function", "inputs": [{"name": "account", "type": "address"}], "outputs": [{"type": "uint256"}], "stateMutability": "view"},
]
# WPLS contract
wpls = w3.eth.contract(
address="0xA1077a294dDE1B09bB078844df40758a5D0f9a27",
abi=ERC20_ABI
)
name = wpls.functions.name().call()
symbol = wpls.functions.symbol().call()
decimals = wpls.functions.decimals().call()
balance = wpls.functions.balanceOf("0x3693693693693693693693693693693693693693").call()
print(f"{name} ({symbol}): {balance / 10**decimals}")
Send a transaction
from web3.middleware import ExtraDataToPOAMiddleware
# PulseChain uses PoS, inject POA middleware
w3.middleware_onion.inject(ExtraDataToPOAMiddleware, layer=0)
account = w3.eth.account.from_key("YOUR_PRIVATE_KEY")
tx = {
"from": account.address,
"to": "0xRECIPIENT_ADDRESS",
"value": w3.to_wei(1, "ether"),
"gas": 21000,
"gasPrice": w3.eth.gas_price,
"nonce": w3.eth.get_transaction_count(account.address),
"chainId": 369,
}
signed = account.sign_transaction(tx)
tx_hash = w3.eth.send_raw_transaction(signed.raw_transaction)
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print(f"Confirmed in block {receipt['blockNumber']}")
Get logs
# Find recent WPLS Transfer events
transfer_topic = w3.keccak(text="Transfer(address,address,uint256)")
logs = w3.eth.get_logs({
"fromBlock": w3.eth.block_number - 100,
"toBlock": "latest",
"address": "0xA1077a294dDE1B09bB078844df40758a5D0f9a27",
"topics": [transfer_topic.hex()]
})
for log in logs:
print(f"Block {log['blockNumber']}: {log['transactionHash'].hex()}")
