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.
CDP 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
= w3.eth.contract(
AUTONITY_CONTRACT =AUTONITY_CONTRACT_ADDRESS,
address=ABIManager.load_abi("Autonity"),
abi
)
= autonity.config()["contracts"]["stabilization_contract"]
STABILIZATION_CONTRACT_ADDRESS = w3.eth.contract(
STABILIZATION_CONTRACT =STABILIZATION_CONTRACT_ADDRESS,
address=ABIManager.load_abi("Stabilization"),
abi )
CDP user operations
= w3.to_wei(1, "ether")
APPROVE_VALUE = w3.to_wei(1, "ether")
DEPOSIT_VALUE = w3.to_wei(5, "ether")
BORROW_VALUE = w3.to_wei(5, "ether")
REPAY_VALUE = w3.to_wei(0.5, "ether") WITHDRAW_VALUE
Approve the Stabilization contract
# Prepare function transaction.
= AUTONITY_CONTRACT.functions.approve(
function =STABILIZATION_CONTRACT_ADDRESS,
spender=APPROVE_VALUE,
amount
)= 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': 46316, 'gasPrice': 1459201755, 'maxFeePerGas': 1459201755, 'maxPriorityFeePerGas': 459201755, 'hash': HexBytes('0xb508229145a3add2073c74ecbed7b821e6d93745a69dd040d1ad95564f5e38b2'), 'input': '0x095ea7b300000000000000000000000029b2440db4a256b0c1e6d3b4cdcaa68e2440a08f0000000000000000000000000000000000000000000000000de0b6b3a7640000', 'nonce': 106, 'to': '0xBd770416a3345F91E4B34576cb804a576fa48EB1', 'transactionIndex': None, 'value': 0, 'type': 2, 'accessList': [], 'chainId': 65100001, 'v': 1, 'r': HexBytes('0x167506bea870d066fb408a5e60edacac520bcee7d20530b95b8be61096bc742e'), 's': HexBytes('0x6ab0d0c8cff108f8ac70f6f24217563d8aac15862da5475445978435f9424d37')})
Deposit NTN to the Stabilization contract
# Prepare function transaction.
= STABILIZATION_CONTRACT.functions.deposit(amount=DEPOSIT_VALUE)
function = 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': 60101, 'gasPrice': 1459201755, 'maxFeePerGas': 1459201755, 'maxPriorityFeePerGas': 459201755, 'hash': HexBytes('0xec68e05824081424b72126c9760c89c980de298f3ab7888c2e0a0bd366bcd912'), 'input': '0xb6b55f250000000000000000000000000000000000000000000000000de0b6b3a7640000', 'nonce': 107, 'to': '0x29b2440db4A256B0c1E6d3B4CDcaA68E2440A08f', 'transactionIndex': None, 'value': 0, 'type': 2, 'accessList': [], 'chainId': 65100001, 'v': 1, 'r': HexBytes('0xe7f6f62d2a85d812f976e1566cd4d840608732095fbf6a0073feebbbade1b066'), 's': HexBytes('0x4248fb57ea984111acfcb4dee8f8ae7eca7355a7c889c88672e442b9e91aff40')})
Check collateral price
= getattr(STABILIZATION_CONTRACT.functions, "collateralPrice", None)
stabilization_fn = stabilization_fn(*[]).call()
collateral_price print(w3.from_wei(collateral_price, "ether"))
10.1993872
Calculate borrow limit
= w3.to_wei(1, "ether")
collateral = w3.to_wei(1, "ether")
target_price = w3.to_wei(2, "ether") mcr
= getattr(STABILIZATION_CONTRACT.functions, "borrowLimit", None)
stabilization_fn = stabilization_fn(
borrow_limit *[
collateral,
collateral_price,
target_price,
mcr,
]
).call()print(w3.from_wei(borrow_limit, "ether"))
5.0996936
Borrow ATN from CDP
# Prepare function transaction.
= STABILIZATION_CONTRACT.functions.borrow(amount=BORROW_VALUE)
function = 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': 81180, 'gasPrice': 1459201755, 'maxFeePerGas': 1459201755, 'maxPriorityFeePerGas': 459201755, 'hash': HexBytes('0xc836631dde00ec21032f9fd1411b5469bac3b6ede4acf5b719cdbb37df1a3177'), 'input': '0xc5ebeaec0000000000000000000000000000000000000000000000004563918244f40000', 'nonce': 108, 'to': '0x29b2440db4A256B0c1E6d3B4CDcaA68E2440A08f', 'transactionIndex': None, 'value': 0, 'type': 2, 'accessList': [], 'chainId': 65100001, 'v': 1, 'r': HexBytes('0x93943ee8aebe46d40c8a9ca1f8ba5de4281beb2b2f66c8fafff203baa5b0fb30'), 's': HexBytes('0x12de72164d2fd9d8e0712f5c36f1bb9a3e2ca0aa6308b3440f266355ddb64ea6')})
Check debt amount.
= int(datetime.utcnow().replace(tzinfo=timezone.utc).timestamp())
UTCNOW
= getattr(STABILIZATION_CONTRACT.functions, "debtAmount", None)
stabilization_fn = stabilization_fn(*[ACCOUNT, UTCNOW]).call()
debt_amount print(w3.from_wei(debt_amount, "ether"))
5.000003995435385964
Repay ATN to CDP
# Prepare function transaction.
= STABILIZATION_CONTRACT.functions.repay()
function = function.build_transaction(
function_transaction
{"from": ACCOUNT,
"value": REPAY_VALUE,
}
)
# 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': 51491, 'gasPrice': 1459201755, 'maxFeePerGas': 1459201755, 'maxPriorityFeePerGas': 459201755, 'hash': HexBytes('0x1d15f38a01d834c3da95d0d49a397edcc6b11e7396f85d4d30d69380c2ef381b'), 'input': '0x402d8883', 'nonce': 109, 'to': '0x29b2440db4A256B0c1E6d3B4CDcaA68E2440A08f', 'transactionIndex': None, 'value': 5000000000000000000, 'type': 2, 'accessList': [], 'chainId': 65100001, 'v': 1, 'r': HexBytes('0xd600de37b6133a00fceb83e959b126d67ed817c81365245fbba101e7807ba6'), 's': HexBytes('0x659fd22e11ef695bd38459e41f9a3f23649e80071a312a6e121e11a8371ef7d1')})
Withdraw NTN from CDP
# Prepare function transaction.
= STABILIZATION_CONTRACT.functions.withdraw(amount=WITHDRAW_VALUE)
function = 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': 78355, 'gasPrice': 1459201755, 'maxFeePerGas': 1459201755, 'maxPriorityFeePerGas': 459201755, 'hash': HexBytes('0x1a4ef9002edb39ae4a476039f0bea86af2126533deae3f88deeaf91abfc699e6'), 'input': '0x2e1a7d4d00000000000000000000000000000000000000000000000006f05b59d3b20000', 'nonce': 110, 'to': '0x29b2440db4A256B0c1E6d3B4CDcaA68E2440A08f', 'transactionIndex': None, 'value': 0, 'type': 2, 'accessList': [], 'chainId': 65100001, 'v': 0, 'r': HexBytes('0x5dee7711aeeb24b8c3c3cdc59cf0027ab5ad20f1f9bbf2eeca80e10ab6c8373a'), 's': HexBytes('0x66beddb16f9e4fbe53b190a40122b696273b1e6f2e5803450243df7cd848e1f0')})
Check if CDP is liquidatable
= getattr(STABILIZATION_CONTRACT.functions, "isLiquidatable", None)
stabilization_fn = stabilization_fn(*[ACCOUNT]).call()
is_liquidatable print(is_liquidatable)
False
Liquidate a CDP
= ""
LIQUIDATE_ACCOUNT assert Web3.is_checksum_address(LIQUIDATE_ACCOUNT)
= w3.to_wei(0, "ether") LIQUIDATE_VALUE
# Prepare function transaction.
= STABILIZATION_CONTRACT.functions.liquidate(LIQUIDATE_ACCOUNT)
function = function.build_transaction(
function_transaction
{"from": ACCOUNT,
"value": LIQUIDATE_VALUE,
}
)
# Sent transaction.
= w3.eth.send_transaction(function_transaction)
tx_hash
print(w3.eth.get_transaction(tx_hash))