afp-sdk 0.5.3__py3-none-any.whl → 0.6.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.
Files changed (49) hide show
  1. afp/__init__.py +4 -1
  2. afp/afp.py +11 -0
  3. afp/api/admin.py +1 -1
  4. afp/api/base.py +11 -2
  5. afp/api/margin_account.py +12 -148
  6. afp/api/product.py +302 -148
  7. afp/api/trading.py +19 -22
  8. afp/auth.py +14 -0
  9. afp/bindings/__init__.py +30 -16
  10. afp/bindings/admin_facet.py +890 -0
  11. afp/bindings/clearing_facet.py +356 -773
  12. afp/bindings/facade.py +9 -6
  13. afp/bindings/final_settlement_facet.py +258 -99
  14. afp/bindings/margin_account.py +524 -839
  15. afp/bindings/margin_account_facet.py +722 -0
  16. afp/bindings/margin_account_registry.py +184 -310
  17. afp/bindings/mark_price_tracker_facet.py +74 -16
  18. afp/bindings/product_registry.py +1577 -541
  19. afp/bindings/product_registry_facet.py +1467 -0
  20. afp/bindings/system_viewer.py +592 -369
  21. afp/bindings/types.py +223 -0
  22. afp/config.py +4 -0
  23. afp/constants.py +49 -6
  24. afp/decorators.py +25 -3
  25. afp/dtos.py +142 -0
  26. afp/exceptions.py +14 -0
  27. afp/exchange.py +13 -8
  28. afp/hashing.py +7 -5
  29. afp/ipfs.py +245 -0
  30. afp/json-schemas/bafyreiaw34o6l3rmatabzbds2i2myazdw2yolevcpsoyd2i2g3ms7wa2eq.json +1 -0
  31. afp/json-schemas/bafyreibnfg6nq74dvpkre5rakkccij7iadp5rxpim7omsatjnrpmj3y7v4.json +1 -0
  32. afp/json-schemas/bafyreicgr6dfo5yduixjkcifghiulskfegwojvuwodtouvivl362zndhxe.json +1 -0
  33. afp/json-schemas/bafyreicheoypx6synljushh7mq2572iyhlolf4nake2p5dwobgnj3r5eua.json +1 -0
  34. afp/json-schemas/bafyreid35a67db4sqh4fs6boddyt2xvscbqy6nqvsp5jjur56qhkw4ixre.json +1 -0
  35. afp/json-schemas/bafyreidzs7okcpqiss6ztftltyptqwnw5e5opsy5yntospekjha4kpykaa.json +1 -0
  36. afp/json-schemas/bafyreifcec2km7hxwq6oqzjlspni2mgipetjb7pqtaewh2efislzoctboi.json +1 -0
  37. afp/json-schemas/bafyreihn3oiaxffe4e2w7pwtreadpw3obfd7gqlogbcxm56jc2hzfvco74.json +1 -0
  38. afp/json-schemas/bafyreihur3dzwhja6uxsbcw6eeoj3xmmc4e3zkmyzpot5v5dleevxe5zam.json +1 -0
  39. afp/schemas.py +228 -176
  40. afp/types.py +169 -0
  41. afp/validators.py +218 -8
  42. {afp_sdk-0.5.3.dist-info → afp_sdk-0.6.0.dist-info}/METADATA +73 -10
  43. afp_sdk-0.6.0.dist-info/RECORD +50 -0
  44. afp/bindings/auctioneer_facet.py +0 -752
  45. afp/bindings/bankruptcy_facet.py +0 -391
  46. afp/bindings/trading_protocol.py +0 -1158
  47. afp_sdk-0.5.3.dist-info/RECORD +0 -37
  48. {afp_sdk-0.5.3.dist-info → afp_sdk-0.6.0.dist-info}/WHEEL +0 -0
  49. {afp_sdk-0.5.3.dist-info → afp_sdk-0.6.0.dist-info}/licenses/LICENSE +0 -0
afp/__init__.py CHANGED
@@ -1,12 +1,15 @@
1
1
  """Autonomous Futures Protocol Python SDK."""
2
2
 
3
- from . import bindings
3
+ from . import bindings, enums, exceptions, schemas
4
4
  from .afp import AFP
