afp-sdk 0.1.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.
- afp/__init__.py +10 -0
- afp/api/__init__.py +0 -0
- afp/api/admin.py +55 -0
- afp/api/base.py +76 -0
- afp/api/builder.py +201 -0
- afp/api/clearing.py +377 -0
- afp/api/liquidation.py +167 -0
- afp/api/trading.py +342 -0
- afp/bindings/__init__.py +48 -0
- afp/bindings/auctioneer_facet.py +758 -0
- afp/bindings/bankruptcy_facet.py +355 -0
- afp/bindings/clearing_facet.py +1019 -0
- afp/bindings/erc20.py +379 -0
- afp/bindings/facade.py +87 -0
- afp/bindings/final_settlement_facet.py +268 -0
- afp/bindings/margin_account.py +1355 -0
- afp/bindings/margin_account_registry.py +617 -0
- afp/bindings/mark_price_tracker_facet.py +111 -0
- afp/bindings/oracle_provider.py +539 -0
- afp/bindings/product_registry.py +1302 -0
- afp/bindings/trading_protocol.py +1181 -0
- afp/config.py +38 -0
- afp/decorators.py +74 -0
- afp/enums.py +27 -0
- afp/exceptions.py +26 -0
- afp/exchange.py +151 -0
- afp/py.typed +0 -0
- afp/schemas.py +207 -0
- afp/signing.py +69 -0
- afp/validators.py +43 -0
- afp_sdk-0.1.0.dist-info/METADATA +180 -0
- afp_sdk-0.1.0.dist-info/RECORD +34 -0
- afp_sdk-0.1.0.dist-info/WHEEL +4 -0
- afp_sdk-0.1.0.dist-info/licenses/LICENSE +21 -0
afp/bindings/erc20.py
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
"""ERC20 contract binding and data structures."""
|
|
2
|
+
|
|
3
|
+
import typing
|
|
4
|
+
|
|
5
|
+
import eth_typing
|
|
6
|
+
import web3
|
|
7
|
+
from web3.contract import contract
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ERC20:
|
|
11
|
+
"""ERC20 contract binding.
|
|
12
|
+
|
|
13
|
+
Parameters
|
|
14
|
+
----------
|
|
15
|
+
w3 : web3.Web3
|
|
16
|
+
address : eth_typing.ChecksumAddress
|
|
17
|
+
The address of a deployed ERC20 contract.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
_contract: contract.Contract
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
w3: web3.Web3,
|
|
25
|
+
address: eth_typing.ChecksumAddress,
|
|
26
|
+
):
|
|
27
|
+
self._contract = w3.eth.contract(
|
|
28
|
+
address=address,
|
|
29
|
+
abi=ABI,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def Approval(self) -> contract.ContractEvent:
|
|
34
|
+
"""Binding for `event Approval` on the ERC20 contract."""
|
|
35
|
+
return self._contract.events.Approval
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def Transfer(self) -> contract.ContractEvent:
|
|
39
|
+
"""Binding for `event Transfer` on the ERC20 contract."""
|
|
40
|
+
return self._contract.events.Transfer
|
|
41
|
+
|
|
42
|
+
def allowance(
|
|
43
|
+
self,
|
|
44
|
+
owner: eth_typing.ChecksumAddress,
|
|
45
|
+
spender: eth_typing.ChecksumAddress,
|
|
46
|
+
) -> int:
|
|
47
|
+
"""Binding for `allowance` on the ERC20 contract.
|
|
48
|
+
|
|
49
|
+
Parameters
|
|
50
|
+
----------
|
|
51
|
+
owner : eth_typing.ChecksumAddress
|
|
52
|
+
spender : eth_typing.ChecksumAddress
|
|
53
|
+
|
|
54
|
+
Returns
|
|
55
|
+
-------
|
|
56
|
+
int
|
|
57
|
+
"""
|
|
58
|
+
return_value = self._contract.functions.allowance(
|
|
59
|
+
owner,
|
|
60
|
+
spender,
|
|
61
|
+
).call()
|
|
62
|
+
return int(return_value)
|
|
63
|
+
|
|
64
|
+
def approve(
|
|
65
|
+
self,
|
|
66
|
+
spender: eth_typing.ChecksumAddress,
|
|
67
|
+
value: int,
|
|
68
|
+
) -> contract.ContractFunction:
|
|
69
|
+
"""Binding for `approve` on the ERC20 contract.
|
|
70
|
+
|
|
71
|
+
Parameters
|
|
72
|
+
----------
|
|
73
|
+
spender : eth_typing.ChecksumAddress
|
|
74
|
+
value : int
|
|
75
|
+
|
|
76
|
+
Returns
|
|
77
|
+
-------
|
|
78
|
+
web3.contract.contract.ContractFunction
|
|
79
|
+
A contract function instance to be sent in a transaction.
|
|
80
|
+
"""
|
|
81
|
+
return self._contract.functions.approve(
|
|
82
|
+
spender,
|
|
83
|
+
value,
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
def balance_of(
|
|
87
|
+
self,
|
|
88
|
+
account: eth_typing.ChecksumAddress,
|
|
89
|
+
) -> int:
|
|
90
|
+
"""Binding for `balanceOf` on the ERC20 contract.
|
|
91
|
+
|
|
92
|
+
Parameters
|
|
93
|
+
----------
|
|
94
|
+
account : eth_typing.ChecksumAddress
|
|
95
|
+
|
|
96
|
+
Returns
|
|
97
|
+
-------
|
|
98
|
+
int
|
|
99
|
+
"""
|
|
100
|
+
return_value = self._contract.functions.balanceOf(
|
|
101
|
+
account,
|
|
102
|
+
).call()
|
|
103
|
+
return int(return_value)
|
|
104
|
+
|
|
105
|
+
def decimals(
|
|
106
|
+
self,
|
|
107
|
+
) -> int:
|
|
108
|
+
"""Binding for `decimals` on the ERC20 contract.
|
|
109
|
+
|
|
110
|
+
Returns
|
|
111
|
+
-------
|
|
112
|
+
int
|
|
113
|
+
"""
|
|
114
|
+
return_value = self._contract.functions.decimals().call()
|
|
115
|
+
return int(return_value)
|
|
116
|
+
|
|
117
|
+
def name(
|
|
118
|
+
self,
|
|
119
|
+
) -> str:
|
|
120
|
+
"""Binding for `name` on the ERC20 contract.
|
|
121
|
+
|
|
122
|
+
Returns
|
|
123
|
+
-------
|
|
124
|
+
str
|
|
125
|
+
"""
|
|
126
|
+
return_value = self._contract.functions.name().call()
|
|
127
|
+
return str(return_value)
|
|
128
|
+
|
|
129
|
+
def symbol(
|
|
130
|
+
self,
|
|
131
|
+
) -> str:
|
|
132
|
+
"""Binding for `symbol` on the ERC20 contract.
|
|
133
|
+
|
|
134
|
+
Returns
|
|
135
|
+
-------
|
|
136
|
+
str
|
|
137
|
+
"""
|
|
138
|
+
return_value = self._contract.functions.symbol().call()
|
|
139
|
+
return str(return_value)
|
|
140
|
+
|
|
141
|
+
def total_supply(
|
|
142
|
+
self,
|
|
143
|
+
) -> int:
|
|
144
|
+
"""Binding for `totalSupply` on the ERC20 contract.
|
|
145
|
+
|
|
146
|
+
Returns
|
|
147
|
+
-------
|
|
148
|
+
int
|
|
149
|
+
"""
|
|
150
|
+
return_value = self._contract.functions.totalSupply().call()
|
|
151
|
+
return int(return_value)
|
|
152
|
+
|
|
153
|
+
def transfer(
|
|
154
|
+
self,
|
|
155
|
+
to: eth_typing.ChecksumAddress,
|
|
156
|
+
value: int,
|
|
157
|
+
) -> contract.ContractFunction:
|
|
158
|
+
"""Binding for `transfer` on the ERC20 contract.
|
|
159
|
+
|
|
160
|
+
Parameters
|
|
161
|
+
----------
|
|
162
|
+
to : eth_typing.ChecksumAddress
|
|
163
|
+
value : int
|
|
164
|
+
|
|
165
|
+
Returns
|
|
166
|
+
-------
|
|
167
|
+
web3.contract.contract.ContractFunction
|
|
168
|
+
A contract function instance to be sent in a transaction.
|
|
169
|
+
"""
|
|
170
|
+
return self._contract.functions.transfer(
|
|
171
|
+
to,
|
|
172
|
+
value,
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
def transfer_from(
|
|
176
|
+
self,
|
|
177
|
+
from_: eth_typing.ChecksumAddress,
|
|
178
|
+
to: eth_typing.ChecksumAddress,
|
|
179
|
+
value: int,
|
|
180
|
+
) -> contract.ContractFunction:
|
|
181
|
+
"""Binding for `transferFrom` on the ERC20 contract.
|
|
182
|
+
|
|
183
|
+
Parameters
|
|
184
|
+
----------
|
|
185
|
+
from_ : eth_typing.ChecksumAddress
|
|
186
|
+
to : eth_typing.ChecksumAddress
|
|
187
|
+
value : int
|
|
188
|
+
|
|
189
|
+
Returns
|
|
190
|
+
-------
|
|
191
|
+
web3.contract.contract.ContractFunction
|
|
192
|
+
A contract function instance to be sent in a transaction.
|
|
193
|
+
"""
|
|
194
|
+
return self._contract.functions.transferFrom(
|
|
195
|
+
from_,
|
|
196
|
+
to,
|
|
197
|
+
value,
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
ABI = typing.cast(
|
|
202
|
+
eth_typing.ABI,
|
|
203
|
+
[
|
|
204
|
+
{
|
|
205
|
+
"inputs": [
|
|
206
|
+
{"internalType": "address", "name": "spender", "type": "address"},
|
|
207
|
+
{"internalType": "uint256", "name": "allowance", "type": "uint256"},
|
|
208
|
+
{"internalType": "uint256", "name": "needed", "type": "uint256"},
|
|
209
|
+
],
|
|
210
|
+
"name": "ERC20InsufficientAllowance",
|
|
211
|
+
"type": "error",
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
"inputs": [
|
|
215
|
+
{"internalType": "address", "name": "sender", "type": "address"},
|
|
216
|
+
{"internalType": "uint256", "name": "balance", "type": "uint256"},
|
|
217
|
+
{"internalType": "uint256", "name": "needed", "type": "uint256"},
|
|
218
|
+
],
|
|
219
|
+
"name": "ERC20InsufficientBalance",
|
|
220
|
+
"type": "error",
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
"inputs": [
|
|
224
|
+
{"internalType": "address", "name": "approver", "type": "address"}
|
|
225
|
+
],
|
|
226
|
+
"name": "ERC20InvalidApprover",
|
|
227
|
+
"type": "error",
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
"inputs": [
|
|
231
|
+
{"internalType": "address", "name": "receiver", "type": "address"}
|
|
232
|
+
],
|
|
233
|
+
"name": "ERC20InvalidReceiver",
|
|
234
|
+
"type": "error",
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
"inputs": [
|
|
238
|
+
{"internalType": "address", "name": "sender", "type": "address"}
|
|
239
|
+
],
|
|
240
|
+
"name": "ERC20InvalidSender",
|
|
241
|
+
"type": "error",
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
"inputs": [
|
|
245
|
+
{"internalType": "address", "name": "spender", "type": "address"}
|
|
246
|
+
],
|
|
247
|
+
"name": "ERC20InvalidSpender",
|
|
248
|
+
"type": "error",
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
"anonymous": False,
|
|
252
|
+
"inputs": [
|
|
253
|
+
{
|
|
254
|
+
"indexed": True,
|
|
255
|
+
"internalType": "address",
|
|
256
|
+
"name": "owner",
|
|
257
|
+
"type": "address",
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
"indexed": True,
|
|
261
|
+
"internalType": "address",
|
|
262
|
+
"name": "spender",
|
|
263
|
+
"type": "address",
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
"indexed": False,
|
|
267
|
+
"internalType": "uint256",
|
|
268
|
+
"name": "value",
|
|
269
|
+
"type": "uint256",
|
|
270
|
+
},
|
|
271
|
+
],
|
|
272
|
+
"name": "Approval",
|
|
273
|
+
"type": "event",
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
"anonymous": False,
|
|
277
|
+
"inputs": [
|
|
278
|
+
{
|
|
279
|
+
"indexed": True,
|
|
280
|
+
"internalType": "address",
|
|
281
|
+
"name": "from",
|
|
282
|
+
"type": "address",
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
"indexed": True,
|
|
286
|
+
"internalType": "address",
|
|
287
|
+
"name": "to",
|
|
288
|
+
"type": "address",
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
"indexed": False,
|
|
292
|
+
"internalType": "uint256",
|
|
293
|
+
"name": "value",
|
|
294
|
+
"type": "uint256",
|
|
295
|
+
},
|
|
296
|
+
],
|
|
297
|
+
"name": "Transfer",
|
|
298
|
+
"type": "event",
|
|
299
|
+
},
|
|
300
|
+
{
|
|
301
|
+
"inputs": [
|
|
302
|
+
{"internalType": "address", "name": "owner", "type": "address"},
|
|
303
|
+
{"internalType": "address", "name": "spender", "type": "address"},
|
|
304
|
+
],
|
|
305
|
+
"name": "allowance",
|
|
306
|
+
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
|
|
307
|
+
"stateMutability": "view",
|
|
308
|
+
"type": "function",
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
"inputs": [
|
|
312
|
+
{"internalType": "address", "name": "spender", "type": "address"},
|
|
313
|
+
{"internalType": "uint256", "name": "value", "type": "uint256"},
|
|
314
|
+
],
|
|
315
|
+
"name": "approve",
|
|
316
|
+
"outputs": [{"internalType": "bool", "name": "", "type": "bool"}],
|
|
317
|
+
"stateMutability": "nonpayable",
|
|
318
|
+
"type": "function",
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
"inputs": [
|
|
322
|
+
{"internalType": "address", "name": "account", "type": "address"}
|
|
323
|
+
],
|
|
324
|
+
"name": "balanceOf",
|
|
325
|
+
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
|
|
326
|
+
"stateMutability": "view",
|
|
327
|
+
"type": "function",
|
|
328
|
+
},
|
|
329
|
+
{
|
|
330
|
+
"inputs": [],
|
|
331
|
+
"name": "decimals",
|
|
332
|
+
"outputs": [{"internalType": "uint8", "name": "", "type": "uint8"}],
|
|
333
|
+
"stateMutability": "view",
|
|
334
|
+
"type": "function",
|
|
335
|
+
},
|
|
336
|
+
{
|
|
337
|
+
"inputs": [],
|
|
338
|
+
"name": "name",
|
|
339
|
+
"outputs": [{"internalType": "string", "name": "", "type": "string"}],
|
|
340
|
+
"stateMutability": "view",
|
|
341
|
+
"type": "function",
|
|
342
|
+
},
|
|
343
|
+
{
|
|
344
|
+
"inputs": [],
|
|
345
|
+
"name": "symbol",
|
|
346
|
+
"outputs": [{"internalType": "string", "name": "", "type": "string"}],
|
|
347
|
+
"stateMutability": "view",
|
|
348
|
+
"type": "function",
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
"inputs": [],
|
|
352
|
+
"name": "totalSupply",
|
|
353
|
+
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
|
|
354
|
+
"stateMutability": "view",
|
|
355
|
+
"type": "function",
|
|
356
|
+
},
|
|
357
|
+
{
|
|
358
|
+
"inputs": [
|
|
359
|
+
{"internalType": "address", "name": "to", "type": "address"},
|
|
360
|
+
{"internalType": "uint256", "name": "value", "type": "uint256"},
|
|
361
|
+
],
|
|
362
|
+
"name": "transfer",
|
|
363
|
+
"outputs": [{"internalType": "bool", "name": "", "type": "bool"}],
|
|
364
|
+
"stateMutability": "nonpayable",
|
|
365
|
+
"type": "function",
|
|
366
|
+
},
|
|
367
|
+
{
|
|
368
|
+
"inputs": [
|
|
369
|
+
{"internalType": "address", "name": "from", "type": "address"},
|
|
370
|
+
{"internalType": "address", "name": "to", "type": "address"},
|
|
371
|
+
{"internalType": "uint256", "name": "value", "type": "uint256"},
|
|
372
|
+
],
|
|
373
|
+
"name": "transferFrom",
|
|
374
|
+
"outputs": [{"internalType": "bool", "name": "", "type": "bool"}],
|
|
375
|
+
"stateMutability": "nonpayable",
|
|
376
|
+
"type": "function",
|
|
377
|
+
},
|
|
378
|
+
],
|
|
379
|
+
)
|
afp/bindings/facade.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
from itertools import chain
|
|
2
|
+
|
|
3
|
+
from web3 import Web3
|
|
4
|
+
|
|
5
|
+
from .. import config
|
|
6
|
+
from . import (
|
|
7
|
+
auctioneer_facet,
|
|
8
|
+
bankruptcy_facet,
|
|
9
|
+
clearing_facet,
|
|
10
|
+
final_settlement_facet,
|
|
11
|
+
margin_account_registry,
|
|
12
|
+
mark_price_tracker_facet,
|
|
13
|
+
oracle_provider,
|
|
14
|
+
product_registry,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
# In order to include a facet in the ClearingDiamond facade:
|
|
18
|
+
# 1. Add its ABI to CLEARING_DIAMOND_ABI
|
|
19
|
+
# 2. Set its contract binding as a superclass of ClearingDiamond
|
|
20
|
+
|
|
21
|
+
CLEARING_DIAMOND_ABI = list(
|
|
22
|
+
chain(
|
|
23
|
+
auctioneer_facet.ABI,
|
|
24
|
+
bankruptcy_facet.ABI,
|
|
25
|
+
clearing_facet.ABI,
|
|
26
|
+
final_settlement_facet.ABI,
|
|
27
|
+
mark_price_tracker_facet.ABI,
|
|
28
|
+
)
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ClearingDiamond(
|
|
33
|
+
auctioneer_facet.AuctioneerFacet,
|
|
34
|
+
bankruptcy_facet.BankruptcyFacet,
|
|
35
|
+
clearing_facet.ClearingFacet,
|
|
36
|
+
final_settlement_facet.FinalSettlementFacet,
|
|
37
|
+
mark_price_tracker_facet.MarkPriceTrackerFacet,
|
|
38
|
+
):
|
|
39
|
+
"""ClearingDiamond contract binding.
|
|
40
|
+
|
|
41
|
+
Includes all functions inherited from various facets.
|
|
42
|
+
|
|
43
|
+
Parameters
|
|
44
|
+
----------
|
|
45
|
+
w3 : web3.Web3
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
def __init__(self, w3: Web3):
|
|
49
|
+
self._contract = w3.eth.contract(
|
|
50
|
+
address=config.CLEARING_DIAMOND_ADDRESS, abi=CLEARING_DIAMOND_ABI
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class MarginAccountRegistry(margin_account_registry.MarginAccountRegistry):
|
|
55
|
+
"""MarginAccountRegistry contract binding.
|
|
56
|
+
|
|
57
|
+
Parameters
|
|
58
|
+
----------
|
|
59
|
+
w3 : web3.Web3
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
def __init__(self, w3: Web3):
|
|
63
|
+
super().__init__(w3, config.MARGIN_ACCOUNT_REGISTRY_ADDRESS)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class OracleProvider(oracle_provider.OracleProvider):
|
|
67
|
+
"""OracleProvider contract binding.
|
|
68
|
+
|
|
69
|
+
Parameters
|
|
70
|
+
----------
|
|
71
|
+
w3 : web3.Web3
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
def __init__(self, w3: Web3):
|
|
75
|
+
super().__init__(w3, config.ORACLE_PROVIDER_ADDRESS)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class ProductRegistry(product_registry.ProductRegistry):
|
|
79
|
+
"""ProductRegistry contract binding.
|
|
80
|
+
|
|
81
|
+
Parameters
|
|
82
|
+
----------
|
|
83
|
+
w3 : web3.Web3
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
def __init__(self, w3: Web3):
|
|
87
|
+
super().__init__(w3, config.PRODUCT_REGISTRY_ADDRESS)
|