-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkeepyr_utils.py
225 lines (171 loc) · 6.97 KB
/
keepyr_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import math
import time
from typing import Optional, Union
from dataclasses import dataclass
from solana.rpc.core import _COMMITMENT_TO_SOLDERS
from solders.transaction import VersionedTransaction # type: ignore
from solders.transaction_status import TransactionErrorType # type: ignore
from solders.instruction import Instruction # type: ignore
from solders.address_lookup_table_account import AddressLookupTableAccount # type: ignore
from solders.pubkey import Pubkey # type: ignore
from solders.keypair import Keypair # type: ignore
from solders.compute_budget import set_compute_unit_limit # type: ignore
from solders.rpc.config import RpcSimulateTransactionConfig # type: ignore
from solders.rpc.requests import SimulateVersionedTransaction # type: ignore
from solders.rpc.responses import SimulateTransactionResp # type: ignore
from driftpy.drift_client import DriftClient
from driftpy.tx.standard_tx_sender import StandardTxSender
from driftpy.dlob.dlob import DLOB, NodeToFill, NodeToTrigger
from driftpy.dlob.dlob_node import DLOBNode
from driftpy.dlob.node_list import get_order_signature
from driftpy.types import OraclePriceData, MarketType
from custom_log import get_custom_logger
COMPUTE_BUDGET_PROGRAM = Pubkey.from_string(
"ComputeBudget111111111111111111111111111111"
)
logger = get_custom_logger(__name__)
@dataclass
class SimulateAndGetTxWithCUsResponse:
cu_estimate: int
tx: VersionedTransaction
sim_tx_logs: Optional[list[str]] = None
sim_error: Optional[Union[TransactionErrorType, str]] = None
def get_best_limit_bid_exclusionary(
dlob: DLOB,
market_index: int,
market_type: MarketType,
slot: int,
oracle_price_data: OraclePriceData,
excluded_pubkey: str,
excluded_user_accounts_and_order: list[tuple[str, int]] = [],
uncross: bool = False,
) -> Optional[DLOBNode]:
bids = dlob.get_resting_limit_bids(
market_index, slot, market_type, oracle_price_data
)
for bid in bids:
if hasattr(bid, "user_account"):
if str(bid.user_account) == excluded_pubkey:
continue
if hasattr(bid, "order"):
order_id = bid.order.order_id
price = bid.order.price
if uncross and price > oracle_price_data.price:
continue
if any(
entry[0] == str(bid.user_account) and entry[1] == (order_id or -1)
for entry in excluded_user_accounts_and_order
):
continue
return bid
return None
def get_best_limit_ask_exclusionary(
dlob: DLOB,
market_index: int,
market_type: MarketType,
slot: int,
oracle_price_data: OraclePriceData,
excluded_pubkey: str,
excluded_user_accounts_and_order: list[tuple[str, int]] = [],
uncross: bool = False,
) -> Optional[DLOBNode]:
asks = dlob.get_resting_limit_asks(
market_index, slot, market_type, oracle_price_data
)
for ask in asks:
if hasattr(ask, "user_account"):
if str(ask.user_account) == excluded_pubkey:
continue
if hasattr(ask, "order"):
order_id = ask.order.order_id
price = ask.order.price
if uncross and price < oracle_price_data.price:
continue
if any(
entry[0] == str(ask.user_account) and entry[1] == (order_id or -1)
for entry in excluded_user_accounts_and_order
):
continue
return ask
return None
def round_down_to_nearest(num: int, nearest: int = 100) -> int:
if nearest == 0:
return num # we will just return the number if asked to round to 0
return math.floor(num / nearest) * nearest
def decode_name(bytes_list: list[int]) -> str:
byte_array = bytes(bytes_list)
return byte_array.decode("utf-8").strip()
def get_node_to_fill_signature(node: NodeToFill) -> str:
if not node.node.user_account: # type: ignore
return "~"
return f"{node.node.user_account}-{node.node.order.order_id}" # type: ignore
def get_node_to_trigger_signature(node: NodeToTrigger) -> str:
return get_order_signature(node.node.order.order_id, node.node.user_account) # type: ignore
def get_fill_signature_from_user_account_and_order_id(
user_account: str, order_id: int
) -> str:
return f"{user_account}-{order_id}"
def is_set_compute_units_ix(ix: Instruction) -> bool:
if (ix.program_id == COMPUTE_BUDGET_PROGRAM) and (ix.data[0] == 2):
return True
return False
async def simulate_and_get_tx_with_cus(
ixs: list[Instruction],
drift_client: DriftClient,
tx_sender: StandardTxSender,
lookup_tables: AddressLookupTableAccount,
additional_signers: list[Keypair],
cu_limit_multiplier: float = 1.0,
log_sim_duration: bool = False,
do_sim: bool = True,
):
str_err: Optional[str] = None
if len(ixs) == 0:
raise ValueError("cannot simulate empty tx")
set_cu_limit_ix_idx = -1
for idx, ix in enumerate(ixs):
if is_set_compute_units_ix(ix):
set_cu_limit_ix_idx = idx
break
tx = await tx_sender.get_versioned_tx(
ixs, drift_client.wallet.payer, lookup_tables, additional_signers # type: ignore
)
if not do_sim:
return SimulateAndGetTxWithCUsResponse(-1, tx)
try:
start = time.time()
# manually simulate transaction because we need replace recent blockhash, which AsyncClient.simulate_transaction() doesn't expose
commitment = _COMMITMENT_TO_SOLDERS[drift_client.connection.commitment]
config = RpcSimulateTransactionConfig(
commitment=commitment, replace_recent_blockhash=True
)
body = SimulateVersionedTransaction(tx, config)
resp = await drift_client.connection._provider.make_request(
body, SimulateTransactionResp
)
if log_sim_duration:
logger.info(f"Simulated tx took: {time.time() - start} time")
except Exception as e:
logger.error(e)
if not resp:
raise ValueError("Failed to simulate transaction")
if resp.value.units_consumed is None:
raise ValueError("Failed to get CUs from simulate transaction")
sim_tx_logs = resp.value.logs
order_not_exist = any("Order does not exist" in log for log in sim_tx_logs)
if order_not_exist:
logger.error("Order already filled")
str_err = "Order already filled"
cu_estimate = resp.value.units_consumed
if set_cu_limit_ix_idx == -1:
ixs.insert(0, set_compute_unit_limit(int(cu_estimate * cu_limit_multiplier)))
else:
ixs[set_cu_limit_ix_idx] = set_compute_unit_limit(
int(cu_estimate * cu_limit_multiplier)
)
tx = await tx_sender.get_versioned_tx(
ixs, drift_client.wallet.payer, lookup_tables, additional_signers # type: ignore
)
return SimulateAndGetTxWithCUsResponse(
cu_estimate, tx, sim_tx_logs, resp.value.err or str_err
)