5
5
  from .auth import Authenticator, KeyfileAuthenticator, PrivateKeyAuthenticator
6
6
  from .exceptions import AFPException
7
7
 
8
8
  __all__ = (
9
9
  "bindings",
10
+ "enums",
11
+ "exceptions",
12
+ "schemas",
10
13
  "AFP",
11
14
  "AFPException",
12
15
  "Authenticator",
afp/afp.py CHANGED
@@ -27,6 +27,13 @@ class AFP:
27
27
  The REST API base URL of the exchange. Defaults to the URL of the AutEx
28
28
  exchange. Its default value can be overridden with the `AFP_EXCHANGE_URL`
29
29
  environment variable.
30
+ ipfs_api_url : str, optional
31
+ The RPC API root URL of an IPFS node that supports Kubo RPC API v0, required
32
+ for product registration. Defaults to the URL of a local IPFS node. Its default
33
+ value can be overridden with the `AFP_IPFS_API_URL` environment variable.
34
+ ipfs_api_key : str, optional
35
+ The access token to include in API requests to the IPFS client. Its default
36
+ value can be overridden with the `AFP_IPFS_API_KEY` environment variable.
30
37
  chain_id : str, optional
31
38
  The chain ID of the Autonity network. Defauls to the chain ID of Autonity
32
39
  Mainnet. Its default value can be overridden with the `AFP_CHAIN_ID` environment
@@ -79,6 +86,8 @@ class AFP:
79
86
  authenticator: Authenticator | None = None,
80
87
  rpc_url: str | None = defaults.RPC_URL,
81
88
  exchange_url: str = defaults.EXCHANGE_URL,
89
+ ipfs_api_url: str = defaults.IPFS_API_URL,
90
+ ipfs_api_key: str | None = defaults.IPFS_API_KEY,
82
91
  chain_id: int = defaults.CHAIN_ID,
83
92
  gas_limit: int | None = defaults.GAS_LIMIT,
84
93
  max_fee_per_gas: int | None = defaults.MAX_FEE_PER_GAS,
@@ -97,6 +106,8 @@ class AFP:
97
106
  authenticator=authenticator,
98
107
  exchange_url=exchange_url,
99
108
  rpc_url=rpc_url,
109
+ ipfs_api_url=ipfs_api_url,
110
+ ipfs_api_key=ipfs_api_key,
100
111
  chain_id=chain_id,
101
112
  gas_limit=gas_limit,
102
113
  max_fee_per_gas=max_fee_per_gas,
afp/api/admin.py CHANGED
@@ -1,7 +1,7 @@
1
1
  from .. import validators
2
2
  from ..decorators import refresh_token_on_expiry
3
3
  from ..enums import ListingState
4
- from ..schemas import ExchangeProductListingSubmission, ExchangeProductUpdateSubmission
4
+ from ..dtos import ExchangeProductListingSubmission, ExchangeProductUpdateSubmission
5
5
  from .base import ExchangeAPI
6
6
 
7
7
 
afp/api/base.py CHANGED
@@ -11,11 +11,13 @@ from web3.contract.contract import ContractFunction
11
11
  from web3.types import TxParams
12
12
 
13
13
  from ..auth import Authenticator
14
- from ..config import Config
15
14
  from ..bindings.erc20 import ERC20
15
+ from ..config import Config
16
+ from ..dtos import LoginSubmission
16
17
  from ..exceptions import ConfigurationError
17
18
  from ..exchange import ExchangeClient
18
- from ..schemas import LoginSubmission, Transaction
19
+ from ..ipfs import IPFSClient
20
+ from ..schemas import Transaction
19
21
 
20
22
 
21
23
  class BaseAPI(ABC):
@@ -127,3 +129,10 @@ class ExchangeAPI(BaseAPI, ABC):
127
129
  statement=None,
128
130
  )
129
131
  return message.prepare_message()
132
+
133
+
134
+ class IPFSManager(ABC):
135
+ _ipfs_client: IPFSClient
136
+
137
+ def __init__(self, config: Config):
138
+ self._ipfs_client = IPFSClient(config.ipfs_api_url, config.ipfs_api_key)
afp/api/margin_account.py CHANGED
@@ -1,6 +1,5 @@
1
1
  from decimal import Decimal
