@cowprotocol/sdk-trading 0.3.2-beta.0 → 0.4.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 +210 -1
- package/dist/index.d.mts +76 -7
- package/dist/index.d.ts +76 -7
- package/dist/index.js +363 -72
- package/dist/index.mjs +343 -52
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -44,9 +44,20 @@ Main functions:
|
|
|
44
44
|
- `postLimitOrder` - Create a limit order.
|
|
45
45
|
- `getQuote` - Fetch a quote for a swap order.
|
|
46
46
|
|
|
47
|
+
Order Management:
|
|
48
|
+
|
|
49
|
+
- `getOrder` - Retrieve order details by UID.
|
|
50
|
+
- `offChainCancelOrder` - Cancel an order off-chain (soft cancel, free and fast).
|
|
51
|
+
- `onChainCancelOrder` - Cancel an order on-chain (hard cancel, requires gas).
|
|
52
|
+
|
|
53
|
+
Token Approval:
|
|
54
|
+
|
|
55
|
+
- `getCowProtocolAllowance` - Check current token allowance for CoW Protocol.
|
|
56
|
+
- `approveCowProtocol` - Approve CoW Protocol to spend tokens.
|
|
57
|
+
|
|
47
58
|
Special cases:
|
|
48
59
|
|
|
49
|
-
-
|
|
60
|
+
- `setTraderParams` - In case if you work with different chains and need to switch between them in runtime.
|
|
50
61
|
- `postSellNativeCurrencyOrder` - Sell blockchain native tokens (e.g., ETH on Ethereum).
|
|
51
62
|
- `getPreSignTransaction` - Sign an order using a smart contract wallet.
|
|
52
63
|
|
|
@@ -641,3 +652,201 @@ console.log('Order created, id: ', orderId)
|
|
|
641
652
|
#### Limit order
|
|
642
653
|
|
|
643
654
|
Same as for the swap order but without the `quoteRequest` parameter.
|
|
655
|
+
|
|
656
|
+
## Order Management
|
|
657
|
+
|
|
658
|
+
The SDK provides methods to manage existing orders, including retrieving order details and canceling orders.
|
|
659
|
+
|
|
660
|
+
### getOrder
|
|
661
|
+
|
|
662
|
+
Fetches details about an existing order by its UID.
|
|
663
|
+
|
|
664
|
+
**Parameters:**
|
|
665
|
+
- `orderUid` - The unique identifier of the order
|
|
666
|
+
- `chainId` - (Optional) Chain ID, uses trader params if not provided
|
|
667
|
+
|
|
668
|
+
**Returns:** `Promise<EnrichedOrder>` - Full order details including status, amounts, and metadata
|
|
669
|
+
|
|
670
|
+
#### Example
|
|
671
|
+
|
|
672
|
+
```typescript
|
|
673
|
+
import { TradingSdk } from '@cowprotocol/sdk-trading'
|
|
674
|
+
|
|
675
|
+
const sdk = new TradingSdk({
|
|
676
|
+
chainId: SupportedChainId.MAINNET,
|
|
677
|
+
appCode: '<YOUR_APP_CODE>',
|
|
678
|
+
}, {}, adapter)
|
|
679
|
+
|
|
680
|
+
const orderUid = '0xd64389693b6cf89ad6c140a113b10df08073e5ef3063d05a02f3f42e1a42f0ad...'
|
|
681
|
+
|
|
682
|
+
const order = await sdk.getOrder({ orderUid })
|
|
683
|
+
|
|
684
|
+
console.log('Order status:', order.status)
|
|
685
|
+
console.log('Sell amount:', order.sellAmount)
|
|
686
|
+
console.log('Buy amount:', order.buyAmount)
|
|
687
|
+
```
|
|
688
|
+
|
|
689
|
+
### offChainCancelOrder
|
|
690
|
+
|
|
691
|
+
Cancels an order off-chain by sending a signed cancellation request to the order book API. This is a "soft cancel" that is faster and doesn't require gas, but requires order book support.
|
|
692
|
+
|
|
693
|
+
You always can cancel orders using [CoW Swap](https://swap.cow.fi), [see the tutorial for more details](https://docs.cow.fi/cow-protocol/tutorials/cow-swap/swap#cancel-your-order).
|
|
694
|
+
|
|
695
|
+
**Parameters:**
|
|
696
|
+
- `orderUid` - The unique identifier of the order to cancel
|
|
697
|
+
- `chainId` - (Optional) Chain ID, uses trader params if not provided
|
|
698
|
+
- `signer` - (Optional) Custom signer, uses trader params signer if not provided
|
|
699
|
+
|
|
700
|
+
**Returns:** `Promise<boolean>` - True if cancellation was successful
|
|
701
|
+
|
|
702
|
+
#### Example
|
|
703
|
+
|
|
704
|
+
```typescript
|
|
705
|
+
import { TradingSdk } from '@cowprotocol/sdk-trading'
|
|
706
|
+
|
|
707
|
+
const sdk = new TradingSdk({
|
|
708
|
+
chainId: SupportedChainId.MAINNET,
|
|
709
|
+
appCode: '<YOUR_APP_CODE>',
|
|
710
|
+
}, {}, adapter)
|
|
711
|
+
|
|
712
|
+
const orderUid = '0xd64389693b6cf89ad6c140a113b10df08073e5ef3063d05a02f3f42e1a42f0ad...'
|
|
713
|
+
|
|
714
|
+
const success = await sdk.offChainCancelOrder({ orderUid })
|
|
715
|
+
|
|
716
|
+
if (success) {
|
|
717
|
+
console.log('Order cancelled successfully (off-chain)')
|
|
718
|
+
}
|
|
719
|
+
```
|
|
720
|
+
|
|
721
|
+
### onChainCancelOrder
|
|
722
|
+
|
|
723
|
+
Cancels an order on-chain by sending a transaction to invalidate it. This is a "hard cancel" that requires gas but guarantees cancellation. Automatically detects whether to use the Settlement contract or EthFlow contract based on order type.
|
|
724
|
+
|
|
725
|
+
**Parameters:**
|
|
726
|
+
- `orderUid` - The unique identifier of the order to cancel
|
|
727
|
+
- `chainId` - (Optional) Chain ID, uses trader params if not provided
|
|
728
|
+
- `signer` - (Optional) Custom signer, uses trader params signer if not provided
|
|
729
|
+
|
|
730
|
+
**Returns:** `Promise<string>` - Transaction hash of the cancellation
|
|
731
|
+
|
|
732
|
+
#### Example
|
|
733
|
+
|
|
734
|
+
```typescript
|
|
735
|
+
import { TradingSdk } from '@cowprotocol/sdk-trading'
|
|
736
|
+
|
|
737
|
+
const sdk = new TradingSdk({
|
|
738
|
+
chainId: SupportedChainId.MAINNET,
|
|
739
|
+
appCode: '<YOUR_APP_CODE>',
|
|
740
|
+
}, {}, adapter)
|
|
741
|
+
|
|
742
|
+
const orderUid = '0xd64389693b6cf89ad6c140a113b10df08073e5ef3063d05a02f3f42e1a42f0ad...'
|
|
743
|
+
|
|
744
|
+
const txHash = await sdk.onChainCancelOrder({ orderUid })
|
|
745
|
+
|
|
746
|
+
console.log('Cancellation transaction:', txHash)
|
|
747
|
+
```
|
|
748
|
+
|
|
749
|
+
## Token Approval
|
|
750
|
+
|
|
751
|
+
Before trading ERC-20 tokens, you need to approve the CoW Protocol to spend them. The SDK provides methods to check and manage token approvals.
|
|
752
|
+
|
|
753
|
+
### getCowProtocolAllowance
|
|
754
|
+
|
|
755
|
+
Checks the current allowance for the CoW Protocol Vault Relayer to spend an ERC-20 token.
|
|
756
|
+
|
|
757
|
+
**Parameters:**
|
|
758
|
+
- `tokenAddress` - The ERC-20 token contract address
|
|
759
|
+
- `owner` - The address of the token owner
|
|
760
|
+
- `chainId` - (Optional) Chain ID, uses trader params if not provided
|
|
761
|
+
|
|
762
|
+
**Returns:** `Promise<bigint>` - Current allowance amount
|
|
763
|
+
|
|
764
|
+
#### Example
|
|
765
|
+
|
|
766
|
+
```typescript
|
|
767
|
+
import { TradingSdk } from '@cowprotocol/sdk-trading'
|
|
768
|
+
|
|
769
|
+
const sdk = new TradingSdk({
|
|
770
|
+
chainId: SupportedChainId.MAINNET,
|
|
771
|
+
appCode: '<YOUR_APP_CODE>',
|
|
772
|
+
}, {}, adapter)
|
|
773
|
+
|
|
774
|
+
const tokenAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' // USDC
|
|
775
|
+
const owner = '0x...' // Your wallet address
|
|
776
|
+
|
|
777
|
+
const allowance = await sdk.getCowProtocolAllowance({
|
|
778
|
+
tokenAddress,
|
|
779
|
+
owner,
|
|
780
|
+
})
|
|
781
|
+
|
|
782
|
+
console.log('Current allowance:', allowance.toString())
|
|
783
|
+
```
|
|
784
|
+
|
|
785
|
+
### approveCowProtocol
|
|
786
|
+
|
|
787
|
+
Approves the CoW Protocol Vault Relayer to spend a specified amount of an ERC-20 token. This creates an on-chain approval transaction.
|
|
788
|
+
|
|
789
|
+
**Parameters:**
|
|
790
|
+
- `tokenAddress` - The ERC-20 token contract address
|
|
791
|
+
- `amount` - The amount to approve (as bigint)
|
|
792
|
+
- `chainId` - (Optional) Chain ID, uses trader params if not provided
|
|
793
|
+
- `signer` - (Optional) Custom signer, uses trader params signer if not provided
|
|
794
|
+
|
|
795
|
+
**Returns:** `Promise<string>` - Transaction hash of the approval
|
|
796
|
+
|
|
797
|
+
#### Example
|
|
798
|
+
|
|
799
|
+
```typescript
|
|
800
|
+
import { TradingSdk } from '@cowprotocol/sdk-trading'
|
|
801
|
+
import { parseUnits } from 'viem' // or ethers
|
|
802
|
+
|
|
803
|
+
const sdk = new TradingSdk({
|
|
804
|
+
chainId: SupportedChainId.MAINNET,
|
|
805
|
+
appCode: '<YOUR_APP_CODE>',
|
|
806
|
+
}, {}, adapter)
|
|
807
|
+
|
|
808
|
+
const tokenAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' // USDC
|
|
809
|
+
const amount = parseUnits('1000', 6) // 1000 USDC (6 decimals)
|
|
810
|
+
|
|
811
|
+
const txHash = await sdk.approveCowProtocol({
|
|
812
|
+
tokenAddress,
|
|
813
|
+
amount,
|
|
814
|
+
})
|
|
815
|
+
|
|
816
|
+
console.log('Approval transaction:', txHash)
|
|
817
|
+
```
|
|
818
|
+
|
|
819
|
+
#### Smart Approval Flow
|
|
820
|
+
|
|
821
|
+
It's recommended to check the current allowance before approving to avoid unnecessary transactions:
|
|
822
|
+
|
|
823
|
+
```typescript
|
|
824
|
+
import { TradingSdk } from '@cowprotocol/sdk-trading'
|
|
825
|
+
import { parseUnits } from 'viem'
|
|
826
|
+
|
|
827
|
+
const sdk = new TradingSdk({
|
|
828
|
+
chainId: SupportedChainId.MAINNET,
|
|
829
|
+
appCode: '<YOUR_APP_CODE>',
|
|
830
|
+
}, {}, adapter)
|
|
831
|
+
|
|
832
|
+
const tokenAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' // USDC
|
|
833
|
+
const owner = '0x...' // Your wallet address
|
|
834
|
+
const requiredAmount = parseUnits('1000', 6) // 1000 USDC
|
|
835
|
+
|
|
836
|
+
// Check current allowance
|
|
837
|
+
const currentAllowance = await sdk.getCowProtocolAllowance({
|
|
838
|
+
tokenAddress,
|
|
839
|
+
owner,
|
|
840
|
+
})
|
|
841
|
+
|
|
842
|
+
// Only approve if needed
|
|
843
|
+
if (currentAllowance < requiredAmount) {
|
|
844
|
+
const txHash = await sdk.approveCowProtocol({
|
|
845
|
+
tokenAddress,
|
|
846
|
+
amount: requiredAmount,
|
|
847
|
+
})
|
|
848
|
+
console.log('Approval transaction:', txHash)
|
|
849
|
+
} else {
|
|
850
|
+
console.log('Sufficient allowance already exists')
|
|
851
|
+
}
|
|
852
|
+
```
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { cowAppDataLatestScheme, AppDataParams, LatestAppDataDocVersion } from '@cowprotocol/sdk-app-data';
|
|
2
|
-
import { OrderKind, OrderParameters, TokenAmount, SigningScheme, OrderQuoteRequest, QuoteAmountsAndCosts, OrderQuoteResponse, AppData, AppDataHash, Signature, OrderBookApi } from '@cowprotocol/sdk-order-book';
|
|
2
|
+
import { OrderKind, OrderParameters, TokenAmount, SigningScheme, OrderQuoteRequest, QuoteAmountsAndCosts, OrderQuoteResponse, AppData, AppDataHash, Signature, OrderBookApi, EnrichedOrder } from '@cowprotocol/sdk-order-book';
|
|
3
3
|
import { AccountAddress, SignerLike, AbstractSigner, Signer, AbstractProviderAdapter, EthFlowContract } from '@cowprotocol/sdk-common';
|
|
4
4
|
import { UnsignedOrder } from '@cowprotocol/sdk-order-signing';
|
|
5
5
|
import { CowEnv, SupportedChainId } from '@cowprotocol/sdk-config';
|
|
@@ -55,6 +55,11 @@ interface TradeOptionalParameters {
|
|
|
55
55
|
slippageBps?: cowAppDataLatestScheme.SlippageBips;
|
|
56
56
|
receiver?: OrderParameters['receiver'];
|
|
57
57
|
validFor?: OrderParameters['validTo'];
|
|
58
|
+
/**
|
|
59
|
+
* Exact validTo timestamp in seconds since epoch.
|
|
60
|
+
* If provided, this will be used instead of calculating from validFor.
|
|
61
|
+
*/
|
|
62
|
+
validTo?: OrderParameters['validTo'];
|
|
58
63
|
partnerFee?: cowAppDataLatestScheme.PartnerFee;
|
|
59
64
|
}
|
|
60
65
|
/**
|
|
@@ -69,6 +74,16 @@ interface TraderParameters {
|
|
|
69
74
|
type QuoterParameters = Omit<TraderParameters, 'signer'> & {
|
|
70
75
|
account: AccountAddress;
|
|
71
76
|
};
|
|
77
|
+
interface SlippageToleranceResponse {
|
|
78
|
+
slippageBps: number | null;
|
|
79
|
+
}
|
|
80
|
+
interface SlippageToleranceRequest {
|
|
81
|
+
chainId: SupportedChainId;
|
|
82
|
+
sellToken: OrderParameters['sellToken'];
|
|
83
|
+
buyToken: OrderParameters['buyToken'];
|
|
84
|
+
sellAmount?: bigint;
|
|
85
|
+
buyAmount?: bigint;
|
|
86
|
+
}
|
|
72
87
|
/**
|
|
73
88
|
* Trade type, assets, amounts, and optional parameters.
|
|
74
89
|
*/
|
|
@@ -108,6 +123,10 @@ interface SwapAdvancedSettings {
|
|
|
108
123
|
appData?: AppDataParams;
|
|
109
124
|
additionalParams?: PostTradeAdditionalParams;
|
|
110
125
|
quoteSigner?: SignerLike;
|
|
126
|
+
/**
|
|
127
|
+
* An optional callback to use custom logic for slippage suggestion
|
|
128
|
+
*/
|
|
129
|
+
getSlippageSuggestion?(request: SlippageToleranceRequest): Promise<SlippageToleranceResponse>;
|
|
111
130
|
}
|
|
112
131
|
interface LimitOrderAdvancedSettings {
|
|
113
132
|
appData?: AppDataParams;
|
|
@@ -234,9 +253,12 @@ declare function getQuoteWithSigner(swapParameters: SwapParameters, advancedSett
|
|
|
234
253
|
|
|
235
254
|
declare function postSellNativeCurrencyOrder(orderBookApi: OrderBookApi, appData: Pick<TradingAppDataInfo, 'fullAppData' | 'appDataKeccak256'>, _params: LimitTradeParametersFromQuote, additionalParams?: PostTradeAdditionalParams, paramSigner?: SignerLike): Promise<OrderPostingResult>;
|
|
236
255
|
|
|
237
|
-
declare function getPreSignTransaction(signer: Signer, chainId: SupportedChainId,
|
|
256
|
+
declare function getPreSignTransaction(signer: Signer, chainId: SupportedChainId, orderId: string): Promise<TradingTransactionParams>;
|
|
238
257
|
|
|
239
258
|
type WithPartialTraderParams<T> = T & Partial<TraderParameters>;
|
|
259
|
+
type OrderTraderParams = WithPartialTraderParams<{
|
|
260
|
+
orderUid: string;
|
|
261
|
+
}>;
|
|
240
262
|
interface TradingSdkOptions {
|
|
241
263
|
enableLogging: boolean;
|
|
242
264
|
orderBookApi: OrderBookApi;
|
|
@@ -273,10 +295,56 @@ declare class TradingSdk {
|
|
|
273
295
|
* ```
|
|
274
296
|
*/
|
|
275
297
|
postSellNativeCurrencyOrder(params: WithPartialTraderParams<TradeParameters>, advancedSettings?: SwapAdvancedSettings): Promise<ReturnType<typeof postSellNativeCurrencyOrder>>;
|
|
276
|
-
getPreSignTransaction(params:
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
298
|
+
getPreSignTransaction(params: OrderTraderParams): ReturnType<typeof getPreSignTransaction>;
|
|
299
|
+
getOrder(params: OrderTraderParams): Promise<EnrichedOrder>;
|
|
300
|
+
offChainCancelOrder(params: OrderTraderParams): Promise<boolean>;
|
|
301
|
+
onChainCancelOrder(params: OrderTraderParams, _order?: EnrichedOrder): Promise<string>;
|
|
302
|
+
/**
|
|
303
|
+
* Checks the current allowance for the CoW Protocol Vault Relayer to spend an ERC-20 token.
|
|
304
|
+
*
|
|
305
|
+
* @param params - Parameters including token address and owner address
|
|
306
|
+
* @returns Promise resolving to the current allowance amount as a bigint
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```typescript
|
|
310
|
+
* const params = {
|
|
311
|
+
* tokenAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
|
|
312
|
+
* owner: '0x123...',
|
|
313
|
+
* chainId: 1,
|
|
314
|
+
* }
|
|
315
|
+
*
|
|
316
|
+
* const allowance = await sdk.getCowProtocolAllowance(params)
|
|
317
|
+
* console.log('Current allowance:', allowance.toString())
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
320
|
+
getCowProtocolAllowance(params: WithPartialTraderParams<{
|
|
321
|
+
tokenAddress: string;
|
|
322
|
+
owner: string;
|
|
323
|
+
}>): Promise<bigint>;
|
|
324
|
+
/**
|
|
325
|
+
* Approves the CoW Protocol Vault Relayer to spend a specified amount of an ERC-20 token.
|
|
326
|
+
* This method creates an on-chain approval transaction.
|
|
327
|
+
*
|
|
328
|
+
* @param params - Parameters including token address and amount to approve
|
|
329
|
+
* @returns Promise resolving to the transaction hash of the approval transaction
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```typescript
|
|
333
|
+
* const params = {
|
|
334
|
+
* tokenAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
|
|
335
|
+
* amount: '1000000000', // 1000 USDC (6 decimals)
|
|
336
|
+
* chainId: 1,
|
|
337
|
+
* }
|
|
338
|
+
*
|
|
339
|
+
* const txHash = await sdk.approveCowProtocol(params)
|
|
340
|
+
* console.log('Approval transaction:', txHash)
|
|
341
|
+
* ```
|
|
342
|
+
*/
|
|
343
|
+
approveCowProtocol(params: WithPartialTraderParams<{
|
|
344
|
+
tokenAddress: string;
|
|
345
|
+
amount: bigint;
|
|
346
|
+
}>): Promise<string>;
|
|
347
|
+
private resolveOrderBookApi;
|
|
280
348
|
private mergeParams;
|
|
281
349
|
}
|
|
282
350
|
|
|
@@ -308,6 +376,7 @@ interface SuggestSlippageBps {
|
|
|
308
376
|
quote: OrderQuoteResponse;
|
|
309
377
|
trader: QuoterParameters;
|
|
310
378
|
advancedSettings?: SwapAdvancedSettings;
|
|
379
|
+
volumeMultiplierPercent?: number;
|
|
311
380
|
}
|
|
312
381
|
/**
|
|
313
382
|
* Return the slippage in BPS that would allow the fee to increase by the multiplying factor percent.
|
|
@@ -333,4 +402,4 @@ declare function getTradeParametersAfterQuote({ quoteParameters, sellToken, }: {
|
|
|
333
402
|
sellToken: string;
|
|
334
403
|
}): TradeParameters;
|
|
335
404
|
|
|
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 };
|
|
405
|
+
export { type AppDataRootSchema, type BuildAppDataParams, type EthFlowOrderExistsCallback, type LimitOrderAdvancedSettings, type LimitOrderParameters, type LimitTradeParameters, type LimitTradeParametersFromQuote, ORDER_PRIMARY_TYPE, type OrderPostingResult, type OrderTraderParams, 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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { cowAppDataLatestScheme, AppDataParams, LatestAppDataDocVersion } from '@cowprotocol/sdk-app-data';
|
|
2
|
-
import { OrderKind, OrderParameters, TokenAmount, SigningScheme, OrderQuoteRequest, QuoteAmountsAndCosts, OrderQuoteResponse, AppData, AppDataHash, Signature, OrderBookApi } from '@cowprotocol/sdk-order-book';
|
|
2
|
+
import { OrderKind, OrderParameters, TokenAmount, SigningScheme, OrderQuoteRequest, QuoteAmountsAndCosts, OrderQuoteResponse, AppData, AppDataHash, Signature, OrderBookApi, EnrichedOrder } from '@cowprotocol/sdk-order-book';
|
|
3
3
|
import { AccountAddress, SignerLike, AbstractSigner, Signer, AbstractProviderAdapter, EthFlowContract } from '@cowprotocol/sdk-common';
|
|
4
4
|
import { UnsignedOrder } from '@cowprotocol/sdk-order-signing';
|
|
5
5
|
import { CowEnv, SupportedChainId } from '@cowprotocol/sdk-config';
|
|
@@ -55,6 +55,11 @@ interface TradeOptionalParameters {
|
|
|
55
55
|
slippageBps?: cowAppDataLatestScheme.SlippageBips;
|
|
56
56
|
receiver?: OrderParameters['receiver'];
|
|
57
57
|
validFor?: OrderParameters['validTo'];
|
|
58
|
+
/**
|
|
59
|
+
* Exact validTo timestamp in seconds since epoch.
|
|
60
|
+
* If provided, this will be used instead of calculating from validFor.
|
|
61
|
+
*/
|
|
62
|
+
validTo?: OrderParameters['validTo'];
|
|
58
63
|
partnerFee?: cowAppDataLatestScheme.PartnerFee;
|
|
59
64
|
}
|
|
60
65
|
/**
|
|
@@ -69,6 +74,16 @@ interface TraderParameters {
|
|
|
69
74
|
type QuoterParameters = Omit<TraderParameters, 'signer'> & {
|
|
70
75
|
account: AccountAddress;
|
|
71
76
|
};
|
|
77
|
+
interface SlippageToleranceResponse {
|
|
78
|
+
slippageBps: number | null;
|
|
79
|
+
}
|
|
80
|
+
interface SlippageToleranceRequest {
|
|
81
|
+
chainId: SupportedChainId;
|
|
82
|
+
sellToken: OrderParameters['sellToken'];
|
|
83
|
+
buyToken: OrderParameters['buyToken'];
|
|
84
|
+
sellAmount?: bigint;
|
|
85
|
+
buyAmount?: bigint;
|
|
86
|
+
}
|
|
72
87
|
/**
|
|
73
88
|
* Trade type, assets, amounts, and optional parameters.
|
|
74
89
|
*/
|
|
@@ -108,6 +123,10 @@ interface SwapAdvancedSettings {
|
|
|
108
123
|
appData?: AppDataParams;
|
|
109
124
|
additionalParams?: PostTradeAdditionalParams;
|
|
110
125
|
quoteSigner?: SignerLike;
|
|
126
|
+
/**
|
|
127
|
+
* An optional callback to use custom logic for slippage suggestion
|
|
128
|
+
*/
|
|
129
|
+
getSlippageSuggestion?(request: SlippageToleranceRequest): Promise<SlippageToleranceResponse>;
|
|
111
130
|
}
|
|
112
131
|
interface LimitOrderAdvancedSettings {
|
|
113
132
|
appData?: AppDataParams;
|
|
@@ -234,9 +253,12 @@ declare function getQuoteWithSigner(swapParameters: SwapParameters, advancedSett
|
|
|
234
253
|
|
|
235
254
|
declare function postSellNativeCurrencyOrder(orderBookApi: OrderBookApi, appData: Pick<TradingAppDataInfo, 'fullAppData' | 'appDataKeccak256'>, _params: LimitTradeParametersFromQuote, additionalParams?: PostTradeAdditionalParams, paramSigner?: SignerLike): Promise<OrderPostingResult>;
|
|
236
255
|
|
|
237
|
-
declare function getPreSignTransaction(signer: Signer, chainId: SupportedChainId,
|
|
256
|
+
declare function getPreSignTransaction(signer: Signer, chainId: SupportedChainId, orderId: string): Promise<TradingTransactionParams>;
|
|
238
257
|
|
|
239
258
|
type WithPartialTraderParams<T> = T & Partial<TraderParameters>;
|
|
259
|
+
type OrderTraderParams = WithPartialTraderParams<{
|
|
260
|
+
orderUid: string;
|
|
261
|
+
}>;
|
|
240
262
|
interface TradingSdkOptions {
|
|
241
263
|
enableLogging: boolean;
|
|
242
264
|
orderBookApi: OrderBookApi;
|
|
@@ -273,10 +295,56 @@ declare class TradingSdk {
|
|
|
273
295
|
* ```
|
|
274
296
|
*/
|
|
275
297
|
postSellNativeCurrencyOrder(params: WithPartialTraderParams<TradeParameters>, advancedSettings?: SwapAdvancedSettings): Promise<ReturnType<typeof postSellNativeCurrencyOrder>>;
|
|
276
|
-
getPreSignTransaction(params:
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
298
|
+
getPreSignTransaction(params: OrderTraderParams): ReturnType<typeof getPreSignTransaction>;
|
|
299
|
+
getOrder(params: OrderTraderParams): Promise<EnrichedOrder>;
|
|
300
|
+
offChainCancelOrder(params: OrderTraderParams): Promise<boolean>;
|
|
301
|
+
onChainCancelOrder(params: OrderTraderParams, _order?: EnrichedOrder): Promise<string>;
|
|
302
|
+
/**
|
|
303
|
+
* Checks the current allowance for the CoW Protocol Vault Relayer to spend an ERC-20 token.
|
|
304
|
+
*
|
|
305
|
+
* @param params - Parameters including token address and owner address
|
|
306
|
+
* @returns Promise resolving to the current allowance amount as a bigint
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```typescript
|
|
310
|
+
* const params = {
|
|
311
|
+
* tokenAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
|
|
312
|
+
* owner: '0x123...',
|
|
313
|
+
* chainId: 1,
|
|
314
|
+
* }
|
|
315
|
+
*
|
|
316
|
+
* const allowance = await sdk.getCowProtocolAllowance(params)
|
|
317
|
+
* console.log('Current allowance:', allowance.toString())
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
320
|
+
getCowProtocolAllowance(params: WithPartialTraderParams<{
|
|
321
|
+
tokenAddress: string;
|
|
322
|
+
owner: string;
|
|
323
|
+
}>): Promise<bigint>;
|
|
324
|
+
/**
|
|
325
|
+
* Approves the CoW Protocol Vault Relayer to spend a specified amount of an ERC-20 token.
|
|
326
|
+
* This method creates an on-chain approval transaction.
|
|
327
|
+
*
|
|
328
|
+
* @param params - Parameters including token address and amount to approve
|
|
329
|
+
* @returns Promise resolving to the transaction hash of the approval transaction
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```typescript
|
|
333
|
+
* const params = {
|
|
334
|
+
* tokenAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
|
|
335
|
+
* amount: '1000000000', // 1000 USDC (6 decimals)
|
|
336
|
+
* chainId: 1,
|
|
337
|
+
* }
|
|
338
|
+
*
|
|
339
|
+
* const txHash = await sdk.approveCowProtocol(params)
|
|
340
|
+
* console.log('Approval transaction:', txHash)
|
|
341
|
+
* ```
|
|
342
|
+
*/
|
|
343
|
+
approveCowProtocol(params: WithPartialTraderParams<{
|
|
344
|
+
tokenAddress: string;
|
|
345
|
+
amount: bigint;
|
|
346
|
+
}>): Promise<string>;
|
|
347
|
+
private resolveOrderBookApi;
|
|
280
348
|
private mergeParams;
|
|
281
349
|
}
|
|
282
350
|
|
|
@@ -308,6 +376,7 @@ interface SuggestSlippageBps {
|
|
|
308
376
|
quote: OrderQuoteResponse;
|
|
309
377
|
trader: QuoterParameters;
|
|
310
378
|
advancedSettings?: SwapAdvancedSettings;
|
|
379
|
+
volumeMultiplierPercent?: number;
|
|
311
380
|
}
|
|
312
381
|
/**
|
|
313
382
|
* Return the slippage in BPS that would allow the fee to increase by the multiplying factor percent.
|
|
@@ -333,4 +402,4 @@ declare function getTradeParametersAfterQuote({ quoteParameters, sellToken, }: {
|
|
|
333
402
|
sellToken: string;
|
|
334
403
|
}): TradeParameters;
|
|
335
404
|
|
|
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 };
|
|
405
|
+
export { type AppDataRootSchema, type BuildAppDataParams, type EthFlowOrderExistsCallback, type LimitOrderAdvancedSettings, type LimitOrderParameters, type LimitTradeParameters, type LimitTradeParametersFromQuote, ORDER_PRIMARY_TYPE, type OrderPostingResult, type OrderTraderParams, 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 };
|