You are viewing an archive of the Piccadilly Circus Games Competition. Join our Discord for the latest information.

CDP Recipes

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

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

AUTONITY_CONTRACT = w3.eth.contract(
    address=AUTONITY_CONTRACT_ADDRESS,
    abi=ABIManager.load_abi("Autonity"),
)

STABILIZATION_CONTRACT_ADDRESS = autonity.config()["contracts"]["stabilization_contract"]
STABILIZATION_CONTRACT = w3.eth.contract(
    address=STABILIZATION_CONTRACT_ADDRESS,
    abi=ABIManager.load_abi("Stabilization"),
)

CDP user operations

APPROVE_VALUE = w3.to_wei(1, "ether")
DEPOSIT_VALUE = w3.to_wei(1, "ether")
BORROW_VALUE = w3.to_wei(5, "ether")
REPAY_VALUE = w3.to_wei(5, "ether")
WITHDRAW_VALUE = w3.to_wei(0.5, "ether")

Approve the Stabilization contract

# Prepare function transaction.
function = AUTONITY_CONTRACT.functions.approve(
    spender=STABILIZATION_CONTRACT_ADDRESS,
    amount=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': '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.
function = STABILIZATION_CONTRACT.functions.deposit(amount=DEPOSIT_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': 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

stabilization_fn = getattr(STABILIZATION_CONTRACT.functions, "collateralPrice", None)
collateral_price = stabilization_fn(*[]).call()
print(w3.from_wei(collateral_price, "ether"))
10.1993872

Calculate borrow limit

collateral = w3.to_wei(1, "ether")
target_price = w3.to_wei(1, "ether")
mcr = w3.to_wei(2, "ether")
stabilization_fn = getattr(STABILIZATION_CONTRACT.functions, "borrowLimit", None)
borrow_limit = stabilization_fn(
    *[
        collateral,
        collateral_price,
        target_price,
        mcr,
    ]
).call()
print(w3.from_wei(borrow_limit, "ether"))
5.0996936

Borrow ATN from CDP

# Prepare function transaction.
function = STABILIZATION_CONTRACT.functions.borrow(amount=BORROW_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': 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.

UTCNOW = int(datetime.utcnow().replace(tzinfo=timezone.utc).timestamp())

stabilization_fn = getattr(STABILIZATION_CONTRACT.functions, "debtAmount", None)
debt_amount = stabilization_fn(*[ACCOUNT, UTCNOW]).call()
print(w3.from_wei(debt_amount, "ether"))
5.000003995435385964

Repay ATN to CDP

# Prepare function transaction.
function = STABILIZATION_CONTRACT.functions.repay()
function_transaction = function.build_transaction(
    {
        "from": ACCOUNT,
        "value": REPAY_VALUE,
    }
)

# 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': 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.
function = STABILIZATION_CONTRACT.functions.withdraw(amount=WITHDRAW_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': 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

stabilization_fn = getattr(STABILIZATION_CONTRACT.functions, "isLiquidatable", None)
is_liquidatable = stabilization_fn(*[ACCOUNT]).call()
print(is_liquidatable)
False

Liquidate a CDP

LIQUIDATE_ACCOUNT = ""
assert Web3.is_checksum_address(LIQUIDATE_ACCOUNT)
LIQUIDATE_VALUE = w3.to_wei(0, "ether")
# Prepare function transaction.
function = STABILIZATION_CONTRACT.functions.liquidate(LIQUIDATE_ACCOUNT)
function_transaction = function.build_transaction(
    {
        "from": ACCOUNT,
        "value": LIQUIDATE_VALUE,
    }
)

# Sent transaction.
tx_hash = w3.eth.send_transaction(function_transaction)

print(w3.eth.get_transaction(tx_hash))