afp-sdk 0.1.0__tar.gz
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_sdk-0.1.0/.env.template +6 -0
- afp_sdk-0.1.0/.envrc +5 -0
- afp_sdk-0.1.0/CHANGELOG.md +5 -0
- afp_sdk-0.1.0/LICENSE +21 -0
- afp_sdk-0.1.0/PKG-INFO +180 -0
- afp_sdk-0.1.0/README.md +155 -0
- afp_sdk-0.1.0/afp/__init__.py +10 -0
- afp_sdk-0.1.0/afp/api/__init__.py +0 -0
- afp_sdk-0.1.0/afp/api/admin.py +55 -0
- afp_sdk-0.1.0/afp/api/base.py +76 -0
- afp_sdk-0.1.0/afp/api/builder.py +201 -0
- afp_sdk-0.1.0/afp/api/clearing.py +377 -0
- afp_sdk-0.1.0/afp/api/liquidation.py +167 -0
- afp_sdk-0.1.0/afp/api/trading.py +342 -0
- afp_sdk-0.1.0/afp/bindings/__init__.py +48 -0
- afp_sdk-0.1.0/afp/bindings/auctioneer_facet.py +758 -0
- afp_sdk-0.1.0/afp/bindings/bankruptcy_facet.py +355 -0
- afp_sdk-0.1.0/afp/bindings/clearing_facet.py +1019 -0
- afp_sdk-0.1.0/afp/bindings/erc20.py +379 -0
- afp_sdk-0.1.0/afp/bindings/facade.py +87 -0
- afp_sdk-0.1.0/afp/bindings/final_settlement_facet.py +268 -0
- afp_sdk-0.1.0/afp/bindings/margin_account.py +1355 -0
- afp_sdk-0.1.0/afp/bindings/margin_account_registry.py +617 -0
- afp_sdk-0.1.0/afp/bindings/mark_price_tracker_facet.py +111 -0
- afp_sdk-0.1.0/afp/bindings/oracle_provider.py +539 -0
- afp_sdk-0.1.0/afp/bindings/product_registry.py +1302 -0
- afp_sdk-0.1.0/afp/bindings/trading_protocol.py +1181 -0
- afp_sdk-0.1.0/afp/config.py +38 -0
- afp_sdk-0.1.0/afp/decorators.py +74 -0
- afp_sdk-0.1.0/afp/enums.py +27 -0
- afp_sdk-0.1.0/afp/exceptions.py +26 -0
- afp_sdk-0.1.0/afp/exchange.py +151 -0
- afp_sdk-0.1.0/afp/py.typed +0 -0
- afp_sdk-0.1.0/afp/schemas.py +207 -0
- afp_sdk-0.1.0/afp/signing.py +69 -0
- afp_sdk-0.1.0/afp/validators.py +43 -0
- afp_sdk-0.1.0/devenv.lock +139 -0
- afp_sdk-0.1.0/devenv.nix +19 -0
- afp_sdk-0.1.0/devenv.yaml +20 -0
- afp_sdk-0.1.0/examples/cancel_order.py +43 -0
- afp_sdk-0.1.0/examples/create_product.py +46 -0
- afp_sdk-0.1.0/examples/execute_trade.py +67 -0
- afp_sdk-0.1.0/pyproject.toml +97 -0
- afp_sdk-0.1.0/tests/__init__.py +0 -0
- afp_sdk-0.1.0/tests/test_decorators.py +96 -0
- afp_sdk-0.1.0/tests/test_hashing.py +65 -0
- afp_sdk-0.1.0/tests/test_login.py +26 -0
- afp_sdk-0.1.0/tests/test_schemas.py +17 -0
- afp_sdk-0.1.0/tests/test_validators.py +47 -0
- afp_sdk-0.1.0/uv.lock +1757 -0
afp_sdk-0.1.0/.envrc
ADDED
afp_sdk-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Autonity
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
afp_sdk-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: afp-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Autonity Futures Protocol Python SDK
|
|
5
|
+
Keywords: autonity,web3,trading,crypto,prediction,forecast,markets
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Programming Language :: Python
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Requires-Dist: decorator>=5.2.1
|
|
14
|
+
Requires-Dist: inflection>=0.5.1
|
|
15
|
+
Requires-Dist: pydantic>=2.10.6
|
|
16
|
+
Requires-Dist: requests>=2.32.0
|
|
17
|
+
Requires-Dist: siwe>=4.4.0
|
|
18
|
+
Requires-Dist: web3>=7.6.0
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Project-URL: Changes, https://github.com/autonity/afp-sdk/blob/master/CHANGELOG.md
|
|
21
|
+
Project-URL: Homepage, https://github.com/autonity/afp-sdk
|
|
22
|
+
Project-URL: Issues, https://github.com/autonity/afp-sdk/issues
|
|
23
|
+
Project-URL: Source, https://github.com/autonity/afp-sdk
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# Autonity Futures Protocol Python SDK
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
This library is published on PyPI as the [afp-sdk](https://pypi.org/project/afp-sdk/)
|
|
31
|
+
package. It can be installed in a virtualenv with:
|
|
32
|
+
|
|
33
|
+
```py
|
|
34
|
+
pip install afp-sdk
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Overview
|
|
38
|
+
|
|
39
|
+
The `afp` package consists of the following:
|
|
40
|
+
|
|
41
|
+
- `afp` top-level module: High-level API for interacting with the Clearing
|
|
42
|
+
System and the AutEx exchange.
|
|
43
|
+
- `afp.bindings` submodule: Low-level API that provides typed Python bindings
|
|
44
|
+
for the Clearing System smart contracts.
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
### Preparation
|
|
49
|
+
|
|
50
|
+
In order to use the AFP system, traders need to prepare the following:
|
|
51
|
+
|
|
52
|
+
- The ID of a product to be traded.
|
|
53
|
+
- The address of the product's collateral token.
|
|
54
|
+
- An Autonity account for managing the margin account. It needs to hold a
|
|
55
|
+
balance in ATN (for paying gas fee) and in the product's collateral token.
|
|
56
|
+
- An Autonity account for signing intents. The two accounts can be the same.
|
|
57
|
+
- The address of an Autonity RPC provider. They can be found on
|
|
58
|
+
[Chainlist](https://chainlist.org/?search=autonity&testnets=true).
|
|
59
|
+
|
|
60
|
+
We can store those in the following constants (using random example IDs):
|
|
61
|
+
|
|
62
|
+
```py
|
|
63
|
+
import os
|
|
64
|
+
|
|
65
|
+
PRODUCT_ID = "0x38d502bb683f53ec7c3d7a14b4aa47ac717659e121426131c0189c15bf4b9460"
|
|
66
|
+
COLLATERAL_ASSET = "0xD1A1e4035a164cF42228A8aAaBC2c0Ac9e49687B"
|
|
67
|
+
MARGIN_ACCOUNT_PRIVATE_KEY = os.environ["MARGIN_ACCOUNT_PRIVATE_KEY"]
|
|
68
|
+
INTENT_ACCOUNT_PRIVATE_KEY = os.environ["INTENT_ACCOUNT_PRIVATE_KEY"]
|
|
69
|
+
AUTONITY_RPC_URL = "https://bakerloo.autonity-apis.com"
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Account IDs (addresses) may be retrieved from the private keys with `eth_account`:
|
|
73
|
+
|
|
74
|
+
```py
|
|
75
|
+
from eth_account import Account
|
|
76
|
+
|
|
77
|
+
MARGIN_ACCOUNT_ID = Account.from_key(MARGIN_ACCOUNT_PRIVATE_KEY).address
|
|
78
|
+
INTENT_ACCOUNT_ID = Account.from_key(INTENT_ACCOUNT_PRIVATE_KEY).address
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Clearing API
|
|
82
|
+
|
|
83
|
+
Functions of the Clearing API can be accessed via the `afp.Clearing`
|
|
84
|
+
session object. It connects to the specified Autonity RPC provider and
|
|
85
|
+
communicates with the Clearing System smart contracts.
|
|
86
|
+
|
|
87
|
+
```py
|
|
88
|
+
import afp
|
|
89
|
+
|
|
90
|
+
clearing = afp.Clearing(MARGIN_ACCOUNT_PRIVATE_KEY, AUTONITY_RPC_URL)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Collateral can be deposited into the margin account with
|
|
94
|
+
`clearing.deposit_into_margin_account()`.
|
|
95
|
+
|
|
96
|
+
```py
|
|
97
|
+
from decimal import Decimal
|
|
98
|
+
|
|
99
|
+
clearing.deposit_into_margin_account(COLLATERAL_ASSET, Decimal("100.00"))
|
|
100
|
+
print(clearing.capital(COLLATERAL_ASSET))
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
The intent account should be authorized to submit orders. This is only required
|
|
104
|
+
if the intent account and the margin account are different.
|
|
105
|
+
|
|
106
|
+
```py
|
|
107
|
+
clearing.authorize(COLLATERAL_ASSET, INTENT_ACCOUNT_ID)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Trading API
|
|
111
|
+
|
|
112
|
+
The functions of the trading API can be accessed via the `afp.Trading` session
|
|
113
|
+
object. It communicates with the AutEx exchange and authenticates on creation with
|
|
114
|
+
the intent account's private key.
|
|
115
|
+
|
|
116
|
+
```py
|
|
117
|
+
trading = afp.Trading(INTENT_ACCOUNT_PRIVATE_KEY)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
To start trading a product, its parameters shall be retrieved from the server.
|
|
121
|
+
|
|
122
|
+
```py
|
|
123
|
+
product = trading.product(PRODUCT_ID)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Intents can be created with `trading.create_intent()`. Intent creation involves
|
|
127
|
+
hashing and signing the intent data. (The intent account's address is derived
|
|
128
|
+
from the private key specified in the `Trading` constructor.)
|
|
129
|
+
|
|
130
|
+
```py
|
|
131
|
+
from datetime import datetime, timedelta
|
|
132
|
+
from decimal import Decimal
|
|
133
|
+
|
|
134
|
+
intent = trading.create_intent(
|
|
135
|
+
margin_account_id=MARGIN_ACCOUNT_ID,
|
|
136
|
+
product=product,
|
|
137
|
+
side="bid",
|
|
138
|
+
limit_price=Decimal("1.23"),
|
|
139
|
+
quantity=2,
|
|
140
|
+
max_trading_fee_rate=Decimal("0.1"),
|
|
141
|
+
good_until_time=datetime.now() + timedelta(hours=1),
|
|
142
|
+
)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
The intent expressing a limit order can then be submitted to the exchange with
|
|
146
|
+
`trading.submit_limit_order()` that returns the created order object.
|
|
147
|
+
|
|
148
|
+
```py
|
|
149
|
+
order = trading.submit_limit_order(intent)
|
|
150
|
+
print(order)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
The exchange then performs various checks to ensure that the order is valid. To
|
|
154
|
+
ensure that the order has been accepted, its state can be polled with
|
|
155
|
+
`trading.order()`.
|
|
156
|
+
|
|
157
|
+
```py
|
|
158
|
+
order = trading.order(order.id)
|
|
159
|
+
print(order.state)
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Fills of orders submitted by the authenticated intent account can be queried
|
|
163
|
+
with `trading.order_fills()`.
|
|
164
|
+
|
|
165
|
+
```py
|
|
166
|
+
fills = trading.order_fills(product_id=PRODUCT_ID)
|
|
167
|
+
print(fills)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
See further code examples in the [examples](./examples/) directory.
|
|
171
|
+
|
|
172
|
+
## Development
|
|
173
|
+
|
|
174
|
+
The package uses [`uv`](https://docs.astral.sh/uv/) as project manager.
|
|
175
|
+
|
|
176
|
+
- Dependecies can be installed with the `uv sync` command.
|
|
177
|
+
- Linters can be executed with the `uv run poe lint` command.
|
|
178
|
+
- Tests can be executed with the `uv run poe test` command.
|
|
179
|
+
- Distributions can be checked before release with the `uv run poe check-dist` command.
|
|
180
|
+
- Markdown API documentation can be generated with the `uv run poe doc-gen` command.
|
afp_sdk-0.1.0/README.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# Autonity Futures Protocol Python SDK
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
This library is published on PyPI as the [afp-sdk](https://pypi.org/project/afp-sdk/)
|
|
6
|
+
package. It can be installed in a virtualenv with:
|
|
7
|
+
|
|
8
|
+
```py
|
|
9
|
+
pip install afp-sdk
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Overview
|
|
13
|
+
|
|
14
|
+
The `afp` package consists of the following:
|
|
15
|
+
|
|
16
|
+
- `afp` top-level module: High-level API for interacting with the Clearing
|
|
17
|
+
System and the AutEx exchange.
|
|
18
|
+
- `afp.bindings` submodule: Low-level API that provides typed Python bindings
|
|
19
|
+
for the Clearing System smart contracts.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Preparation
|
|
24
|
+
|
|
25
|
+
In order to use the AFP system, traders need to prepare the following:
|
|
26
|
+
|
|
27
|
+
- The ID of a product to be traded.
|
|
28
|
+
- The address of the product's collateral token.
|
|
29
|
+
- An Autonity account for managing the margin account. It needs to hold a
|
|
30
|
+
balance in ATN (for paying gas fee) and in the product's collateral token.
|
|
31
|
+
- An Autonity account for signing intents. The two accounts can be the same.
|
|
32
|
+
- The address of an Autonity RPC provider. They can be found on
|
|
33
|
+
[Chainlist](https://chainlist.org/?search=autonity&testnets=true).
|
|
34
|
+
|
|
35
|
+
We can store those in the following constants (using random example IDs):
|
|
36
|
+
|
|
37
|
+
```py
|
|
38
|
+
import os
|
|
39
|
+
|
|
40
|
+
PRODUCT_ID = "0x38d502bb683f53ec7c3d7a14b4aa47ac717659e121426131c0189c15bf4b9460"
|
|
41
|
+
COLLATERAL_ASSET = "0xD1A1e4035a164cF42228A8aAaBC2c0Ac9e49687B"
|
|
42
|
+
MARGIN_ACCOUNT_PRIVATE_KEY = os.environ["MARGIN_ACCOUNT_PRIVATE_KEY"]
|
|
43
|
+
INTENT_ACCOUNT_PRIVATE_KEY = os.environ["INTENT_ACCOUNT_PRIVATE_KEY"]
|
|
44
|
+
AUTONITY_RPC_URL = "https://bakerloo.autonity-apis.com"
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Account IDs (addresses) may be retrieved from the private keys with `eth_account`:
|
|
48
|
+
|
|
49
|
+
```py
|
|
50
|
+
from eth_account import Account
|
|
51
|
+
|
|
52
|
+
MARGIN_ACCOUNT_ID = Account.from_key(MARGIN_ACCOUNT_PRIVATE_KEY).address
|
|
53
|
+
INTENT_ACCOUNT_ID = Account.from_key(INTENT_ACCOUNT_PRIVATE_KEY).address
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Clearing API
|
|
57
|
+
|
|
58
|
+
Functions of the Clearing API can be accessed via the `afp.Clearing`
|
|
59
|
+
session object. It connects to the specified Autonity RPC provider and
|
|
60
|
+
communicates with the Clearing System smart contracts.
|
|
61
|
+
|
|
62
|
+
```py
|
|
63
|
+
import afp
|
|
64
|
+
|
|
65
|
+
clearing = afp.Clearing(MARGIN_ACCOUNT_PRIVATE_KEY, AUTONITY_RPC_URL)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Collateral can be deposited into the margin account with
|
|
69
|
+
`clearing.deposit_into_margin_account()`.
|
|
70
|
+
|
|
71
|
+
```py
|
|
72
|
+
from decimal import Decimal
|
|
73
|
+
|
|
74
|
+
clearing.deposit_into_margin_account(COLLATERAL_ASSET, Decimal("100.00"))
|
|
75
|
+
print(clearing.capital(COLLATERAL_ASSET))
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
The intent account should be authorized to submit orders. This is only required
|
|
79
|
+
if the intent account and the margin account are different.
|
|
80
|
+
|
|
81
|
+
```py
|
|
82
|
+
clearing.authorize(COLLATERAL_ASSET, INTENT_ACCOUNT_ID)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Trading API
|
|
86
|
+
|
|
87
|
+
The functions of the trading API can be accessed via the `afp.Trading` session
|
|
88
|
+
object. It communicates with the AutEx exchange and authenticates on creation with
|
|
89
|
+
the intent account's private key.
|
|
90
|
+
|
|
91
|
+
```py
|
|
92
|
+
trading = afp.Trading(INTENT_ACCOUNT_PRIVATE_KEY)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
To start trading a product, its parameters shall be retrieved from the server.
|
|
96
|
+
|
|
97
|
+
```py
|
|
98
|
+
product = trading.product(PRODUCT_ID)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Intents can be created with `trading.create_intent()`. Intent creation involves
|
|
102
|
+
hashing and signing the intent data. (The intent account's address is derived
|
|
103
|
+
from the private key specified in the `Trading` constructor.)
|
|
104
|
+
|
|
105
|
+
```py
|
|
106
|
+
from datetime import datetime, timedelta
|
|
107
|
+
from decimal import Decimal
|
|
108
|
+
|
|
109
|
+
intent = trading.create_intent(
|
|
110
|
+
margin_account_id=MARGIN_ACCOUNT_ID,
|
|
111
|
+
product=product,
|
|
112
|
+
side="bid",
|
|
113
|
+
limit_price=Decimal("1.23"),
|
|
114
|
+
quantity=2,
|
|
115
|
+
max_trading_fee_rate=Decimal("0.1"),
|
|
116
|
+
good_until_time=datetime.now() + timedelta(hours=1),
|
|
117
|
+
)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
The intent expressing a limit order can then be submitted to the exchange with
|
|
121
|
+
`trading.submit_limit_order()` that returns the created order object.
|
|
122
|
+
|
|
123
|
+
```py
|
|
124
|
+
order = trading.submit_limit_order(intent)
|
|
125
|
+
print(order)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The exchange then performs various checks to ensure that the order is valid. To
|
|
129
|
+
ensure that the order has been accepted, its state can be polled with
|
|
130
|
+
`trading.order()`.
|
|
131
|
+
|
|
132
|
+
```py
|
|
133
|
+
order = trading.order(order.id)
|
|
134
|
+
print(order.state)
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Fills of orders submitted by the authenticated intent account can be queried
|
|
138
|
+
with `trading.order_fills()`.
|
|
139
|
+
|
|
140
|
+
```py
|
|
141
|
+
fills = trading.order_fills(product_id=PRODUCT_ID)
|
|
142
|
+
print(fills)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
See further code examples in the [examples](./examples/) directory.
|
|
146
|
+
|
|
147
|
+
## Development
|
|
148
|
+
|
|
149
|
+
The package uses [`uv`](https://docs.astral.sh/uv/) as project manager.
|
|
150
|
+
|
|
151
|
+
- Dependecies can be installed with the `uv sync` command.
|
|
152
|
+
- Linters can be executed with the `uv run poe lint` command.
|
|
153
|
+
- Tests can be executed with the `uv run poe test` command.
|
|
154
|
+
- Distributions can be checked before release with the `uv run poe check-dist` command.
|
|
155
|
+
- Markdown API documentation can be generated with the `uv run poe doc-gen` command.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""Autonity Futures Protocol Python SDK."""
|
|
2
|
+
|
|
3
|
+
from afp import bindings
|
|
4
|
+
from .api.admin import Admin
|
|
5
|
+
from .api.builder import Builder
|
|
6
|
+
from .api.clearing import Clearing
|
|
7
|
+
from .api.liquidation import Liquidation
|
|
8
|
+
from .api.trading import Trading
|
|
9
|
+
|
|
10
|
+
__all__ = ("bindings", "Admin", "Builder", "Clearing", "Liquidation", "Trading")
|
|
File without changes
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from .. import validators
|
|
2
|
+
from ..decorators import refresh_token_on_expiry
|
|
3
|
+
from ..schemas import ExchangeProductSubmission
|
|
4
|
+
from .base import ExchangeAPI
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Admin(ExchangeAPI):
|
|
8
|
+
"""API for AutEx administration, restricted to AutEx admins.
|
|
9
|
+
|
|
10
|
+
Authenticates with the exchange on creation.
|
|
11
|
+
|
|
12
|
+
Parameters
|
|
13
|
+
----------
|
|
14
|
+
private_key : str
|
|
15
|
+
The private key of the exchange adminstrator account.
|
|
16
|
+
|
|
17
|
+
Raises
|
|
18
|
+
------
|
|
19
|
+
afp.exceptions.AuthenticationError
|
|
20
|
+
If the exchange rejects the login attempt.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
@refresh_token_on_expiry
|
|
24
|
+
def approve_product(self, product_id: str) -> None:
|
|
25
|
+
"""Approves a product for trading on the exchange.
|
|
26
|
+
|
|
27
|
+
Parameters
|
|
28
|
+
----------
|
|
29
|
+
product_id : str
|
|
30
|
+
|
|
31
|
+
Raises
|
|
32
|
+
------
|
|
33
|
+
afp.exceptions.AuthorizationError
|
|
34
|
+
If the configured account is not an exchange administrator.
|
|
35
|
+
"""
|
|
36
|
+
product = ExchangeProductSubmission(id=product_id)
|
|
37
|
+
self._exchange.approve_product(product)
|
|
38
|
+
|
|
39
|
+
@refresh_token_on_expiry
|
|
40
|
+
def delist_product(self, product_id: str) -> None:
|
|
41
|
+
"""Delists a product from the exchange.
|
|
42
|
+
|
|
43
|
+
New order submissions of this product will be rejected.
|
|
44
|
+
|
|
45
|
+
Parameters
|
|
46
|
+
----------
|
|
47
|
+
product_id : str
|
|
48
|
+
|
|
49
|
+
Raises
|
|
50
|
+
------
|
|
51
|
+
afp.exceptions.AuthorizationError
|
|
52
|
+
If the configured account is not an exchange administrator.
|
|
53
|
+
"""
|
|
54
|
+
value = validators.validate_hexstr32(product_id)
|
|
55
|
+
self._exchange.delist_product(value)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from abc import ABC
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
from functools import cache
|
|
4
|
+
from typing import cast
|
|
5
|
+
from urllib.parse import urlparse
|
|
6
|
+
|
|
7
|
+
from eth_account.account import Account
|
|
8
|
+
from eth_account.signers.local import LocalAccount
|
|
9
|
+
from eth_typing.evm import ChecksumAddress
|
|
10
|
+
from siwe import ISO8601Datetime, SiweMessage, siwe # type: ignore (untyped library)
|
|
11
|
+
from web3 import Web3, HTTPProvider
|
|
12
|
+
from web3.middleware import Middleware, SignAndSendRawMiddlewareBuilder
|
|
13
|
+
|
|
14
|
+
from .. import config, signing
|
|
15
|
+
from ..bindings.erc20 import ERC20
|
|
16
|
+
from ..exchange import ExchangeClient
|
|
17
|
+
from ..schemas import LoginSubmission
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
EXCHANGE_DOMAIN = urlparse(config.EXCHANGE_URL).netloc
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ClearingSystemAPI(ABC):
|
|
24
|
+
_account: LocalAccount
|
|
25
|
+
_w3: Web3
|
|
26
|
+
|
|
27
|
+
def __init__(self, private_key: str, autonity_rpc_url: str):
|
|
28
|
+
self._account = Account.from_key(private_key)
|
|
29
|
+
self._w3 = Web3(HTTPProvider(autonity_rpc_url))
|
|
30
|
+
|
|
31
|
+
# Configure the default sender account
|
|
32
|
+
self._w3.eth.default_account = self._account.address
|
|
33
|
+
signing_middleware = SignAndSendRawMiddlewareBuilder.build(self._account)
|
|
34
|
+
self._w3.middleware_onion.add(cast(Middleware, signing_middleware))
|
|
35
|
+
|
|
36
|
+
@cache
|
|
37
|
+
def _decimals(self, collateral_asset: ChecksumAddress) -> int:
|
|
38
|
+
token_contract = ERC20(self._w3, collateral_asset)
|
|
39
|
+
return token_contract.decimals()
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class ExchangeAPI(ABC):
|
|
43
|
+
_account: LocalAccount
|
|
44
|
+
_exchange: ExchangeClient
|
|
45
|
+
_trading_protocol_id: str
|
|
46
|
+
|
|
47
|
+
def __init__(self, private_key: str):
|
|
48
|
+
self._account = Account.from_key(private_key)
|
|
49
|
+
self._exchange = ExchangeClient()
|
|
50
|
+
self._login()
|
|
51
|
+
|
|
52
|
+
def _login(self):
|
|
53
|
+
nonce = self._exchange.generate_login_nonce()
|
|
54
|
+
message = self._generate_eip4361_message(self._account, nonce)
|
|
55
|
+
signature = signing.sign_message(self._account, message.encode("ascii"))
|
|
56
|
+
|
|
57
|
+
login_submission = LoginSubmission(
|
|
58
|
+
message=message, signature=Web3.to_hex(signature)
|
|
59
|
+
)
|
|
60
|
+
exchange_parameters = self._exchange.login(login_submission)
|
|
61
|
+
|
|
62
|
+
self._trading_protocol_id = exchange_parameters.trading_protocol_id
|
|
63
|
+
|
|
64
|
+
@staticmethod
|
|
65
|
+
def _generate_eip4361_message(account: LocalAccount, nonce: str) -> str:
|
|
66
|
+
message = SiweMessage(
|
|
67
|
+
domain=EXCHANGE_DOMAIN,
|
|
68
|
+
address=account.address,
|
|
69
|
+
uri=config.EXCHANGE_URL,
|
|
70
|
+
version=siwe.VersionEnum.one, # type: ignore
|
|
71
|
+
chain_id=config.CHAIN_ID,
|
|
72
|
+
issued_at=ISO8601Datetime.from_datetime(datetime.now()),
|
|
73
|
+
nonce=nonce,
|
|
74
|
+
statement=None,
|
|
75
|
+
)
|
|
76
|
+
return message.prepare_message()
|