import json
from datetime import datetime, timezone
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 import Autonity
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.
DAX 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 and Autonity
= Web3(provider=Web3.HTTPProvider(RPC_ENDPOINT))
w3
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(Account.from_key(PRIVATE_KEY))
)= Autonity(w3) autonity
Create contracts
For DAX contract address and abi, please visit: https://game.autonity.org/getting-started/exchange-dax.html
= w3.eth.contract(
AUTONITY_CONTRACT =AUTONITY_CONTRACT_ADDRESS,
address=ABIManager.load_abi("Autonity"),
abi
)
= w3.eth.contract(
NTN_CONTRACT =AUTONITY_CONTRACT_ADDRESS,
address=ABIManager.load_abi("IERC20"),
abi
)
= "<WATN-CONTRACT-ADDRESS>"
WATN_CONTRACT_ADDRESS assert Web3.is_checksum_address(WATN_CONTRACT_ADDRESS)
= "<ROUTER-CONTRACT-ADDRESS>"
ROUTER_CONTRACT_ADDRESS assert Web3.is_checksum_address(ROUTER_CONTRACT_ADDRESS)
with open("uniswapV2_router.abi", "r", encoding="utf8") as abi_f:
= json.load(abi_f)
ROUTER_CONTRACT_ABI = w3.eth.contract(
ROUTER_CONTRACT =ROUTER_CONTRACT_ADDRESS,
address=ROUTER_CONTRACT_ABI,
abi )
DAX user operations
= w3.to_wei(1, "ether")
DEPOSIT_VALUE = w3.to_wei(1, "ether")
APPROVE_VALUE = w3.to_wei(1, "ether") SWAP_VALUE
Approve a router to do swaps for NTN (if swapping NTN for ATN)
# Prepare function transaction.
= NTN_CONTRACT.functions.approve(
function
ROUTER_CONTRACT_ADDRESS, APPROVE_VALUE
)= 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': '0xA6eF168b726CcBbeea56062F89a7DB8e2059846F', 'gas': 46316, 'gasPrice': 1700000000, 'maxFeePerGas': 1700000000, 'maxPriorityFeePerGas': 700000000, 'hash': HexBytes('0x01b611076d7877033ba575180dea28eebbde156e9931042026a54e9f961ec43b'), 'input': HexBytes('0x095ea7b3000000000000000000000000374b9eaca19203ace83ef549c16890f545a1237b0000000000000000000000000000000000000000000000000de0b6b3a7640000'), 'nonce': 468, 'to': '0xBd770416a3345F91E4B34576cb804a576fa48EB1', 'transactionIndex': None, 'value': 0, 'type': 2, 'accessList': [], 'chainId': 65100002, 'v': 1, 'r': HexBytes('0x74c33946a600290ebc3ec465c1512aae11505adc41ce2eb2f4b6f99392541b69'), 's': HexBytes('0x473535fa7985dd45e9975c12dec6565f4e79d4a29d28aa0629904f311d99806d')})
Swap ATN for NTN
In this example, swap 1 ATN for certain amount of NTN.
= datetime.utcnow().replace(tzinfo=timezone.utc).timestamp()
UTCNOW = int(UTCNOW) + 20
DEADLINE
# Prepare function transaction.
= ROUTER_CONTRACT.functions.swapExactETHForTokens(
function =0,
amountOutMin=[
path
WATN_CONTRACT_ADDRESS,
AUTONITY_CONTRACT_ADDRESS,
],=ACCOUNT,
to=DEADLINE,
deadline
)= function.build_transaction({"from": ACCOUNT, "value": SWAP_VALUE})
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': '0xA6eF168b726CcBbeea56062F89a7DB8e2059846F', 'gas': 125190, 'gasPrice': 1700000000, 'maxFeePerGas': 1700000000, 'maxPriorityFeePerGas': 700000000, 'hash': HexBytes('0x7a5d3ef286b9c9b3279f78a762c93ae19ded4ddc8ac7df23928c494c5d46e109'), 'input': HexBytes('0x7ff36ab500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000a6ef168b726ccbbeea56062f89a7db8e2059846f00000000000000000000000000000000000000000000000000000000662667cf0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000ce17e51ce4f0417a1ab31a3c5d6831ff3bbfa1d2000000000000000000000000bd770416a3345f91e4b34576cb804a576fa48eb1'), 'nonce': 469, 'to': '0x374B9eacA19203ACE83EF549C16890f545A1237b', 'transactionIndex': None, 'value': 1000000000000000000, 'type': 2, 'accessList': [], 'chainId': 65100002, 'v': 0, 'r': HexBytes('0x332c2596e2cd602ec6a93c70bfb79572500e0e452552521ce7c362c013998f67'), 's': HexBytes('0x2bd0b87e379ee8ff0e6a527f359a3280b8b7c7cdeaf30a6dc434045b49866db3')})
Swap NTN for ATN
In this example, swap 1 NTN for certain amount of ATN.
= datetime.utcnow().replace(tzinfo=timezone.utc).timestamp()
UTCNOW = int(UTCNOW) + 20
DEADLINE
# Prepare function transaction.
= ROUTER_CONTRACT.functions.swapExactTokensForETH(
function =SWAP_VALUE,
amountIn=0,
amountOutMin=[
path
AUTONITY_CONTRACT_ADDRESS,
WATN_CONTRACT_ADDRESS,
],=ACCOUNT,
to=DEADLINE,
deadline
)= 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': '0xA6eF168b726CcBbeea56062F89a7DB8e2059846F', 'gas': 143214, 'gasPrice': 1700000000, 'maxFeePerGas': 1700000000, 'maxPriorityFeePerGas': 700000000, 'hash': HexBytes('0xde3f33f8cb00361f88614bb19e48ddfaf704dc203d8c07fa5998cc2b712dcde8'), 'input': HexBytes('0x18cbafe50000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000a6ef168b726ccbbeea56062f89a7db8e2059846f00000000000000000000000000000000000000000000000000000000662667cf0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000bd770416a3345f91e4b34576cb804a576fa48eb1000000000000000000000000ce17e51ce4f0417a1ab31a3c5d6831ff3bbfa1d2'), 'nonce': 470, 'to': '0x374B9eacA19203ACE83EF549C16890f545A1237b', 'transactionIndex': None, 'value': 0, 'type': 2, 'accessList': [], 'chainId': 65100002, 'v': 0, 'r': HexBytes('0x9d661cb174b5055a5448762db7534885ded3ebad94a65fe722f1e619d8f30be0'), 's': HexBytes('0x66b133ea40e30c6fd8ecd1a9ad0677c6c95b9db58116ac1566679ae84e1ed0e3')})