Skip to content

Commit 8d5dd34

Browse files
committed
fix: wrap quote generation in runOnce
1 parent 157801a commit 8d5dd34

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

packages/apps/ability-aerodrome-swap/src/generated/lit-action.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"ipfsCid": "QmZfkwAFi3FqFHYugGCk7Zbd1BTW5GJnJ4trGHW3eWFac5"
2+
"ipfsCid": "QmazSsUszAndaJDfXQ6AiXyMjibCZBHA5NEzR2dy7VJzH5"
33
}

packages/apps/ability-aerodrome-swap/src/lib/vincent-ability.ts

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,37 @@ import {
1515
} from './schemas';
1616
import { AbilityAction, CheckNativeTokenBalanceResultSuccess } from './types';
1717
import { checkErc20Allowance, checkErc20Balance, checkNativeTokenBalance } from './ability-checks';
18-
import { executeSwapParams, getChainConfig, getSwapVars } from 'sugar-sdk/primitives';
18+
import { executeSwapParams, getChainConfig, getSwapVars, type Quote } from 'sugar-sdk/primitives';
1919
import { findSupportedTokenOnBase } from './ability-helpers/find-supported-token-on-base';
2020
import { sendAerodromeSwapTx, sendErc20ApprovalTx } from './ability-helpers';
2121

2222
export const bigintReplacer = (key: any, value: any) => {
2323
return typeof value === 'bigint' ? value.toString() : value;
2424
};
2525

26+
export const sugarSdkQuoteBigintReviver = (key: any, value: any) => {
27+
// Convert string values that were BigInts back to BigInt for the
28+
// Sugar SDK Quote object:
29+
// sugar-sdk/dist/primitives/externals/app/src/hooks/types.d.ts
30+
if (
31+
typeof value === 'string' &&
32+
(key === 'amount' ||
33+
key === 'amountOut' ||
34+
key === 'priceImpact' ||
35+
key === 'balance' ||
36+
key === 'price' ||
37+
key === 'pool_fee' ||
38+
key === 'balanceValue')
39+
) {
40+
try {
41+
return BigInt(value);
42+
} catch {
43+
return value;
44+
}
45+
}
46+
return value;
47+
};
48+
2649
export const vincentAbility = createVincentAbility({
2750
packageName: '@lit-protocol/vincent-ability-aerodrome-swap' as const,
2851
abilityDescription: 'Ability to swap tokens on Aerodrome' as const,
@@ -305,19 +328,45 @@ export const vincentAbility = createVincentAbility({
305328
}
306329

307330
// 3.2 Get the swap quote
308-
// TODO Wrap in runOnce
309331
console.log('[@lit-protocol/vincent-ability-aerodrome-swap execute] Getting swap quote');
310-
const quote = await getQuoteForSwap({
311-
config: sugarConfigBaseMainnet,
312-
fromToken: sugarTokenIn,
313-
toToken: sugarTokenOut,
314-
amountIn: requiredTokenInAmount.toBigInt(),
315-
});
316-
if (quote === null) {
332+
const quoteResponse = await Lit.Actions.runOnce(
333+
{ waitForResponse: true, name: 'Aerodrome swap quote' },
334+
async () => {
335+
try {
336+
const quote = await getQuoteForSwap({
337+
config: sugarConfigBaseMainnet,
338+
fromToken: sugarTokenIn,
339+
toToken: sugarTokenOut,
340+
amountIn: requiredTokenInAmount.toBigInt(),
341+
});
342+
if (quote === null) {
343+
return JSON.stringify({
344+
status: 'error',
345+
error: 'No Aerodrome swap quote available for the desired swap tokens and amounts',
346+
});
347+
}
348+
return JSON.stringify(
349+
{
350+
status: 'success',
351+
quote,
352+
},
353+
bigintReplacer,
354+
);
355+
} catch (error) {
356+
return JSON.stringify({
357+
status: 'error',
358+
error: error instanceof Error ? error.message : String(error),
359+
});
360+
}
361+
},
362+
);
363+
const parsedQuoteResponse = JSON.parse(quoteResponse, sugarSdkQuoteBigintReviver);
364+
if (parsedQuoteResponse.status === 'error') {
317365
return fail({
318-
reason: 'No Aerodrome swap quote available for the desired swap tokens and amounts',
366+
reason: parsedQuoteResponse.error,
319367
});
320368
}
369+
const { quote }: { quote: Quote } = parsedQuoteResponse;
321370

322371
// 3.3 Get the Sugar calldata for the swap quote
323372
console.log(

0 commit comments

Comments
 (0)