@cowprotocol/sdk-trading 0.1.3 → 0.2.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/dist/index.d.mts +15 -1
- package/dist/index.d.ts +15 -1
- package/dist/index.js +79 -44
- package/dist/index.mjs +65 -30
- package/package.json +9 -9
package/dist/index.d.mts
CHANGED
|
@@ -69,6 +69,16 @@ interface TraderParameters {
|
|
|
69
69
|
type QuoterParameters = Omit<TraderParameters, 'signer'> & {
|
|
70
70
|
account: AccountAddress;
|
|
71
71
|
};
|
|
72
|
+
interface SlippageToleranceResponse {
|
|
73
|
+
slippageBps: number | null;
|
|
74
|
+
}
|
|
75
|
+
interface SlippageToleranceRequest {
|
|
76
|
+
chainId: SupportedChainId;
|
|
77
|
+
sellToken: OrderParameters['sellToken'];
|
|
78
|
+
buyToken: OrderParameters['buyToken'];
|
|
79
|
+
sellAmount?: bigint;
|
|
80
|
+
buyAmount?: bigint;
|
|
81
|
+
}
|
|
72
82
|
/**
|
|
73
83
|
* Trade type, assets, amounts, and optional parameters.
|
|
74
84
|
*/
|
|
@@ -108,6 +118,10 @@ interface SwapAdvancedSettings {
|
|
|
108
118
|
appData?: AppDataParams;
|
|
109
119
|
additionalParams?: PostTradeAdditionalParams;
|
|
110
120
|
quoteSigner?: SignerLike;
|
|
121
|
+
/**
|
|
122
|
+
* An optional callback to use custom logic for slippage suggestion
|
|
123
|
+
*/
|
|
124
|
+
getSlippageSuggestion?(request: SlippageToleranceRequest): Promise<SlippageToleranceResponse>;
|
|
111
125
|
}
|
|
112
126
|
interface LimitOrderAdvancedSettings {
|
|
113
127
|
appData?: AppDataParams;
|
|
@@ -333,4 +347,4 @@ declare function getTradeParametersAfterQuote({ quoteParameters, sellToken, }: {
|
|
|
333
347
|
sellToken: string;
|
|
334
348
|
}): TradeParameters;
|
|
335
349
|
|
|
336
|
-
export { type AppDataRootSchema, type BuildAppDataParams, type EthFlowOrderExistsCallback, type LimitOrderAdvancedSettings, type LimitOrderParameters, type LimitTradeParameters, type LimitTradeParametersFromQuote, ORDER_PRIMARY_TYPE, type OrderPostingResult, type OrderTypedData, type PostTradeAdditionalParams, type QuoteAndPost, type QuoteResults, type QuoteResultsSerialized, type QuoteResultsWithSigner, type QuoterParameters, type SigningStepManager, type SwapAdvancedSettings, type SwapParameters, type TradeBaseParameters, type TradeOptionalParameters, type TradeParameters, type TraderParameters, type TradingAppDataInfo, TradingSdk, type TradingSdkOptions, type TradingTransactionParams, type WithPartialTraderParams, buildAppData, calculateUniqueOrderId, generateAppDataFromDoc, getEthFlowContract, getEthFlowTransaction, getOrderToSign, getPartnerFeeBps, getPreSignTransaction, getQuote, getQuoteWithSigner, getTradeParametersAfterQuote, mapQuoteAmountsAndCosts, mergeAppDataDoc, postCoWProtocolTrade, postLimitOrder, postSellNativeCurrencyOrder, postSwapOrder, postSwapOrderFromQuote, suggestSlippageBps, swapParamsToLimitOrderParams };
|
|
350
|
+
export { type AppDataRootSchema, type BuildAppDataParams, type EthFlowOrderExistsCallback, type LimitOrderAdvancedSettings, type LimitOrderParameters, type LimitTradeParameters, type LimitTradeParametersFromQuote, ORDER_PRIMARY_TYPE, type OrderPostingResult, type OrderTypedData, type PostTradeAdditionalParams, type QuoteAndPost, type QuoteResults, type QuoteResultsSerialized, type QuoteResultsWithSigner, type QuoterParameters, type SigningStepManager, type SlippageToleranceRequest, type SlippageToleranceResponse, type SwapAdvancedSettings, type SwapParameters, type TradeBaseParameters, type TradeOptionalParameters, type TradeParameters, type TraderParameters, type TradingAppDataInfo, TradingSdk, type TradingSdkOptions, type TradingTransactionParams, type WithPartialTraderParams, buildAppData, calculateUniqueOrderId, generateAppDataFromDoc, getEthFlowContract, getEthFlowTransaction, getOrderToSign, getPartnerFeeBps, getPreSignTransaction, getQuote, getQuoteWithSigner, getTradeParametersAfterQuote, mapQuoteAmountsAndCosts, mergeAppDataDoc, postCoWProtocolTrade, postLimitOrder, postSellNativeCurrencyOrder, postSwapOrder, postSwapOrderFromQuote, suggestSlippageBps, swapParamsToLimitOrderParams };
|
package/dist/index.d.ts
CHANGED
|
@@ -69,6 +69,16 @@ interface TraderParameters {
|
|
|
69
69
|
type QuoterParameters = Omit<TraderParameters, 'signer'> & {
|
|
70
70
|
account: AccountAddress;
|
|
71
71
|
};
|
|
72
|
+
interface SlippageToleranceResponse {
|
|
73
|
+
slippageBps: number | null;
|
|
74
|
+
}
|
|
75
|
+
interface SlippageToleranceRequest {
|
|
76
|
+
chainId: SupportedChainId;
|
|
77
|
+
sellToken: OrderParameters['sellToken'];
|
|
78
|
+
buyToken: OrderParameters['buyToken'];
|
|
79
|
+
sellAmount?: bigint;
|
|
80
|
+
buyAmount?: bigint;
|
|
81
|
+
}
|
|
72
82
|
/**
|
|
73
83
|
* Trade type, assets, amounts, and optional parameters.
|
|
74
84
|
*/
|
|
@@ -108,6 +118,10 @@ interface SwapAdvancedSettings {
|
|
|
108
118
|
appData?: AppDataParams;
|
|
109
119
|
additionalParams?: PostTradeAdditionalParams;
|
|
110
120
|
quoteSigner?: SignerLike;
|
|
121
|
+
/**
|
|
122
|
+
* An optional callback to use custom logic for slippage suggestion
|
|
123
|
+
*/
|
|
124
|
+
getSlippageSuggestion?(request: SlippageToleranceRequest): Promise<SlippageToleranceResponse>;
|
|
111
125
|
}
|
|
112
126
|
interface LimitOrderAdvancedSettings {
|
|
113
127
|
appData?: AppDataParams;
|
|
@@ -333,4 +347,4 @@ declare function getTradeParametersAfterQuote({ quoteParameters, sellToken, }: {
|
|
|
333
347
|
sellToken: string;
|
|
334
348
|
}): TradeParameters;
|
|
335
349
|
|
|
336
|
-
export { type AppDataRootSchema, type BuildAppDataParams, type EthFlowOrderExistsCallback, type LimitOrderAdvancedSettings, type LimitOrderParameters, type LimitTradeParameters, type LimitTradeParametersFromQuote, ORDER_PRIMARY_TYPE, type OrderPostingResult, type OrderTypedData, type PostTradeAdditionalParams, type QuoteAndPost, type QuoteResults, type QuoteResultsSerialized, type QuoteResultsWithSigner, type QuoterParameters, type SigningStepManager, type SwapAdvancedSettings, type SwapParameters, type TradeBaseParameters, type TradeOptionalParameters, type TradeParameters, type TraderParameters, type TradingAppDataInfo, TradingSdk, type TradingSdkOptions, type TradingTransactionParams, type WithPartialTraderParams, buildAppData, calculateUniqueOrderId, generateAppDataFromDoc, getEthFlowContract, getEthFlowTransaction, getOrderToSign, getPartnerFeeBps, getPreSignTransaction, getQuote, getQuoteWithSigner, getTradeParametersAfterQuote, mapQuoteAmountsAndCosts, mergeAppDataDoc, postCoWProtocolTrade, postLimitOrder, postSellNativeCurrencyOrder, postSwapOrder, postSwapOrderFromQuote, suggestSlippageBps, swapParamsToLimitOrderParams };
|
|
350
|
+
export { type AppDataRootSchema, type BuildAppDataParams, type EthFlowOrderExistsCallback, type LimitOrderAdvancedSettings, type LimitOrderParameters, type LimitTradeParameters, type LimitTradeParametersFromQuote, ORDER_PRIMARY_TYPE, type OrderPostingResult, type OrderTypedData, type PostTradeAdditionalParams, type QuoteAndPost, type QuoteResults, type QuoteResultsSerialized, type QuoteResultsWithSigner, type QuoterParameters, type SigningStepManager, type SlippageToleranceRequest, type SlippageToleranceResponse, type SwapAdvancedSettings, type SwapParameters, type TradeBaseParameters, type TradeOptionalParameters, type TradeParameters, type TraderParameters, type TradingAppDataInfo, TradingSdk, type TradingSdkOptions, type TradingTransactionParams, type WithPartialTraderParams, buildAppData, calculateUniqueOrderId, generateAppDataFromDoc, getEthFlowContract, getEthFlowTransaction, getOrderToSign, getPartnerFeeBps, getPreSignTransaction, getQuote, getQuoteWithSigner, getTradeParametersAfterQuote, mapQuoteAmountsAndCosts, mergeAppDataDoc, postCoWProtocolTrade, postLimitOrder, postSellNativeCurrencyOrder, postSwapOrder, postSwapOrderFromQuote, suggestSlippageBps, swapParamsToLimitOrderParams };
|
package/dist/index.js
CHANGED
|
@@ -63,12 +63,7 @@ var import_sdk_order_book = require("@cowprotocol/sdk-order-book");
|
|
|
63
63
|
var import_sdk_config = require("@cowprotocol/sdk-config");
|
|
64
64
|
var DEFAULT_QUOTE_VALIDITY = 60 * 30;
|
|
65
65
|
var DEFAULT_SLIPPAGE_BPS = 50;
|
|
66
|
-
var ETH_FLOW_DEFAULT_SLIPPAGE_BPS =
|
|
67
|
-
...(0, import_sdk_config.mapSupportedNetworks)(DEFAULT_SLIPPAGE_BPS),
|
|
68
|
-
// 0.5% by default for most chains
|
|
69
|
-
[import_sdk_config.SupportedChainId.MAINNET]: 200
|
|
70
|
-
// 2% for mainnet
|
|
71
|
-
};
|
|
66
|
+
var ETH_FLOW_DEFAULT_SLIPPAGE_BPS = (0, import_sdk_config.mapSupportedNetworks)(DEFAULT_SLIPPAGE_BPS);
|
|
72
67
|
var SIGN_SCHEME_MAP = {
|
|
73
68
|
[import_sdk_order_book.EcdsaSigningScheme.EIP712]: import_sdk_order_book.SigningScheme.EIP712,
|
|
74
69
|
[import_sdk_order_book.EcdsaSigningScheme.ETHSIGN]: import_sdk_order_book.SigningScheme.ETHSIGN
|
|
@@ -409,8 +404,8 @@ async function postCoWProtocolTrade(orderBookApi, appData, params, additionalPar
|
|
|
409
404
|
}
|
|
410
405
|
|
|
411
406
|
// src/getQuote.ts
|
|
412
|
-
var
|
|
413
|
-
var
|
|
407
|
+
var import_sdk_common10 = require("@cowprotocol/sdk-common");
|
|
408
|
+
var import_sdk_order_book7 = require("@cowprotocol/sdk-order-book");
|
|
414
409
|
|
|
415
410
|
// src/appDataUtils.ts
|
|
416
411
|
var import_sdk_app_data = require("@cowprotocol/sdk-app-data");
|
|
@@ -552,9 +547,50 @@ function suggestSlippageBps(params) {
|
|
|
552
547
|
return Math.max(Math.min(slippageBps, MAX_SLIPPAGE_BPS), getDefaultSlippageBps(trader.chainId, isEthFlow));
|
|
553
548
|
}
|
|
554
549
|
|
|
550
|
+
// src/resolveSlippageSuggestion.ts
|
|
551
|
+
var import_sdk_order_book6 = require("@cowprotocol/sdk-order-book");
|
|
552
|
+
var import_sdk_common9 = require("@cowprotocol/sdk-common");
|
|
553
|
+
async function resolveSlippageSuggestion(chainId, tradeParameters, trader, quote, isEthFlow, advancedSettings) {
|
|
554
|
+
const suggestSlippageParams = {
|
|
555
|
+
isEthFlow,
|
|
556
|
+
quote,
|
|
557
|
+
tradeParameters,
|
|
558
|
+
trader,
|
|
559
|
+
advancedSettings
|
|
560
|
+
};
|
|
561
|
+
const getSlippageSuggestion = advancedSettings?.getSlippageSuggestion;
|
|
562
|
+
const priceQuality = advancedSettings?.quoteRequest?.priceQuality ?? import_sdk_order_book6.PriceQuality.OPTIMAL;
|
|
563
|
+
const defaultSuggestion = suggestSlippageBps(suggestSlippageParams);
|
|
564
|
+
if (priceQuality === import_sdk_order_book6.PriceQuality.FAST || !getSlippageSuggestion) {
|
|
565
|
+
return { slippageBps: defaultSuggestion };
|
|
566
|
+
}
|
|
567
|
+
const amountsAndCosts = (0, import_sdk_order_book6.getQuoteAmountsAndCosts)({
|
|
568
|
+
orderParams: quote.quote,
|
|
569
|
+
slippagePercentBps: 0,
|
|
570
|
+
partnerFeeBps: getPartnerFeeBps(tradeParameters.partnerFee),
|
|
571
|
+
sellDecimals: tradeParameters.sellTokenDecimals,
|
|
572
|
+
buyDecimals: tradeParameters.buyTokenDecimals
|
|
573
|
+
});
|
|
574
|
+
try {
|
|
575
|
+
const suggestedSlippage = await getSlippageSuggestion({
|
|
576
|
+
chainId,
|
|
577
|
+
sellToken: tradeParameters.sellToken,
|
|
578
|
+
buyToken: tradeParameters.buyToken,
|
|
579
|
+
sellAmount: amountsAndCosts.afterSlippage.sellAmount,
|
|
580
|
+
buyAmount: amountsAndCosts.afterSlippage.buyAmount
|
|
581
|
+
});
|
|
582
|
+
return {
|
|
583
|
+
slippageBps: Math.max(suggestedSlippage.slippageBps ?? 0, defaultSuggestion)
|
|
584
|
+
};
|
|
585
|
+
} catch (e) {
|
|
586
|
+
(0, import_sdk_common9.log)(`getSlippageSuggestion() error: ${e.message || String(e)}`);
|
|
587
|
+
return { slippageBps: defaultSuggestion };
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
555
591
|
// src/getQuote.ts
|
|
556
592
|
var ETH_FLOW_AUX_QUOTE_PARAMS = {
|
|
557
|
-
signingScheme:
|
|
593
|
+
signingScheme: import_sdk_order_book7.SigningScheme.EIP1271,
|
|
558
594
|
onchainOrder: true,
|
|
559
595
|
// Ethflow orders are subsidized in the backend.
|
|
560
596
|
// This means we can assume the verification gas costs are zero for the quote/fee estimation
|
|
@@ -574,13 +610,13 @@ async function getQuoteRaw(_tradeParameters, trader, advancedSettings, _orderBoo
|
|
|
574
610
|
slippageBps,
|
|
575
611
|
env = "prod"
|
|
576
612
|
} = tradeParameters;
|
|
577
|
-
(0,
|
|
613
|
+
(0, import_sdk_common10.log)(
|
|
578
614
|
`getQuote for: Swap ${amount} ${sellToken} for ${buyToken} on chain ${chainId} with ${slippageBps !== void 0 ? `${slippageBps} BPS` : "AUTO"} slippage`
|
|
579
615
|
);
|
|
580
|
-
const orderBookApi = _orderBookApi || new
|
|
616
|
+
const orderBookApi = _orderBookApi || new import_sdk_order_book7.OrderBookApi({ chainId, env });
|
|
581
617
|
const receiver = tradeParameters.receiver || from;
|
|
582
618
|
const isSell = kind === "sell";
|
|
583
|
-
(0,
|
|
619
|
+
(0, import_sdk_common10.log)("Building app data...");
|
|
584
620
|
const defaultSlippageBps = getDefaultSlippageBps(chainId, isEthFlow);
|
|
585
621
|
const slippageBpsOrDefault = slippageBps ?? defaultSlippageBps;
|
|
586
622
|
const buildAppDataParams = {
|
|
@@ -591,7 +627,7 @@ async function getQuoteRaw(_tradeParameters, trader, advancedSettings, _orderBoo
|
|
|
591
627
|
};
|
|
592
628
|
const appDataInfo = await buildAppData(buildAppDataParams, advancedSettings?.appData);
|
|
593
629
|
const { appDataKeccak256, fullAppData } = appDataInfo;
|
|
594
|
-
(0,
|
|
630
|
+
(0, import_sdk_common10.log)(`App data: appDataKeccak256=${appDataKeccak256} fullAppData=${fullAppData}`);
|
|
595
631
|
const quoteRequest = {
|
|
596
632
|
from,
|
|
597
633
|
sellToken,
|
|
@@ -600,31 +636,32 @@ async function getQuoteRaw(_tradeParameters, trader, advancedSettings, _orderBoo
|
|
|
600
636
|
validFor,
|
|
601
637
|
appData: fullAppData,
|
|
602
638
|
appDataHash: appDataKeccak256,
|
|
603
|
-
priceQuality:
|
|
639
|
+
priceQuality: import_sdk_order_book7.PriceQuality.OPTIMAL,
|
|
604
640
|
// Do not change this parameter because we rely on the fact that quote has id
|
|
605
|
-
signingScheme:
|
|
641
|
+
signingScheme: import_sdk_order_book7.SigningScheme.EIP712,
|
|
606
642
|
...isEthFlow ? ETH_FLOW_AUX_QUOTE_PARAMS : {},
|
|
607
|
-
...isSell ? { kind:
|
|
643
|
+
...isSell ? { kind: import_sdk_order_book7.OrderQuoteSideKindSell.SELL, sellAmountBeforeFee: amount } : { kind: import_sdk_order_book7.OrderQuoteSideKindBuy.BUY, buyAmountAfterFee: amount },
|
|
608
644
|
...advancedSettings?.quoteRequest
|
|
609
645
|
};
|
|
610
|
-
(0,
|
|
646
|
+
(0, import_sdk_common10.log)("Getting quote...");
|
|
611
647
|
const quote = await orderBookApi.getQuote(quoteRequest);
|
|
612
|
-
const suggestedSlippageBps =
|
|
613
|
-
|
|
614
|
-
quote,
|
|
648
|
+
const { slippageBps: suggestedSlippageBps } = await resolveSlippageSuggestion(
|
|
649
|
+
chainId,
|
|
615
650
|
tradeParameters,
|
|
616
651
|
trader,
|
|
652
|
+
quote,
|
|
653
|
+
isEthFlow,
|
|
617
654
|
advancedSettings
|
|
618
|
-
|
|
655
|
+
);
|
|
619
656
|
const commonResult = {
|
|
620
657
|
isEthFlow,
|
|
621
658
|
quote,
|
|
622
659
|
orderBookApi,
|
|
623
|
-
suggestedSlippageBps
|
|
660
|
+
suggestedSlippageBps: suggestedSlippageBps || defaultSlippageBps
|
|
624
661
|
};
|
|
625
662
|
if (slippageBps === void 0) {
|
|
626
|
-
if (suggestedSlippageBps
|
|
627
|
-
(0,
|
|
663
|
+
if (suggestedSlippageBps) {
|
|
664
|
+
(0, import_sdk_common10.log)(
|
|
628
665
|
`Suggested slippage is greater than ${defaultSlippageBps} BPS (default), using the suggested slippage (${suggestedSlippageBps} BPS)`
|
|
629
666
|
);
|
|
630
667
|
const newAppDataInfo = await buildAppData(
|
|
@@ -634,7 +671,7 @@ async function getQuoteRaw(_tradeParameters, trader, advancedSettings, _orderBoo
|
|
|
634
671
|
},
|
|
635
672
|
advancedSettings?.appData
|
|
636
673
|
);
|
|
637
|
-
(0,
|
|
674
|
+
(0, import_sdk_common10.log)(
|
|
638
675
|
`App data with new suggested slippage: appDataKeccak256=${newAppDataInfo.appDataKeccak256} fullAppData=${newAppDataInfo.fullAppData}`
|
|
639
676
|
);
|
|
640
677
|
return {
|
|
@@ -644,9 +681,7 @@ async function getQuoteRaw(_tradeParameters, trader, advancedSettings, _orderBoo
|
|
|
644
681
|
slippageBps: suggestedSlippageBps
|
|
645
682
|
};
|
|
646
683
|
} else {
|
|
647
|
-
(0,
|
|
648
|
-
`Suggested slippage is only ${suggestedSlippageBps} BPS. Using the default slippage (${defaultSlippageBps} BPS)`
|
|
649
|
-
);
|
|
684
|
+
(0, import_sdk_common10.log)(`No suggested slippage. Using the default slippage (${defaultSlippageBps} BPS)`);
|
|
650
685
|
}
|
|
651
686
|
}
|
|
652
687
|
return {
|
|
@@ -661,7 +696,7 @@ async function getQuote(_tradeParameters, trader, advancedSettings, _orderBookAp
|
|
|
661
696
|
const { quote, orderBookApi, tradeParameters, slippageBps, suggestedSlippageBps, appDataInfo, isEthFlow } = await getQuoteRaw(_tradeParameters, trader, advancedSettings, _orderBookApi);
|
|
662
697
|
const { partnerFee, sellTokenDecimals, buyTokenDecimals } = tradeParameters;
|
|
663
698
|
const { chainId, account: from } = trader;
|
|
664
|
-
const amountsAndCosts = (0,
|
|
699
|
+
const amountsAndCosts = (0, import_sdk_order_book7.getQuoteAmountsAndCosts)({
|
|
665
700
|
orderParams: quote.quote,
|
|
666
701
|
slippagePercentBps: slippageBps,
|
|
667
702
|
partnerFeeBps: getPartnerFeeBps(partnerFee),
|
|
@@ -688,7 +723,7 @@ async function getQuote(_tradeParameters, trader, advancedSettings, _orderBookAp
|
|
|
688
723
|
};
|
|
689
724
|
}
|
|
690
725
|
async function getTrader(swapParameters) {
|
|
691
|
-
const signer = (0,
|
|
726
|
+
const signer = (0, import_sdk_common10.getGlobalAdapter)().signerOrNull();
|
|
692
727
|
const account = swapParameters.owner || await signer?.getAddress();
|
|
693
728
|
return {
|
|
694
729
|
chainId: swapParameters.chainId,
|
|
@@ -697,7 +732,7 @@ async function getTrader(swapParameters) {
|
|
|
697
732
|
};
|
|
698
733
|
}
|
|
699
734
|
async function getQuoteWithSigner(swapParameters, advancedSettings, orderBookApi) {
|
|
700
|
-
const adapter = (0,
|
|
735
|
+
const adapter = (0, import_sdk_common10.getGlobalAdapter)();
|
|
701
736
|
const signer = swapParameters.signer ? adapter.createSigner(swapParameters.signer) : adapter.signer;
|
|
702
737
|
const trader = await getTrader(swapParameters);
|
|
703
738
|
const result = await getQuote(swapParameters, trader, advancedSettings, orderBookApi);
|
|
@@ -754,8 +789,8 @@ async function postSwapOrderFromQuote({
|
|
|
754
789
|
}
|
|
755
790
|
|
|
756
791
|
// src/postLimitOrder.ts
|
|
757
|
-
var
|
|
758
|
-
var
|
|
792
|
+
var import_sdk_order_book8 = require("@cowprotocol/sdk-order-book");
|
|
793
|
+
var import_sdk_common11 = require("@cowprotocol/sdk-common");
|
|
759
794
|
async function postLimitOrder(params, advancedSettings, _orderBookApi) {
|
|
760
795
|
const appDataSlippage = advancedSettings?.appData?.metadata?.quote?.slippageBips;
|
|
761
796
|
const partnerFeeOverride = advancedSettings?.appData?.metadata?.partnerFee;
|
|
@@ -772,9 +807,9 @@ async function postLimitOrder(params, advancedSettings, _orderBookApi) {
|
|
|
772
807
|
params.env = "prod";
|
|
773
808
|
}
|
|
774
809
|
const { appCode, chainId, sellToken, buyToken, sellAmount, buyAmount, partnerFee } = params;
|
|
775
|
-
(0,
|
|
776
|
-
const orderBookApi = _orderBookApi || new
|
|
777
|
-
(0,
|
|
810
|
+
(0, import_sdk_common11.log)(`Limit order ${sellAmount} ${sellToken} for ${buyAmount} ${buyToken} on chain ${chainId}`);
|
|
811
|
+
const orderBookApi = _orderBookApi || new import_sdk_order_book8.OrderBookApi({ chainId, env: params.env });
|
|
812
|
+
(0, import_sdk_common11.log)("Building app data...");
|
|
778
813
|
const appDataInfo = await buildAppData(
|
|
779
814
|
{
|
|
780
815
|
slippageBps: params.slippageBps,
|
|
@@ -789,9 +824,9 @@ async function postLimitOrder(params, advancedSettings, _orderBookApi) {
|
|
|
789
824
|
|
|
790
825
|
// src/getPreSignTransaction.ts
|
|
791
826
|
var import_sdk_config5 = require("@cowprotocol/sdk-config");
|
|
792
|
-
var
|
|
827
|
+
var import_sdk_common12 = require("@cowprotocol/sdk-common");
|
|
793
828
|
async function getPreSignTransaction(signer, chainId, account, orderId) {
|
|
794
|
-
const contract =
|
|
829
|
+
const contract = import_sdk_common12.ContractFactory.createSettlementContract(account, signer);
|
|
795
830
|
const settlementContractAddress = import_sdk_config5.COW_PROTOCOL_SETTLEMENT_CONTRACT_ADDRESS[chainId];
|
|
796
831
|
const preSignatureCall = contract.interface.encodeFunctionData("setPreSignature", [orderId, true]);
|
|
797
832
|
const gas = await contract.estimateGas.setPreSignature?.(orderId, true).catch(() => GAS_LIMIT_DEFAULT) || GAS_LIMIT_DEFAULT;
|
|
@@ -805,17 +840,17 @@ async function getPreSignTransaction(signer, chainId, account, orderId) {
|
|
|
805
840
|
}
|
|
806
841
|
|
|
807
842
|
// src/tradingSdk.ts
|
|
808
|
-
var import_sdk_common12 = require("@cowprotocol/sdk-common");
|
|
809
843
|
var import_sdk_common13 = require("@cowprotocol/sdk-common");
|
|
844
|
+
var import_sdk_common14 = require("@cowprotocol/sdk-common");
|
|
810
845
|
var TradingSdk = class {
|
|
811
846
|
constructor(traderParams = {}, options = {}, adapter) {
|
|
812
847
|
this.traderParams = traderParams;
|
|
813
848
|
this.options = options;
|
|
814
849
|
if (options.enableLogging !== void 0) {
|
|
815
|
-
(0,
|
|
850
|
+
(0, import_sdk_common13.enableLogging)(options.enableLogging);
|
|
816
851
|
}
|
|
817
852
|
if (adapter) {
|
|
818
|
-
(0,
|
|
853
|
+
(0, import_sdk_common14.setGlobalAdapter)(adapter);
|
|
819
854
|
}
|
|
820
855
|
}
|
|
821
856
|
setTraderParams(params) {
|
|
@@ -847,7 +882,7 @@ var TradingSdk = class {
|
|
|
847
882
|
}),
|
|
848
883
|
// It's important to get a fresh instance of the signer
|
|
849
884
|
// Because quote might be called with another signer
|
|
850
|
-
signer: (0,
|
|
885
|
+
signer: (0, import_sdk_common13.getGlobalAdapter)().signer
|
|
851
886
|
}
|
|
852
887
|
},
|
|
853
888
|
advancedSettings2
|
|
@@ -908,7 +943,7 @@ var TradingSdk = class {
|
|
|
908
943
|
);
|
|
909
944
|
}
|
|
910
945
|
async getPreSignTransaction(params) {
|
|
911
|
-
const adapter = (0,
|
|
946
|
+
const adapter = (0, import_sdk_common13.getGlobalAdapter)();
|
|
912
947
|
const traderParams = this.mergeParams(params);
|
|
913
948
|
const signer = traderParams.signer ? adapter.createSigner(traderParams.signer) : adapter.signer;
|
|
914
949
|
return getPreSignTransaction(signer, traderParams.chainId, params.account, params.orderId);
|
|
@@ -917,7 +952,7 @@ var TradingSdk = class {
|
|
|
917
952
|
const { chainId, signer, appCode, env } = params;
|
|
918
953
|
const traderParams = {
|
|
919
954
|
chainId: chainId || this.traderParams.chainId,
|
|
920
|
-
signer: signer || this.traderParams.signer || (0,
|
|
955
|
+
signer: signer || this.traderParams.signer || (0, import_sdk_common13.getGlobalAdapter)().signer,
|
|
921
956
|
appCode: appCode || this.traderParams.appCode,
|
|
922
957
|
env: env || this.traderParams.env
|
|
923
958
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -3,15 +3,10 @@ import { SigningScheme as SigningScheme3 } from "@cowprotocol/sdk-order-book";
|
|
|
3
3
|
|
|
4
4
|
// src/consts.ts
|
|
5
5
|
import { EcdsaSigningScheme, SigningScheme } from "@cowprotocol/sdk-order-book";
|
|
6
|
-
import { mapSupportedNetworks
|
|
6
|
+
import { mapSupportedNetworks } from "@cowprotocol/sdk-config";
|
|
7
7
|
var DEFAULT_QUOTE_VALIDITY = 60 * 30;
|
|
8
8
|
var DEFAULT_SLIPPAGE_BPS = 50;
|
|
9
|
-
var ETH_FLOW_DEFAULT_SLIPPAGE_BPS =
|
|
10
|
-
...mapSupportedNetworks(DEFAULT_SLIPPAGE_BPS),
|
|
11
|
-
// 0.5% by default for most chains
|
|
12
|
-
[SupportedChainId.MAINNET]: 200
|
|
13
|
-
// 2% for mainnet
|
|
14
|
-
};
|
|
9
|
+
var ETH_FLOW_DEFAULT_SLIPPAGE_BPS = mapSupportedNetworks(DEFAULT_SLIPPAGE_BPS);
|
|
15
10
|
var SIGN_SCHEME_MAP = {
|
|
16
11
|
[EcdsaSigningScheme.EIP712]: SigningScheme.EIP712,
|
|
17
12
|
[EcdsaSigningScheme.ETHSIGN]: SigningScheme.ETHSIGN
|
|
@@ -364,13 +359,13 @@ async function postCoWProtocolTrade(orderBookApi, appData, params, additionalPar
|
|
|
364
359
|
}
|
|
365
360
|
|
|
366
361
|
// src/getQuote.ts
|
|
367
|
-
import { getGlobalAdapter as getGlobalAdapter5, log as
|
|
362
|
+
import { getGlobalAdapter as getGlobalAdapter5, log as log4 } from "@cowprotocol/sdk-common";
|
|
368
363
|
import {
|
|
369
|
-
getQuoteAmountsAndCosts as
|
|
364
|
+
getQuoteAmountsAndCosts as getQuoteAmountsAndCosts3,
|
|
370
365
|
OrderBookApi as OrderBookApi3,
|
|
371
366
|
OrderQuoteSideKindBuy,
|
|
372
367
|
OrderQuoteSideKindSell,
|
|
373
|
-
PriceQuality,
|
|
368
|
+
PriceQuality as PriceQuality2,
|
|
374
369
|
SigningScheme as SigningScheme4
|
|
375
370
|
} from "@cowprotocol/sdk-order-book";
|
|
376
371
|
|
|
@@ -517,6 +512,47 @@ function suggestSlippageBps(params) {
|
|
|
517
512
|
return Math.max(Math.min(slippageBps, MAX_SLIPPAGE_BPS), getDefaultSlippageBps(trader.chainId, isEthFlow));
|
|
518
513
|
}
|
|
519
514
|
|
|
515
|
+
// src/resolveSlippageSuggestion.ts
|
|
516
|
+
import { getQuoteAmountsAndCosts as getQuoteAmountsAndCosts2, PriceQuality } from "@cowprotocol/sdk-order-book";
|
|
517
|
+
import { log as log3 } from "@cowprotocol/sdk-common";
|
|
518
|
+
async function resolveSlippageSuggestion(chainId, tradeParameters, trader, quote, isEthFlow, advancedSettings) {
|
|
519
|
+
const suggestSlippageParams = {
|
|
520
|
+
isEthFlow,
|
|
521
|
+
quote,
|
|
522
|
+
tradeParameters,
|
|
523
|
+
trader,
|
|
524
|
+
advancedSettings
|
|
525
|
+
};
|
|
526
|
+
const getSlippageSuggestion = advancedSettings?.getSlippageSuggestion;
|
|
527
|
+
const priceQuality = advancedSettings?.quoteRequest?.priceQuality ?? PriceQuality.OPTIMAL;
|
|
528
|
+
const defaultSuggestion = suggestSlippageBps(suggestSlippageParams);
|
|
529
|
+
if (priceQuality === PriceQuality.FAST || !getSlippageSuggestion) {
|
|
530
|
+
return { slippageBps: defaultSuggestion };
|
|
531
|
+
}
|
|
532
|
+
const amountsAndCosts = getQuoteAmountsAndCosts2({
|
|
533
|
+
orderParams: quote.quote,
|
|
534
|
+
slippagePercentBps: 0,
|
|
535
|
+
partnerFeeBps: getPartnerFeeBps(tradeParameters.partnerFee),
|
|
536
|
+
sellDecimals: tradeParameters.sellTokenDecimals,
|
|
537
|
+
buyDecimals: tradeParameters.buyTokenDecimals
|
|
538
|
+
});
|
|
539
|
+
try {
|
|
540
|
+
const suggestedSlippage = await getSlippageSuggestion({
|
|
541
|
+
chainId,
|
|
542
|
+
sellToken: tradeParameters.sellToken,
|
|
543
|
+
buyToken: tradeParameters.buyToken,
|
|
544
|
+
sellAmount: amountsAndCosts.afterSlippage.sellAmount,
|
|
545
|
+
buyAmount: amountsAndCosts.afterSlippage.buyAmount
|
|
546
|
+
});
|
|
547
|
+
return {
|
|
548
|
+
slippageBps: Math.max(suggestedSlippage.slippageBps ?? 0, defaultSuggestion)
|
|
549
|
+
};
|
|
550
|
+
} catch (e) {
|
|
551
|
+
log3(`getSlippageSuggestion() error: ${e.message || String(e)}`);
|
|
552
|
+
return { slippageBps: defaultSuggestion };
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
520
556
|
// src/getQuote.ts
|
|
521
557
|
var ETH_FLOW_AUX_QUOTE_PARAMS = {
|
|
522
558
|
signingScheme: SigningScheme4.EIP1271,
|
|
@@ -539,13 +575,13 @@ async function getQuoteRaw(_tradeParameters, trader, advancedSettings, _orderBoo
|
|
|
539
575
|
slippageBps,
|
|
540
576
|
env = "prod"
|
|
541
577
|
} = tradeParameters;
|
|
542
|
-
|
|
578
|
+
log4(
|
|
543
579
|
`getQuote for: Swap ${amount} ${sellToken} for ${buyToken} on chain ${chainId} with ${slippageBps !== void 0 ? `${slippageBps} BPS` : "AUTO"} slippage`
|
|
544
580
|
);
|
|
545
581
|
const orderBookApi = _orderBookApi || new OrderBookApi3({ chainId, env });
|
|
546
582
|
const receiver = tradeParameters.receiver || from;
|
|
547
583
|
const isSell = kind === "sell";
|
|
548
|
-
|
|
584
|
+
log4("Building app data...");
|
|
549
585
|
const defaultSlippageBps = getDefaultSlippageBps(chainId, isEthFlow);
|
|
550
586
|
const slippageBpsOrDefault = slippageBps ?? defaultSlippageBps;
|
|
551
587
|
const buildAppDataParams = {
|
|
@@ -556,7 +592,7 @@ async function getQuoteRaw(_tradeParameters, trader, advancedSettings, _orderBoo
|
|
|
556
592
|
};
|
|
557
593
|
const appDataInfo = await buildAppData(buildAppDataParams, advancedSettings?.appData);
|
|
558
594
|
const { appDataKeccak256, fullAppData } = appDataInfo;
|
|
559
|
-
|
|
595
|
+
log4(`App data: appDataKeccak256=${appDataKeccak256} fullAppData=${fullAppData}`);
|
|
560
596
|
const quoteRequest = {
|
|
561
597
|
from,
|
|
562
598
|
sellToken,
|
|
@@ -565,31 +601,32 @@ async function getQuoteRaw(_tradeParameters, trader, advancedSettings, _orderBoo
|
|
|
565
601
|
validFor,
|
|
566
602
|
appData: fullAppData,
|
|
567
603
|
appDataHash: appDataKeccak256,
|
|
568
|
-
priceQuality:
|
|
604
|
+
priceQuality: PriceQuality2.OPTIMAL,
|
|
569
605
|
// Do not change this parameter because we rely on the fact that quote has id
|
|
570
606
|
signingScheme: SigningScheme4.EIP712,
|
|
571
607
|
...isEthFlow ? ETH_FLOW_AUX_QUOTE_PARAMS : {},
|
|
572
608
|
...isSell ? { kind: OrderQuoteSideKindSell.SELL, sellAmountBeforeFee: amount } : { kind: OrderQuoteSideKindBuy.BUY, buyAmountAfterFee: amount },
|
|
573
609
|
...advancedSettings?.quoteRequest
|
|
574
610
|
};
|
|
575
|
-
|
|
611
|
+
log4("Getting quote...");
|
|
576
612
|
const quote = await orderBookApi.getQuote(quoteRequest);
|
|
577
|
-
const suggestedSlippageBps =
|
|
578
|
-
|
|
579
|
-
quote,
|
|
613
|
+
const { slippageBps: suggestedSlippageBps } = await resolveSlippageSuggestion(
|
|
614
|
+
chainId,
|
|
580
615
|
tradeParameters,
|
|
581
616
|
trader,
|
|
617
|
+
quote,
|
|
618
|
+
isEthFlow,
|
|
582
619
|
advancedSettings
|
|
583
|
-
|
|
620
|
+
);
|
|
584
621
|
const commonResult = {
|
|
585
622
|
isEthFlow,
|
|
586
623
|
quote,
|
|
587
624
|
orderBookApi,
|
|
588
|
-
suggestedSlippageBps
|
|
625
|
+
suggestedSlippageBps: suggestedSlippageBps || defaultSlippageBps
|
|
589
626
|
};
|
|
590
627
|
if (slippageBps === void 0) {
|
|
591
|
-
if (suggestedSlippageBps
|
|
592
|
-
|
|
628
|
+
if (suggestedSlippageBps) {
|
|
629
|
+
log4(
|
|
593
630
|
`Suggested slippage is greater than ${defaultSlippageBps} BPS (default), using the suggested slippage (${suggestedSlippageBps} BPS)`
|
|
594
631
|
);
|
|
595
632
|
const newAppDataInfo = await buildAppData(
|
|
@@ -599,7 +636,7 @@ async function getQuoteRaw(_tradeParameters, trader, advancedSettings, _orderBoo
|
|
|
599
636
|
},
|
|
600
637
|
advancedSettings?.appData
|
|
601
638
|
);
|
|
602
|
-
|
|
639
|
+
log4(
|
|
603
640
|
`App data with new suggested slippage: appDataKeccak256=${newAppDataInfo.appDataKeccak256} fullAppData=${newAppDataInfo.fullAppData}`
|
|
604
641
|
);
|
|
605
642
|
return {
|
|
@@ -609,9 +646,7 @@ async function getQuoteRaw(_tradeParameters, trader, advancedSettings, _orderBoo
|
|
|
609
646
|
slippageBps: suggestedSlippageBps
|
|
610
647
|
};
|
|
611
648
|
} else {
|
|
612
|
-
|
|
613
|
-
`Suggested slippage is only ${suggestedSlippageBps} BPS. Using the default slippage (${defaultSlippageBps} BPS)`
|
|
614
|
-
);
|
|
649
|
+
log4(`No suggested slippage. Using the default slippage (${defaultSlippageBps} BPS)`);
|
|
615
650
|
}
|
|
616
651
|
}
|
|
617
652
|
return {
|
|
@@ -626,7 +661,7 @@ async function getQuote(_tradeParameters, trader, advancedSettings, _orderBookAp
|
|
|
626
661
|
const { quote, orderBookApi, tradeParameters, slippageBps, suggestedSlippageBps, appDataInfo, isEthFlow } = await getQuoteRaw(_tradeParameters, trader, advancedSettings, _orderBookApi);
|
|
627
662
|
const { partnerFee, sellTokenDecimals, buyTokenDecimals } = tradeParameters;
|
|
628
663
|
const { chainId, account: from } = trader;
|
|
629
|
-
const amountsAndCosts =
|
|
664
|
+
const amountsAndCosts = getQuoteAmountsAndCosts3({
|
|
630
665
|
orderParams: quote.quote,
|
|
631
666
|
slippagePercentBps: slippageBps,
|
|
632
667
|
partnerFeeBps: getPartnerFeeBps(partnerFee),
|
|
@@ -720,7 +755,7 @@ async function postSwapOrderFromQuote({
|
|
|
720
755
|
|
|
721
756
|
// src/postLimitOrder.ts
|
|
722
757
|
import { OrderBookApi as OrderBookApi4 } from "@cowprotocol/sdk-order-book";
|
|
723
|
-
import { log as
|
|
758
|
+
import { log as log5 } from "@cowprotocol/sdk-common";
|
|
724
759
|
async function postLimitOrder(params, advancedSettings, _orderBookApi) {
|
|
725
760
|
const appDataSlippage = advancedSettings?.appData?.metadata?.quote?.slippageBips;
|
|
726
761
|
const partnerFeeOverride = advancedSettings?.appData?.metadata?.partnerFee;
|
|
@@ -737,9 +772,9 @@ async function postLimitOrder(params, advancedSettings, _orderBookApi) {
|
|
|
737
772
|
params.env = "prod";
|
|
738
773
|
}
|
|
739
774
|
const { appCode, chainId, sellToken, buyToken, sellAmount, buyAmount, partnerFee } = params;
|
|
740
|
-
|
|
775
|
+
log5(`Limit order ${sellAmount} ${sellToken} for ${buyAmount} ${buyToken} on chain ${chainId}`);
|
|
741
776
|
const orderBookApi = _orderBookApi || new OrderBookApi4({ chainId, env: params.env });
|
|
742
|
-
|
|
777
|
+
log5("Building app data...");
|
|
743
778
|
const appDataInfo = await buildAppData(
|
|
744
779
|
{
|
|
745
780
|
slippageBps: params.slippageBps,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/sdk-trading",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "CowProtocol trading",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -29,18 +29,18 @@
|
|
|
29
29
|
"ts-jest": "^29.0.0",
|
|
30
30
|
"tsx": "^4.19.4",
|
|
31
31
|
"@cow-sdk/typescript-config": "0.0.0-beta.0",
|
|
32
|
-
"@cowprotocol/sdk-ethers-v5-adapter": "0.1.
|
|
33
|
-
"@cowprotocol/sdk-ethers-v6-adapter": "0.1.
|
|
34
|
-
"@cowprotocol/sdk-viem-adapter": "0.1.
|
|
32
|
+
"@cowprotocol/sdk-ethers-v5-adapter": "0.1.1",
|
|
33
|
+
"@cowprotocol/sdk-ethers-v6-adapter": "0.1.1",
|
|
34
|
+
"@cowprotocol/sdk-viem-adapter": "0.1.1"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"deepmerge": "^4.3.1",
|
|
38
|
-
"@cowprotocol/sdk-
|
|
38
|
+
"@cowprotocol/sdk-app-data": "4.0.1",
|
|
39
39
|
"@cowprotocol/sdk-config": "0.1.0",
|
|
40
|
-
"@cowprotocol/sdk-
|
|
41
|
-
"@cowprotocol/sdk-order-
|
|
42
|
-
"@cowprotocol/sdk-
|
|
43
|
-
"@cowprotocol/sdk-
|
|
40
|
+
"@cowprotocol/sdk-order-book": "0.1.1",
|
|
41
|
+
"@cowprotocol/sdk-order-signing": "0.1.4",
|
|
42
|
+
"@cowprotocol/sdk-common": "0.2.0",
|
|
43
|
+
"@cowprotocol/sdk-contracts-ts": "0.3.0"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
46
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|