@@ -46,12 +46,12 @@ export class SwapTool extends BaseTool {
4646 private getSupportedChains ( ) : string [ ] {
4747 // Get networks from agent's wallet
4848 const agentNetworks = Object . keys ( this . agent . getNetworks ( ) ) ;
49-
49+
5050 // Intersect with supported chains from providers
5151 const providerChains = Array . from ( this . supportedChains ) ;
52-
52+
5353 // Return intersection of agent networks and provider supported chains
54- return agentNetworks . filter ( network =>
54+ return agentNetworks . filter ( network =>
5555 providerChains . includes ( network )
5656 ) ;
5757 }
@@ -128,112 +128,119 @@ export class SwapTool extends BaseTool {
128128 description : this . getDescription ( ) ,
129129 schema : this . getSchema ( ) ,
130130 func : async ( args : any ) => {
131- const {
132- fromToken,
133- toToken,
134- amount,
135- amountType,
136- chain = this . defaultChain ,
137- provider : preferredProvider ,
138- slippage = this . defaultSlippage ,
139- } = args ;
140-
141- // Get agent's wallet and address
142- const wallet = this . agent . getWallet ( ) ;
143- const userAddress = await wallet . getAddress ( chain ) ;
144-
145- // Validate chain is supported
146- const supportedChains = this . getSupportedChains ( ) ;
147- if ( ! supportedChains . includes ( chain ) ) {
148- throw new Error ( `Chain ${ chain } is not supported. Supported chains: ${ supportedChains . join ( ', ' ) } ` ) ;
149- }
150-
151- const swapParams : SwapParams = {
152- fromToken,
153- toToken,
154- amount,
155- type : amountType ,
156- slippage,
157- } ;
158-
159- let selectedProvider : ISwapProvider ;
160- let quote : SwapQuote ;
161-
162- if ( preferredProvider ) {
163- selectedProvider = this . registry . getProvider ( preferredProvider ) ;
164- // Validate provider supports the chain
165- if ( ! selectedProvider . getSupportedChains ( ) . includes ( chain ) ) {
166- throw new Error ( `Provider ${ preferredProvider } does not support chain ${ chain } ` ) ;
131+ try {
132+ const {
133+ fromToken,
134+ toToken,
135+ amount,
136+ amountType,
137+ chain = this . defaultChain ,
138+ provider : preferredProvider ,
139+ slippage = this . defaultSlippage ,
140+ } = args ;
141+
142+ // Get agent's wallet and address
143+ const wallet = this . agent . getWallet ( ) ;
144+ const userAddress = await wallet . getAddress ( chain ) ;
145+
146+ // Validate chain is supported
147+ const supportedChains = this . getSupportedChains ( ) ;
148+ if ( ! supportedChains . includes ( chain ) ) {
149+ throw new Error ( `Chain ${ chain } is not supported. Supported chains: ${ supportedChains . join ( ', ' ) } ` ) ;
167150 }
168- quote = await selectedProvider . getQuote ( swapParams ) ;
169- } else {
170- const bestQuote = await this . findBestQuote ( {
171- ...swapParams ,
172- chain,
173- } ) ;
174- selectedProvider = bestQuote . provider ;
175- quote = bestQuote . quote ;
176- }
177-
178- // Build swap transaction
179- const swapTx = await selectedProvider . buildSwapTransaction ( quote , userAddress ) ;
180151
181- // Check if approval is needed and handle it
182- const allowance = await selectedProvider . checkAllowance (
183- quote . fromToken ,
184- userAddress ,
185- swapTx . to
186- ) ;
187- const requiredAmount = BigInt ( quote . fromAmount ) ;
152+ const swapParams : SwapParams = {
153+ fromToken,
154+ toToken,
155+ amount,
156+ type : amountType ,
157+ slippage,
158+ } ;
159+
160+ let selectedProvider : ISwapProvider ;
161+ let quote : SwapQuote ;
162+
163+ if ( preferredProvider ) {
164+ selectedProvider = this . registry . getProvider ( preferredProvider ) ;
165+ // Validate provider supports the chain
166+ if ( ! selectedProvider . getSupportedChains ( ) . includes ( chain ) ) {
167+ throw new Error ( `Provider ${ preferredProvider } does not support chain ${ chain } ` ) ;
168+ }
169+ quote = await selectedProvider . getQuote ( swapParams ) ;
170+ } else {
171+ const bestQuote = await this . findBestQuote ( {
172+ ...swapParams ,
173+ chain,
174+ } ) ;
175+ selectedProvider = bestQuote . provider ;
176+ quote = bestQuote . quote ;
177+ }
188178
179+ // Build swap transaction
180+ const swapTx = await selectedProvider . buildSwapTransaction ( quote , userAddress ) ;
189181
190- if ( allowance < requiredAmount ) {
191- const approveTx = await selectedProvider . buildApproveTransaction (
182+ // Check if approval is needed and handle it
183+ const allowance = await selectedProvider . checkAllowance (
192184 quote . fromToken ,
193- swapTx . to ,
194- quote . fromAmount ,
195- userAddress
185+ userAddress ,
186+ swapTx . to
196187 ) ;
197-
198- console . log ( '🤖 Approving...' ) ;
199- // Sign and send approval transaction
200- const approveReceipt = await wallet . signAndSendTransaction ( chain , {
201- to : approveTx . to ,
202- data : approveTx . data ,
203- value : BigInt ( approveTx . value ) ,
204- gasLimit : BigInt ( approveTx . gasLimit ) ,
188+ const requiredAmount = BigInt ( quote . fromAmount ) ;
189+
190+
191+ if ( allowance < requiredAmount ) {
192+ const approveTx = await selectedProvider . buildApproveTransaction (
193+ quote . fromToken ,
194+ swapTx . to ,
195+ quote . fromAmount ,
196+ userAddress
197+ ) ;
198+
199+ console . log ( '🤖 Approving...' ) ;
200+ // Sign and send approval transaction
201+ const approveReceipt = await wallet . signAndSendTransaction ( chain , {
202+ to : approveTx . to ,
203+ data : approveTx . data ,
204+ value : BigInt ( approveTx . value ) ,
205+ gasLimit : BigInt ( approveTx . gasLimit ) ,
206+ } ) ;
207+
208+ console . log ( '🤖 ApproveReceipt:' , approveReceipt ) ;
209+
210+ // Wait for approval to be mined
211+ await approveReceipt . wait ( ) ;
212+ }
213+ console . log ( '🤖 Swapping...' ) ;
214+
215+ // Sign and send swap transaction
216+ const receipt = await wallet . signAndSendTransaction ( chain , {
217+ to : swapTx . to ,
218+ data : swapTx . data ,
219+ value : BigInt ( swapTx . value ) ,
220+ gasLimit : BigInt ( swapTx . gasLimit ) ,
205221 } ) ;
206222
207- console . log ( '🤖 ApproveReceipt:' , approveReceipt ) ;
208-
209- // Wait for approval to be mined
210- await approveReceipt . wait ( ) ;
223+ // Wait for transaction to be mined
224+ const finalReceipt = await receipt . wait ( ) ;
225+
226+ // Return result as JSON string
227+ return JSON . stringify ( {
228+ provider : selectedProvider . getName ( ) ,
229+ fromToken : quote . fromToken ,
230+ toToken : quote . toToken ,
231+ fromAmount : quote . fromAmount . toString ( ) ,
232+ toAmount : quote . toAmount . toString ( ) ,
233+ transactionHash : finalReceipt . hash ,
234+ priceImpact : quote . priceImpact ,
235+ type : quote . type ,
236+ chain,
237+ } ) ;
238+ } catch ( error ) {
239+ return JSON . stringify ( {
240+ status : 'error' ,
241+ message : error ,
242+ } ) ;
211243 }
212- console . log ( '🤖 Swapping...' ) ;
213-
214- // Sign and send swap transaction
215- const receipt = await wallet . signAndSendTransaction ( chain , {
216- to : swapTx . to ,
217- data : swapTx . data ,
218- value : BigInt ( swapTx . value ) ,
219- gasLimit : BigInt ( swapTx . gasLimit ) ,
220- } ) ;
221-
222- // Wait for transaction to be mined
223- const finalReceipt = await receipt . wait ( ) ;
224-
225- // Return result as JSON string
226- return JSON . stringify ( {
227- provider : selectedProvider . getName ( ) ,
228- fromToken : quote . fromToken ,
229- toToken : quote . toToken ,
230- fromAmount : quote . fromAmount . toString ( ) ,
231- toAmount : quote . toAmount . toString ( ) ,
232- transactionHash : finalReceipt . hash ,
233- priceImpact : quote . priceImpact ,
234- type : quote . type ,
235- chain,
236- } ) ;
237244 } ,
238245 } ) ;
239246 }
0 commit comments