2
2
  from functools import cache
3
- from typing import Iterable
4
3
 
5
4
  from eth_typing.evm import ChecksumAddress
6
5
  from hexbytes import HexBytes
@@ -9,54 +8,26 @@ from web3.exceptions import ContractCustomError
9
8
 
10
9
  from .. import validators
11
10
  from ..bindings import (
12
- BidData,
13
- ClearingDiamond,
14
11
  MarginAccount as MarginContract,
15
12
  MarginAccountRegistry,
16
13
  ProductRegistry,
17
- Side as OnChainOrderSide,
18
14
  )
19
15
  from ..bindings.erc20 import ERC20
20
16
  from ..bindings.facade import CLEARING_DIAMOND_ABI
21
17
  from ..bindings.margin_account import ABI as MARGIN_CONTRACT_ABI
22
18
  from ..bindings.margin_account_registry import ABI as MARGIN_ACCOUNT_REGISTRY_ABI
23
- from ..bindings.product_registry import ABI as PRODUCT_REGISTRY_ABI
24
19
  from ..decorators import convert_web3_error
25
20
  from ..exceptions import NotFoundError
26
- from ..schemas import AuctionData, Bid, OrderSide, Position, Transaction
21
+ from ..schemas import Position, Transaction
27
22
  from .base import ClearingSystemAPI
28
23
 
29
24
 
30
25
  class MarginAccount(ClearingSystemAPI):
31
26
  """API for managing margin accounts."""
32
27
 
33
- ### Factories ###
34
-
35
- @staticmethod
36
- def create_bid(product_id: str, price: Decimal, quantity: int, side: str) -> Bid:
37
- """Create a bid to be submitted to a liquidation auction.
38
-
39
- Parameters
40
- ----------
41
- product_id : str
42
- price: Decimal
43
- quantity: int
44
- side: str
45
-
46
- Returns
47
- -------
48
- afp.schemas.Bid
49
- """
50
- return Bid(
51
- product_id=product_id,
52
- price=price,
53
- quantity=quantity,
54
- side=getattr(OrderSide, side.upper()),
55
- )
56
-
57
28
  ### Transactions ###
58
29
 
59
- @convert_web3_error(MARGIN_CONTRACT_ABI)
30
+ @convert_web3_error(MARGIN_CONTRACT_ABI, CLEARING_DIAMOND_ABI)
60
31
  def authorize(self, collateral_asset: str, intent_account_id: str) -> Transaction:
61
32
  """Authorizes a blockchain account to submit intents to the clearing system
62
33
  using the margin account associated with the collateral asset.
@@ -79,7 +50,7 @@ class MarginAccount(ClearingSystemAPI):
79
50
  self._margin_contract(collateral_asset).authorize(intent_account_id)
80
51
  )
81
52
 
82
- @convert_web3_error(MARGIN_CONTRACT_ABI)
53
+ @convert_web3_error(MARGIN_CONTRACT_ABI, CLEARING_DIAMOND_ABI)
83
54
  def deposit(
84
55
  self, collateral_asset: str, amount: Decimal
85
56
  ) -> tuple[Transaction, Transaction]:
@@ -118,7 +89,7 @@ class MarginAccount(ClearingSystemAPI):
118
89
  )
119
90
  return (tx1, tx2)
120
91
 
121
- @convert_web3_error(MARGIN_CONTRACT_ABI)
92
+ @convert_web3_error(MARGIN_CONTRACT_ABI, CLEARING_DIAMOND_ABI)
122
93
  def withdraw(self, collateral_asset: str, amount: Decimal) -> Transaction:
123
94
  """Withdraws the specified amount of collateral tokens from the margin account
124
95
  associated with the collateral asset.
@@ -141,78 +112,9 @@ class MarginAccount(ClearingSystemAPI):
141
112
  self._margin_contract(collateral_asset).withdraw(token_amount)
142
113
  )
143
114
 
