@cowprotocol/sdk-order-book 0.6.6 → 1.1.0
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.
- package/README.md +33 -0
- package/dist/index.d.mts +159 -38
- package/dist/index.d.ts +159 -38
- package/dist/index.js +143 -158
- package/dist/index.mjs +138 -156
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -121,6 +121,39 @@ const orderBookApi = new OrderBookApi({
|
|
|
121
121
|
})
|
|
122
122
|
```
|
|
123
123
|
|
|
124
|
+
### Partner API (Authenticated Access)
|
|
125
|
+
|
|
126
|
+
Partners can use the Partner API for authenticated, rate-limited access with higher request quotas. Provide your API key and the SDK will automatically route requests through the partner gateway and attach the `X-API-Key` header.
|
|
127
|
+
|
|
128
|
+
| Environment | Partner API Base URL |
|
|
129
|
+
|-------------|---------------------|
|
|
130
|
+
| Production | `https://partners.cow.fi` |
|
|
131
|
+
| Staging | `https://partners.barn.cow.fi` |
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
const orderBookApi = new OrderBookApi({
|
|
135
|
+
chainId: SupportedChainId.MAINNET,
|
|
136
|
+
apiKey: 'your-partner-api-key',
|
|
137
|
+
})
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
To use the Partner API with the Trading SDK, pass a configured `OrderBookApi` instance:
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import { TradingSdk } from '@cowprotocol/sdk-trading'
|
|
144
|
+
|
|
145
|
+
const orderBookApi = new OrderBookApi({
|
|
146
|
+
chainId: SupportedChainId.MAINNET,
|
|
147
|
+
apiKey: 'your-partner-api-key',
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
const sdk = new TradingSdk(
|
|
151
|
+
{ chainId: SupportedChainId.MAINNET, appCode: 'YOUR_APP_CODE' },
|
|
152
|
+
{ orderBookApi },
|
|
153
|
+
adapter,
|
|
154
|
+
)
|
|
155
|
+
```
|
|
156
|
+
|
|
124
157
|
### Rate Limiting Configuration
|
|
125
158
|
|
|
126
159
|
```typescript
|
package/dist/index.d.mts
CHANGED
|
@@ -1049,18 +1049,22 @@ interface Costs<T> {
|
|
|
1049
1049
|
/**
|
|
1050
1050
|
* Details about costs and amounts, costs and fees of a quote.
|
|
1051
1051
|
*
|
|
1052
|
-
* CoW Protocol quote has amounts (sell/buy)
|
|
1053
|
-
*
|
|
1052
|
+
* CoW Protocol quote has amounts (sell/buy), network fee, and protocol fee.
|
|
1053
|
+
* On the client side (after /quote response) we add partner fee and slippage.
|
|
1054
|
+
* CoW Protocol supports both sell and buy orders and the fees and costs are calculated differently.
|
|
1054
1055
|
*
|
|
1055
1056
|
* The order of adding fees and costs is as follows:
|
|
1056
|
-
* 1.
|
|
1057
|
-
* 2. Partner fee is added to the surplus amount (sell amount for sell-orders, buy amount for buy-orders)
|
|
1058
|
-
* 3. Protocol fee is already baked into the quoted amounts:
|
|
1057
|
+
* 1. Protocol fee is already baked into the quoted amounts:
|
|
1059
1058
|
* - for SELL orders it has been deducted from the buy amount
|
|
1060
|
-
* - for BUY orders it has been added on top of the sell amount
|
|
1059
|
+
* - for BUY orders it has been added on top of the sell amount
|
|
1060
|
+
* 2. Network fee:
|
|
1061
|
+
* - for SELL orders it has been deducted from the sell amount
|
|
1062
|
+
* - for BUY orders it's not added to any amount, and provided separately as `feeAmount`
|
|
1063
|
+
* 3. Partner fee is added to the surplus amount (sell amount for sell-orders, buy amount for buy-orders)
|
|
1064
|
+
* 4. Slippage is added to the surplus amount (sell amount for sell-orders, buy amount for buy-orders)
|
|
1061
1065
|
*
|
|
1062
|
-
* For sell-orders the partner fee
|
|
1063
|
-
* For buy-orders the partner fee
|
|
1066
|
+
* For sell-orders the partner fee and slippage are subtracted from the buy amount after network costs.
|
|
1067
|
+
* For buy-orders the partner fee and slippage are added on top of the sell amount after network costs.
|
|
1064
1068
|
*/
|
|
1065
1069
|
interface QuoteAmountsAndCosts<T = bigint> {
|
|
1066
1070
|
/**
|
|
@@ -1077,6 +1081,9 @@ interface QuoteAmountsAndCosts<T = bigint> {
|
|
|
1077
1081
|
* so UIs can decide how to show it to the user.
|
|
1078
1082
|
*/
|
|
1079
1083
|
costs: Costs<T>;
|
|
1084
|
+
/**
|
|
1085
|
+
* Before all the fees and costs. Spot price amounts
|
|
1086
|
+
*/
|
|
1080
1087
|
beforeAllFees: Amounts<T>;
|
|
1081
1088
|
/**
|
|
1082
1089
|
* Amounts before network costs. This amount could be shown to the user to reflect how much they are expected to get
|
|
@@ -1084,17 +1091,22 @@ interface QuoteAmountsAndCosts<T = bigint> {
|
|
|
1084
1091
|
*/
|
|
1085
1092
|
beforeNetworkCosts: Amounts<T>;
|
|
1086
1093
|
/**
|
|
1087
|
-
*
|
|
1094
|
+
* 1. Same with beforeNetworkCosts.
|
|
1095
|
+
* `beforeNetworkCosts` was here even before protocol fee existed, and we keep it for backward compatibility
|
|
1096
|
+
*/
|
|
1097
|
+
afterProtocolFees: Amounts<T>;
|
|
1098
|
+
/**
|
|
1099
|
+
* 2. Amounts after including network costs.
|
|
1088
1100
|
*/
|
|
1089
1101
|
afterNetworkCosts: Amounts<T>;
|
|
1090
1102
|
/**
|
|
1091
|
-
* Amounts after including partner fees (if any).
|
|
1103
|
+
* 3. Amounts after including partner fees (if any).
|
|
1092
1104
|
*
|
|
1093
1105
|
* This amount could be shown to the user, as the expected to receive amount already including any fees or costs.
|
|
1094
1106
|
*/
|
|
1095
1107
|
afterPartnerFees: Amounts<T>;
|
|
1096
1108
|
/**
|
|
1097
|
-
* Amounts after including the slippage tolerance.
|
|
1109
|
+
* 4. Amounts after including the slippage tolerance.
|
|
1098
1110
|
*
|
|
1099
1111
|
* This is the minimum that the user will receive and the amount they will sign.
|
|
1100
1112
|
*
|
|
@@ -1102,6 +1114,10 @@ interface QuoteAmountsAndCosts<T = bigint> {
|
|
|
1102
1114
|
* event of the buy token appreciating.
|
|
1103
1115
|
*/
|
|
1104
1116
|
afterSlippage: Amounts<T>;
|
|
1117
|
+
/**
|
|
1118
|
+
* Amounts that supposed to be signed as part of order
|
|
1119
|
+
*/
|
|
1120
|
+
amountsToSign: Amounts<T>;
|
|
1105
1121
|
}
|
|
1106
1122
|
|
|
1107
1123
|
/**
|
|
@@ -1113,6 +1129,18 @@ declare const ORDER_BOOK_PROD_CONFIG: ApiBaseUrls;
|
|
|
1113
1129
|
* An object containing *staging* environment base URLs for each supported `chainId`.
|
|
1114
1130
|
*/
|
|
1115
1131
|
declare const ORDER_BOOK_STAGING_CONFIG: ApiBaseUrls;
|
|
1132
|
+
/**
|
|
1133
|
+
* An object containing *partner production* environment base URLs for each supported `chainId`.
|
|
1134
|
+
* Used when apiKey is set; requests include X-API-Key header.
|
|
1135
|
+
* @see {@link https://partners.cow.fi}
|
|
1136
|
+
*/
|
|
1137
|
+
declare const ORDER_BOOK_PARTNER_PROD_CONFIG: ApiBaseUrls;
|
|
1138
|
+
/**
|
|
1139
|
+
* An object containing *partner staging* environment base URLs for each supported `chainId`.
|
|
1140
|
+
* Used when apiKey is set and env is staging; requests include X-API-Key header.
|
|
1141
|
+
* @see {@link https://partners.barn.cow.fi}
|
|
1142
|
+
*/
|
|
1143
|
+
declare const ORDER_BOOK_PARTNER_STAGING_CONFIG: ApiBaseUrls;
|
|
1116
1144
|
/**
|
|
1117
1145
|
* The parameters for the `getOrders` request.
|
|
1118
1146
|
*/
|
|
@@ -1328,8 +1356,9 @@ declare class OrderBookApi {
|
|
|
1328
1356
|
*/
|
|
1329
1357
|
private getContextWithOverride;
|
|
1330
1358
|
/**
|
|
1331
|
-
* Get the base URLs for the API endpoints given the
|
|
1332
|
-
*
|
|
1359
|
+
* Get the base URLs for the API endpoints given the context.
|
|
1360
|
+
* Uses partner URLs when apiKey is set (and no custom baseUrls override).
|
|
1361
|
+
* @param context The merged API context for the request.
|
|
1333
1362
|
* @returns The base URLs for the API endpoints.
|
|
1334
1363
|
*/
|
|
1335
1364
|
private getApiBaseUrls;
|
|
@@ -1385,44 +1414,136 @@ interface FetchParams {
|
|
|
1385
1414
|
* @param body The body of the request.
|
|
1386
1415
|
* @param rateLimiter The rate limiter to use.
|
|
1387
1416
|
* @param backoffOpts The backoff options to use.
|
|
1417
|
+
* @param additionalHeaders Optional additional headers (e.g. X-API-Key for Partner API).
|
|
1388
1418
|
* @returns The response of the request.
|
|
1389
1419
|
* @throws If the API returns an error or if the request fails.
|
|
1390
1420
|
*/
|
|
1391
|
-
declare function request<T>(baseUrl: string, { path, query, method, body }: FetchParams, rateLimiter: RateLimiter, backoffOpts: BackoffOptions): Promise<T>;
|
|
1421
|
+
declare function request<T>(baseUrl: string, { path, query, method, body }: FetchParams, rateLimiter: RateLimiter, backoffOpts: BackoffOptions, additionalHeaders?: Record<string, string>): Promise<T>;
|
|
1392
1422
|
|
|
1393
|
-
|
|
1394
|
-
|
|
1423
|
+
type OrderAmountsBig = {
|
|
1424
|
+
sellAmount: bigint;
|
|
1425
|
+
buyAmount: bigint;
|
|
1426
|
+
};
|
|
1427
|
+
interface QuoteParameters {
|
|
1395
1428
|
sellDecimals: number;
|
|
1396
1429
|
buyDecimals: number;
|
|
1397
|
-
|
|
1398
|
-
partnerFeeBps: number | undefined;
|
|
1430
|
+
orderParams: OrderParameters;
|
|
1399
1431
|
protocolFeeBps: number | undefined;
|
|
1400
1432
|
}
|
|
1401
|
-
|
|
1402
|
-
sellDecimals: number;
|
|
1403
|
-
buyDecimals: number;
|
|
1433
|
+
interface QuoteAmountsAndCostsParams {
|
|
1404
1434
|
orderParams: OrderParameters;
|
|
1405
|
-
protocolFeeBps
|
|
1406
|
-
|
|
1435
|
+
protocolFeeBps: number | undefined;
|
|
1436
|
+
partnerFeeBps: number | undefined;
|
|
1437
|
+
slippagePercentBps: number;
|
|
1438
|
+
}
|
|
1439
|
+
interface QuotePriceParams {
|
|
1440
|
+
numerator: bigint;
|
|
1441
|
+
denominator: bigint;
|
|
1442
|
+
}
|
|
1443
|
+
interface QuoteAmountsWithNetworkCosts {
|
|
1407
1444
|
isSell: boolean;
|
|
1408
|
-
|
|
1409
|
-
sellAmountAfterNetworkCosts: bigint;
|
|
1410
|
-
buyAmountAfterNetworkCosts: bigint;
|
|
1411
|
-
buyAmountBeforeNetworkCosts: bigint;
|
|
1445
|
+
quotePriceParams: QuotePriceParams;
|
|
1412
1446
|
networkCostAmount: bigint;
|
|
1413
1447
|
sellAmountBeforeNetworkCosts: bigint;
|
|
1414
|
-
|
|
1448
|
+
buyAmountAfterNetworkCosts: bigint;
|
|
1449
|
+
sellAmountAfterNetworkCosts: bigint;
|
|
1450
|
+
buyAmountBeforeNetworkCosts: bigint;
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
/**
|
|
1454
|
+
* Calculates all quote amount stages and costs from a `/quote` API response.
|
|
1455
|
+
*
|
|
1456
|
+
* Takes the raw order parameters (where protocol fee and network costs are already baked in)
|
|
1457
|
+
* and reconstructs every intermediate amount stage: before all fees, after protocol fees,
|
|
1458
|
+
* after network costs, after partner fees, and after slippage.
|
|
1459
|
+
*
|
|
1460
|
+
* The returned {@link QuoteAmountsAndCosts} includes `amountsToSign` — the final sell/buy
|
|
1461
|
+
* amounts that should be used when signing the order.
|
|
1462
|
+
*
|
|
1463
|
+
* @see {@link ./README.md} for a detailed explanation of the fee application order.
|
|
1464
|
+
*
|
|
1465
|
+
* @param params - Quote parameters including order params, fee BPS values, and slippage.
|
|
1466
|
+
* @returns All amount stages, cost breakdowns, and the amounts to sign.
|
|
1467
|
+
*/
|
|
1415
1468
|
declare function getQuoteAmountsAndCosts(params: QuoteAmountsAndCostsParams): QuoteAmountsAndCosts;
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1469
|
+
|
|
1470
|
+
interface QuoteAmountsAfterPartnerFeeParams {
|
|
1471
|
+
afterNetworkCosts: Amounts<bigint>;
|
|
1472
|
+
beforeAllFees: Amounts<bigint>;
|
|
1473
|
+
isSell: boolean;
|
|
1474
|
+
partnerFeeBps: number;
|
|
1475
|
+
}
|
|
1476
|
+
interface QuoteAmountsAfterPartnerFee {
|
|
1477
|
+
partnerFeeAmount: bigint;
|
|
1478
|
+
afterPartnerFees: OrderAmountsBig;
|
|
1479
|
+
}
|
|
1420
1480
|
/**
|
|
1421
|
-
*
|
|
1422
|
-
*
|
|
1423
|
-
*
|
|
1424
|
-
*
|
|
1481
|
+
* Calculates the partner fee amount and adjusts quote amounts accordingly.
|
|
1482
|
+
*
|
|
1483
|
+
* The partner fee is computed relative to the spot price (`beforeAllFees`) to ensure
|
|
1484
|
+
* it reflects the full trade volume, not the volume already reduced by protocol fee.
|
|
1485
|
+
*
|
|
1486
|
+
* - **SELL orders:** partner fee is subtracted from `buyAmount` (user receives less)
|
|
1487
|
+
* - **BUY orders:** partner fee is added to `sellAmount` (user pays more)
|
|
1488
|
+
*
|
|
1489
|
+
* @param params.afterNetworkCosts - Amounts after network costs, used as the base for adjustment.
|
|
1490
|
+
* @param params.beforeAllFees - Spot price amounts used to calculate the fee percentage.
|
|
1491
|
+
* @param params.isSell - Whether this is a sell order.
|
|
1492
|
+
* @param params.partnerFeeBps - Partner fee in basis points (0 = no partner fee).
|
|
1493
|
+
* @returns The partner fee amount and the adjusted amounts after partner fees.
|
|
1494
|
+
*/
|
|
1495
|
+
declare function getQuoteAmountsAfterPartnerFee(params: QuoteAmountsAfterPartnerFeeParams): QuoteAmountsAfterPartnerFee;
|
|
1496
|
+
|
|
1497
|
+
interface QuoteAmountsAfterSlippageParams {
|
|
1498
|
+
afterPartnerFees: OrderAmountsBig;
|
|
1499
|
+
isSell: boolean;
|
|
1500
|
+
slippageBps: number;
|
|
1501
|
+
}
|
|
1502
|
+
interface QuoteAmountsAfterSlippage {
|
|
1503
|
+
afterSlippage: OrderAmountsBig;
|
|
1504
|
+
}
|
|
1505
|
+
/**
|
|
1506
|
+
* Applies slippage tolerance to the quote amounts after all fees have been applied.
|
|
1507
|
+
*
|
|
1508
|
+
* Slippage protects the user from price movements between quoting and execution.
|
|
1509
|
+
* It is always applied to the non-fixed (surplus) side of the order:
|
|
1510
|
+
*
|
|
1511
|
+
* - **SELL orders:** slippage reduces `buyAmount` (user accepts receiving less)
|
|
1512
|
+
* - **BUY orders:** slippage increases `sellAmount` (user accepts paying more)
|
|
1513
|
+
*
|
|
1514
|
+
* @param params.afterPartnerFees - Amounts after partner fees have been applied.
|
|
1515
|
+
* @param params.isSell - Whether this is a sell order.
|
|
1516
|
+
* @param params.slippageBps - Slippage tolerance in basis points.
|
|
1517
|
+
* @returns The adjusted amounts after slippage tolerance.
|
|
1518
|
+
*/
|
|
1519
|
+
declare function getQuoteAmountsAfterSlippage(params: QuoteAmountsAfterSlippageParams): QuoteAmountsAfterSlippage;
|
|
1520
|
+
|
|
1521
|
+
interface ProtocolFeeAmountParams {
|
|
1522
|
+
orderParams: OrderParameters;
|
|
1523
|
+
protocolFeeBps: number;
|
|
1524
|
+
}
|
|
1525
|
+
/**
|
|
1526
|
+
* /quote API returns `OrderParameters` where protocol fee is already included in the amounts
|
|
1527
|
+
* From the quote response we only know:
|
|
1528
|
+
* - protocol fee percent (in BPS)
|
|
1529
|
+
* - quote amount after protocol fee
|
|
1530
|
+
*
|
|
1531
|
+
* To get the protocol fee amount, we need to derive the quote amount BEFORE the protocol fee first
|
|
1532
|
+
* On the API side `quoteAmountAfterProtocolFee` is calculated like that:
|
|
1533
|
+
*
|
|
1534
|
+
* protocolFeePercent = 0.02
|
|
1535
|
+
* quoteAmountBeforeProtocolFee = 100_000
|
|
1536
|
+
* protocolFeeAmount = 100_000 * 0.02 / 100 = 20
|
|
1537
|
+
* quoteAmountAfterProtocolFee = 100_000 - 20 = 99_980
|
|
1538
|
+
*
|
|
1539
|
+
* On the client side, we don't know `quoteAmountBeforeProtocolFee`, so we have to reverse it from `quoteAmountAfterProtocolFee`
|
|
1540
|
+
*
|
|
1541
|
+
* quoteAmountBeforeProtocolFee = 99_980 / (1 - 0.02 / 100) = 100_000
|
|
1542
|
+
*
|
|
1543
|
+
* Note: the example above is for SELL orders, for BUY orders the protocol fee is added to sellAmount instead of substracting from buyAmount
|
|
1544
|
+
*
|
|
1545
|
+
* @param params
|
|
1425
1546
|
*/
|
|
1426
|
-
declare function
|
|
1547
|
+
declare function getProtocolFeeAmount(params: ProtocolFeeAmountParams): bigint;
|
|
1427
1548
|
|
|
1428
|
-
export { type Address, type Amounts, type AppData, type AppDataHash, type AppDataObject, type Auction, type AuctionOrder, type AuctionPrices, type BigUint, BuyTokenDestination, type CallData, type CompetitionAuction, CompetitionOrderStatus, type Costs, DEFAULT_BACKOFF_OPTIONS, DEFAULT_LIMITER_OPTIONS, type EcdsaSignature, EcdsaSigningScheme, type EnrichedOrder, type EthflowData, type ExecutedAmounts, type ExecutedProtocolFee, type FeePolicy, type FetchParams, type GetOrdersRequest, type GetTradesRequest, type InteractionData, type NativePriceResponse, ORDER_BOOK_PROD_CONFIG, ORDER_BOOK_STAGING_CONFIG, OnchainOrderData, type Order, OrderBookApi, OrderBookApiError, type OrderCancellation, OrderCancellationError, type OrderCancellations, OrderClass, type OrderCreation, OrderKind, type OrderMetaData, type OrderParameters, OrderPostError, type OrderQuoteRequest, type OrderQuoteResponse, type OrderQuoteSide, OrderQuoteSideKindBuy, OrderQuoteSideKindSell, type OrderQuoteValidity, OrderStatus, type PreSignature, PriceEstimationError, type PriceImprovement, PriceQuality, type Quote, type QuoteAmountsAndCosts, type QuoteAmountsAndCostsParams, SellTokenSource, type Signature, SigningScheme, type SolverCompetitionResponse, type SolverSettlement, type Surplus, type TokenAmount, type TotalSurplus, type Trade, type TransactionHash, type UID, type Volume,
|
|
1549
|
+
export { type Address, type Amounts, type AppData, type AppDataHash, type AppDataObject, type Auction, type AuctionOrder, type AuctionPrices, type BigUint, BuyTokenDestination, type CallData, type CompetitionAuction, CompetitionOrderStatus, type Costs, DEFAULT_BACKOFF_OPTIONS, DEFAULT_LIMITER_OPTIONS, type EcdsaSignature, EcdsaSigningScheme, type EnrichedOrder, type EthflowData, type ExecutedAmounts, type ExecutedProtocolFee, type FeePolicy, type FetchParams, type GetOrdersRequest, type GetTradesRequest, type InteractionData, type NativePriceResponse, ORDER_BOOK_PARTNER_PROD_CONFIG, ORDER_BOOK_PARTNER_STAGING_CONFIG, ORDER_BOOK_PROD_CONFIG, ORDER_BOOK_STAGING_CONFIG, OnchainOrderData, type Order, type OrderAmountsBig, OrderBookApi, OrderBookApiError, type OrderCancellation, OrderCancellationError, type OrderCancellations, OrderClass, type OrderCreation, OrderKind, type OrderMetaData, type OrderParameters, OrderPostError, type OrderQuoteRequest, type OrderQuoteResponse, type OrderQuoteSide, OrderQuoteSideKindBuy, OrderQuoteSideKindSell, type OrderQuoteValidity, OrderStatus, type PreSignature, PriceEstimationError, type PriceImprovement, PriceQuality, type ProtocolFeeAmountParams, type Quote, type QuoteAmountsAfterSlippage, type QuoteAmountsAfterSlippageParams, type QuoteAmountsAndCosts, type QuoteAmountsAndCostsParams, type QuoteAmountsWithNetworkCosts, type QuoteParameters, type QuotePriceParams, SellTokenSource, type Signature, SigningScheme, type SolverCompetitionResponse, type SolverSettlement, type Surplus, type TokenAmount, type TotalSurplus, type Trade, type TransactionHash, type UID, type Volume, getProtocolFeeAmount, getQuoteAmountsAfterPartnerFee, getQuoteAmountsAfterSlippage, getQuoteAmountsAndCosts, request };
|
package/dist/index.d.ts
CHANGED
|
@@ -1049,18 +1049,22 @@ interface Costs<T> {
|
|
|
1049
1049
|
/**
|
|
1050
1050
|
* Details about costs and amounts, costs and fees of a quote.
|
|
1051
1051
|
*
|
|
1052
|
-
* CoW Protocol quote has amounts (sell/buy)
|
|
1053
|
-
*
|
|
1052
|
+
* CoW Protocol quote has amounts (sell/buy), network fee, and protocol fee.
|
|
1053
|
+
* On the client side (after /quote response) we add partner fee and slippage.
|
|
1054
|
+
* CoW Protocol supports both sell and buy orders and the fees and costs are calculated differently.
|
|
1054
1055
|
*
|
|
1055
1056
|
* The order of adding fees and costs is as follows:
|
|
1056
|
-
* 1.
|
|
1057
|
-
* 2. Partner fee is added to the surplus amount (sell amount for sell-orders, buy amount for buy-orders)
|
|
1058
|
-
* 3. Protocol fee is already baked into the quoted amounts:
|
|
1057
|
+
* 1. Protocol fee is already baked into the quoted amounts:
|
|
1059
1058
|
* - for SELL orders it has been deducted from the buy amount
|
|
1060
|
-
* - for BUY orders it has been added on top of the sell amount
|
|
1059
|
+
* - for BUY orders it has been added on top of the sell amount
|
|
1060
|
+
* 2. Network fee:
|
|
1061
|
+
* - for SELL orders it has been deducted from the sell amount
|
|
1062
|
+
* - for BUY orders it's not added to any amount, and provided separately as `feeAmount`
|
|
1063
|
+
* 3. Partner fee is added to the surplus amount (sell amount for sell-orders, buy amount for buy-orders)
|
|
1064
|
+
* 4. Slippage is added to the surplus amount (sell amount for sell-orders, buy amount for buy-orders)
|
|
1061
1065
|
*
|
|
1062
|
-
* For sell-orders the partner fee
|
|
1063
|
-
* For buy-orders the partner fee
|
|
1066
|
+
* For sell-orders the partner fee and slippage are subtracted from the buy amount after network costs.
|
|
1067
|
+
* For buy-orders the partner fee and slippage are added on top of the sell amount after network costs.
|
|
1064
1068
|
*/
|
|
1065
1069
|
interface QuoteAmountsAndCosts<T = bigint> {
|
|
1066
1070
|
/**
|
|
@@ -1077,6 +1081,9 @@ interface QuoteAmountsAndCosts<T = bigint> {
|
|
|
1077
1081
|
* so UIs can decide how to show it to the user.
|
|
1078
1082
|
*/
|
|
1079
1083
|
costs: Costs<T>;
|
|
1084
|
+
/**
|
|
1085
|
+
* Before all the fees and costs. Spot price amounts
|
|
1086
|
+
*/
|
|
1080
1087
|
beforeAllFees: Amounts<T>;
|
|
1081
1088
|
/**
|
|
1082
1089
|
* Amounts before network costs. This amount could be shown to the user to reflect how much they are expected to get
|
|
@@ -1084,17 +1091,22 @@ interface QuoteAmountsAndCosts<T = bigint> {
|
|
|
1084
1091
|
*/
|
|
1085
1092
|
beforeNetworkCosts: Amounts<T>;
|
|
1086
1093
|
/**
|
|
1087
|
-
*
|
|
1094
|
+
* 1. Same with beforeNetworkCosts.
|
|
1095
|
+
* `beforeNetworkCosts` was here even before protocol fee existed, and we keep it for backward compatibility
|
|
1096
|
+
*/
|
|
1097
|
+
afterProtocolFees: Amounts<T>;
|
|
1098
|
+
/**
|
|
1099
|
+
* 2. Amounts after including network costs.
|
|
1088
1100
|
*/
|
|
1089
1101
|
afterNetworkCosts: Amounts<T>;
|
|
1090
1102
|
/**
|
|
1091
|
-
* Amounts after including partner fees (if any).
|
|
1103
|
+
* 3. Amounts after including partner fees (if any).
|
|
1092
1104
|
*
|
|
1093
1105
|
* This amount could be shown to the user, as the expected to receive amount already including any fees or costs.
|
|
1094
1106
|
*/
|
|
1095
1107
|
afterPartnerFees: Amounts<T>;
|
|
1096
1108
|
/**
|
|
1097
|
-
* Amounts after including the slippage tolerance.
|
|
1109
|
+
* 4. Amounts after including the slippage tolerance.
|
|
1098
1110
|
*
|
|
1099
1111
|
* This is the minimum that the user will receive and the amount they will sign.
|
|
1100
1112
|
*
|
|
@@ -1102,6 +1114,10 @@ interface QuoteAmountsAndCosts<T = bigint> {
|
|
|
1102
1114
|
* event of the buy token appreciating.
|
|
1103
1115
|
*/
|
|
1104
1116
|
afterSlippage: Amounts<T>;
|
|
1117
|
+
/**
|
|
1118
|
+
* Amounts that supposed to be signed as part of order
|
|
1119
|
+
*/
|
|
1120
|
+
amountsToSign: Amounts<T>;
|
|
1105
1121
|
}
|
|
1106
1122
|
|
|
1107
1123
|
/**
|
|
@@ -1113,6 +1129,18 @@ declare const ORDER_BOOK_PROD_CONFIG: ApiBaseUrls;
|
|
|
1113
1129
|
* An object containing *staging* environment base URLs for each supported `chainId`.
|
|
1114
1130
|
*/
|
|
1115
1131
|
declare const ORDER_BOOK_STAGING_CONFIG: ApiBaseUrls;
|
|
1132
|
+
/**
|
|
1133
|
+
* An object containing *partner production* environment base URLs for each supported `chainId`.
|
|
1134
|
+
* Used when apiKey is set; requests include X-API-Key header.
|
|
1135
|
+
* @see {@link https://partners.cow.fi}
|
|
1136
|
+
*/
|
|
1137
|
+
declare const ORDER_BOOK_PARTNER_PROD_CONFIG: ApiBaseUrls;
|
|
1138
|
+
/**
|
|
1139
|
+
* An object containing *partner staging* environment base URLs for each supported `chainId`.
|
|
1140
|
+
* Used when apiKey is set and env is staging; requests include X-API-Key header.
|
|
1141
|
+
* @see {@link https://partners.barn.cow.fi}
|
|
1142
|
+
*/
|
|
1143
|
+
declare const ORDER_BOOK_PARTNER_STAGING_CONFIG: ApiBaseUrls;
|
|
1116
1144
|
/**
|
|
1117
1145
|
* The parameters for the `getOrders` request.
|
|
1118
1146
|
*/
|
|
@@ -1328,8 +1356,9 @@ declare class OrderBookApi {
|
|
|
1328
1356
|
*/
|
|
1329
1357
|
private getContextWithOverride;
|
|
1330
1358
|
/**
|
|
1331
|
-
* Get the base URLs for the API endpoints given the
|
|
1332
|
-
*
|
|
1359
|
+
* Get the base URLs for the API endpoints given the context.
|
|
1360
|
+
* Uses partner URLs when apiKey is set (and no custom baseUrls override).
|
|
1361
|
+
* @param context The merged API context for the request.
|
|
1333
1362
|
* @returns The base URLs for the API endpoints.
|
|
1334
1363
|
*/
|
|
1335
1364
|
private getApiBaseUrls;
|
|
@@ -1385,44 +1414,136 @@ interface FetchParams {
|
|
|
1385
1414
|
* @param body The body of the request.
|
|
1386
1415
|
* @param rateLimiter The rate limiter to use.
|
|
1387
1416
|
* @param backoffOpts The backoff options to use.
|
|
1417
|
+
* @param additionalHeaders Optional additional headers (e.g. X-API-Key for Partner API).
|
|
1388
1418
|
* @returns The response of the request.
|
|
1389
1419
|
* @throws If the API returns an error or if the request fails.
|
|
1390
1420
|
*/
|
|
1391
|
-
declare function request<T>(baseUrl: string, { path, query, method, body }: FetchParams, rateLimiter: RateLimiter, backoffOpts: BackoffOptions): Promise<T>;
|
|
1421
|
+
declare function request<T>(baseUrl: string, { path, query, method, body }: FetchParams, rateLimiter: RateLimiter, backoffOpts: BackoffOptions, additionalHeaders?: Record<string, string>): Promise<T>;
|
|
1392
1422
|
|
|
1393
|
-
|
|
1394
|
-
|
|
1423
|
+
type OrderAmountsBig = {
|
|
1424
|
+
sellAmount: bigint;
|
|
1425
|
+
buyAmount: bigint;
|
|
1426
|
+
};
|
|
1427
|
+
interface QuoteParameters {
|
|
1395
1428
|
sellDecimals: number;
|
|
1396
1429
|
buyDecimals: number;
|
|
1397
|
-
|
|
1398
|
-
partnerFeeBps: number | undefined;
|
|
1430
|
+
orderParams: OrderParameters;
|
|
1399
1431
|
protocolFeeBps: number | undefined;
|
|
1400
1432
|
}
|
|
1401
|
-
|
|
1402
|
-
sellDecimals: number;
|
|
1403
|
-
buyDecimals: number;
|
|
1433
|
+
interface QuoteAmountsAndCostsParams {
|
|
1404
1434
|
orderParams: OrderParameters;
|
|
1405
|
-
protocolFeeBps
|
|
1406
|
-
|
|
1435
|
+
protocolFeeBps: number | undefined;
|
|
1436
|
+
partnerFeeBps: number | undefined;
|
|
1437
|
+
slippagePercentBps: number;
|
|
1438
|
+
}
|
|
1439
|
+
interface QuotePriceParams {
|
|
1440
|
+
numerator: bigint;
|
|
1441
|
+
denominator: bigint;
|
|
1442
|
+
}
|
|
1443
|
+
interface QuoteAmountsWithNetworkCosts {
|
|
1407
1444
|
isSell: boolean;
|
|
1408
|
-
|
|
1409
|
-
sellAmountAfterNetworkCosts: bigint;
|
|
1410
|
-
buyAmountAfterNetworkCosts: bigint;
|
|
1411
|
-
buyAmountBeforeNetworkCosts: bigint;
|
|
1445
|
+
quotePriceParams: QuotePriceParams;
|
|
1412
1446
|
networkCostAmount: bigint;
|
|
1413
1447
|
sellAmountBeforeNetworkCosts: bigint;
|
|
1414
|
-
|
|
1448
|
+
buyAmountAfterNetworkCosts: bigint;
|
|
1449
|
+
sellAmountAfterNetworkCosts: bigint;
|
|
1450
|
+
buyAmountBeforeNetworkCosts: bigint;
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
/**
|
|
1454
|
+
* Calculates all quote amount stages and costs from a `/quote` API response.
|
|
1455
|
+
*
|
|
1456
|
+
* Takes the raw order parameters (where protocol fee and network costs are already baked in)
|
|
1457
|
+
* and reconstructs every intermediate amount stage: before all fees, after protocol fees,
|
|
1458
|
+
* after network costs, after partner fees, and after slippage.
|
|
1459
|
+
*
|
|
1460
|
+
* The returned {@link QuoteAmountsAndCosts} includes `amountsToSign` — the final sell/buy
|
|
1461
|
+
* amounts that should be used when signing the order.
|
|
1462
|
+
*
|
|
1463
|
+
* @see {@link ./README.md} for a detailed explanation of the fee application order.
|
|
1464
|
+
*
|
|
1465
|
+
* @param params - Quote parameters including order params, fee BPS values, and slippage.
|
|
1466
|
+
* @returns All amount stages, cost breakdowns, and the amounts to sign.
|
|
1467
|
+
*/
|
|
1415
1468
|
declare function getQuoteAmountsAndCosts(params: QuoteAmountsAndCostsParams): QuoteAmountsAndCosts;
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1469
|
+
|
|
1470
|
+
interface QuoteAmountsAfterPartnerFeeParams {
|
|
1471
|
+
afterNetworkCosts: Amounts<bigint>;
|
|
1472
|
+
beforeAllFees: Amounts<bigint>;
|
|
1473
|
+
isSell: boolean;
|
|
1474
|
+
partnerFeeBps: number;
|
|
1475
|
+
}
|
|
1476
|
+
interface QuoteAmountsAfterPartnerFee {
|
|
1477
|
+
partnerFeeAmount: bigint;
|
|
1478
|
+
afterPartnerFees: OrderAmountsBig;
|
|
1479
|
+
}
|
|
1420
1480
|
/**
|
|
1421
|
-
*
|
|
1422
|
-
*
|
|
1423
|
-
*
|
|
1424
|
-
*
|
|
1481
|
+
* Calculates the partner fee amount and adjusts quote amounts accordingly.
|
|
1482
|
+
*
|
|
1483
|
+
* The partner fee is computed relative to the spot price (`beforeAllFees`) to ensure
|
|
1484
|
+
* it reflects the full trade volume, not the volume already reduced by protocol fee.
|
|
1485
|
+
*
|
|
1486
|
+
* - **SELL orders:** partner fee is subtracted from `buyAmount` (user receives less)
|
|
1487
|
+
* - **BUY orders:** partner fee is added to `sellAmount` (user pays more)
|
|
1488
|
+
*
|
|
1489
|
+
* @param params.afterNetworkCosts - Amounts after network costs, used as the base for adjustment.
|
|
1490
|
+
* @param params.beforeAllFees - Spot price amounts used to calculate the fee percentage.
|
|
1491
|
+
* @param params.isSell - Whether this is a sell order.
|
|
1492
|
+
* @param params.partnerFeeBps - Partner fee in basis points (0 = no partner fee).
|
|
1493
|
+
* @returns The partner fee amount and the adjusted amounts after partner fees.
|
|
1494
|
+
*/
|
|
1495
|
+
declare function getQuoteAmountsAfterPartnerFee(params: QuoteAmountsAfterPartnerFeeParams): QuoteAmountsAfterPartnerFee;
|
|
1496
|
+
|
|
1497
|
+
interface QuoteAmountsAfterSlippageParams {
|
|
1498
|
+
afterPartnerFees: OrderAmountsBig;
|
|
1499
|
+
isSell: boolean;
|
|
1500
|
+
slippageBps: number;
|
|
1501
|
+
}
|
|
1502
|
+
interface QuoteAmountsAfterSlippage {
|
|
1503
|
+
afterSlippage: OrderAmountsBig;
|
|
1504
|
+
}
|
|
1505
|
+
/**
|
|
1506
|
+
* Applies slippage tolerance to the quote amounts after all fees have been applied.
|
|
1507
|
+
*
|
|
1508
|
+
* Slippage protects the user from price movements between quoting and execution.
|
|
1509
|
+
* It is always applied to the non-fixed (surplus) side of the order:
|
|
1510
|
+
*
|
|
1511
|
+
* - **SELL orders:** slippage reduces `buyAmount` (user accepts receiving less)
|
|
1512
|
+
* - **BUY orders:** slippage increases `sellAmount` (user accepts paying more)
|
|
1513
|
+
*
|
|
1514
|
+
* @param params.afterPartnerFees - Amounts after partner fees have been applied.
|
|
1515
|
+
* @param params.isSell - Whether this is a sell order.
|
|
1516
|
+
* @param params.slippageBps - Slippage tolerance in basis points.
|
|
1517
|
+
* @returns The adjusted amounts after slippage tolerance.
|
|
1518
|
+
*/
|
|
1519
|
+
declare function getQuoteAmountsAfterSlippage(params: QuoteAmountsAfterSlippageParams): QuoteAmountsAfterSlippage;
|
|
1520
|
+
|
|
1521
|
+
interface ProtocolFeeAmountParams {
|
|
1522
|
+
orderParams: OrderParameters;
|
|
1523
|
+
protocolFeeBps: number;
|
|
1524
|
+
}
|
|
1525
|
+
/**
|
|
1526
|
+
* /quote API returns `OrderParameters` where protocol fee is already included in the amounts
|
|
1527
|
+
* From the quote response we only know:
|
|
1528
|
+
* - protocol fee percent (in BPS)
|
|
1529
|
+
* - quote amount after protocol fee
|
|
1530
|
+
*
|
|
1531
|
+
* To get the protocol fee amount, we need to derive the quote amount BEFORE the protocol fee first
|
|
1532
|
+
* On the API side `quoteAmountAfterProtocolFee` is calculated like that:
|
|
1533
|
+
*
|
|
1534
|
+
* protocolFeePercent = 0.02
|
|
1535
|
+
* quoteAmountBeforeProtocolFee = 100_000
|
|
1536
|
+
* protocolFeeAmount = 100_000 * 0.02 / 100 = 20
|
|
1537
|
+
* quoteAmountAfterProtocolFee = 100_000 - 20 = 99_980
|
|
1538
|
+
*
|
|
1539
|
+
* On the client side, we don't know `quoteAmountBeforeProtocolFee`, so we have to reverse it from `quoteAmountAfterProtocolFee`
|
|
1540
|
+
*
|
|
1541
|
+
* quoteAmountBeforeProtocolFee = 99_980 / (1 - 0.02 / 100) = 100_000
|
|
1542
|
+
*
|
|
1543
|
+
* Note: the example above is for SELL orders, for BUY orders the protocol fee is added to sellAmount instead of substracting from buyAmount
|
|
1544
|
+
*
|
|
1545
|
+
* @param params
|
|
1425
1546
|
*/
|
|
1426
|
-
declare function
|
|
1547
|
+
declare function getProtocolFeeAmount(params: ProtocolFeeAmountParams): bigint;
|
|
1427
1548
|
|
|
1428
|
-
export { type Address, type Amounts, type AppData, type AppDataHash, type AppDataObject, type Auction, type AuctionOrder, type AuctionPrices, type BigUint, BuyTokenDestination, type CallData, type CompetitionAuction, CompetitionOrderStatus, type Costs, DEFAULT_BACKOFF_OPTIONS, DEFAULT_LIMITER_OPTIONS, type EcdsaSignature, EcdsaSigningScheme, type EnrichedOrder, type EthflowData, type ExecutedAmounts, type ExecutedProtocolFee, type FeePolicy, type FetchParams, type GetOrdersRequest, type GetTradesRequest, type InteractionData, type NativePriceResponse, ORDER_BOOK_PROD_CONFIG, ORDER_BOOK_STAGING_CONFIG, OnchainOrderData, type Order, OrderBookApi, OrderBookApiError, type OrderCancellation, OrderCancellationError, type OrderCancellations, OrderClass, type OrderCreation, OrderKind, type OrderMetaData, type OrderParameters, OrderPostError, type OrderQuoteRequest, type OrderQuoteResponse, type OrderQuoteSide, OrderQuoteSideKindBuy, OrderQuoteSideKindSell, type OrderQuoteValidity, OrderStatus, type PreSignature, PriceEstimationError, type PriceImprovement, PriceQuality, type Quote, type QuoteAmountsAndCosts, type QuoteAmountsAndCostsParams, SellTokenSource, type Signature, SigningScheme, type SolverCompetitionResponse, type SolverSettlement, type Surplus, type TokenAmount, type TotalSurplus, type Trade, type TransactionHash, type UID, type Volume,
|
|
1549
|
+
export { type Address, type Amounts, type AppData, type AppDataHash, type AppDataObject, type Auction, type AuctionOrder, type AuctionPrices, type BigUint, BuyTokenDestination, type CallData, type CompetitionAuction, CompetitionOrderStatus, type Costs, DEFAULT_BACKOFF_OPTIONS, DEFAULT_LIMITER_OPTIONS, type EcdsaSignature, EcdsaSigningScheme, type EnrichedOrder, type EthflowData, type ExecutedAmounts, type ExecutedProtocolFee, type FeePolicy, type FetchParams, type GetOrdersRequest, type GetTradesRequest, type InteractionData, type NativePriceResponse, ORDER_BOOK_PARTNER_PROD_CONFIG, ORDER_BOOK_PARTNER_STAGING_CONFIG, ORDER_BOOK_PROD_CONFIG, ORDER_BOOK_STAGING_CONFIG, OnchainOrderData, type Order, type OrderAmountsBig, OrderBookApi, OrderBookApiError, type OrderCancellation, OrderCancellationError, type OrderCancellations, OrderClass, type OrderCreation, OrderKind, type OrderMetaData, type OrderParameters, OrderPostError, type OrderQuoteRequest, type OrderQuoteResponse, type OrderQuoteSide, OrderQuoteSideKindBuy, OrderQuoteSideKindSell, type OrderQuoteValidity, OrderStatus, type PreSignature, PriceEstimationError, type PriceImprovement, PriceQuality, type ProtocolFeeAmountParams, type Quote, type QuoteAmountsAfterSlippage, type QuoteAmountsAfterSlippageParams, type QuoteAmountsAndCosts, type QuoteAmountsAndCostsParams, type QuoteAmountsWithNetworkCosts, type QuoteParameters, type QuotePriceParams, SellTokenSource, type Signature, SigningScheme, type SolverCompetitionResponse, type SolverSettlement, type Surplus, type TokenAmount, type TotalSurplus, type Trade, type TransactionHash, type UID, type Volume, getProtocolFeeAmount, getQuoteAmountsAfterPartnerFee, getQuoteAmountsAfterSlippage, getQuoteAmountsAndCosts, request };
|