Skip to content

Commit 888fb85

Browse files
committed
feat: send hooks to quote for proper adapters gas est
1 parent e950a9b commit 888fb85

File tree

2 files changed

+104
-109
lines changed

2 files changed

+104
-109
lines changed

src/components/transactions/Swap/helpers/cow/adapters.helpers.ts

Lines changed: 102 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { valueToBigNumber } from '@aave/math-utils';
1+
import { API_ETH_MOCK_ADDRESS } from '@aave/contract-helpers';
2+
import { normalize, valueToBigNumber } from '@aave/math-utils';
23
import {
34
AppDataParams,
45
getOrderToSign,
@@ -8,9 +9,12 @@ import {
89
SupportedChainId,
910
} from '@cowprotocol/cow-sdk';
1011
import {
12+
AAVE_ADAPTER_FACTORY,
13+
AAVE_POOL_ADDRESS,
1114
AaveCollateralSwapSdk,
1215
AaveFlashLoanType,
1316
EncodedOrder,
17+
FlashLoanHint,
1418
FlashLoanHookAmounts,
1519
HASH_ZERO,
1620
} from '@cowprotocol/sdk-flash-loans';
@@ -176,38 +180,40 @@ export const calculateFlashLoanAmounts = (
176180
* and therefore the quote is more precise and more chances of being executed
177181
* It's important to send the hooks and flashloan hint but not the exact amounts that will be used in the final
178182
*/
179-
export const getAppDataForQuote = async ({}: // user,
180-
// type,
181-
// amount,
182-
// chainId,
183-
// srcToken,
184-
// srcDecimals,
185-
// destToken,
186-
// destDecimals,
187-
{
183+
export const getAppDataForQuote = async ({
184+
user,
185+
type,
186+
sellAmount,
187+
buyAmount,
188+
chainId,
189+
srcToken,
190+
srcDecimals,
191+
destToken,
192+
destDecimals,
193+
}: {
194+
// user,
188195
user: string;
189196
type: SwapType;
190-
amount: string;
197+
sellAmount: string;
198+
buyAmount: string;
191199
chainId: SupportedChainId;
192200
srcToken: string;
193201
srcDecimals: number;
194202
destToken: string;
195203
destDecimals: number;
196204
}): Promise<AppDataParams | undefined> => {
197-
return undefined;
198-
199205
// NOTE: This function is prepared to add solver hooks for accurate network cost estimation,
200206
// but such estimations are not currently supported so solvers are absorbing some costs.
201207
// Disabled for now; enable when proper support becomes available.
202208

203-
// if (type === SwapType.Swap || type === SwapType.WithdrawAndSwap) {
204-
// return undefined; // no flashloan needed - plain swap
205-
// }
209+
if (type === SwapType.Swap || type === SwapType.WithdrawAndSwap) {
210+
return undefined; // no flashloan needed - plain swap
211+
}
206212

207-
// const factory =
208-
// AAVE_ADAPTER_FACTORY[chainId].length > 0 ? AAVE_ADAPTER_FACTORY[chainId] : API_ETH_MOCK_ADDRESS;
209-
// const pool =
210-
// AAVE_POOL_ADDRESS[chainId].length > 0 ? AAVE_POOL_ADDRESS[chainId] : API_ETH_MOCK_ADDRESS;
213+
const factory =
214+
AAVE_ADAPTER_FACTORY[chainId].length > 0 ? AAVE_ADAPTER_FACTORY[chainId] : API_ETH_MOCK_ADDRESS;
215+
const pool =
216+
AAVE_POOL_ADDRESS[chainId].length > 0 ? AAVE_POOL_ADDRESS[chainId] : API_ETH_MOCK_ADDRESS;
211217
// const AAVE_SWAP_TYPE_TO_COW_TYPE: Partial<Record<SwapType, AaveFlashLoanType>> = {
212218
// [SwapType.CollateralSwap]: AaveFlashLoanType.CollateralSwap,
213219
// [SwapType.DebtSwap]: AaveFlashLoanType.DebtSwap,
@@ -216,101 +222,89 @@ export const getAppDataForQuote = async ({}: // user,
216222
// const dappId =
217223
// AAVE_DAPP_ID_PER_TYPE[AAVE_SWAP_TYPE_TO_COW_TYPE[type] ?? AaveFlashLoanType.CollateralSwap];
218224

219-
// // const flashLoanSdk = new AaveCollateralSwapSdk();
220-
// // const { flashLoanFeeAmount, sellAmountToSign } = flashLoanSdk.calculateFlashLoanAmounts({
221-
// // sellAmount: BigInt(normalize(amount, -srcDecimals)),
222-
// // flashLoanFeeBps: FLASH_LOAN_FEE_BPS,
223-
// // });
225+
const flashLoanSdk = new AaveCollateralSwapSdk();
226+
const { flashLoanFeeAmount, sellAmountToSign } = flashLoanSdk.calculateFlashLoanAmounts({
227+
sellAmount: BigInt(normalize(sellAmount, -srcDecimals)),
228+
flashLoanFeeBps: FLASH_LOAN_FEE_BPS,
229+
});
224230

225-
// // let cowType: AaveFlashLoanType;
226-
// // if (type === SwapType.CollateralSwap) {
227-
// // cowType = AaveFlashLoanType.CollateralSwap;
228-
// // } else if (type === SwapType.DebtSwap) {
229-
// // cowType = AaveFlashLoanType.DebtSwap;
230-
// // } else if(type === SwapType.RepayWithCollateral) {
231-
// // cowType = AaveFlashLoanType.RepayCollateral;
232-
// // } else {
233-
// // throw new Error('Invalid swap type');
234-
// // }
231+
let cowType: AaveFlashLoanType;
232+
if (type === SwapType.CollateralSwap) {
233+
cowType = AaveFlashLoanType.CollateralSwap;
234+
} else if (type === SwapType.DebtSwap) {
235+
cowType = AaveFlashLoanType.DebtSwap;
236+
} else if (type === SwapType.RepayWithCollateral) {
237+
cowType = AaveFlashLoanType.RepayCollateral;
238+
} else {
239+
throw new Error('Invalid swap type');
240+
}
235241

236-
// // const hookAmounts: FlashLoanHookAmounts = {
237-
// // flashLoanAmount: amount,
238-
// // flashLoanFeeAmount: flashLoanFeeAmount.toString(),
239-
// // sellAssetAmount: sellAmountToSign.toString(),
240-
// // buyAssetAmount: amount,
241-
// // }
242+
const hookAmounts: FlashLoanHookAmounts = {
243+
flashLoanAmount: sellAmount,
244+
flashLoanFeeAmount: flashLoanFeeAmount.toString(),
245+
sellAssetAmount: sellAmountToSign.toString(),
246+
buyAssetAmount: buyAmount,
247+
};
242248

243-
// const flashloan: FlashLoanHint = {
244-
// amount, // this is actually in UNDERLYING but aave tokens are 1:1
245-
// receiver: factory,
246-
// liquidityProvider: pool,
247-
// protocolAdapter: factory,
248-
// token: srcToken,
249-
// };
249+
const flashloan: FlashLoanHint = {
250+
amount: sellAmount, // this is actually in UNDERLYING but aave tokens are 1:1
251+
receiver: factory,
252+
liquidityProvider: pool,
253+
protocolAdapter: factory,
254+
token: srcToken,
255+
};
250256

251-
// // const limitOrder: LimitTradeParameters = {
252-
// // kind: OrderKind.SELL,
253-
// // sellToken: srcToken,
254-
// // sellTokenDecimals: srcDecimals,
255-
// // buyToken: destToken,
256-
// // buyTokenDecimals: destDecimals,
257-
// // sellAmount: normalize(amount, -srcDecimals).toString(),
258-
// // buyAmount: amount,
259-
// // }
257+
const limitOrder: LimitTradeParameters = {
258+
kind: OrderKind.SELL,
259+
sellToken: srcToken,
260+
sellTokenDecimals: srcDecimals,
261+
buyToken: destToken,
262+
buyTokenDecimals: destDecimals,
263+
sellAmount: normalize(sellAmount, -srcDecimals).toString(),
264+
buyAmount: buyAmount,
265+
};
260266

261-
// // const orderToSign = getOrderToSign(
262-
// // {
263-
// // chainId,
264-
// // from: user,
265-
// // networkCostsAmount: '0',
266-
// // isEthFlow: false,
267-
// // applyCostsSlippageAndFees: false,
268-
// // },
269-
// // limitOrder,
270-
// // HASH_ZERO
271-
// // );
267+
const orderToSign = getOrderToSign(
268+
{
269+
chainId,
270+
from: user,
271+
networkCostsAmount: '0',
272+
isEthFlow: false,
273+
applyCostsSlippageAndFees: false,
274+
},
275+
limitOrder,
276+
HASH_ZERO
277+
);
272278

273-
// // const encodedOrder: EncodedOrder = {
274-
// // ...OrderSigningUtils.encodeUnsignedOrder(orderToSign),
275-
// // appData: HASH_ZERO,
276-
// // }
279+
const encodedOrder: EncodedOrder = {
280+
...OrderSigningUtils.encodeUnsignedOrder(orderToSign),
281+
appData: HASH_ZERO,
282+
};
277283

278-
// // const hooks = await getOrderHooks(
279-
// // cowType,
280-
// // chainId,
281-
// // user as `0x${string}`,
282-
// // zeroAddress,
283-
// // hookAmounts,
284-
// // {
285-
// // ...encodedOrder,
286-
// // receiver: zeroAddress,
287-
// // },
288-
// // );
284+
const adapterInstance = await flashLoanSdk.getExpectedInstanceAddress(
285+
cowType,
286+
chainId,
287+
user as `0x${string}`,
288+
hookAmounts,
289+
encodedOrder
290+
);
289291

290-
// // TODO: send proper calldatas when available so solvers can properly simulate
291-
// const hooks = {
292-
// pre: [
293-
// {
294-
// target: factory,
295-
// callData: '0x',
296-
// gasLimit: 160k DEFAULT_HOOK_GAS_LIMIT.pre.toString(),
297-
// dappId,
298-
// },
299-
// ],
300-
// post: [
301-
// {
302-
// target: 0x,
303-
// callData: '0x',
304-
// gasLimit: 160k DEFAULT_HOOK_GAS_LIMIT.post.toString(),
305-
// dappId,
306-
// },
307-
// ],
308-
// };
292+
const hooks = await flashLoanSdk.getOrderHooks(
293+
cowType,
294+
chainId,
295+
user as `0x${string}`,
296+
adapterInstance,
297+
hookAmounts,
298+
{
299+
...encodedOrder,
300+
receiver: adapterInstance,
301+
}
302+
);
309303

310-
// return {
311-
// metadata: {
312-
// flashloan,
313-
// hooks,
314-
// },
315-
// };
304+
return {
305+
metadata: {
306+
flashloan,
307+
hooks,
308+
},
309+
};
316310
};

src/components/transactions/Swap/helpers/cow/rates.helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ export async function getCowProtocolSellRates({
111111
user,
112112
type: swapType,
113113
chainId,
114-
amount,
114+
sellAmount: amount,
115+
buyAmount: amount, // TODO: Should we ask first for a normal quote and then a second quote having a realistic buy amount?
115116
srcToken,
116117
srcDecimals,
117118
destToken,

0 commit comments

Comments
 (0)