144
- @convert_web3_error(CLEARING_DIAMOND_ABI)
145
- def request_liquidation(
146
- self, margin_account_id: str, collateral_asset: str
147
- ) -> Transaction:
148
- """Request a liquidation auction to be started.
149
-
150
- Parameters
151
- ----------
152
- margin_account_id : str
153
- The ID of the margin account to be liquidated.
154
- collateral_asset : str
155
- The address of the collateral token that the margin account is trading with.
156
-
157
- Returns
158
- -------
159
- afp.schemas.Transaction
160
- Transaction parameters.
161
- """
162
- margin_account_id = validators.validate_address(margin_account_id)
163
- collateral_asset = validators.validate_address(collateral_asset)
164
-
165
- clearing_contract = ClearingDiamond(
166
- self._w3, self._config.clearing_diamond_address
167
- )
168
- return self._transact(
169
- clearing_contract.request_liquidation(margin_account_id, collateral_asset)
170
- )
171
-
172
- @convert_web3_error(CLEARING_DIAMOND_ABI, PRODUCT_REGISTRY_ABI)
173
- def submit_bids(
174
- self, margin_account_id: str, collateral_asset: str, bids: Iterable[Bid]
175
- ) -> Transaction:
176
- """Submit bids to a liquidation auction.
177
-
178
- Parameters
179
- ----------
180
- margin_account_id : str
181
- The ID of the margin account that is being liquidated.
182
- collateral_asset : str
183
- The address of the collateral token that the margin account is trading with.
184
- bids: list of afp.schemas.Bid
185
-
186
- Returns
187
- -------
188
- afp.schemas.Transaction
189
- Transaction parameters.
190
- """
191
- margin_account_id = validators.validate_address(margin_account_id)
192
- collateral_asset = validators.validate_address(collateral_asset)
193
-
194
- converted_bids = [
195
- BidData(
196
- product_id=HexBytes(bid.product_id),
197
- price=int(bid.price * 10 ** self._tick_size(bid.product_id)),
198
- quantity=bid.quantity,
199
- side=getattr(OnChainOrderSide, bid.side.name),
200
- )
201
- for bid in bids
202
- ]
203
-
204
- clearing_contract = ClearingDiamond(
205
- self._w3, self._config.clearing_diamond_address
206
- )
207
- return self._transact(
208
- clearing_contract.bid_auction(
209
- margin_account_id, collateral_asset, converted_bids
210
- )
211
- )
212
-
213
115
  ### Views ###
214
116
 
215
- @convert_web3_error(MARGIN_CONTRACT_ABI)
117
+ @convert_web3_error(MARGIN_CONTRACT_ABI, CLEARING_DIAMOND_ABI)
216
118
  def capital(self, collateral_asset: str) -> Decimal:
217
119
  """Returns the amount of collateral tokens in the margin account associated
218
120
  with the collateral asset.
@@ -232,7 +134,7 @@ class MarginAccount(ClearingSystemAPI):
232
134
  )
233
135
  return Decimal(amount) / 10 ** self._decimals(collateral_asset)
234
136
 
235
- @convert_web3_error(MARGIN_CONTRACT_ABI)
137
+ @convert_web3_error(MARGIN_CONTRACT_ABI, CLEARING_DIAMOND_ABI)
236
138
  def position(self, collateral_asset: str, position_id: str) -> Position:
237
139
  """Returns the parameters of a position in the margin account associated with
238
140
  the collateral asset.
@@ -261,7 +163,7 @@ class MarginAccount(ClearingSystemAPI):
261
163
  pnl=Decimal(data.pnl) / 10**decimals,
262
164
  )
263
165
 
264
- @convert_web3_error(MARGIN_CONTRACT_ABI)
166
+ @convert_web3_error(MARGIN_CONTRACT_ABI, CLEARING_DIAMOND_ABI)
265
167
  def positions(self, collateral_asset: str) -> list[Position]:
266
168
  """Returns all positions in the margin account associated with the collateral
267
169
  asset.
@@ -281,7 +183,7 @@ class MarginAccount(ClearingSystemAPI):
281
183
  )
282
184
  return [self.position(collateral_asset, Web3.to_hex(id)) for id in position_ids]
283
185
 
284
- @convert_web3_error(MARGIN_CONTRACT_ABI)
186
+ @convert_web3_error(MARGIN_CONTRACT_ABI, CLEARING_DIAMOND_ABI)
285
187
  def equity(self, collateral_asset: str) -> Decimal:
286
188
  """Returns the margin account equity in the margin account associated with the
