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
RPC_ENDPOINT = "https://rpc1.piccadilly.autonity.org"Account constants
ACCOUNT = "<YOUR-ACCOUNT-ADDRESS>"
assert Web3.is_checksum_address(ACCOUNT)
KEYFILE = "<YOUR-LOCAL-KEYFILE-ADDRESS>"
PASSPHRASE = "<YOUR-ACCOUNT-PASSPHRASE>"Generate private key
with open(KEYFILE) as keyfile:
keyfile_contents = keyfile.read()
PRIVATE_KEY = eth_keys.keys.PrivateKey(
account.Account.decrypt(keyfile_contents, PASSPHRASE)
)Create Web3
w3 = Web3(provider=Web3.HTTPProvider(RPC_ENDPOINT))
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(Account.from_key(PRIVATE_KEY))
)Create contract
NTN_CONTRACT = w3.eth.contract(
address=AUTONITY_CONTRACT_ADDRESS,
abi=ABIManager.load_abi("IERC20"),
)Check account balances
Check ATN balance
atn_balance = w3.eth.get_balance(ACCOUNT)
print(w3.from_wei(atn_balance, "ether"))493.813856717014311982
Check NTN balance
ntn_balance = NTN_CONTRACT.functions.balanceOf(ACCOUNT).call()
print(w3.from_wei(ntn_balance, "ether"))89.85863947801900256
Submit transactions
RECIPIENT = "<YOUR-RECIPIENT-ADDRESS>"
assert Web3.is_checksum_address(RECIPIENT)
ATN_VALUE = w3.to_wei(0.01, "ether")
NTN_VALUE = w3.to_wei(0.01, "ether")Submit an ATN transaction
# Prepare transaction.
transaction = {
"from": ACCOUNT,
"to": RECIPIENT,
"value": ATN_VALUE,
}
# Sent transaction.
tx_hash = w3.eth.send_transaction(transaction)
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.
function = NTN_CONTRACT.functions.transfer(
recipient=RECIPIENT, amount=NTN_VALUE
)
function_transaction = function.build_transaction({"from": ACCOUNT})
# Sent transaction.
tx_hash = w3.eth.send_transaction(function_transaction)
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')})