-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathswap.py
More file actions
208 lines (196 loc) · 8.21 KB
/
swap.py
File metadata and controls
208 lines (196 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
from typing import List, Optional
from fastapi import APIRouter, Depends, Path, Query, HTTPException
from fastapi.security import HTTPBearer
from pydantic import conint
from meta_aggregation_api.config.auth import AuthJWT
from meta_aggregation_api.models.meta_agg_models import (
MetaPriceModel,
ProviderQuoteResponse,
)
from meta_aggregation_api.rest_api import dependencies
from meta_aggregation_api.utils.common import address_to_lower
from meta_aggregation_api.utils.errors import responses
PRICE_CACHE_TTL_SEC = 5
swap_route = APIRouter()
@swap_route.get('/{chain_id}/price', response_model=MetaPriceModel, responses=responses)
@swap_route.get(
'/{chain_id}/price/', response_model=MetaPriceModel, include_in_schema=False
)
async def get_swap_price(
buy_token: address_to_lower = Query(..., alias='buyToken'),
sell_token: address_to_lower = Query(..., alias='sellToken'),
sell_amount: conint(gt=0) = Query(..., alias='sellAmount'),
chain_id: int = Path(..., description='Chain ID'),
gas_price: Optional[int] = Query(
None, description='Gas price', gt=0, alias='gasPrice'
),
slippage_percentage: Optional[float] = Query(
0.005, gte=0, alias='slippagePercentage'
),
taker_address: Optional[address_to_lower] = Query(None, alias='takerAddress'),
fee_recipient: Optional[address_to_lower] = Query(None, alias='feeRecipient'),
buy_token_percentage_fee: Optional[float] = Query(
None, alias='buyTokenPercentageFee'
),
provider: Optional[str] = Query(None, alias='provider'),
meta_aggregation_service: dependencies.MetaAggregationService = Depends(
dependencies.meta_aggregation_service
),
) -> MetaPriceModel:
"""
Price endpoints are used to get the best price for a swap. It does not return data for swap and therefore
require any approvals. If you want to get data for swap, use /price_response endpoint.
- **buy_token**: Address of the token to buy
- **sell_token**: Address of the token to sell
- **sell_amount**: Amount of the token to sell in base units (e.g. 1 ETH = 10**18)
- **chain_id**: Chain ID. See /info for supported chains
- **gas_price**: Gas price in wei (optional)
- **slippage_percentage**: Slippage percentage (0.01 = 1%) (default: 0.005)
- **taker_address**: Address of the taker (optional)
- **fee_recipient**: Address of the fee recipient (optional)
- **buy_token_percentage_fee**: Percentage of the buy token fee (optional) (0.01 = 1%)
- **provider**: Provider name from /info (optional). If not specified, the best price will be returned
"""
params = {
"buy_token": buy_token,
"sell_token": sell_token,
"sell_amount": sell_amount,
"chain_id": chain_id,
"gas_price": gas_price,
"slippage_percentage": slippage_percentage,
"taker_address": taker_address,
"fee_recipient": fee_recipient,
"buy_token_percentage_fee": buy_token_percentage_fee,
}
if provider:
res = await meta_aggregation_service.get_provider_price(
provider=provider, **params
)
if not res:
raise HTTPException(
status_code=404,
detail='No prices found',
)
return res
else:
res = await meta_aggregation_service.get_swap_meta_price(**params)
if not res:
raise HTTPException(
status_code=404,
detail='No prices found',
)
return next((quote for quote in res if quote.is_best), None)
@swap_route.get(
'/{chain_id}/price/all', response_model=List[MetaPriceModel], responses=responses
)
@swap_route.get(
'/{chain_id}/price/all/',
include_in_schema=False,
response_model=List[MetaPriceModel],
)
async def get_all_swap_prices(
buy_token: address_to_lower = Query(..., alias='buyToken'),
sell_token: address_to_lower = Query(..., alias='sellToken'),
sell_amount: conint(gt=0) = Query(..., alias='sellAmount'),
chain_id: int = Path(..., description='Chain ID'),
gas_price: Optional[int] = Query(
None, description='Gas price', gt=0, alias='gasPrice'
),
slippage_percentage: Optional[float] = Query(0.005, alias='slippagePercentage'),
taker_address: Optional[address_to_lower] = Query(None, alias='takerAddress'),
fee_recipient: Optional[address_to_lower] = Query(None, alias='feeRecipient'),
buy_token_percentage_fee: Optional[float] = Query(
None, alias='buyTokenPercentageFee'
),
meta_aggregation_service: dependencies.MetaAggregationService = Depends(
dependencies.meta_aggregation_service
),
) -> List[MetaPriceModel]:
"""
Works the same as /price endpoint, but returns all prices from all supported providers.
- **buy_token**: Address of the token to buy
- **sell_token**: Address of the token to sell
- **sell_amount**: Amount of the token to sell in base units (e.g. 1 ETH = 10**18)
- **chain_id**: Chain ID. See /info for supported chains
- **gas_price**: Gas price in wei (optional)
- **slippage_percentage**: Slippage percentage (0.01 = 1%) (default: 0.005)
- **taker_address**: Address of the taker (optional)
- **fee_recipient**: Address of the fee recipient (optional)
- **buy_token_percentage_fee**: Percentage of the buy token fee (optional) (0.01 = 1%)
"""
res = await meta_aggregation_service.get_swap_meta_price(
buy_token=buy_token,
sell_token=sell_token,
sell_amount=sell_amount,
chain_id=chain_id,
gas_price=gas_price,
slippage_percentage=slippage_percentage,
taker_address=taker_address,
fee_recipient=fee_recipient,
buy_token_percentage_fee=buy_token_percentage_fee,
)
if not res:
raise HTTPException(
status_code=404,
detail='No prices found',
)
return res
@swap_route.get(
'/{chain_id}/quote',
response_model=ProviderQuoteResponse,
responses=responses,
# dependencies=[Depends(HTTPBearer())],
)
@swap_route.get(
'/{chain_id}/quote/',
response_model=ProviderQuoteResponse,
include_in_schema=False,
# dependencies=[Depends(HTTPBearer())],
)
async def get_swap_quote(
# authorize: AuthJWT = Depends(),
buy_token: address_to_lower = Query(..., alias='buyToken'),
sell_token: address_to_lower = Query(..., alias='sellToken'),
sell_amount: conint(gt=0) = Query(..., alias='sellAmount'),
chain_id: int = Path(..., description='Chain ID'),
provider: str = Query(..., alias='provider'),
taker_address: address_to_lower = Query(..., alias='takerAddress'),
gas_price: Optional[int] = Query(
None, description='Gas price', gt=0, alias='gasPrice'
),
slippage_percentage: Optional[float] = Query(0.005, alias='slippagePercentage'),
fee_recipient: Optional[address_to_lower] = Query(None, alias='feeRecipient'),
buy_token_percentage_fee: Optional[float] = Query(
None, alias='buyTokenPercentageFee'
),
meta_aggregation_service: dependencies.MetaAggregationService = Depends(
dependencies.meta_aggregation_service
),
) -> ProviderQuoteResponse:
"""
Returns a data for swap from a specific provider.
- **buy_token**:Address of the token to buy
- **sell_token**:Address of the token to sell
- **sell_amount**:Amount of the token to sell in base units (e.g. 1 ETH = 10**18)
- **chain_id**: Chain ID. See /info for supported chains
- **provider**: Provider name from /info
- **gas_price**: Gas price in wei (optional)
- **slippage_percentage**: Slippage percentage (0.01 = 1%) (default: 0.005)
- **taker_address**: Address of the taker (optional)
- **fee_recipient**: Address of the fee recipient (optional)
- **buy_token_percentage_fee**: Percentage of the buy token fee (optional) (0.01 = 1%)
"""
# authorize.jwt_required()
quote = await meta_aggregation_service.get_meta_swap_quote(
buy_token=buy_token,
sell_token=sell_token,
sell_amount=sell_amount,
chain_id=chain_id,
provider=provider,
gas_price=gas_price,
slippage_percentage=slippage_percentage,
taker_address=taker_address,
fee_recipient=fee_recipient,
buy_token_percentage_fee=buy_token_percentage_fee,
)
return quote