avantis-trader-sdk 0.8.2__py3-none-any.whl → 0.8.3__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.
- avantis_trader_sdk/__init__.py +5 -5
- avantis_trader_sdk/abis/AggregatorV3Interface.json +606 -606
- avantis_trader_sdk/abis/IPyth.sol/IPyth.dbg.json +4 -4
- avantis_trader_sdk/abis/Referral.sol/ReferralStorage.json +7132 -7132
- avantis_trader_sdk/abis/Sanctions.json +190 -190
- avantis_trader_sdk/abis/USDC.sol/USDC.dbg.json +4 -4
- avantis_trader_sdk/abis/interfaces/ICallbacks.sol/ICallbacks.json +2637 -2637
- avantis_trader_sdk/abis/interfaces/IExecute.sol/IExecute.json +1628 -1628
- avantis_trader_sdk/abis/interfaces/IPairInfos.sol/IPairInfos.json +2781 -2781
- avantis_trader_sdk/abis/interfaces/IPairStorage.sol/IPairStorage.json +3729 -3729
- avantis_trader_sdk/abis/interfaces/IPriceAggregator.sol/IPriceAggregator.json +2330 -2330
- avantis_trader_sdk/abis/interfaces/IReferral.sol/IReferral.json +1890 -1890
- avantis_trader_sdk/abis/interfaces/ITradingStorage.sol/ITradingStorage.json +7022 -7022
- avantis_trader_sdk/abis/interfaces/ITranche.sol/ITranche.json +1283 -1283
- avantis_trader_sdk/abis/interfaces/IVaultManager.sol/IVaultManager.json +2424 -2424
- avantis_trader_sdk/abis/interfaces/IVeTranche.sol/IVeTranche.json +855 -855
- avantis_trader_sdk/abis/library/PositionMath.sol/PositionMath.dbg.json +4 -4
- avantis_trader_sdk/abis/library/PositionMath.sol/PositionMath.json +10 -10
- avantis_trader_sdk/abis/testnet/USDC.sol/USDC.dbg.json +4 -4
- avantis_trader_sdk/abis/testnet/USDC.sol/USDC.json +320 -320
- avantis_trader_sdk/client.py +369 -367
- avantis_trader_sdk/config.py +14 -14
- avantis_trader_sdk/feed/feed_client.py +263 -261
- avantis_trader_sdk/rpc/asset_parameters.py +499 -499
- avantis_trader_sdk/rpc/blended.py +71 -71
- avantis_trader_sdk/rpc/category_parameters.py +216 -216
- avantis_trader_sdk/rpc/fee_parameters.py +237 -237
- avantis_trader_sdk/rpc/pairs_cache.py +130 -130
- avantis_trader_sdk/rpc/rpc_helpers.py +8 -8
- avantis_trader_sdk/rpc/snapshot.py +142 -142
- avantis_trader_sdk/rpc/trade.py +701 -710
- avantis_trader_sdk/rpc/trading_parameters.py +139 -139
- avantis_trader_sdk/types.py +462 -462
- avantis_trader_sdk/utils.py +78 -78
- {avantis_trader_sdk-0.8.2.dist-info → avantis_trader_sdk-0.8.3.dist-info}/METADATA +124 -113
- {avantis_trader_sdk-0.8.2.dist-info → avantis_trader_sdk-0.8.3.dist-info}/RECORD +38 -39
- {avantis_trader_sdk-0.8.2.dist-info → avantis_trader_sdk-0.8.3.dist-info}/WHEEL +1 -1
- avantis_trader_sdk/feed/feedIds.json +0 -214
- {avantis_trader_sdk-0.8.2.dist-info → avantis_trader_sdk-0.8.3.dist-info}/top_level.txt +0 -0
avantis_trader_sdk/client.py
CHANGED
|
@@ -1,367 +1,369 @@
|
|
|
1
|
-
import json
|
|
2
|
-
from pathlib import Path
|
|
3
|
-
from web3 import Web3, AsyncWeb3
|
|
4
|
-
from .config import CONTRACT_ADDRESSES
|
|
5
|
-
from .rpc.pairs_cache import PairsCache
|
|
6
|
-
from .rpc.asset_parameters import AssetParametersRPC
|
|
7
|
-
from .rpc.category_parameters import CategoryParametersRPC
|
|
8
|
-
from .rpc.blended import BlendedRPC
|
|
9
|
-
from .rpc.fee_parameters import FeeParametersRPC
|
|
10
|
-
from .rpc.trading_parameters import TradingParametersRPC
|
|
11
|
-
from .rpc.snapshot import SnapshotRPC
|
|
12
|
-
from .rpc.trade import TradeRPC
|
|
13
|
-
from .utils import decoder
|
|
14
|
-
from .feed.feed_client import FeedClient
|
|
15
|
-
|
|
16
|
-
from .signers.base import BaseSigner
|
|
17
|
-
from .signers.local_signer import LocalSigner
|
|
18
|
-
from .signers.kms_signer import KMSSigner
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
class TraderClient:
|
|
22
|
-
"""
|
|
23
|
-
This class provides methods to interact with the Avantis smart contracts.
|
|
24
|
-
"""
|
|
25
|
-
|
|
26
|
-
def __init__(
|
|
27
|
-
self,
|
|
28
|
-
provider_url,
|
|
29
|
-
l1_provider_url="https://eth.llamarpc.com",
|
|
30
|
-
signer: BaseSigner = None,
|
|
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
|
-
self.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
self.
|
|
64
|
-
self.
|
|
65
|
-
self.
|
|
66
|
-
self.
|
|
67
|
-
self.
|
|
68
|
-
self.
|
|
69
|
-
self.
|
|
70
|
-
|
|
71
|
-
self.
|
|
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
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
1
|
+
import json
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from web3 import Web3, AsyncWeb3
|
|
4
|
+
from .config import CONTRACT_ADDRESSES
|
|
5
|
+
from .rpc.pairs_cache import PairsCache
|
|
6
|
+
from .rpc.asset_parameters import AssetParametersRPC
|
|
7
|
+
from .rpc.category_parameters import CategoryParametersRPC
|
|
8
|
+
from .rpc.blended import BlendedRPC
|
|
9
|
+
from .rpc.fee_parameters import FeeParametersRPC
|
|
10
|
+
from .rpc.trading_parameters import TradingParametersRPC
|
|
11
|
+
from .rpc.snapshot import SnapshotRPC
|
|
12
|
+
from .rpc.trade import TradeRPC
|
|
13
|
+
from .utils import decoder
|
|
14
|
+
from .feed.feed_client import FeedClient
|
|
15
|
+
|
|
16
|
+
from .signers.base import BaseSigner
|
|
17
|
+
from .signers.local_signer import LocalSigner
|
|
18
|
+
from .signers.kms_signer import KMSSigner
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class TraderClient:
|
|
22
|
+
"""
|
|
23
|
+
This class provides methods to interact with the Avantis smart contracts.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
provider_url,
|
|
29
|
+
l1_provider_url="https://eth.llamarpc.com",
|
|
30
|
+
signer: BaseSigner = None,
|
|
31
|
+
feed_client: FeedClient = None,
|
|
32
|
+
):
|
|
33
|
+
"""
|
|
34
|
+
Constructor for the TraderClient class.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
provider_url: The URL of the Ethereum node provider.
|
|
38
|
+
l1_provider_url (optional): The URL of the L1 Ethereum node provider.
|
|
39
|
+
signer (optional): The signer to use for signing transactions.
|
|
40
|
+
"""
|
|
41
|
+
self.web3 = Web3(
|
|
42
|
+
Web3.HTTPProvider(provider_url, request_kwargs={"timeout": 60})
|
|
43
|
+
)
|
|
44
|
+
self.async_web3 = AsyncWeb3(
|
|
45
|
+
AsyncWeb3.AsyncHTTPProvider(provider_url, request_kwargs={"timeout": 60})
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
self.l1_web3 = Web3(
|
|
49
|
+
Web3.HTTPProvider(l1_provider_url, request_kwargs={"timeout": 60})
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
self.l1_async_web3 = AsyncWeb3(
|
|
53
|
+
AsyncWeb3.AsyncHTTPProvider(l1_provider_url, request_kwargs={"timeout": 60})
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
self.contracts = self.load_contracts()
|
|
57
|
+
self.chain_id = self.web3.eth.chain_id
|
|
58
|
+
|
|
59
|
+
self.utils = {
|
|
60
|
+
"decoder": lambda *args, **kwargs: decoder(self.web3, *args, **kwargs)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
self.pairs_cache = PairsCache(self)
|
|
64
|
+
self.asset_parameters = AssetParametersRPC(self)
|
|
65
|
+
self.category_parameters = CategoryParametersRPC(self)
|
|
66
|
+
self.blended = BlendedRPC(self)
|
|
67
|
+
self.fee_parameters = FeeParametersRPC(self)
|
|
68
|
+
self.trading_parameters = TradingParametersRPC(self)
|
|
69
|
+
self.snapshot = SnapshotRPC(self)
|
|
70
|
+
self.feed_client = feed_client or FeedClient()
|
|
71
|
+
self.trade = TradeRPC(self, self.feed_client)
|
|
72
|
+
|
|
73
|
+
self.signer = signer
|
|
74
|
+
|
|
75
|
+
def load_contract(self, name):
|
|
76
|
+
"""
|
|
77
|
+
Loads the contract ABI and address from the local filesystem.
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
name: The name of the contract.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
A Contract object.
|
|
84
|
+
"""
|
|
85
|
+
abi_path = Path(__file__).parent / "abis" / f"{name}.sol" / f"{name}.json"
|
|
86
|
+
with open(abi_path) as abi_file:
|
|
87
|
+
abi = json.load(abi_file)
|
|
88
|
+
address = CONTRACT_ADDRESSES[name]
|
|
89
|
+
return self.async_web3.eth.contract(address=address, abi=abi["abi"])
|
|
90
|
+
|
|
91
|
+
def load_contracts(self):
|
|
92
|
+
"""
|
|
93
|
+
Loads all the contracts mentioned in the config from the local filesystem.
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
A dictionary containing the contract names as keys and the Contract objects as values.
|
|
97
|
+
"""
|
|
98
|
+
return {name: self.load_contract(name) for name in CONTRACT_ADDRESSES.keys()}
|
|
99
|
+
|
|
100
|
+
async def read_contract(self, contract_name, function_name, *args, decode=True):
|
|
101
|
+
"""
|
|
102
|
+
Calls a read-only function of a contract.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
contract_name: The name of the contract.
|
|
106
|
+
function_name: The name of the function.
|
|
107
|
+
args: The arguments to the function.
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
The result of the function call.
|
|
111
|
+
"""
|
|
112
|
+
contract = self.contracts.get(contract_name)
|
|
113
|
+
if not contract:
|
|
114
|
+
raise ValueError(f"Contract {contract_name} not found")
|
|
115
|
+
|
|
116
|
+
raw_data = await contract.functions[function_name](*args).call()
|
|
117
|
+
|
|
118
|
+
if decode:
|
|
119
|
+
return self.utils["decoder"](contract, function_name, raw_data)
|
|
120
|
+
|
|
121
|
+
return raw_data
|
|
122
|
+
|
|
123
|
+
async def write_contract(self, contract_name, function_name, *args, **kwargs):
|
|
124
|
+
"""
|
|
125
|
+
Calls a write function of a contract.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
contract_name: The name of the contract.
|
|
129
|
+
function_name: The name of the function.
|
|
130
|
+
args: The arguments to the function.
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
The transaction hash or the transaction object if signer is None.
|
|
134
|
+
"""
|
|
135
|
+
contract = self.contracts.get(contract_name)
|
|
136
|
+
if not contract:
|
|
137
|
+
raise ValueError(f"Contract {contract_name} not found")
|
|
138
|
+
|
|
139
|
+
if self.has_signer() and "from" not in kwargs:
|
|
140
|
+
kwargs["from"] = self.get_signer().get_ethereum_address()
|
|
141
|
+
|
|
142
|
+
if "chainId" not in kwargs:
|
|
143
|
+
kwargs["chainId"] = self.chain_id
|
|
144
|
+
|
|
145
|
+
if "nonce" not in kwargs:
|
|
146
|
+
kwargs["nonce"] = await self.get_transaction_count(kwargs["from"])
|
|
147
|
+
|
|
148
|
+
transaction = await contract.functions[function_name](*args).build_transaction(
|
|
149
|
+
kwargs
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
if not self.has_signer():
|
|
153
|
+
return transaction
|
|
154
|
+
|
|
155
|
+
receipt = await self.sign_and_get_receipt(transaction)
|
|
156
|
+
return receipt
|
|
157
|
+
|
|
158
|
+
def set_signer(self, signer: BaseSigner):
|
|
159
|
+
"""
|
|
160
|
+
Sets the signer.
|
|
161
|
+
"""
|
|
162
|
+
self.signer = signer
|
|
163
|
+
|
|
164
|
+
def get_signer(self):
|
|
165
|
+
"""
|
|
166
|
+
Gets the signer.
|
|
167
|
+
"""
|
|
168
|
+
return self.signer
|
|
169
|
+
|
|
170
|
+
def remove_signer(self):
|
|
171
|
+
"""
|
|
172
|
+
Removes the signer.
|
|
173
|
+
"""
|
|
174
|
+
self.signer = None
|
|
175
|
+
|
|
176
|
+
def has_signer(self):
|
|
177
|
+
"""
|
|
178
|
+
Checks if the signer is set.
|
|
179
|
+
"""
|
|
180
|
+
return self.signer is not None
|
|
181
|
+
|
|
182
|
+
def set_local_signer(self, private_key):
|
|
183
|
+
"""
|
|
184
|
+
Sets the local signer.
|
|
185
|
+
"""
|
|
186
|
+
self.signer = LocalSigner(private_key, self.async_web3)
|
|
187
|
+
|
|
188
|
+
def set_aws_kms_signer(self, kms_key_id, region_name="us-east-1"):
|
|
189
|
+
"""
|
|
190
|
+
Sets the AWS KMS signer.
|
|
191
|
+
"""
|
|
192
|
+
self.signer = KMSSigner(self.async_web3, kms_key_id, region_name)
|
|
193
|
+
|
|
194
|
+
async def sign_transaction(self, transaction):
|
|
195
|
+
"""
|
|
196
|
+
Signs a transaction.
|
|
197
|
+
|
|
198
|
+
Args:
|
|
199
|
+
transaction: The transaction object.
|
|
200
|
+
|
|
201
|
+
Returns:
|
|
202
|
+
The signed transaction object.
|
|
203
|
+
"""
|
|
204
|
+
if not self.has_signer():
|
|
205
|
+
raise ValueError(
|
|
206
|
+
"No signer is set. Please set a signer using `set_signer`."
|
|
207
|
+
)
|
|
208
|
+
return await self.signer.sign_transaction(transaction)
|
|
209
|
+
|
|
210
|
+
async def send_and_get_transaction_hash(self, signed_txn):
|
|
211
|
+
"""
|
|
212
|
+
Gets the transaction hash.
|
|
213
|
+
|
|
214
|
+
Args:
|
|
215
|
+
signed_txn: The signed transaction object.
|
|
216
|
+
|
|
217
|
+
Returns:
|
|
218
|
+
The transaction hash.
|
|
219
|
+
"""
|
|
220
|
+
return await self.async_web3.eth.send_raw_transaction(signed_txn.rawTransaction)
|
|
221
|
+
|
|
222
|
+
async def wait_for_transaction_receipt(self, tx_hash):
|
|
223
|
+
"""
|
|
224
|
+
Waits for the transaction to be mined.
|
|
225
|
+
|
|
226
|
+
Args:
|
|
227
|
+
tx_hash: The transaction hash.
|
|
228
|
+
|
|
229
|
+
Returns:
|
|
230
|
+
The transaction receipt.
|
|
231
|
+
"""
|
|
232
|
+
return await self.async_web3.eth.wait_for_transaction_receipt(tx_hash)
|
|
233
|
+
|
|
234
|
+
async def sign_and_get_receipt(self, transaction):
|
|
235
|
+
"""
|
|
236
|
+
Signs a transaction and waits for it to be mined.
|
|
237
|
+
|
|
238
|
+
Args:
|
|
239
|
+
transaction: The transaction object.
|
|
240
|
+
|
|
241
|
+
Returns:
|
|
242
|
+
The transaction receipt.
|
|
243
|
+
"""
|
|
244
|
+
gas_estimate = await self.get_gas_estimate(transaction)
|
|
245
|
+
transaction["gas"] = gas_estimate
|
|
246
|
+
signed_txn = await self.sign_transaction(transaction)
|
|
247
|
+
tx_hash = await self.send_and_get_transaction_hash(signed_txn)
|
|
248
|
+
return await self.wait_for_transaction_receipt(tx_hash)
|
|
249
|
+
|
|
250
|
+
async def get_transaction_count(self, address=None):
|
|
251
|
+
"""
|
|
252
|
+
Gets the transaction count.
|
|
253
|
+
|
|
254
|
+
Args:
|
|
255
|
+
address (optional): The address.
|
|
256
|
+
|
|
257
|
+
Returns:
|
|
258
|
+
The transaction count.
|
|
259
|
+
"""
|
|
260
|
+
if address is None:
|
|
261
|
+
address = self.get_signer().get_ethereum_address()
|
|
262
|
+
return await self.async_web3.eth.get_transaction_count(address)
|
|
263
|
+
|
|
264
|
+
async def get_gas_price(self):
|
|
265
|
+
"""
|
|
266
|
+
Gets the gas price.
|
|
267
|
+
|
|
268
|
+
Returns:
|
|
269
|
+
The gas price.
|
|
270
|
+
"""
|
|
271
|
+
return await self.async_web3.eth.gas_price
|
|
272
|
+
|
|
273
|
+
async def get_chain_id(self):
|
|
274
|
+
"""
|
|
275
|
+
Gets the chain id.
|
|
276
|
+
|
|
277
|
+
Returns:
|
|
278
|
+
The chain id.
|
|
279
|
+
"""
|
|
280
|
+
return await self.async_web3.eth.chain_id
|
|
281
|
+
|
|
282
|
+
async def get_balance(self, address=None):
|
|
283
|
+
"""
|
|
284
|
+
Gets the balance.
|
|
285
|
+
|
|
286
|
+
Args:
|
|
287
|
+
address (optional): The address.
|
|
288
|
+
|
|
289
|
+
Returns:
|
|
290
|
+
The balance.
|
|
291
|
+
"""
|
|
292
|
+
if address is None:
|
|
293
|
+
address = self.get_signer().get_ethereum_address()
|
|
294
|
+
balance = await self.async_web3.eth.get_balance(address)
|
|
295
|
+
return balance / 10**18
|
|
296
|
+
|
|
297
|
+
async def get_usdc_balance(self, address=None):
|
|
298
|
+
"""
|
|
299
|
+
Gets the USDC balance.
|
|
300
|
+
|
|
301
|
+
Args:
|
|
302
|
+
address (optional): The address.
|
|
303
|
+
|
|
304
|
+
Returns:
|
|
305
|
+
The USDC balance.
|
|
306
|
+
"""
|
|
307
|
+
if address is None:
|
|
308
|
+
address = self.get_signer().get_ethereum_address()
|
|
309
|
+
balance = await self.read_contract("USDC", "balanceOf", address, decode=False)
|
|
310
|
+
return balance / 10**6
|
|
311
|
+
|
|
312
|
+
async def get_usdc_allowance_for_trading(self, address=None):
|
|
313
|
+
"""
|
|
314
|
+
Gets the USDC allowance for the Trading Storage contract.
|
|
315
|
+
|
|
316
|
+
Args:
|
|
317
|
+
address (optional): The address.
|
|
318
|
+
|
|
319
|
+
Returns:
|
|
320
|
+
The USDC allowance.
|
|
321
|
+
"""
|
|
322
|
+
if address is None:
|
|
323
|
+
address = self.get_signer().get_ethereum_address()
|
|
324
|
+
|
|
325
|
+
trading_storage_address = self.contracts["TradingStorage"].address
|
|
326
|
+
|
|
327
|
+
allowance = await self.read_contract(
|
|
328
|
+
"USDC", "allowance", address, trading_storage_address, decode=False
|
|
329
|
+
)
|
|
330
|
+
return allowance / 10**6
|
|
331
|
+
|
|
332
|
+
async def approve_usdc_for_trading(self, amount=100000):
|
|
333
|
+
"""
|
|
334
|
+
Approves the USDC amount for the Trading Storage contract.
|
|
335
|
+
|
|
336
|
+
Args:
|
|
337
|
+
amount (optional): The amount to approve. Defaults to $100,000.
|
|
338
|
+
|
|
339
|
+
Returns:
|
|
340
|
+
The transaction hash.
|
|
341
|
+
"""
|
|
342
|
+
trading_storage_address = self.contracts["TradingStorage"].address
|
|
343
|
+
return await self.write_contract(
|
|
344
|
+
"USDC", "approve", trading_storage_address, int(amount * 10**6)
|
|
345
|
+
)
|
|
346
|
+
|
|
347
|
+
async def get_gas_estimate(self, transaction):
|
|
348
|
+
"""
|
|
349
|
+
Gets the gas estimate.
|
|
350
|
+
|
|
351
|
+
Args:
|
|
352
|
+
transaction: The transaction object.
|
|
353
|
+
|
|
354
|
+
Returns:
|
|
355
|
+
The gas estimate.
|
|
356
|
+
"""
|
|
357
|
+
return await self.async_web3.eth.estimate_gas(transaction)
|
|
358
|
+
|
|
359
|
+
async def get_transaction_hex(self, transaction):
|
|
360
|
+
"""
|
|
361
|
+
Gets the transaction hex.
|
|
362
|
+
|
|
363
|
+
Args:
|
|
364
|
+
transaction: The transaction object.
|
|
365
|
+
|
|
366
|
+
Returns:
|
|
367
|
+
The transaction hex.
|
|
368
|
+
"""
|
|
369
|
+
return transaction.hex()
|