Skip to content

Instantly share code, notes, and snippets.

@martinvol
Created June 10, 2020 17:43
Show Gist options
  • Save martinvol/0126a44562e96c4f57568c92be97b95f to your computer and use it in GitHub Desktop.
Save martinvol/0126a44562e96c4f57568c92be97b95f to your computer and use it in GitHub Desktop.
Connecting to the Celo Network from Python using web3
import web3
min_registry_abi = [
{
"name": "getAddressForString",
"constant": True,
"inputs": [{
"type": "string"
}],
"outputs": [{
"type": "address"
}],
"type": "function"
},
]
min_reserve_abi = [{
"name": "getReserveGoldBalance",
"constant": True,
"inputs": [],
"outputs": [{
"type": "uint256"
}],
"type": "function"
}, {
"name": "getUnfrozenReserveGoldBalance",
"constant": True,
"inputs": [],
"outputs": [{
"type": "uint256"
}],
"type": "function"
}, {
"name": "getAssetAllocationSymbols",
"constant": True,
"inputs": [],
"outputs": [{
"type": "bytes32[]"
}],
"type": "function"
}, {
"name": "getAssetAllocationWeights",
"constant": True,
"inputs": [],
"outputs": [{
"type": "uint256[]"
}],
"type": "function"
}]
_REGISTRY_ADDR = '0x000000000000000000000000000000000000ce10'
_CGLD_DECIMAL_UNITS = 18
node_url = "https://rc1-forno.celo-testnet.org"
_w3 = web3.Web3(web3.Web3.HTTPProvider(node_url))
_w3.middleware_onion.inject(web3.middleware.geth_poa_middleware, layer=0)
_registry_contract = _w3.eth.contract(address=_REGISTRY_ADDR, abi=min_registry_abi)
_reserve_addr = _registry_contract.functions.getAddressForString('Reserve').call()
_reserve_contract = _w3.eth.contract(address=_reserve_addr, abi=min_reserve_abi)
last_block = _w3.eth.getBlock(_w3.eth.blockNumber)
print(f"Last block in node is #{last_block}")
reserve_frozen_gold_balance_ = _reserve_contract.functions.getUnfrozenReserveGoldBalance().call() / pow(10, _CGLD_DECIMAL_UNITS)
print(f"Unfrozen Balance in reserve is: {reserve_frozen_gold_balance_}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment