Skip to content

Commit 18fc1c1

Browse files
committed
fix: refactor swap scenario
1 parent e0b5195 commit 18fc1c1

File tree

1 file changed

+44
-47
lines changed

1 file changed

+44
-47
lines changed

packages/spec/swap/business.spec.ts

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import {
66
SwapKind,
77
} from '@aave/client';
88
import {
9-
prepareSwap,
9+
prepareTokenSwap,
1010
swap,
11-
swapQuote,
1211
swapStatus,
1312
userSwaps,
1413
} from '@aave/client/actions';
@@ -40,58 +39,56 @@ describe('Aave V4 Swap Scenarios', () => {
4039
});
4140

4241
it(`Then the user's swap positions are updated`, async ({ annotate }) => {
43-
const swapResult = await swapQuote(client, {
44-
amount: bigDecimal('20'),
45-
sell: { erc20: ETHEREUM_USDC_ADDRESS },
46-
buy: { erc20: ETHEREUM_WETH_ADDRESS },
47-
chainId: ETHEREUM_FORK_ID,
48-
from: evmAddress(user.account.address),
49-
kind: SwapKind.Sell,
50-
receiver: evmAddress(user.account.address),
51-
})
52-
.andThen((quote) =>
53-
prepareSwap(client, {
54-
limit: {
55-
quoteId: quote.quoteId,
56-
},
57-
}),
58-
)
59-
.andThen((swapPlan) => {
60-
switch (swapPlan.__typename) {
61-
case 'SwapByIntent':
62-
return signSwapTypedDataWith(user, swapPlan.data).andThen(
42+
const swapResult = await prepareTokenSwap(client, {
43+
market: {
44+
amount: bigDecimal('20'),
45+
sell: { erc20: ETHEREUM_USDC_ADDRESS },
46+
buy: { erc20: ETHEREUM_WETH_ADDRESS },
47+
chainId: ETHEREUM_FORK_ID,
48+
kind: SwapKind.Sell,
49+
receiver: evmAddress(user.account.address),
50+
user: evmAddress(user.account.address),
51+
},
52+
}).andThen((swapPlan) => {
53+
switch (swapPlan.__typename) {
54+
case 'SwapByIntent':
55+
return signSwapTypedDataWith(user, swapPlan.data).andThen(
56+
(signature) => {
57+
return swap(client, {
58+
intent: {
59+
quoteId: swapPlan.quote.quoteId,
60+
signature: signature,
61+
},
62+
});
63+
},
64+
);
65+
case 'SwapByIntentWithApprovalRequired':
66+
return sendTransaction(user, swapPlan.approval).andThen(() =>
67+
signSwapTypedDataWith(user, swapPlan.data).andThen(
6368
(signature) => {
6469
return swap(client, {
6570
intent: {
6671
quoteId: swapPlan.quote.quoteId,
67-
signature: signature.value,
72+
signature: signature,
6873
},
6974
});
7075
},
71-
);
72-
case 'SwapByIntentWithApprovalRequired':
73-
return sendTransaction(user, swapPlan.approval).andThen(() =>
74-
signSwapTypedDataWith(user, swapPlan.data).andThen(
75-
(signature) => {
76-
return swap(client, {
77-
intent: {
78-
quoteId: swapPlan.quote.quoteId,
79-
signature: signature.value,
80-
},
81-
});
82-
},
83-
),
84-
);
85-
case 'SwapByTransaction':
86-
return swap(client, {
87-
transaction: { quoteId: swapPlan.quote.quoteId },
88-
});
89-
case 'InsufficientBalanceError':
90-
throw new Error(
91-
`Insufficient balance: ${swapPlan.required.value} required.`,
92-
);
93-
}
94-
});
76+
),
77+
);
78+
case 'SwapByTransaction':
79+
return swap(client, {
80+
transaction: { quoteId: swapPlan.quote.quoteId },
81+
});
82+
case 'InsufficientBalanceError':
83+
throw new Error(
84+
`Insufficient balance: ${swapPlan.required.value} required.`,
85+
);
86+
default:
87+
throw new Error(
88+
`Unexpected swap plan type: ${(swapPlan as unknown as { __typename: string }).__typename}`,
89+
);
90+
}
91+
});
9592

9693
assertOk(swapResult);
9794
invariant(

0 commit comments

Comments
 (0)