@cowprotocol/sdk-flash-loans 1.0.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 +448 -0
- package/dist/index.d.mts +269 -0
- package/dist/index.d.ts +269 -0
- package/dist/index.js +663 -0
- package/dist/index.mjs +629 -0
- package/package.json +52 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { TradeParameters, SwapAdvancedSettings, TradingSdk, OrderPostingResult } from '@cowprotocol/sdk-trading';
|
|
2
|
+
import { AccountAddress, TransactionResponse } from '@cowprotocol/sdk-common';
|
|
3
|
+
import { SupportedChainId } from '@cowprotocol/sdk-config';
|
|
4
|
+
import { Address } from '@cowprotocol/sdk-order-book';
|
|
5
|
+
import { UnsignedOrder } from '@cowprotocol/sdk-order-signing';
|
|
6
|
+
|
|
7
|
+
interface FlashLoanHookAmounts {
|
|
8
|
+
flashLoanAmount: string;
|
|
9
|
+
flashLoanFeeAmount: string;
|
|
10
|
+
sellAssetAmount: string;
|
|
11
|
+
buyAssetAmount: string;
|
|
12
|
+
}
|
|
13
|
+
interface FlashLoanHint {
|
|
14
|
+
amount: string;
|
|
15
|
+
receiver: string;
|
|
16
|
+
liquidityProvider: string;
|
|
17
|
+
protocolAdapter: string;
|
|
18
|
+
token: string;
|
|
19
|
+
}
|
|
20
|
+
interface CollateralOrderData {
|
|
21
|
+
owner: string;
|
|
22
|
+
receiver: string;
|
|
23
|
+
sellToken: string;
|
|
24
|
+
buyToken: string;
|
|
25
|
+
sellAmount: string;
|
|
26
|
+
buyAmount: string;
|
|
27
|
+
kind: string;
|
|
28
|
+
validTo: number;
|
|
29
|
+
flashLoanAmount: string;
|
|
30
|
+
flashLoanFeeAmount: string;
|
|
31
|
+
hookSellTokenAmount: string;
|
|
32
|
+
hookBuyTokenAmount: string;
|
|
33
|
+
}
|
|
34
|
+
type EncodedOrder = Record<string, string | number>;
|
|
35
|
+
/**
|
|
36
|
+
* Parameters for executing a collateral swap using Aave flash loans.
|
|
37
|
+
*/
|
|
38
|
+
interface CollateralSwapParams {
|
|
39
|
+
/** The blockchain network to execute the swap on. */
|
|
40
|
+
chainId: SupportedChainId;
|
|
41
|
+
/** Trade parameters including tokens, amounts, and validity period. */
|
|
42
|
+
tradeParameters: TradeParameters;
|
|
43
|
+
/** The address of the collateral token to be approved for the flash loan adapter. */
|
|
44
|
+
collateralToken: Address;
|
|
45
|
+
/** The flash loan fee as a percentage (e.g., 0.05 for 0.05%). Defaults to 0. */
|
|
46
|
+
flashLoanFeePercent?: number;
|
|
47
|
+
/** Optional settings for controlling approval and permit behavior. */
|
|
48
|
+
settings?: {
|
|
49
|
+
/** Set to true to prevent automatic token approval. Default: false. */
|
|
50
|
+
preventApproval?: boolean;
|
|
51
|
+
/** EIP-2612 permit data for gasless approval. If provided, uses permit instead of approve. */
|
|
52
|
+
collateralPermit?: CollateralPermitData;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
interface CollateralSwapTradeParams {
|
|
56
|
+
chainId: SupportedChainId;
|
|
57
|
+
validTo: number;
|
|
58
|
+
owner: AccountAddress;
|
|
59
|
+
flashLoanFeeAmount: bigint;
|
|
60
|
+
}
|
|
61
|
+
interface CollateralSwapOrder {
|
|
62
|
+
sellAmount: bigint;
|
|
63
|
+
buyAmount: bigint;
|
|
64
|
+
orderToSign: UnsignedOrder;
|
|
65
|
+
collateralPermit?: CollateralPermitData;
|
|
66
|
+
}
|
|
67
|
+
interface CollateralSwapQuoteParams extends Omit<TradeParameters, 'owner' | 'validTo'>, CollateralSwapTradeParams {
|
|
68
|
+
}
|
|
69
|
+
interface CollateralSwapPostParams {
|
|
70
|
+
swapSettings: SwapAdvancedSettings;
|
|
71
|
+
instanceAddress: AccountAddress;
|
|
72
|
+
}
|
|
73
|
+
interface CollateralParameters {
|
|
74
|
+
trader: AccountAddress;
|
|
75
|
+
collateralToken: string;
|
|
76
|
+
amount: bigint;
|
|
77
|
+
instanceAddress: AccountAddress;
|
|
78
|
+
}
|
|
79
|
+
interface CollateralPermitData {
|
|
80
|
+
amount: string;
|
|
81
|
+
deadline: number;
|
|
82
|
+
v: number;
|
|
83
|
+
r: string;
|
|
84
|
+
s: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* SDK for executing Aave flash loan operations, particularly collateral swaps.
|
|
89
|
+
*
|
|
90
|
+
* @remarks This SDK facilitates flash loan-based collateral swaps using Aave Protocol V3,
|
|
91
|
+
* integrated with CoW Protocol for optimal trading execution. It handles the
|
|
92
|
+
* complex flow of flash loans, order creation, and hook configuration.
|
|
93
|
+
*
|
|
94
|
+
* @see https://docs.aave.com/developers/guides/flash-loans
|
|
95
|
+
* @see https://docs.cow.fi/
|
|
96
|
+
*/
|
|
97
|
+
declare class AaveCollateralSwapSdk {
|
|
98
|
+
/**
|
|
99
|
+
* Executes a collateral swap using Aave flash loans.
|
|
100
|
+
*
|
|
101
|
+
* @remarks This method orchestrates a complex flash loan operation:
|
|
102
|
+
* 1. Borrows the sell token via Aave flash loan
|
|
103
|
+
* 2. Uses CoW hooks to deploy adapter contracts and manage the swap
|
|
104
|
+
* 3. Executes a CoW Protocol swap to the buy token
|
|
105
|
+
* 4. Repays the flash loan with fees
|
|
106
|
+
*
|
|
107
|
+
* The order is signed using EIP-1271 with a deterministically generated
|
|
108
|
+
* smart contract address as the signer.
|
|
109
|
+
*
|
|
110
|
+
* @param {CollateralSwapParams} params - Configuration for the collateral swap.
|
|
111
|
+
* @param {TradingSdk} tradingSdk - @cowprotocol/sdk-trading.
|
|
112
|
+
* @returns {Promise<OrderPostingResult>} The result of posting the order to CoW Protocol.
|
|
113
|
+
*
|
|
114
|
+
* @throws Will throw if the quote fails, contract deployment fails, or gas estimation fails.
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
collateralSwap(params: CollateralSwapParams, tradingSdk: TradingSdk): Promise<OrderPostingResult>;
|
|
118
|
+
/**
|
|
119
|
+
* Prepares quote parameters for the collateral swap operation.
|
|
120
|
+
*
|
|
121
|
+
* @remarks This method calculates the adjusted swap amount after deducting the flash loan fee,
|
|
122
|
+
* resolves the trader address, and computes the order validity timestamp. The flash
|
|
123
|
+
* loan fee is deducted from the sell amount before requesting a quote to ensure the
|
|
124
|
+
* final swap amount covers both the desired output and the flash loan repayment.
|
|
125
|
+
*
|
|
126
|
+
* @param {CollateralSwapParams} params - The collateral swap parameters including chain ID,
|
|
127
|
+
* trade parameters, and optional flash loan fee percentage.
|
|
128
|
+
* @returns {Promise<CollateralSwapQuoteParams>} Quote parameters with adjusted amounts and
|
|
129
|
+
* computed validity period.
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
getSwapQuoteParams(params: CollateralSwapParams): Promise<CollateralSwapQuoteParams>;
|
|
133
|
+
/**
|
|
134
|
+
* Generates order posting settings including hooks and flash loan metadata.
|
|
135
|
+
*
|
|
136
|
+
* @remarks This method constructs the complete order posting configuration for a flash loan-based
|
|
137
|
+
* collateral swap. It:
|
|
138
|
+
* - Encodes the order data for EIP-1271 signature verification
|
|
139
|
+
* - Calculates the deterministic address for the adapter contract
|
|
140
|
+
* - Configures pre and post-execution hooks for flash loan management
|
|
141
|
+
* - Generates flash loan hint metadata conforming to the flashloan v0.2.0 schema
|
|
142
|
+
* - Sets up EIP-1271 signing scheme with the expected instance address
|
|
143
|
+
* - Includes hooks configuration following the hooks v0.2.0 schema
|
|
144
|
+
*
|
|
145
|
+
* The flash loan metadata includes information about the Aave pool, protocol adapter,
|
|
146
|
+
* and token amounts required for execution. The hooks enable the order to trigger
|
|
147
|
+
* flash loan deployment (pre-hook) and collateral swap execution (post-hook).
|
|
148
|
+
*
|
|
149
|
+
* @param {CollateralSwapTradeParams} params - Trade parameters including chain ID, validity period,
|
|
150
|
+
* owner address, and flash loan fee amount.
|
|
151
|
+
* @param {CollateralSwapOrder} settings - Order configuration including sell/buy amounts, the
|
|
152
|
+
* unsigned order to sign, and optional collateral permit data.
|
|
153
|
+
* @returns {Promise<CollateralSwapPostParams>} Object containing swap advanced settings with
|
|
154
|
+
* appData metadata (flashloan + hooks) and the
|
|
155
|
+
* deterministic adapter instance address.
|
|
156
|
+
*
|
|
157
|
+
* @throws Will throw if contract address calculation fails or gas estimation fails.
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
getOrderPostingSettings(params: CollateralSwapTradeParams, settings: CollateralSwapOrder): Promise<CollateralSwapPostParams>;
|
|
161
|
+
/**
|
|
162
|
+
* Checks the current allowance for the flash loan adapter to spend collateral tokens.
|
|
163
|
+
*
|
|
164
|
+
* @remarks This method queries the ERC-20 token contract to determine how many tokens
|
|
165
|
+
* the deterministic adapter instance address is approved to spend on behalf
|
|
166
|
+
* of the trader. This is useful for verifying if approval is needed before
|
|
167
|
+
* executing a collateral swap.
|
|
168
|
+
*
|
|
169
|
+
* @param {CollateralParameters} params - Parameters including trader address, collateral
|
|
170
|
+
* token address, and adapter instance address.
|
|
171
|
+
* @returns {Promise<bigint>} The current allowance amount in token's smallest unit (wei).
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* const allowance = await sdk.getCollateralAllowance({
|
|
176
|
+
* trader: '0x...',
|
|
177
|
+
* collateralToken: '0x...',
|
|
178
|
+
* amount: BigInt('1000000000000000000'),
|
|
179
|
+
* instanceAddress: '0x...',
|
|
180
|
+
* })
|
|
181
|
+
* console.log('Current allowance:', allowance.toString())
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
getCollateralAllowance(params: CollateralParameters): Promise<bigint>;
|
|
185
|
+
/**
|
|
186
|
+
* Approves the flash loan adapter to spend collateral tokens.
|
|
187
|
+
*
|
|
188
|
+
* @remarks This method sends an on-chain approval transaction to allow the deterministic
|
|
189
|
+
* adapter instance address to spend the specified amount of collateral tokens.
|
|
190
|
+
* The approval is required before executing a collateral swap unless using
|
|
191
|
+
* EIP-2612 permit or preventApproval settings.
|
|
192
|
+
*
|
|
193
|
+
* @param {CollateralParameters} params - Parameters including trader address, collateral
|
|
194
|
+
* token address, amount to approve, and adapter
|
|
195
|
+
* instance address.
|
|
196
|
+
* @returns {Promise<TransactionResponse>} The transaction response from the approval transaction.
|
|
197
|
+
*
|
|
198
|
+
* @throws Will throw if the approval transaction fails or is rejected.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* const txResponse = await sdk.approveCollateral({
|
|
203
|
+
* trader: '0x...',
|
|
204
|
+
* collateralToken: '0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d',
|
|
205
|
+
* amount: BigInt('20000000000000000000'),
|
|
206
|
+
* instanceAddress: '0x...',
|
|
207
|
+
* })
|
|
208
|
+
* console.log('Approval transaction hash:', txResponse.hash)
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
approveCollateral(params: CollateralParameters): Promise<TransactionResponse>;
|
|
212
|
+
getExpectedInstanceAddress(chainId: SupportedChainId, trader: AccountAddress, hookAmounts: FlashLoanHookAmounts, order: EncodedOrder): Promise<AccountAddress>;
|
|
213
|
+
private approveCollateralIfNeeded;
|
|
214
|
+
calculateFlashLoanAmounts({ sellAmount, flashLoanFeePercent }: {
|
|
215
|
+
sellAmount: bigint;
|
|
216
|
+
flashLoanFeePercent: number;
|
|
217
|
+
}): {
|
|
218
|
+
flashLoanFeeAmount: bigint;
|
|
219
|
+
sellAmountToSign: bigint;
|
|
220
|
+
};
|
|
221
|
+
private getPreHookCallData;
|
|
222
|
+
private buildHookOrderData;
|
|
223
|
+
private getPostHookCallData;
|
|
224
|
+
private getOrderHooks;
|
|
225
|
+
private adapterEIP1271Signature;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
declare const HASH_ZERO = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
229
|
+
declare const AAVE_POOL_ADDRESS: Record<SupportedChainId, string>;
|
|
230
|
+
declare const AAVE_ADAPTER_FACTORY: {
|
|
231
|
+
100: string;
|
|
232
|
+
1: string;
|
|
233
|
+
56: string;
|
|
234
|
+
137: string;
|
|
235
|
+
232: string;
|
|
236
|
+
8453: string;
|
|
237
|
+
42161: string;
|
|
238
|
+
43114: string;
|
|
239
|
+
11155111: string;
|
|
240
|
+
};
|
|
241
|
+
declare const AAVE_COLLATERAL_SWAP_ADAPTER_HOOK: {
|
|
242
|
+
100: string;
|
|
243
|
+
1: string;
|
|
244
|
+
56: string;
|
|
245
|
+
137: string;
|
|
246
|
+
232: string;
|
|
247
|
+
8453: string;
|
|
248
|
+
42161: string;
|
|
249
|
+
43114: string;
|
|
250
|
+
11155111: string;
|
|
251
|
+
};
|
|
252
|
+
declare const DEFAULT_HOOK_GAS_LIMIT: {
|
|
253
|
+
pre: bigint;
|
|
254
|
+
post: bigint;
|
|
255
|
+
};
|
|
256
|
+
declare const PERCENT_SCALE = 10000;
|
|
257
|
+
declare const DEFAULT_VALIDITY: number;
|
|
258
|
+
declare const GAS_ESTIMATION_ADDITION_PERCENT = 10;
|
|
259
|
+
declare const ADAPTER_DOMAIN_NAME = "AaveAdapterFactory";
|
|
260
|
+
declare const ADAPTER_DOMAIN_VERSION = "1";
|
|
261
|
+
declare const ADAPTER_SIGNATURE_TYPES: {
|
|
262
|
+
AdapterOrderSig: {
|
|
263
|
+
name: string;
|
|
264
|
+
type: string;
|
|
265
|
+
}[];
|
|
266
|
+
};
|
|
267
|
+
declare const EMPTY_PERMIT: CollateralPermitData;
|
|
268
|
+
|
|
269
|
+
export { AAVE_ADAPTER_FACTORY, AAVE_COLLATERAL_SWAP_ADAPTER_HOOK, AAVE_POOL_ADDRESS, ADAPTER_DOMAIN_NAME, ADAPTER_DOMAIN_VERSION, ADAPTER_SIGNATURE_TYPES, AaveCollateralSwapSdk, type CollateralOrderData, type CollateralParameters, type CollateralPermitData, type CollateralSwapOrder, type CollateralSwapParams, type CollateralSwapPostParams, type CollateralSwapQuoteParams, type CollateralSwapTradeParams, DEFAULT_HOOK_GAS_LIMIT, DEFAULT_VALIDITY, EMPTY_PERMIT, type EncodedOrder, type FlashLoanHint, type FlashLoanHookAmounts, GAS_ESTIMATION_ADDITION_PERCENT, HASH_ZERO, PERCENT_SCALE };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { TradeParameters, SwapAdvancedSettings, TradingSdk, OrderPostingResult } from '@cowprotocol/sdk-trading';
|
|
2
|
+
import { AccountAddress, TransactionResponse } from '@cowprotocol/sdk-common';
|
|
3
|
+
import { SupportedChainId } from '@cowprotocol/sdk-config';
|
|
4
|
+
import { Address } from '@cowprotocol/sdk-order-book';
|
|
5
|
+
import { UnsignedOrder } from '@cowprotocol/sdk-order-signing';
|
|
6
|
+
|
|
7
|
+
interface FlashLoanHookAmounts {
|
|
8
|
+
flashLoanAmount: string;
|
|
9
|
+
flashLoanFeeAmount: string;
|
|
10
|
+
sellAssetAmount: string;
|
|
11
|
+
buyAssetAmount: string;
|
|
12
|
+
}
|
|
13
|
+
interface FlashLoanHint {
|
|
14
|
+
amount: string;
|
|
15
|
+
receiver: string;
|
|
16
|
+
liquidityProvider: string;
|
|
17
|
+
protocolAdapter: string;
|
|
18
|
+
token: string;
|
|
19
|
+
}
|
|
20
|
+
interface CollateralOrderData {
|
|
21
|
+
owner: string;
|
|
22
|
+
receiver: string;
|
|
23
|
+
sellToken: string;
|
|
24
|
+
buyToken: string;
|
|
25
|
+
sellAmount: string;
|
|
26
|
+
buyAmount: string;
|
|
27
|
+
kind: string;
|
|
28
|
+
validTo: number;
|
|
29
|
+
flashLoanAmount: string;
|
|
30
|
+
flashLoanFeeAmount: string;
|
|
31
|
+
hookSellTokenAmount: string;
|
|
32
|
+
hookBuyTokenAmount: string;
|
|
33
|
+
}
|
|
34
|
+
type EncodedOrder = Record<string, string | number>;
|
|
35
|
+
/**
|
|
36
|
+
* Parameters for executing a collateral swap using Aave flash loans.
|
|
37
|
+
*/
|
|
38
|
+
interface CollateralSwapParams {
|
|
39
|
+
/** The blockchain network to execute the swap on. */
|
|
40
|
+
chainId: SupportedChainId;
|
|
41
|
+
/** Trade parameters including tokens, amounts, and validity period. */
|
|
42
|
+
tradeParameters: TradeParameters;
|
|
43
|
+
/** The address of the collateral token to be approved for the flash loan adapter. */
|
|
44
|
+
collateralToken: Address;
|
|
45
|
+
/** The flash loan fee as a percentage (e.g., 0.05 for 0.05%). Defaults to 0. */
|
|
46
|
+
flashLoanFeePercent?: number;
|
|
47
|
+
/** Optional settings for controlling approval and permit behavior. */
|
|
48
|
+
settings?: {
|
|
49
|
+
/** Set to true to prevent automatic token approval. Default: false. */
|
|
50
|
+
preventApproval?: boolean;
|
|
51
|
+
/** EIP-2612 permit data for gasless approval. If provided, uses permit instead of approve. */
|
|
52
|
+
collateralPermit?: CollateralPermitData;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
interface CollateralSwapTradeParams {
|
|
56
|
+
chainId: SupportedChainId;
|
|
57
|
+
validTo: number;
|
|
58
|
+
owner: AccountAddress;
|
|
59
|
+
flashLoanFeeAmount: bigint;
|
|
60
|
+
}
|
|
61
|
+
interface CollateralSwapOrder {
|
|
62
|
+
sellAmount: bigint;
|
|
63
|
+
buyAmount: bigint;
|
|
64
|
+
orderToSign: UnsignedOrder;
|
|
65
|
+
collateralPermit?: CollateralPermitData;
|
|
66
|
+
}
|
|
67
|
+
interface CollateralSwapQuoteParams extends Omit<TradeParameters, 'owner' | 'validTo'>, CollateralSwapTradeParams {
|
|
68
|
+
}
|
|
69
|
+
interface CollateralSwapPostParams {
|
|
70
|
+
swapSettings: SwapAdvancedSettings;
|
|
71
|
+
instanceAddress: AccountAddress;
|
|
72
|
+
}
|
|
73
|
+
interface CollateralParameters {
|
|
74
|
+
trader: AccountAddress;
|
|
75
|
+
collateralToken: string;
|
|
76
|
+
amount: bigint;
|
|
77
|
+
instanceAddress: AccountAddress;
|
|
78
|
+
}
|
|
79
|
+
interface CollateralPermitData {
|
|
80
|
+
amount: string;
|
|
81
|
+
deadline: number;
|
|
82
|
+
v: number;
|
|
83
|
+
r: string;
|
|
84
|
+
s: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* SDK for executing Aave flash loan operations, particularly collateral swaps.
|
|
89
|
+
*
|
|
90
|
+
* @remarks This SDK facilitates flash loan-based collateral swaps using Aave Protocol V3,
|
|
91
|
+
* integrated with CoW Protocol for optimal trading execution. It handles the
|
|
92
|
+
* complex flow of flash loans, order creation, and hook configuration.
|
|
93
|
+
*
|
|
94
|
+
* @see https://docs.aave.com/developers/guides/flash-loans
|
|
95
|
+
* @see https://docs.cow.fi/
|
|
96
|
+
*/
|
|
97
|
+
declare class AaveCollateralSwapSdk {
|
|
98
|
+
/**
|
|
99
|
+
* Executes a collateral swap using Aave flash loans.
|
|
100
|
+
*
|
|
101
|
+
* @remarks This method orchestrates a complex flash loan operation:
|
|
102
|
+
* 1. Borrows the sell token via Aave flash loan
|
|
103
|
+
* 2. Uses CoW hooks to deploy adapter contracts and manage the swap
|
|
104
|
+
* 3. Executes a CoW Protocol swap to the buy token
|
|
105
|
+
* 4. Repays the flash loan with fees
|
|
106
|
+
*
|
|
107
|
+
* The order is signed using EIP-1271 with a deterministically generated
|
|
108
|
+
* smart contract address as the signer.
|
|
109
|
+
*
|
|
110
|
+
* @param {CollateralSwapParams} params - Configuration for the collateral swap.
|
|
111
|
+
* @param {TradingSdk} tradingSdk - @cowprotocol/sdk-trading.
|
|
112
|
+
* @returns {Promise<OrderPostingResult>} The result of posting the order to CoW Protocol.
|
|
113
|
+
*
|
|
114
|
+
* @throws Will throw if the quote fails, contract deployment fails, or gas estimation fails.
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
collateralSwap(params: CollateralSwapParams, tradingSdk: TradingSdk): Promise<OrderPostingResult>;
|
|
118
|
+
/**
|
|
119
|
+
* Prepares quote parameters for the collateral swap operation.
|
|
120
|
+
*
|
|
121
|
+
* @remarks This method calculates the adjusted swap amount after deducting the flash loan fee,
|
|
122
|
+
* resolves the trader address, and computes the order validity timestamp. The flash
|
|
123
|
+
* loan fee is deducted from the sell amount before requesting a quote to ensure the
|
|
124
|
+
* final swap amount covers both the desired output and the flash loan repayment.
|
|
125
|
+
*
|
|
126
|
+
* @param {CollateralSwapParams} params - The collateral swap parameters including chain ID,
|
|
127
|
+
* trade parameters, and optional flash loan fee percentage.
|
|
128
|
+
* @returns {Promise<CollateralSwapQuoteParams>} Quote parameters with adjusted amounts and
|
|
129
|
+
* computed validity period.
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
getSwapQuoteParams(params: CollateralSwapParams): Promise<CollateralSwapQuoteParams>;
|
|
133
|
+
/**
|
|
134
|
+
* Generates order posting settings including hooks and flash loan metadata.
|
|
135
|
+
*
|
|
136
|
+
* @remarks This method constructs the complete order posting configuration for a flash loan-based
|
|
137
|
+
* collateral swap. It:
|
|
138
|
+
* - Encodes the order data for EIP-1271 signature verification
|
|
139
|
+
* - Calculates the deterministic address for the adapter contract
|
|
140
|
+
* - Configures pre and post-execution hooks for flash loan management
|
|
141
|
+
* - Generates flash loan hint metadata conforming to the flashloan v0.2.0 schema
|
|
142
|
+
* - Sets up EIP-1271 signing scheme with the expected instance address
|
|
143
|
+
* - Includes hooks configuration following the hooks v0.2.0 schema
|
|
144
|
+
*
|
|
145
|
+
* The flash loan metadata includes information about the Aave pool, protocol adapter,
|
|
146
|
+
* and token amounts required for execution. The hooks enable the order to trigger
|
|
147
|
+
* flash loan deployment (pre-hook) and collateral swap execution (post-hook).
|
|
148
|
+
*
|
|
149
|
+
* @param {CollateralSwapTradeParams} params - Trade parameters including chain ID, validity period,
|
|
150
|
+
* owner address, and flash loan fee amount.
|
|
151
|
+
* @param {CollateralSwapOrder} settings - Order configuration including sell/buy amounts, the
|
|
152
|
+
* unsigned order to sign, and optional collateral permit data.
|
|
153
|
+
* @returns {Promise<CollateralSwapPostParams>} Object containing swap advanced settings with
|
|
154
|
+
* appData metadata (flashloan + hooks) and the
|
|
155
|
+
* deterministic adapter instance address.
|
|
156
|
+
*
|
|
157
|
+
* @throws Will throw if contract address calculation fails or gas estimation fails.
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
getOrderPostingSettings(params: CollateralSwapTradeParams, settings: CollateralSwapOrder): Promise<CollateralSwapPostParams>;
|
|
161
|
+
/**
|
|
162
|
+
* Checks the current allowance for the flash loan adapter to spend collateral tokens.
|
|
163
|
+
*
|
|
164
|
+
* @remarks This method queries the ERC-20 token contract to determine how many tokens
|
|
165
|
+
* the deterministic adapter instance address is approved to spend on behalf
|
|
166
|
+
* of the trader. This is useful for verifying if approval is needed before
|
|
167
|
+
* executing a collateral swap.
|
|
168
|
+
*
|
|
169
|
+
* @param {CollateralParameters} params - Parameters including trader address, collateral
|
|
170
|
+
* token address, and adapter instance address.
|
|
171
|
+
* @returns {Promise<bigint>} The current allowance amount in token's smallest unit (wei).
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* const allowance = await sdk.getCollateralAllowance({
|
|
176
|
+
* trader: '0x...',
|
|
177
|
+
* collateralToken: '0x...',
|
|
178
|
+
* amount: BigInt('1000000000000000000'),
|
|
179
|
+
* instanceAddress: '0x...',
|
|
180
|
+
* })
|
|
181
|
+
* console.log('Current allowance:', allowance.toString())
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
getCollateralAllowance(params: CollateralParameters): Promise<bigint>;
|
|
185
|
+
/**
|
|
186
|
+
* Approves the flash loan adapter to spend collateral tokens.
|
|
187
|
+
*
|
|
188
|
+
* @remarks This method sends an on-chain approval transaction to allow the deterministic
|
|
189
|
+
* adapter instance address to spend the specified amount of collateral tokens.
|
|
190
|
+
* The approval is required before executing a collateral swap unless using
|
|
191
|
+
* EIP-2612 permit or preventApproval settings.
|
|
192
|
+
*
|
|
193
|
+
* @param {CollateralParameters} params - Parameters including trader address, collateral
|
|
194
|
+
* token address, amount to approve, and adapter
|
|
195
|
+
* instance address.
|
|
196
|
+
* @returns {Promise<TransactionResponse>} The transaction response from the approval transaction.
|
|
197
|
+
*
|
|
198
|
+
* @throws Will throw if the approval transaction fails or is rejected.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* const txResponse = await sdk.approveCollateral({
|
|
203
|
+
* trader: '0x...',
|
|
204
|
+
* collateralToken: '0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d',
|
|
205
|
+
* amount: BigInt('20000000000000000000'),
|
|
206
|
+
* instanceAddress: '0x...',
|
|
207
|
+
* })
|
|
208
|
+
* console.log('Approval transaction hash:', txResponse.hash)
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
approveCollateral(params: CollateralParameters): Promise<TransactionResponse>;
|
|
212
|
+
getExpectedInstanceAddress(chainId: SupportedChainId, trader: AccountAddress, hookAmounts: FlashLoanHookAmounts, order: EncodedOrder): Promise<AccountAddress>;
|
|
213
|
+
private approveCollateralIfNeeded;
|
|
214
|
+
calculateFlashLoanAmounts({ sellAmount, flashLoanFeePercent }: {
|
|
215
|
+
sellAmount: bigint;
|
|
216
|
+
flashLoanFeePercent: number;
|
|
217
|
+
}): {
|
|
218
|
+
flashLoanFeeAmount: bigint;
|
|
219
|
+
sellAmountToSign: bigint;
|
|
220
|
+
};
|
|
221
|
+
private getPreHookCallData;
|
|
222
|
+
private buildHookOrderData;
|
|
223
|
+
private getPostHookCallData;
|
|
224
|
+
private getOrderHooks;
|
|
225
|
+
private adapterEIP1271Signature;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
declare const HASH_ZERO = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
229
|
+
declare const AAVE_POOL_ADDRESS: Record<SupportedChainId, string>;
|
|
230
|
+
declare const AAVE_ADAPTER_FACTORY: {
|
|
231
|
+
100: string;
|
|
232
|
+
1: string;
|
|
233
|
+
56: string;
|
|
234
|
+
137: string;
|
|
235
|
+
232: string;
|
|
236
|
+
8453: string;
|
|
237
|
+
42161: string;
|
|
238
|
+
43114: string;
|
|
239
|
+
11155111: string;
|
|
240
|
+
};
|
|
241
|
+
declare const AAVE_COLLATERAL_SWAP_ADAPTER_HOOK: {
|
|
242
|
+
100: string;
|
|
243
|
+
1: string;
|
|
244
|
+
56: string;
|
|
245
|
+
137: string;
|
|
246
|
+
232: string;
|
|
247
|
+
8453: string;
|
|
248
|
+
42161: string;
|
|
249
|
+
43114: string;
|
|
250
|
+
11155111: string;
|
|
251
|
+
};
|
|
252
|
+
declare const DEFAULT_HOOK_GAS_LIMIT: {
|
|
253
|
+
pre: bigint;
|
|
254
|
+
post: bigint;
|
|
255
|
+
};
|
|
256
|
+
declare const PERCENT_SCALE = 10000;
|
|
257
|
+
declare const DEFAULT_VALIDITY: number;
|
|
258
|
+
declare const GAS_ESTIMATION_ADDITION_PERCENT = 10;
|
|
259
|
+
declare const ADAPTER_DOMAIN_NAME = "AaveAdapterFactory";
|
|
260
|
+
declare const ADAPTER_DOMAIN_VERSION = "1";
|
|
261
|
+
declare const ADAPTER_SIGNATURE_TYPES: {
|
|
262
|
+
AdapterOrderSig: {
|
|
263
|
+
name: string;
|
|
264
|
+
type: string;
|
|
265
|
+
}[];
|
|
266
|
+
};
|
|
267
|
+
declare const EMPTY_PERMIT: CollateralPermitData;
|
|
268
|
+
|
|
269
|
+
export { AAVE_ADAPTER_FACTORY, AAVE_COLLATERAL_SWAP_ADAPTER_HOOK, AAVE_POOL_ADDRESS, ADAPTER_DOMAIN_NAME, ADAPTER_DOMAIN_VERSION, ADAPTER_SIGNATURE_TYPES, AaveCollateralSwapSdk, type CollateralOrderData, type CollateralParameters, type CollateralPermitData, type CollateralSwapOrder, type CollateralSwapParams, type CollateralSwapPostParams, type CollateralSwapQuoteParams, type CollateralSwapTradeParams, DEFAULT_HOOK_GAS_LIMIT, DEFAULT_VALIDITY, EMPTY_PERMIT, type EncodedOrder, type FlashLoanHint, type FlashLoanHookAmounts, GAS_ESTIMATION_ADDITION_PERCENT, HASH_ZERO, PERCENT_SCALE };
|