import eth_keys
from eth_account import account, Account
from web3 import Web3
from web3.middleware import construct_sign_and_send_raw_middleware
from autonity.abi_manager import ABIManager
from autonity import AUTONITY_CONTRACT_ADDRESS
The Piccadilly Circus Games have now finished.
You are viewing an archive of the Piccadilly Circus Games Competition. Join our Discord for the latest information.
Starter Recipes
Preliminary
Network constants
= "https://rpc1.piccadilly.autonity.org" RPC_ENDPOINT
Account constants
= "<YOUR-ACCOUNT-ADDRESS>"
ACCOUNT assert Web3.is_checksum_address(ACCOUNT)
= "<YOUR-LOCAL-KEYFILE-ADDRESS>"
KEYFILE = "<YOUR-ACCOUNT-PASSPHRASE>" PASSPHRASE
Generate private key
with open(KEYFILE) as keyfile:
= keyfile.read()
keyfile_contents = eth_keys.keys.PrivateKey(
PRIVATE_KEY
account.Account.decrypt(keyfile_contents, PASSPHRASE) )
Create Web3
= Web3(provider=Web3.HTTPProvider(RPC_ENDPOINT))
w3
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(Account.from_key(PRIVATE_KEY)) )
Create contract
= w3.eth.contract(
NTN_CONTRACT =AUTONITY_CONTRACT_ADDRESS,
address=ABIManager.load_abi("IERC20"),
abi )
Check account balances
Check ATN balance
= w3.eth.get_balance(ACCOUNT)
atn_balance print(w3.from_wei(atn_balance, "ether"))
493.813856717014311982
Check NTN balance
= NTN_CONTRACT.functions.balanceOf(ACCOUNT).call()
ntn_balance print(w3.from_wei(ntn_balance, "ether"))
89.85863947801900256
Submit transactions
= "<YOUR-RECIPIENT-ADDRESS>"
RECIPIENT assert Web3.is_checksum_address(RECIPIENT)
= w3.to_wei(0.01, "ether")
ATN_VALUE = w3.to_wei(0.01, "ether") NTN_VALUE
Submit an ATN transaction
# Prepare transaction.
= {
transaction "from": ACCOUNT,
"to": RECIPIENT,
"value": ATN_VALUE,
}
# Sent transaction.
= w3.eth.send_transaction(transaction)
tx_hash
print(w3.eth.get_transaction(tx_hash))
AttributeDict({'blockHash': None, 'blockNumber': None, 'from': '0xfd1ac0e99E9BD153F49080A96eb44843211E5C9f', 'gas': 21000, 'gasPrice': 1459201755, 'maxFeePerGas': 1459201755, 'maxPriorityFeePerGas': 459201755, 'hash': HexBytes('0x606c007733a0734ae19b572d529beae21c0a7a464ef333a4ce9372506a4d7f6c'), 'input': '0x', 'nonce': 98, 'to': '0x2C5DFC8BCf08f795A83bE86698cb12790b270E67', 'transactionIndex': None, 'value': 10000000000000000, 'type': 2, 'accessList': [], 'chainId': 65100001, 'v': 1, 'r': HexBytes('0x08165e957c27cb7a78ca82707b14c9a2ed114d9a458db1047d85992dacda9649'), 's': HexBytes('0x736c074696c84358af542e309df54bf434ad0e35aa5ea538ddb67fedb854fa81')})
Submit a NTN transaction
# Prepare function transaction.
= NTN_CONTRACT.functions.transfer(
function =RECIPIENT, amount=NTN_VALUE
recipient
)= function.build_transaction({"from": ACCOUNT})
function_transaction
# Sent transaction.
= w3.eth.send_transaction(function_transaction)
tx_hash
print(w3.eth.get_transaction(tx_hash))
AttributeDict({'blockHash': None, 'blockNumber': None, 'from': '0xfd1ac0e99E9BD153F49080A96eb44843211E5C9f', 'gas': 34603, 'gasPrice': 1459201755, 'maxFeePerGas': 1459201755, 'maxPriorityFeePerGas': 459201755, 'hash': HexBytes('0x1f74ca25d86542b86b6ba55f76d903370eb491d6ce4bc3fc2f342a3ae10fe3e7'), 'input': '0xa9059cbb0000000000000000000000002c5dfc8bcf08f795a83be86698cb12790b270e67000000000000000000000000000000000000000000000000002386f26fc10000', 'nonce': 99, 'to': '0xBd770416a3345F91E4B34576cb804a576fa48EB1', 'transactionIndex': None, 'value': 0, 'type': 2, 'accessList': [], 'chainId': 65100001, 'v': 0, 'r': HexBytes('0xba01c1e23b659de3c0f1d7626914acec3a30c783d9100ae0803047e62ddb4f74'), 's': HexBytes('0x07d2609a7820dc48a07d9e607624e10af5a9be96acd6149aed080bc21275de7a')})