Added logging objects to classes.

This commit is contained in:
Geoff Taylor 2021-04-26 16:43:40 +01:00
parent add7f5648c
commit 1c6c81cc0d
7 changed files with 213 additions and 156 deletions

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "eleven-plenty",
"id": "instrumental-enlargement",
"metadata": {},
"source": [
"# ⚠ Warning\n",
@ -16,7 +16,7 @@
},
{
"cell_type": "markdown",
"id": "limiting-stanford",
"id": "alleged-creator",
"metadata": {},
"source": [
"# 🥭 AccountScout\n",
@ -28,7 +28,7 @@
},
{
"cell_type": "markdown",
"id": "documentary-darwin",
"id": "becoming-kingston",
"metadata": {},
"source": [
"## Required Accounts\n",
@ -47,7 +47,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "finished-today",
"id": "regulated-senate",
"metadata": {
"jupyter": {
"source_hidden": true
@ -55,6 +55,7 @@
},
"outputs": [],
"source": [
"import logging\n",
"import typing\n",
"\n",
"from solana.publickey import PublicKey\n",
@ -67,7 +68,7 @@
},
{
"cell_type": "markdown",
"id": "velvet-contributor",
"id": "varying-cocktail",
"metadata": {},
"source": [
"# ScoutReport class\n",
@ -78,7 +79,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "expensive-producer",
"id": "short-british",
"metadata": {},
"outputs": [],
"source": [
@ -141,7 +142,7 @@
},
{
"cell_type": "markdown",
"id": "dated-citizenship",
"id": "governing-intervention",
"metadata": {},
"source": [
"# AccountScout class\n",
@ -154,7 +155,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "unlimited-study",
"id": "peaceful-clear",
"metadata": {},
"outputs": [],
"source": [
@ -234,7 +235,7 @@
},
{
"cell_type": "markdown",
"id": "italian-demographic",
"id": "advised-stanford",
"metadata": {},
"source": [
"# 🏃 Running\n",
@ -247,7 +248,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "joint-savage",
"id": "mental-burns",
"metadata": {},
"outputs": [],
"source": [
@ -257,11 +258,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "rational-agriculture",
"id": "million-intelligence",
"metadata": {},
"outputs": [],
"source": [
"if __name__ == \"__main__\":\n",
" logging.basicConfig(level=logging.DEBUG)\n",
"\n",
" if ACCOUNT_TO_VERIFY == \"\":\n",
" raise Exception(\"No account to look up - try setting the variable ACCOUNT_TO_LOOK_UP to an account public key.\")\n",
"\n",

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "responsible-sample",
"id": "specific-litigation",
"metadata": {},
"source": [
"# ⚠ Warning\n",
@ -16,7 +16,7 @@
},
{
"cell_type": "markdown",
"id": "adequate-rotation",
"id": "brilliant-burlington",
"metadata": {},
"source": [
"# 🥭 Classes\n",
@ -33,7 +33,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "silver-prospect",
"id": "choice-sunset",
"metadata": {
"jupyter": {
"source_hidden": true
@ -43,6 +43,7 @@
"source": [
"import datetime\n",
"import enum\n",
"import logging\n",
"import typing\n",
"\n",
"import Layouts as layouts\n",
@ -60,7 +61,7 @@
},
{
"cell_type": "markdown",
"id": "developed-initial",
"id": "loaded-baltimore",
"metadata": {},
"source": [
"## Version enum\n",
@ -71,7 +72,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "clean-recorder",
"id": "likely-harmony",
"metadata": {},
"outputs": [],
"source": [
@ -86,7 +87,7 @@
},
{
"cell_type": "markdown",
"id": "noticed-immunology",
"id": "sustainable-liverpool",
"metadata": {},
"source": [
"## SerumAccountFlags class\n",
@ -97,13 +98,14 @@
{
"cell_type": "code",
"execution_count": null,
"id": "damaged-sport",
"id": "adaptive-light",
"metadata": {},
"outputs": [],
"source": [
"class SerumAccountFlags:\n",
" def __init__(self, version: Version, initialized: bool, market: bool, open_orders: bool, request_queue: bool,\n",
" event_queue: bool, bids: bool, asks: bool, disabled: bool):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.version: Version = version\n",
" self.initialized = initialized\n",
" self.market = market\n",
@ -139,7 +141,7 @@
},
{
"cell_type": "markdown",
"id": "massive-choice",
"id": "brilliant-block",
"metadata": {},
"source": [
"## MangoAccountFlags class\n",
@ -150,12 +152,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "leading-external",
"id": "assigned-acrylic",
"metadata": {},
"outputs": [],
"source": [
"class MangoAccountFlags:\n",
" def __init__(self, version: Version, initialized: bool, group: bool, margin_account: bool, srm_account: bool):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.version: Version = version\n",
" self.initialized = initialized\n",
" self.group = group\n",
@ -183,7 +186,7 @@
},
{
"cell_type": "markdown",
"id": "talented-therapy",
"id": "indoor-marking",
"metadata": {},
"source": [
"## Index class"
@ -192,12 +195,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "french-sunrise",
"id": "yellow-miami",
"metadata": {},
"outputs": [],
"source": [
"class Index:\n",
" def __init__(self, version: Version, last_update: datetime.datetime, borrow: Decimal, deposit: Decimal):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.version: Version = version\n",
" self.last_update: datetime.datetime = last_update\n",
" self.borrow: Decimal = borrow\n",
@ -218,7 +222,7 @@
},
{
"cell_type": "markdown",
"id": "fewer-eating",
"id": "special-paradise",
"metadata": {},
"source": [
"## AggregatorConfig class"
@ -227,7 +231,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "jewish-specification",
"id": "offshore-albuquerque",
"metadata": {},
"outputs": [],
"source": [
@ -235,6 +239,7 @@
" def __init__(self, version: Version, description: str, decimals: Decimal, restart_delay: Decimal,\n",
" max_submissions: Decimal, min_submissions: Decimal, reward_amount: Decimal,\n",
" reward_token_account: PublicKey):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.version: Version = version\n",
" self.description: str = description\n",
" self.decimals: Decimal = decimals\n",
@ -259,7 +264,7 @@
},
{
"cell_type": "markdown",
"id": "formal-language",
"id": "bright-constitution",
"metadata": {},
"source": [
"## Round class"
@ -268,12 +273,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "endangered-directive",
"id": "wooden-prerequisite",
"metadata": {},
"outputs": [],
"source": [
"class Round:\n",
" def __init__(self, version: Version, id: Decimal, created_at: datetime.datetime, updated_at: datetime.datetime):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.version: Version = version\n",
" self.id: Decimal = id\n",
" self.created_at: datetime.datetime = created_at\n",
@ -292,7 +298,7 @@
},
{
"cell_type": "markdown",
"id": "potential-reader",
"id": "intended-princess",
"metadata": {},
"source": [
"## Answer class"
@ -301,12 +307,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "naked-worker",
"id": "geological-merit",
"metadata": {},
"outputs": [],
"source": [
"class Answer:\n",
" def __init__(self, version: Version, round_id: Decimal, median: Decimal, created_at: datetime.datetime, updated_at: datetime.datetime):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.version: Version = version\n",
" self.round_id: Decimal = round_id\n",
" self.median: Decimal = median\n",
@ -326,7 +333,7 @@
},
{
"cell_type": "markdown",
"id": "residential-scientist",
"id": "regulated-worship",
"metadata": {},
"source": [
"## Aggregator class"
@ -335,7 +342,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "loaded-lecture",
"id": "mighty-amateur",
"metadata": {},
"outputs": [],
"source": [
@ -343,6 +350,7 @@
" def __init__(self, version: Version, config: AggregatorConfig, initialized: bool, name: str,\n",
" address: PublicKey, owner: PublicKey, round_: Round, round_submissions: PublicKey,\n",
" answer: Answer, answer_submissions: PublicKey):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.version: Version = version\n",
" self.config: AggregatorConfig = config\n",
" self.initialized: bool = initialized\n",
@ -401,7 +409,7 @@
},
{
"cell_type": "markdown",
"id": "later-belarus",
"id": "narrative-revision",
"metadata": {},
"source": [
"## BasketTokenMetadata class\n"
@ -410,12 +418,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "greatest-causing",
"id": "brief-occasions",
"metadata": {},
"outputs": [],
"source": [
"class BasketTokenMetadata:\n",
" def __init__(self, name: str, mint: PublicKey, decimals: Decimal, vault: PublicKey, index: Index):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.name: str = name\n",
" self.mint: PublicKey = mint\n",
" self.decimals: Decimal = decimals\n",
@ -438,7 +447,7 @@
},
{
"cell_type": "markdown",
"id": "intensive-regulation",
"id": "opposite-turkey",
"metadata": {},
"source": [
"## MarketMetadata class"
@ -447,13 +456,14 @@
{
"cell_type": "code",
"execution_count": null,
"id": "short-malawi",
"id": "extreme-marsh",
"metadata": {},
"outputs": [],
"source": [
"class MarketMetadata:\n",
" def __init__(self, name: str, address: PublicKey, base: BasketTokenMetadata, quote: BasketTokenMetadata,\n",
" spot: PublicKey, oracle: PublicKey, decimals: Decimal):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.name: str = name\n",
" self.address: PublicKey = address\n",
" self.base: BasketTokenMetadata = base\n",
@ -482,7 +492,7 @@
},
{
"cell_type": "markdown",
"id": "golden-intermediate",
"id": "worth-commons",
"metadata": {},
"source": [
"## Group class"
@ -491,7 +501,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "adaptive-protection",
"id": "front-pendant",
"metadata": {},
"outputs": [],
"source": [
@ -503,6 +513,7 @@
" total_deposits: typing.List[Decimal], total_borrows: typing.List[Decimal],\n",
" maint_coll_ratio: Decimal, init_coll_ratio: Decimal, srm_vault: PublicKey,\n",
" admin: PublicKey, borrow_limits: typing.List[Decimal]):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.version: Version = version\n",
" self.context: Context = context\n",
" self.address: PublicKey = address\n",
@ -609,7 +620,7 @@
},
{
"cell_type": "markdown",
"id": "neither-infrastructure",
"id": "sunrise-genome",
"metadata": {},
"source": [
"## TokenAccount class"
@ -618,12 +629,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "headed-sacrifice",
"id": "immune-struggle",
"metadata": {},
"outputs": [],
"source": [
"class TokenAccount:\n",
" def __init__(self, version: Version, mint: PublicKey, owner: PublicKey, amount: Decimal):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.version: Version = version\n",
" self.mint: PublicKey = mint\n",
" self.owner: PublicKey = owner\n",
@ -650,7 +662,7 @@
},
{
"cell_type": "markdown",
"id": "alleged-firmware",
"id": "indian-literacy",
"metadata": {},
"source": [
"## OpenOrders class\n"
@ -659,7 +671,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "sapphire-qualification",
"id": "separated-edinburgh",
"metadata": {},
"outputs": [],
"source": [
@ -670,6 +682,7 @@
" quote_token_total: Decimal, free_slot_bits: Decimal, is_bid_bits: Decimal,\n",
" orders: typing.List[Decimal], client_ids: typing.List[Decimal],\n",
" referrer_rebate_accrued: Decimal):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.version: Version = version\n",
" self.group: Group = group\n",
" self.address: PublicKey = address\n",
@ -759,7 +772,7 @@
},
{
"cell_type": "markdown",
"id": "primary-netherlands",
"id": "motivated-honey",
"metadata": {},
"source": [
"## BalanceSheet class"
@ -768,12 +781,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "stunning-carnival",
"id": "compliant-glasgow",
"metadata": {},
"outputs": [],
"source": [
"class BalanceSheet:\n",
" def __init__(self, token: typing.Optional[BasketTokenMetadata], liabilities: Decimal, settled_assets: Decimal, unsettled_assets: Decimal):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.token: typing.Optional[BasketTokenMetadata] = token\n",
" self.liabilities: Decimal = liabilities\n",
" self.settled_assets: Decimal = settled_assets\n",
@ -814,7 +828,7 @@
},
{
"cell_type": "markdown",
"id": "asian-donor",
"id": "funky-royal",
"metadata": {},
"source": [
"## MarginAccount class\n"
@ -823,7 +837,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "missing-eight",
"id": "intimate-kingston",
"metadata": {},
"outputs": [],
"source": [
@ -831,6 +845,7 @@
" def __init__(self, version: Version, address: PublicKey, account_flags: MangoAccountFlags,\n",
" mango_group: PublicKey, owner: PublicKey, deposits: typing.List[Decimal],\n",
" borrows: typing.List[Decimal], open_orders: typing.List[PublicKey]):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.version: Version = version\n",
" self.address: PublicKey = address\n",
" self.account_flags: MangoAccountFlags = account_flags\n",
@ -1005,7 +1020,7 @@
},
{
"cell_type": "markdown",
"id": "attended-backing",
"id": "robust-learning",
"metadata": {},
"source": [
"# 🏃 Running"
@ -1014,11 +1029,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "altered-danger",
"id": "retained-litigation",
"metadata": {},
"outputs": [],
"source": [
"if __name__ == \"__main__\":\n",
" logging.basicConfig(level=logging.DEBUG)\n",
"\n",
" import base64\n",
" from Context import default_context\n",
"\n",

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "characteristic-evidence",
"id": "composite-former",
"metadata": {},
"source": [
"# ⚠ Warning\n",
@ -16,7 +16,7 @@
},
{
"cell_type": "markdown",
"id": "regulated-forward",
"id": "accompanied-harvest",
"metadata": {},
"source": [
"# 🥭 Context\n",
@ -26,7 +26,7 @@
},
{
"cell_type": "markdown",
"id": "medium-gravity",
"id": "hydraulic-vampire",
"metadata": {},
"source": [
"## Environment Variables\n",
@ -41,7 +41,7 @@
},
{
"cell_type": "markdown",
"id": "continuous-service",
"id": "executive-wells",
"metadata": {},
"source": [
"## Provided Configured Objects\n",
@ -61,7 +61,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "under-millennium",
"id": "medical-gregory",
"metadata": {
"jupyter": {
"source_hidden": true
@ -69,6 +69,7 @@
},
"outputs": [],
"source": [
"import logging\n",
"import os\n",
"import typing\n",
"\n",
@ -85,7 +86,7 @@
},
{
"cell_type": "markdown",
"id": "naval-killing",
"id": "liked-picnic",
"metadata": {},
"source": [
"## AccountInfo class\n",
@ -96,12 +97,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "pleased-plaza",
"id": "japanese-intro",
"metadata": {},
"outputs": [],
"source": [
"class AccountInfo:\n",
" def __init__(self, address: PublicKey, executable: bool, lamports: Decimal, owner: PublicKey, rent_epoch: Decimal, data: bytes):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.address: PublicKey = address\n",
" self.executable: bool = executable\n",
" self.lamports: Decimal = lamports\n",
@ -139,7 +141,7 @@
},
{
"cell_type": "markdown",
"id": "separate-capacity",
"id": "built-insight",
"metadata": {},
"source": [
"## Context class"
@ -148,13 +150,14 @@
{
"cell_type": "code",
"execution_count": null,
"id": "blessed-biography",
"id": "disturbed-austria",
"metadata": {},
"outputs": [],
"source": [
"class Context:\n",
" def __init__(self, cluster: str, cluster_url: str, program_id: PublicKey, dex_program_id: PublicKey,\n",
" group_name: str, group_id: PublicKey):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.cluster: str = cluster\n",
" self.cluster_url: str = cluster_url\n",
" self.client: Client = Client(cluster_url)\n",
@ -278,7 +281,7 @@
},
{
"cell_type": "markdown",
"id": "rising-anthony",
"id": "fabulous-designation",
"metadata": {},
"source": [
"## default_context object\n",
@ -289,7 +292,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "victorian-gossip",
"id": "adequate-custom",
"metadata": {},
"outputs": [],
"source": [
@ -308,7 +311,7 @@
},
{
"cell_type": "markdown",
"id": "precious-twins",
"id": "vulnerable-prophet",
"metadata": {},
"source": [
"## solana_context object\n",
@ -319,7 +322,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "dramatic-indication",
"id": "electrical-chapter",
"metadata": {},
"outputs": [],
"source": [
@ -331,7 +334,7 @@
},
{
"cell_type": "markdown",
"id": "radical-avenue",
"id": "fuzzy-break",
"metadata": {},
"source": [
"## serum_context object\n",
@ -342,7 +345,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "infinite-giant",
"id": "little-exploration",
"metadata": {},
"outputs": [],
"source": [
@ -354,7 +357,7 @@
},
{
"cell_type": "markdown",
"id": "alone-stanford",
"id": "contrary-fiber",
"metadata": {},
"source": [
"# 🏃 Running\n",
@ -365,11 +368,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "noticed-aquarium",
"id": "boolean-spectacular",
"metadata": {},
"outputs": [],
"source": [
"if __name__ == \"__main__\":\n",
" logging.basicConfig(level=logging.DEBUG)\n",
"\n",
" print(default_context)\n",
"\n",
" single_account_info = default_context.load_account(default_context.dex_program_id)\n",

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "joint-pride",
"id": "compressed-rendering",
"metadata": {},
"source": [
"# ⚠ Warning\n",
@ -16,7 +16,7 @@
},
{
"cell_type": "markdown",
"id": "optical-effects",
"id": "southwest-trauma",
"metadata": {},
"source": [
"# 🥭 Instructions\n",
@ -27,11 +27,16 @@
{
"cell_type": "code",
"execution_count": null,
"id": "buried-colombia",
"metadata": {},
"id": "vocational-arabic",
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [],
"source": [
"import abc\n",
"import logging\n",
"import struct\n",
"import typing\n",
"\n",
@ -50,7 +55,7 @@
},
{
"cell_type": "markdown",
"id": "mobile-enforcement",
"id": "acceptable-sheep",
"metadata": {},
"source": [
"# InstructionBuilder class\n",
@ -61,12 +66,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "interstate-thirty",
"id": "arctic-robertson",
"metadata": {},
"outputs": [],
"source": [
"class InstructionBuilder(metaclass=abc.ABCMeta):\n",
" def __init__(self, context: Context):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.context = context\n",
"\n",
" @abc.abstractmethod\n",
@ -79,7 +85,7 @@
},
{
"cell_type": "markdown",
"id": "according-oriental",
"id": "native-manor",
"metadata": {},
"source": [
"# ForceCancelOrdersInstructionBuilder class"
@ -87,7 +93,7 @@
},
{
"cell_type": "markdown",
"id": "dying-camping",
"id": "unlikely-phone",
"metadata": {},
"source": [
"## Rust Interface\n",
@ -119,7 +125,7 @@
},
{
"cell_type": "markdown",
"id": "golden-falls",
"id": "theoretical-conversation",
"metadata": {},
"source": [
"## Client API call\n",
@ -163,7 +169,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "listed-armor",
"id": "national-behavior",
"metadata": {},
"outputs": [],
"source": [
@ -187,7 +193,7 @@
" self.dex_signer = dex_signer\n",
"\n",
" def build(self) -> TransactionInstruction:\n",
" return TransactionInstruction(\n",
" transaction = TransactionInstruction(\n",
" keys=[\n",
" AccountMeta(is_signer=False, is_writable=True, pubkey=self.group.address),\n",
" AccountMeta(is_signer=True, is_writable=False, pubkey=self.wallet.address),\n",
@ -211,6 +217,8 @@
" program_id=self.context.program_id,\n",
" data=FORCE_CANCEL.build({\"limit\": ForceCancelOrdersInstructionBuilder.max_cancels_per_instruction})\n",
" )\n",
" self.logger.debug(f\"Built transaction: {transaction}\")\n",
" return transaction\n",
"\n",
" @staticmethod\n",
" def from_margin_account_and_market(context: Context, group: Group, wallet: Wallet, margin_account: MarginAccount, market_metadata: MarketMetadata) -> \"ForceCancelOrdersInstructionBuilder\":\n",
@ -221,8 +229,10 @@
" \n",
" return ForceCancelOrdersInstructionBuilder(context, group, wallet, margin_account, market_metadata, market, oracles, dex_signer)\n",
"\n",
" @staticmethod\n",
" @classmethod\n",
" def multiple_instructions_from_margin_account_and_market(context: Context, group: Group, wallet: Wallet, margin_account: MarginAccount, market_metadata: MarketMetadata, at_least_this_many_cancellations: int) -> typing.List[\"ForceCancelOrdersInstructionBuilder\"]:\n",
" logger: logging.Logger = logging.getLogger(cls.__name__)\n",
"\n",
" # We cancel up to max_cancels_per_instruction orders with each instruction, but if\n",
" # we have more than cancel_limit we create more instructions (each handling up to\n",
" # 5 orders)\n",
@ -234,7 +244,9 @@
" instructions: typing.List[ForceCancelOrdersInstructionBuilder] = []\n",
" for counter in range(instruction_count):\n",
" instructions += [ForceCancelOrdersInstructionBuilder.from_margin_account_and_market(context, group, wallet, margin_account, market_metadata)]\n",
" \n",
"\n",
" logger.debug(f\"Built {len(instructions)} transaction(s).\")\n",
"\n",
" return instructions\n",
"\n",
" def __str__(self) -> str:\n",
@ -263,7 +275,7 @@
},
{
"cell_type": "markdown",
"id": "charged-plaintiff",
"id": "second-amino",
"metadata": {},
"source": [
"# LiquidateInstructionBuilder class\n",
@ -275,7 +287,7 @@
},
{
"cell_type": "markdown",
"id": "looking-western",
"id": "cutting-distance",
"metadata": {},
"source": [
"## Rust Interface\n",
@ -321,7 +333,7 @@
},
{
"cell_type": "markdown",
"id": "invisible-theater",
"id": "alternate-america",
"metadata": {},
"source": [
"## Client API call\n",
@ -358,7 +370,7 @@
},
{
"cell_type": "markdown",
"id": "incoming-proceeding",
"id": "caroline-liver",
"metadata": {},
"source": [
"## from_margin_account_and_market() function\n",
@ -379,7 +391,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "applied-exception",
"id": "creative-witness",
"metadata": {},
"outputs": [],
"source": [
@ -397,7 +409,7 @@
" self.maximum_input_amount: Decimal = maximum_input_amount\n",
"\n",
" def build(self) -> TransactionInstruction:\n",
" return TransactionInstruction(\n",
" transaction = TransactionInstruction(\n",
" keys=[\n",
" AccountMeta(is_signer=False, is_writable=True, pubkey=self.group.address),\n",
" AccountMeta(is_signer=True, is_writable=False, pubkey=self.wallet.address),\n",
@ -415,13 +427,17 @@
" program_id=self.context.program_id,\n",
" data=PARTIAL_LIQUIDATE.build({\"maxDeposit\": int(self.maximum_input_amount)})\n",
" )\n",
" self.logger.debug(f\"Built transaction: {transaction}\")\n",
" return transaction\n",
"\n",
" @classmethod\n",
" def from_margin_account_and_market(cls, context: Context, group: Group, wallet: Wallet, margin_account: MarginAccount, prices: typing.List[Decimal]) -> typing.Optional[\"LiquidateInstructionBuilder\"]:\n",
" logger: logging.Logger = logging.getLogger(cls.__name__)\n",
"\n",
" @staticmethod\n",
" def from_margin_account_and_market(context: Context, group: Group, wallet: Wallet, margin_account: MarginAccount, prices: typing.List[Decimal]) -> typing.Optional[\"LiquidateInstructionBuilder\"]:\n",
" oracles = list([mkt.oracle for mkt in group.markets])\n",
"\n",
" balance_sheets = margin_account.get_priced_balance_sheets(group, prices)\n",
" print(\"Priced balance sheets\", balance_sheets)\n",
" logger.debug(\"Priced balance sheets\", balance_sheets)\n",
"\n",
" sorted_by_assets = sorted(balance_sheets, key=lambda sheet: sheet.assets)\n",
" sorted_by_liabilities = sorted(balance_sheets, key=lambda sheet: sheet.liabilities)\n",
@ -434,11 +450,11 @@
" most_assets = sorted_by_assets[1]\n",
"\n",
" if most_assets.value == Decimal(0):\n",
" print(f\"Margin account {margin_account.address} has no assets to take.\")\n",
" logger.warning(f\"Margin account {margin_account.address} has no assets to take.\")\n",
" return None\n",
"\n",
" if most_liabilities.value == Decimal(0):\n",
" print(f\"Margin account {margin_account.address} has no liabilities to fund.\")\n",
" logger.warning(f\"Margin account {margin_account.address} has no liabilities to fund.\")\n",
" return None\n",
"\n",
" wallet_input_token_account = wallet.fetch_largest_token_account(context, most_liabilities.token.mint)\n",
@ -471,7 +487,7 @@
},
{
"cell_type": "markdown",
"id": "baking-genealogy",
"id": "after-measure",
"metadata": {},
"source": [
"# 🏃 Running"
@ -480,11 +496,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "ultimate-zimbabwe",
"id": "regulated-authentication",
"metadata": {},
"outputs": [],
"source": [
"if __name__ == \"__main__\":\n",
" logging.basicConfig(level=logging.DEBUG)\n",
"\n",
" import os.path\n",
"\n",
" filename = \"id.json\"\n",
@ -512,6 +530,7 @@
" prices = group.get_prices()\n",
"\n",
" liquidate = LiquidateInstructionBuilder.from_margin_account_and_market(default_context, group, wallet, margin_account, prices)\n",
" if liquidate is not None:\n",
" liquidate_instruction = liquidate.build()\n",
" print(\"LiquidateInstruction\", liquidate_instruction, \"Data:\", \" \".join(f\"{x:02x}\" for x in liquidate_instruction.data))\n"
]

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "experienced-klein",
"id": "indirect-preference",
"metadata": {},
"source": [
"# ⚠ Warning\n",
@ -16,7 +16,7 @@
},
{
"cell_type": "markdown",
"id": "postal-albert",
"id": "applied-spotlight",
"metadata": {},
"source": [
"# 🥭 Layouts\n",
@ -35,7 +35,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "external-canyon",
"id": "pleasant-butler",
"metadata": {},
"outputs": [],
"source": [
@ -50,7 +50,7 @@
},
{
"cell_type": "markdown",
"id": "knowing-astrology",
"id": "mobile-zambia",
"metadata": {},
"source": [
"# Adapters\n",
@ -60,7 +60,7 @@
},
{
"cell_type": "markdown",
"id": "chubby-truck",
"id": "minute-million",
"metadata": {},
"source": [
"## DecimalAdapter class\n",
@ -71,7 +71,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "warming-installation",
"id": "false-probability",
"metadata": {},
"outputs": [],
"source": [
@ -89,7 +89,7 @@
},
{
"cell_type": "markdown",
"id": "radio-business",
"id": "knowing-cedar",
"metadata": {},
"source": [
"## Float64Adapter class\n",
@ -105,7 +105,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "duplicate-induction",
"id": "educated-cooling",
"metadata": {},
"outputs": [],
"source": [
@ -124,7 +124,7 @@
},
{
"cell_type": "markdown",
"id": "known-jurisdiction",
"id": "expected-squad",
"metadata": {},
"source": [
"## PublicKeyAdapter\n",
@ -135,7 +135,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "desperate-speed",
"id": "vulnerable-forward",
"metadata": {},
"outputs": [],
"source": [
@ -152,7 +152,7 @@
},
{
"cell_type": "markdown",
"id": "speaking-debut",
"id": "eleven-pathology",
"metadata": {},
"source": [
"## DatetimeAdapter\n",
@ -163,7 +163,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "basic-reviewer",
"id": "vertical-anderson",
"metadata": {},
"outputs": [],
"source": [
@ -182,7 +182,7 @@
},
{
"cell_type": "markdown",
"id": "nonprofit-roberts",
"id": "living-harvey",
"metadata": {},
"source": [
"# Layout Structs"
@ -190,7 +190,7 @@
},
{
"cell_type": "markdown",
"id": "joined-fountain",
"id": "amber-headquarters",
"metadata": {},
"source": [
"## SERUM_ACCOUNT_FLAGS\n",
@ -217,7 +217,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "improved-pottery",
"id": "experimental-permission",
"metadata": {},
"outputs": [],
"source": [
@ -238,7 +238,7 @@
},
{
"cell_type": "markdown",
"id": "opened-injury",
"id": "expected-browser",
"metadata": {},
"source": [
"## MANGO_ACCOUNT_FLAGS\n",
@ -263,7 +263,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "respective-remove",
"id": "celtic-clearance",
"metadata": {},
"outputs": [],
"source": [
@ -280,7 +280,7 @@
},
{
"cell_type": "markdown",
"id": "absent-thailand",
"id": "noble-german",
"metadata": {},
"source": [
"## INDEX\n",
@ -300,7 +300,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "selected-contact",
"id": "sensitive-israel",
"metadata": {},
"outputs": [],
"source": [
@ -313,7 +313,7 @@
},
{
"cell_type": "markdown",
"id": "turkish-eight",
"id": "amateur-handle",
"metadata": {},
"source": [
"## AGGREGATOR_CONFIG\n",
@ -349,7 +349,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "individual-fusion",
"id": "biblical-canal",
"metadata": {},
"outputs": [],
"source": [
@ -366,7 +366,7 @@
},
{
"cell_type": "markdown",
"id": "treated-douglas",
"id": "harmful-empty",
"metadata": {},
"source": [
"## ROUND\n",
@ -385,7 +385,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "chinese-regulation",
"id": "proved-sunday",
"metadata": {},
"outputs": [],
"source": [
@ -398,7 +398,7 @@
},
{
"cell_type": "markdown",
"id": "entire-mentor",
"id": "julian-assessment",
"metadata": {},
"source": [
"## ANSWER\n",
@ -418,7 +418,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "monthly-pasta",
"id": "right-rally",
"metadata": {},
"outputs": [],
"source": [
@ -432,7 +432,7 @@
},
{
"cell_type": "markdown",
"id": "broadband-dealer",
"id": "raised-basin",
"metadata": {},
"source": [
"## AGGREGATOR\n",
@ -459,7 +459,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "ongoing-consent",
"id": "naval-dimension",
"metadata": {},
"outputs": [],
"source": [
@ -476,7 +476,7 @@
},
{
"cell_type": "markdown",
"id": "stretch-manhattan",
"id": "controlling-kenya",
"metadata": {},
"source": [
"## GROUP\n",
@ -522,7 +522,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "effective-glass",
"id": "unlikely-equality",
"metadata": {},
"outputs": [],
"source": [
@ -553,7 +553,7 @@
},
{
"cell_type": "markdown",
"id": "stable-catalyst",
"id": "built-superintendent",
"metadata": {},
"source": [
"## TOKEN_ACCOUNT"
@ -562,7 +562,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "provincial-nancy",
"id": "another-ireland",
"metadata": {},
"outputs": [],
"source": [
@ -576,7 +576,7 @@
},
{
"cell_type": "markdown",
"id": "loose-fellowship",
"id": "coordinated-distribution",
"metadata": {},
"source": [
"## OPEN_ORDERS\n",
@ -587,7 +587,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "adaptive-office",
"id": "selected-shadow",
"metadata": {},
"outputs": [],
"source": [
@ -611,7 +611,7 @@
},
{
"cell_type": "markdown",
"id": "eleven-mathematics",
"id": "operational-officer",
"metadata": {},
"source": [
"## MARGIN_ACCOUNT\n",
@ -643,7 +643,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "square-animation",
"id": "flush-notification",
"metadata": {},
"outputs": [],
"source": [
@ -660,7 +660,7 @@
},
{
"cell_type": "markdown",
"id": "egyptian-andrew",
"id": "compliant-joyce",
"metadata": {},
"source": [
"## FORCE_CANCEL"
@ -669,7 +669,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "differential-princess",
"id": "grand-contribution",
"metadata": {},
"outputs": [],
"source": [
@ -681,7 +681,7 @@
},
{
"cell_type": "markdown",
"id": "changing-madison",
"id": "frequent-timothy",
"metadata": {},
"source": [
"## PARTIAL_LIQUIDATE"
@ -690,7 +690,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "illegal-worthy",
"id": "satellite-drive",
"metadata": {},
"outputs": [],
"source": [
@ -702,7 +702,7 @@
},
{
"cell_type": "markdown",
"id": "roman-lecture",
"id": "unlimited-desperate",
"metadata": {},
"source": [
"# 🏃 Running"
@ -711,11 +711,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "northern-mauritius",
"id": "mysterious-spice",
"metadata": {},
"outputs": [],
"source": [
"if __name__ == \"__main__\":\n",
" logging.basicConfig(level=logging.DEBUG)\n",
"\n",
" import base64\n",
"\n",
" encoded = \"AwAAAAAAAACCaOmpoURMK6XHelGTaFawcuQ/78/15LAemWI8jrt3SRKLy2R9i60eclDjuDS8+p/ZhvTUd9G7uQVOYCsR6+BhmqGCiO6EPYP2PQkf/VRTvw7JjXvIjPFJy06QR1Cq1WfTonHl0OjCkyEf60SD07+MFJu5pVWNFGGEO/8AiAYfduaKdnFTaZEHPcK5Eq72WWHeHg2yIbBF09kyeOhlCJwOoG8O5SgpPV8QOA64ZNV4aKroFfADg6kEy/wWCdp3fv0O4GJgAAAAAPH6Ud6jtjwAAQAAAAAAAADiDkkCi9UOAAEAAAAAAAAADuBiYAAAAACNS5bSy7soAAEAAAAAAAAACMvgO+2jCwABAAAAAAAAAA7gYmAAAAAAZFeDUBNVhwABAAAAAAAAABtRNytozC8AAQAAAAAAAABIBGiCcyaEZdNhrTyeqUY692vOzzPdHaxAxguht3JQGlkzjtd05dX9LENHkl2z1XvUbTNKZlweypNRetmH0lmQ9VYQAHqylxZVK65gEg85g27YuSyvOBZAjJyRmYU9KdCO1D+4ehdPu9dQB1yI1uh75wShdAaFn2o4qrMYwq3SQQEAAAAAAAAAAiH1PPJKAuh6oGiE35aGhUQhFi/bxgKOudpFv8HEHNCFDy1uAqR6+CTQmradxC1wyyjL+iSft+5XudJWwSdi7wvphsxb96x7Obj/AgAAAAAKlV4LL5ow6r9LMhIAAAAADvsOtqcVFmChDPzPnwAAAE33lx1h8hPFD04AAAAAAAA8YRV3Oa309B2wGwAAAAAA+yPBZRlZz7b605n+AQAAAACgmZmZmZkZAQAAAAAAAAAAMDMzMzMzMwEAAAAAAAAA25D1XcAtRzSuuyx3U+X7aE9vM1EJySU9KprgL0LMJ/vat9+SEEUZuga7O5tTUrcMDYWDg+LYaAWhSQiN2fYk7aCGAQAAAAAAgIQeAAAAAAAA8gUqAQAAAAYGBgICAAAA\"\n",

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "thirty-prediction",
"id": "chubby-hindu",
"metadata": {},
"source": [
"# ⚠ Warning\n",
@ -16,7 +16,7 @@
},
{
"cell_type": "markdown",
"id": "minute-regulation",
"id": "respective-twins",
"metadata": {},
"source": [
"# 🥭 Liquidation\n",
@ -33,8 +33,12 @@
{
"cell_type": "code",
"execution_count": null,
"id": "necessary-spoke",
"metadata": {},
"id": "meaningful-northeast",
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [],
"source": [
"import time\n",
@ -51,7 +55,7 @@
},
{
"cell_type": "markdown",
"id": "forbidden-token",
"id": "olympic-foster",
"metadata": {},
"source": [
"## Collateralisation Ratios\n",
@ -69,7 +73,7 @@
},
{
"cell_type": "markdown",
"id": "relevant-burst",
"id": "authorized-vulnerability",
"metadata": {},
"source": [
"## Safety\n",
@ -83,7 +87,7 @@
},
{
"cell_type": "markdown",
"id": "liked-browser",
"id": "proof-challenge",
"metadata": {},
"source": [
"# 💧 Liquidation Process"
@ -91,7 +95,7 @@
},
{
"cell_type": "markdown",
"id": "impaired-mirror",
"id": "reverse-trout",
"metadata": {},
"source": [
"## Steps\n",
@ -119,12 +123,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "technological-stake",
"id": "urban-rainbow",
"metadata": {},
"outputs": [],
"source": [
"class Liquidator:\n",
" def __init__(self, context: Context, wallet: Wallet, group: Group):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.context = context\n",
" self.wallet = wallet\n",
" self.group = group\n",
@ -150,12 +155,12 @@
" transaction.add(liquidate_instruction.build())\n",
"\n",
" for instruction in transaction.instructions:\n",
" print(\"INSTRUCTION\")\n",
" print(\" Keys:\")\n",
" self.logger.debug(\"INSTRUCTION\")\n",
" self.logger.debug(\" Keys:\")\n",
" for key in instruction.keys:\n",
" print(\" \", f\"{key.pubkey}\".ljust(45), f\"{key.is_signer}\".ljust(6), f\"{key.is_writable}\".ljust(6))\n",
" print(\" Data:\", \" \".join(f\"{x:02x}\" for x in instruction.data))\n",
" print(\" Program ID:\", instruction.program_id)\n",
" self.logger.debug(\" \", f\"{key.pubkey}\".ljust(45), f\"{key.is_signer}\".ljust(6), f\"{key.is_writable}\".ljust(6))\n",
" self.logger.debug(\" Data:\", \" \".join(f\"{x:02x}\" for x in instruction.data))\n",
" self.logger.debug(\" Program ID:\", instruction.program_id)\n",
"\n",
" transaction_response = self.context.client.send_transaction(transaction, wallet.account)\n",
" return transaction_response[\"result\"]\n",
@ -164,7 +169,7 @@
},
{
"cell_type": "markdown",
"id": "supreme-specialist",
"id": "settled-detroit",
"metadata": {},
"source": [
"# 🏃 Running"
@ -173,7 +178,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "governmental-february",
"id": "intimate-stable",
"metadata": {},
"outputs": [],
"source": [
@ -183,11 +188,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "environmental-calgary",
"id": "knowing-diary",
"metadata": {},
"outputs": [],
"source": [
"if __name__ == \"__main__\":\n",
" logging.basicConfig(level=logging.DEBUG)\n",
"\n",
" if MARGIN_ACCOUNT_TO_LIQUIDATE == \"\":\n",
" raise Exception(\"No margin account to liquidate - try setting the variable MARGIN_ACCOUNT_TO_LIQUIDATE to a margin account public key.\")\n",
"\n",

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "awful-usage",
"id": "fifth-yield",
"metadata": {},
"source": [
"# ⚠ Warning\n",
@ -16,7 +16,7 @@
},
{
"cell_type": "markdown",
"id": "dental-federation",
"id": "cardiovascular-heating",
"metadata": {},
"source": [
"# 🥭 Wallet\n",
@ -29,7 +29,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "controversial-introduction",
"id": "irish-illinois",
"metadata": {
"jupyter": {
"source_hidden": true
@ -38,6 +38,7 @@
"outputs": [],
"source": [
"import json\n",
"import logging\n",
"import typing\n",
"\n",
"from solana.account import Account\n",
@ -48,7 +49,7 @@
},
{
"cell_type": "markdown",
"id": "directed-momentum",
"id": "relative-notebook",
"metadata": {},
"source": [
"## Wallet class\n",
@ -67,12 +68,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "violent-angel",
"id": "female-bailey",
"metadata": {},
"outputs": [],
"source": [
"class Wallet:\n",
" def __init__(self, secret_key):\n",
" self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n",
" self.secret_key = secret_key[0:32]\n",
" self.account = Account(self.secret_key)\n",
"\n",
@ -93,7 +95,7 @@
},
{
"cell_type": "markdown",
"id": "identified-investor",
"id": "alpine-standing",
"metadata": {},
"source": [
"# 🏃 Running\n",
@ -106,11 +108,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "incorporated-river",
"id": "related-thanks",
"metadata": {},
"outputs": [],
"source": [
"if __name__ == \"__main__\":\n",
" logging.basicConfig(level=logging.DEBUG)\n",
"\n",
" import os.path\n",
" from Context import default_context\n",
"\n",