287
189
  collateral asset.
@@ -301,7 +203,7 @@ class MarginAccount(ClearingSystemAPI):
301
203
  )
302
204
  return Decimal(amount) / 10 ** self._decimals(collateral_asset)
303
205
 
304
- @convert_web3_error(MARGIN_CONTRACT_ABI)
206
+ @convert_web3_error(MARGIN_CONTRACT_ABI, CLEARING_DIAMOND_ABI)
305
207
  def maintenance_margin_available(self, collateral_asset: str) -> Decimal:
306
208
  """Returns the maintenance margin available in the margin account associated
307
209
  with the collateral asset.
@@ -321,7 +223,7 @@ class MarginAccount(ClearingSystemAPI):
321
223
  )
322
224
  return Decimal(amount) / 10 ** self._decimals(collateral_asset)
323
225
 
324
- @convert_web3_error(MARGIN_CONTRACT_ABI)
226
+ @convert_web3_error(MARGIN_CONTRACT_ABI, CLEARING_DIAMOND_ABI)
325
227
  def maintenance_margin_used(self, collateral_asset: str) -> Decimal:
326
228
  """Returns the maintenance margin used in the margin account associated with
327
229
  the collateral asset.
@@ -341,7 +243,7 @@ class MarginAccount(ClearingSystemAPI):
341
243
  )
342
244
  return Decimal(amount) / 10 ** self._decimals(collateral_asset)
343
245
 
344
- @convert_web3_error(MARGIN_CONTRACT_ABI)
246
+ @convert_web3_error(MARGIN_CONTRACT_ABI, CLEARING_DIAMOND_ABI)
345
247
  def profit_and_loss(self, collateral_asset: str) -> Decimal:
346
248
  """Returns the profit and loss in the margin account associated with the
347
249
  collateral asset.
@@ -361,7 +263,7 @@ class MarginAccount(ClearingSystemAPI):
361
263
  )
362
264
  return Decimal(amount) / 10 ** self._decimals(collateral_asset)
363
265
 
364
- @convert_web3_error(MARGIN_CONTRACT_ABI)
266
+ @convert_web3_error(MARGIN_CONTRACT_ABI, CLEARING_DIAMOND_ABI)
365
267
  def withdrawable_amount(self, collateral_asset: str) -> Decimal:
366
268
  """Returns the amount of collateral tokens withdrawable from the margin account
367
269
  associated with the collateral asset.
@@ -381,44 +283,6 @@ class MarginAccount(ClearingSystemAPI):
381
283
  )
382
284
  return Decimal(amount) / 10 ** self._decimals(collateral_asset)
383
285
 
384
- @convert_web3_error(CLEARING_DIAMOND_ABI)
385
- def auction_data(
386
- self, margin_account_id: str, collateral_asset: str
387
- ) -> AuctionData:
388
- """Returns information on a liquidation auction.
389
-
390
- Parameters
391
- ----------
392
- margin_account_id : str
393
- The ID of the margin account to be liquidated.
394
- collateral_asset : str
395
- The address of the collateral token that the margin account is trading with.
396
-
397
- Returns
398
- -------
399
- str
400
- The hash of the transaction.
401
- """
402
- margin_account_id = validators.validate_address(margin_account_id)
403
- collateral_asset = validators.validate_address(collateral_asset)
404
-
405
- clearing_contract = ClearingDiamond(
406
- self._w3, self._config.clearing_diamond_address
407
- )
408
- data = clearing_contract.auction_data(margin_account_id, collateral_asset)
409
- divisor = 10 ** self._decimals(collateral_asset)
410
- return AuctionData(
411
- start_block=data.start_block,
412
- margin_account_equity_at_initiation=(
413
- Decimal(data.mae_at_initiation) / divisor
414
- ),
415
- maintenance_margin_used_at_initiation=(
416
- Decimal(data.mmu_at_initiation) / divisor
417
- ),
418
- margin_account_equity_now=(Decimal(data.mae_now) / divisor),
419
- maintenance_margin_used_now=(Decimal(data.mmu_now) / divisor),
420
- )
421
-
422
286
  ### Internal getters ###
423
287
 
424
288
  @cache