@@ -15,14 +15,33 @@ import {
1515} from './schemas' ;
1616import { AbilityAction , CheckNativeTokenBalanceResultSuccess } from './types' ;
1717import { 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' ;
1919import { findSupportedTokenOnBase } from './ability-helpers/find-supported-token-on-base' ;
2020import { sendAerodromeSwapTx , sendErc20ApprovalTx } from './ability-helpers' ;
2121
2222export 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+
2645export 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