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
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 and Autonity
w3 = Web3(provider=Web3.HTTPProvider(RPC_ENDPOINT))
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(Account.from_key(PRIVATE_KEY))
)
autonity = Autonity(w3)Create contracts
For DAX contract address and abi, please visit: https://game.autonity.org/getting-started/exchange-dax.html
AUTONITY_CONTRACT = w3.eth.contract(
address=AUTONITY_CONTRACT_ADDRESS,
abi=ABIManager.load_abi("Autonity"),
)
NTN_CONTRACT = w3.eth.contract(
address=AUTONITY_CONTRACT_ADDRESS,
abi=ABIManager.load_abi("IERC20"),
)
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:
ROUTER_CONTRACT_ABI = json.load(abi_f)
ROUTER_CONTRACT = w3.eth.contract(
address=ROUTER_CONTRACT_ADDRESS,
abi=ROUTER_CONTRACT_ABI,
)DAX user operations
DEPOSIT_VALUE = w3.to_wei(1, "ether")
APPROVE_VALUE = w3.to_wei(1, "ether")
SWAP_VALUE = w3.to_wei(1, "ether")Approve a router to do swaps for NTN (if swapping NTN for ATN)
# Prepare function transaction.
function = NTN_CONTRACT.functions.approve(
ROUTER_CONTRACT_ADDRESS, APPROVE_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': '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.
UTCNOW = datetime.utcnow().replace(tzinfo=timezone.utc).timestamp()
DEADLINE = int(UTCNOW) + 20
# Prepare function transaction.
function = ROUTER_CONTRACT.functions.swapExactETHForTokens(
amountOutMin=0,
path=[
WATN_CONTRACT_ADDRESS,
AUTONITY_CONTRACT_ADDRESS,
],
to=ACCOUNT,
deadline=DEADLINE,
)
function_transaction = function.build_transaction({"from": ACCOUNT, "value": SWAP_VALUE})
# Sent transaction.
tx_hash = w3.eth.send_transaction(function_transaction)
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.
UTCNOW = datetime.utcnow().replace(tzinfo=timezone.utc).timestamp()
DEADLINE = int(UTCNOW) + 20
# Prepare function transaction.
function = ROUTER_CONTRACT.functions.swapExactTokensForETH(
amountIn=SWAP_VALUE,
amountOutMin=0,
path=[
AUTONITY_CONTRACT_ADDRESS,
WATN_CONTRACT_ADDRESS,
],
to=ACCOUNT,
deadline=DEADLINE,
)
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': '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')})