Skip to content

Commit c53b0f3

Browse files
committed
Merge branch 'main'
2 parents 302bc01 + e88d5dc commit c53b0f3

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
ability-aerodrome-swap: minor
3+
---
4+
5+
Wrap Sugar SDK quote generation in a Lit Actions runOnce to avoid the possible scenario where the Lit nodes get back different quotes causing a Lit Action signing failure

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": "Qmd3CAeEco76nuFfgfJSccXokSFjzmejvZT59UrbV2tRku"
2+
"ipfsCid": "QmQpvixAaeDCw1Fkgaqf5EidTMm1PzgKwJjpjFXPmJVaxV"
33
}

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

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,33 @@ 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+
return BigInt(value);
41+
}
42+
return value;
43+
};
44+
2645
export const vincentAbility = createVincentAbility({
2746
packageName: '@lit-protocol/vincent-ability-aerodrome-swap' as const,
2847
abilityDescription: 'Ability to swap tokens on Aerodrome' as const,
@@ -305,19 +324,45 @@ export const vincentAbility = createVincentAbility({
305324
}
306325

307326
// 3.2 Get the swap quote
308-
// TODO Wrap in runOnce
309327
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) {
328+
const quoteResponse = await Lit.Actions.runOnce(
329+
{ waitForResponse: true, name: 'Aerodrome swap quote' },
330+
async () => {
331+
try {
332+
const quote = await getQuoteForSwap({
333+
config: sugarConfigBaseMainnet,
334+
fromToken: sugarTokenIn,
335+
toToken: sugarTokenOut,
336+
amountIn: requiredTokenInAmount.toBigInt(),
337+
});
338+
if (quote === null) {
339+
return JSON.stringify({
340+
status: 'error',
341+
error: 'No Aerodrome swap quote available for the desired swap tokens and amounts',
342+
});
343+
}
344+
return JSON.stringify(
345+
{
346+
status: 'success',
347+
quote,
348+
},
349+
bigintReplacer,
350+
);
351+
} catch (error) {
352+
return JSON.stringify({
353+
status: 'error',
354+
error: error instanceof Error ? error.message : String(error),
355+
});
356+
}
357+
},
358+
);
359+
const parsedQuoteResponse = JSON.parse(quoteResponse, sugarSdkQuoteBigintReviver);
360+
if (parsedQuoteResponse.status === 'error') {
317361
return fail({
318-
reason: 'No Aerodrome swap quote available for the desired swap tokens and amounts',
362+
reason: parsedQuoteResponse.error,
319363
});
320364
}
365+
const { quote }: { quote: Quote } = parsedQuoteResponse;
321366

322367
// 3.3 Get the Sugar calldata for the swap quote
323368
console.log(

0 commit comments

Comments
 (0)