tristero 0.1.7__py3-none-any.whl → 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,392 @@
1
+ from __future__ import annotations
2
+ from typing import Dict, Any
3
+ from .eip712_struct import EIP712Struct
4
+ from eth_abi import encode as abi_encode
5
+ from eth_utils import keccak, to_checksum_address
6
+
7
+
8
+ class EIP712OrderParameters(EIP712Struct):
9
+ srcAsset: str
10
+ dstAsset: str
11
+ srcQuantity: int
12
+ dstQuantity: int
13
+ minQuantity: int
14
+ darkSalt: int
15
+
16
+ SOL_TYPE_NAME = "OrderParameters"
17
+ TYPE_STRING = "OrderParameters(address srcAsset,address dstAsset,uint256 srcQuantity,uint256 dstQuantity,uint256 minQuantity,uint128 darkSalt)"
18
+ TYPE_STRUCT = [
19
+ {"name": "srcAsset", "type": "address"},
20
+ {"name": "dstAsset", "type": "address"},
21
+ {"name": "srcQuantity", "type": "uint256"},
22
+ {"name": "dstQuantity", "type": "uint256"},
23
+ {"name": "minQuantity", "type": "uint256"},
24
+ {"name": "darkSalt", "type": "uint128"},
25
+ ]
26
+
27
+ @classmethod
28
+ def from_dict(cls, data: Dict[str, Any]) -> "EIP712OrderParameters":
29
+ kwargs = {}
30
+
31
+ if "srcAsset" in data:
32
+ kwargs["srcAsset"] = to_checksum_address(data["srcAsset"])
33
+
34
+ if "dstAsset" in data:
35
+ kwargs["dstAsset"] = to_checksum_address(data["dstAsset"])
36
+
37
+ if "srcQuantity" in data:
38
+ kwargs["srcQuantity"] = int(data["srcQuantity"])
39
+
40
+ if "dstQuantity" in data:
41
+ kwargs["dstQuantity"] = int(data["dstQuantity"])
42
+
43
+ if "minQuantity" in data:
44
+ kwargs["minQuantity"] = int(data["minQuantity"])
45
+
46
+ if "darkSalt" in data:
47
+ kwargs["darkSalt"] = int(data["darkSalt"])
48
+
49
+ return cls(**kwargs)
50
+
51
+ def hash(self) -> bytes:
52
+ return keccak(
53
+ abi_encode(
54
+ [
55
+ "bytes32",
56
+ "address",
57
+ "address",
58
+ "uint256",
59
+ "uint256",
60
+ "uint256",
61
+ "uint128",
62
+ ],
63
+ [
64
+ self.type_hash,
65
+ to_checksum_address(self.srcAsset),
66
+ to_checksum_address(self.dstAsset),
67
+ int(self.srcQuantity),
68
+ int(self.dstQuantity),
69
+ int(self.minQuantity),
70
+ int(self.darkSalt),
71
+ ],
72
+ )
73
+ )
74
+
75
+
76
+ class EIP712Domain(EIP712Struct):
77
+ SOL_TYPE_NAME = "EIP712Domain"
78
+
79
+ name: str
80
+ version: str
81
+ chainId: int
82
+ verifyingContract: str
83
+
84
+ TYPE_STRING = "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
85
+ TYPE_STRUCT = [
86
+ {"name": "name", "type": "string"},
87
+ {"name": "version", "type": "string"},
88
+ {"name": "chainId", "type": "uint256"},
89
+ {"name": "verifyingContract", "type": "address"},
90
+ ]
91
+
92
+ @classmethod
93
+ def from_dict(cls, data: Dict[str, Any]) -> "EIP712Domain":
94
+ kwargs = {
95
+ "name": data.get("name", ""),
96
+ "version": str(data.get("version", "1")),
97
+ "chainId": int(data.get("chainId", 0)),
98
+ "verifyingContract": to_checksum_address(
99
+ data.get(
100
+ "verifyingContract",
101
+ "0x0000000000000000000000000000000000000000",
102
+ )
103
+ ),
104
+ }
105
+ return cls(**kwargs)
106
+
107
+ def hash(self) -> bytes:
108
+ return keccak(
109
+ abi_encode(
110
+ ["bytes32", "bytes32", "bytes32", "uint256", "address"],
111
+ [
112
+ self.type_hash,
113
+ keccak(text=self.name),
114
+ keccak(text=self.version),
115
+ int(self.chainId),
116
+ to_checksum_address(self.verifyingContract),
117
+ ],
118
+ )
119
+ )
120
+
121
+
122
+ class EIP712Permit(EIP712Struct):
123
+ SOL_TYPE_NAME = "Permit"
124
+
125
+ owner: str
126
+ spender: str
127
+ value: int
128
+ nonce: int
129
+ deadline: int
130
+
131
+ TYPE_STRING = "Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
132
+ TYPE_STRUCT = [
133
+ {"name": "owner", "type": "address"},
134
+ {"name": "spender", "type": "address"},
135
+ {"name": "value", "type": "uint256"},
136
+ {"name": "nonce", "type": "uint256"},
137
+ {"name": "deadline", "type": "uint256"},
138
+ ]
139
+
140
+ @classmethod
141
+ def from_dict(cls, data: Dict[str, Any]) -> "EIP712Permit":
142
+ kwargs = {}
143
+ if "owner" in data:
144
+ kwargs["owner"] = to_checksum_address(data["owner"])
145
+ if "spender" in data:
146
+ kwargs["spender"] = to_checksum_address(data["spender"])
147
+ if "value" in data:
148
+ kwargs["value"] = int(data["value"])
149
+ if "nonce" in data:
150
+ kwargs["nonce"] = int(data["nonce"])
151
+ if "deadline" in data:
152
+ kwargs["deadline"] = int(data["deadline"])
153
+ return cls(**kwargs)
154
+
155
+ def hash(self) -> bytes:
156
+ return keccak(
157
+ abi_encode(
158
+ [
159
+ "bytes32",
160
+ "address",
161
+ "address",
162
+ "uint256",
163
+ "uint256",
164
+ "uint256",
165
+ ],
166
+ [
167
+ self.type_hash,
168
+ to_checksum_address(self.owner),
169
+ to_checksum_address(self.spender),
170
+ int(self.value),
171
+ int(self.nonce),
172
+ int(self.deadline),
173
+ ],
174
+ )
175
+ )
176
+
177
+
178
+ class EIP712TokenPermissions(EIP712Struct):
179
+ SOL_TYPE_NAME = "TokenPermissions"
180
+
181
+ token: str
182
+ amount: int
183
+
184
+ TYPE_STRING = "TokenPermissions(address token,uint256 amount)"
185
+ TYPE_STRUCT = [
186
+ {"name": "token", "type": "address"},
187
+ {"name": "amount", "type": "uint256"},
188
+ ]
189
+
190
+ @classmethod
191
+ def from_dict(cls, data: Dict[str, Any]) -> "EIP712TokenPermissions":
192
+ kwargs = {}
193
+
194
+ if "token" in data:
195
+ kwargs["token"] = to_checksum_address(data["token"])
196
+
197
+ if "amount" in data:
198
+ kwargs["amount"] = int(data["amount"])
199
+
200
+ return cls(**kwargs)
201
+
202
+ def hash(self):
203
+ return keccak(
204
+ abi_encode(
205
+ ["bytes32", "address", "uint256"],
206
+ [self.type_hash, to_checksum_address(self.token), int(self.amount)],
207
+ )
208
+ )
209
+
210
+
211
+ class EIP712MMWitness(EIP712Struct):
212
+ SOL_TYPE_NAME = "MMWitness"
213
+
214
+ mmToken: str
215
+ amount: int
216
+ orderId: bytes
217
+
218
+ TYPE_STRING = "MMWitness(address mmToken,uint256 amount,bytes32 orderId)"
219
+ TYPE_STRUCT = [
220
+ {"name": "mmToken", "type": "address"},
221
+ {"name": "amount", "type": "uint256"},
222
+ {"name": "orderId", "type": "bytes32"},
223
+ ]
224
+
225
+ @classmethod
226
+ def from_dict(cls, data: Dict[str, Any]) -> "EIP712MMWitness":
227
+ kwargs = {}
228
+
229
+ if "mmToken" in data:
230
+ kwargs["mmToken"] = to_checksum_address(data["mmToken"])
231
+
232
+ if "amount" in data:
233
+ kwargs["amount"] = int(data["amount"])
234
+
235
+ if "orderId" in data:
236
+ order_id = data["orderId"]
237
+ if isinstance(order_id, str):
238
+ if order_id.startswith("0x"):
239
+ kwargs["orderId"] = bytes.fromhex(order_id[2:])
240
+ else:
241
+ kwargs["orderId"] = bytes.fromhex(order_id)
242
+ else:
243
+ kwargs["orderId"] = bytes(order_id)
244
+
245
+ return cls(**kwargs)
246
+
247
+ def hash(self):
248
+ order_id_bytes = self.orderId
249
+ if len(order_id_bytes) < 32:
250
+ order_id_bytes = b"\x00" * (32 - len(order_id_bytes)) + order_id_bytes
251
+ elif len(order_id_bytes) > 32:
252
+ order_id_bytes = order_id_bytes[-32:]
253
+
254
+ return keccak(
255
+ abi_encode(
256
+ ["bytes32", "address", "uint256", "bytes32"],
257
+ [
258
+ self.type_hash,
259
+ to_checksum_address(self.mmToken),
260
+ int(self.amount),
261
+ order_id_bytes,
262
+ ],
263
+ )
264
+ )
265
+
266
+
267
+ class EIP712MarginWitness(EIP712Struct):
268
+ SOL_TYPE_NAME = "MarginWitness"
269
+
270
+ orderId: bytes
271
+ amount: int
272
+ loanToken: str
273
+
274
+ TYPE_STRING = "MarginWitness(bytes32 orderId,uint256 amount,address loanToken)"
275
+ TYPE_STRUCT = [
276
+ {"name": "orderId", "type": "bytes32"},
277
+ {"name": "amount", "type": "uint256"},
278
+ {"name": "loanToken", "type": "address"},
279
+ ]
280
+
281
+ @classmethod
282
+ def from_dict(cls, data: Dict[str, Any]) -> "EIP712MarginWitness":
283
+ kwargs = {}
284
+
285
+ if "orderId" in data:
286
+ order_id = data["orderId"]
287
+ if isinstance(order_id, str):
288
+ if order_id.startswith("0x"):
289
+ kwargs["orderId"] = bytes.fromhex(order_id[2:])
290
+ else:
291
+ kwargs["orderId"] = bytes.fromhex(order_id)
292
+ else:
293
+ kwargs["orderId"] = bytes(order_id)
294
+
295
+ if "amount" in data:
296
+ kwargs["amount"] = int(data["amount"])
297
+
298
+ if "loanToken" in data:
299
+ kwargs["loanToken"] = to_checksum_address(data["loanToken"])
300
+
301
+ return cls(**kwargs)
302
+
303
+ def hash(self):
304
+ order_id_bytes = self.orderId
305
+ if len(order_id_bytes) < 32:
306
+ order_id_bytes = b"\x00" * (32 - len(order_id_bytes)) + order_id_bytes
307
+ elif len(order_id_bytes) > 32:
308
+ order_id_bytes = order_id_bytes[-32:]
309
+
310
+ return keccak(
311
+ abi_encode(
312
+ ["bytes32", "bytes32", "uint256", "address"],
313
+ [
314
+ self.type_hash,
315
+ order_id_bytes,
316
+ int(self.amount),
317
+ to_checksum_address(self.loanToken),
318
+ ],
319
+ )
320
+ )
321
+
322
+
323
+ class EIP712CloseWithSwap(EIP712Struct):
324
+ SOL_TYPE_NAME = "CloseWithSwap"
325
+
326
+ positionId: int
327
+ cashSettle: bool
328
+ fractionBps: int = 10_000
329
+ authorized: str = "0x0000000000000000000000000000000000000000"
330
+ nonce: int = 0
331
+ deadline: int
332
+
333
+ TYPE_STRING = "CloseWithSwap(uint128 positionId,bool cashSettle,uint256 fractionBps,address authorized,uint256 nonce,uint256 deadline)"
334
+ TYPE_STRUCT = [
335
+ {"name": "positionId", "type": "uint128"},
336
+ {"name": "cashSettle", "type": "bool"},
337
+ {"name": "fractionBps", "type": "uint256"},
338
+ {"name": "authorized", "type": "address"},
339
+ {"name": "nonce", "type": "uint256"},
340
+ {"name": "deadline", "type": "uint256"},
341
+ ]
342
+
343
+ @classmethod
344
+ def from_dict(cls, data: Dict[str, Any]) -> "EIP712CloseWithSwap":
345
+ kwargs = {}
346
+
347
+ if "positionId" in data:
348
+ kwargs["positionId"] = int(data["positionId"])
349
+
350
+ if "cashSettle" in data:
351
+ kwargs["cashSettle"] = bool(data["cashSettle"])
352
+
353
+ kwargs["fractionBps"] = int(data.get("fractionBps", 10_000))
354
+
355
+ if "authorized" in data:
356
+ kwargs["authorized"] = to_checksum_address(data["authorized"])
357
+ else:
358
+ kwargs["authorized"] = "0x0000000000000000000000000000000000000000"
359
+
360
+ if "nonce" in data:
361
+ kwargs["nonce"] = int(data["nonce"])
362
+ else:
363
+ kwargs["nonce"] = 0
364
+
365
+ if "deadline" in data:
366
+ kwargs["deadline"] = int(data["deadline"])
367
+
368
+ return cls(**kwargs)
369
+
370
+ def hash(self) -> bytes:
371
+ return keccak(
372
+ abi_encode(
373
+ [
374
+ "bytes32",
375
+ "uint128",
376
+ "bool",
377
+ "uint256",
378
+ "address",
379
+ "uint256",
380
+ "uint256",
381
+ ],
382
+ [
383
+ self.type_hash,
384
+ int(self.positionId),
385
+ bool(self.cashSettle),
386
+ int(self.fractionBps),
387
+ to_checksum_address(self.authorized),
388
+ int(self.nonce),
389
+ int(self.deadline),
390
+ ],
391
+ )
392
+ )