1- import type {
2- HandlerCallback ,
3- IAgentRuntime ,
4- Memory ,
5- State ,
6- } from "@elizaos/core-plugin-v2" ;
7- import { composePrompt , ModelType } from "@elizaos/core-plugin-v2" ;
8- import {
9- type ExtendedChain ,
10- createConfig ,
11- executeRoute ,
12- getRoutes ,
13- } from "@lifi/sdk" ;
14-
15- import { parseEther } from "viem" ;
16- import { type WalletProvider , initWalletProvider } from "../providers/wallet" ;
17- import { bridgeTemplate } from "../templates" ;
18- import type { BridgeParams , Transaction } from "../types" ;
1+ import type { HandlerCallback , IAgentRuntime , Memory , State } from '@elizaos/core-plugin-v2' ;
2+ import { composePrompt , ModelType } from '@elizaos/core-plugin-v2' ;
3+ import { type ExtendedChain , createConfig , executeRoute , getRoutes } from '@lifi/sdk' ;
4+
5+ import { parseEther } from 'viem' ;
6+ import { type WalletProvider , initWalletProvider } from '../providers/wallet' ;
7+ import { bridgeTemplate } from '../templates' ;
8+ import type { BridgeParams , Transaction } from '../types' ;
199
2010export { bridgeTemplate } ;
2111
@@ -24,26 +14,26 @@ export class BridgeAction {
2414
2515 constructor ( private walletProvider : WalletProvider ) {
2616 this . config = createConfig ( {
27- integrator : " eliza" ,
17+ integrator : ' eliza' ,
2818 chains : Object . values ( this . walletProvider . chains ) . map ( ( config ) => ( {
2919 id : config . id ,
3020 name : config . name ,
3121 key : config . name . toLowerCase ( ) ,
32- chainType : " EVM" ,
22+ chainType : ' EVM' ,
3323 nativeToken : {
3424 ...config . nativeCurrency ,
3525 chainId : config . id ,
36- address : " 0x0000000000000000000000000000000000000000" ,
26+ address : ' 0x0000000000000000000000000000000000000000' ,
3727 coinKey : config . nativeCurrency . symbol ,
3828 } ,
3929 metamask : {
4030 chainId : `0x${ config . id . toString ( 16 ) } ` ,
4131 chainName : config . name ,
4232 nativeCurrency : config . nativeCurrency ,
4333 rpcUrls : [ config . rpcUrls . default . http [ 0 ] ] ,
44- blockExplorerUrls : [ config . blockExplorers ?. default . url ] ,
34+ blockExplorerUrls : [ config ? .blockExplorers ?. default ? .url ] ,
4535 } ,
46- diamondAddress : " 0x0000000000000000000000000000000000000000" ,
36+ diamondAddress : ' 0x0000000000000000000000000000000000000000' ,
4737 coin : config . nativeCurrency . symbol ,
4838 mainnet : true ,
4939 } ) ) as ExtendedChain [ ] ,
@@ -64,13 +54,13 @@ export class BridgeAction {
6454 toAddress : params . toAddress || fromAddress ,
6555 } ) ;
6656
67- if ( ! routes . routes . length ) throw new Error ( " No routes found" ) ;
57+ if ( ! routes . routes . length ) throw new Error ( ' No routes found' ) ;
6858
6959 const execution = await executeRoute ( routes . routes [ 0 ] , this . config as any ) ;
7060 const process = execution . steps [ 0 ] ?. execution ?. process [ 0 ] ;
7161
72- if ( ! process ?. status || process . status === " FAILED" ) {
73- throw new Error ( " Transaction failed" ) ;
62+ if ( ! process ?. status || process . status === ' FAILED' ) {
63+ throw new Error ( ' Transaction failed' ) ;
7464 }
7565
7666 return {
@@ -89,7 +79,7 @@ const buildBridgeDetails = async (
8979 wp : WalletProvider
9080) : Promise < BridgeParams > => {
9181 const chains = wp . getSupportedChains ( ) ;
92- state . supportedChains = chains . map ( ( item ) => `"${ item } "` ) . join ( "|" ) ;
82+ state . supportedChains = chains . map ( ( item ) => `"${ item } "` ) . join ( '|' ) ;
9383
9484 // Add balances to state for better context in template
9585 const balances = await wp . getWalletBalances ( ) ;
@@ -98,7 +88,7 @@ const buildBridgeDetails = async (
9888 const chainConfig = wp . getChainConfigs ( chain as any ) ;
9989 return `${ chain } : ${ balance } ${ chainConfig . nativeCurrency . symbol } ` ;
10090 } )
101- . join ( ", " ) ;
91+ . join ( ', ' ) ;
10292
10393 // Compose bridge context
10494 const bridgeContext = composePrompt ( {
@@ -116,13 +106,13 @@ const buildBridgeDetails = async (
116106
117107 if ( ! wp . chains [ fromChain ] ) {
118108 throw new Error (
119- `Source chain ${ fromChain } not configured. Available chains: ${ chains . join ( ", " ) } `
109+ `Source chain ${ fromChain } not configured. Available chains: ${ chains . join ( ', ' ) } `
120110 ) ;
121111 }
122112
123113 if ( ! wp . chains [ toChain ] ) {
124114 throw new Error (
125- `Destination chain ${ toChain } not configured. Available chains: ${ chains . join ( ", " ) } `
115+ `Destination chain ${ toChain } not configured. Available chains: ${ chains . join ( ', ' ) } `
126116 ) ;
127117 }
128118
@@ -139,25 +129,25 @@ const buildBridgeDetails = async (
139129} ;
140130
141131export const bridgeAction = {
142- name : " EVM_BRIDGE_TOKENS" ,
143- description : " Bridge tokens between different chains" ,
132+ name : ' EVM_BRIDGE_TOKENS' ,
133+ description : ' Bridge tokens between different chains' ,
144134 handler : async (
145135 runtime : IAgentRuntime ,
146136 _message : Memory ,
147- state : State ,
148- _options : Record < string , unknown > ,
137+ state ? : State ,
138+ _options ? : Record < string , unknown > ,
149139 callback ?: HandlerCallback
150140 ) => {
151141 const walletProvider = await initWalletProvider ( runtime ) ;
152142 const action = new BridgeAction ( walletProvider ) ;
153143
144+ if ( ! state ) {
145+ state = await runtime . composeState ( _message ) ;
146+ }
147+
154148 try {
155149 // Get bridge parameters
156- const bridgeOptions = await buildBridgeDetails (
157- state ,
158- runtime ,
159- walletProvider
160- ) ;
150+ const bridgeOptions = await buildBridgeDetails ( state , runtime , walletProvider ) ;
161151
162152 const bridgeResp = await action . bridge ( bridgeOptions ) ;
163153 if ( callback ) {
@@ -173,33 +163,36 @@ export const bridgeAction = {
173163 } ) ;
174164 }
175165 return true ;
176- } catch ( error : unknown ) {
177- const errMsg = error instanceof Error ? error . message : String ( error ) ;
178- console . error ( "Error in bridge handler:" , errMsg ) ;
166+ } catch ( error ) {
167+ console . error (
168+ 'Error in bridge handler:' ,
169+ error instanceof Error ? error . message : 'Unknown error'
170+ ) ;
179171 if ( callback ) {
180172 callback ( {
181- text : `Error: ${ errMsg } ` ,
182- content : { error : errMsg } ,
173+ text : `Error: ${ error instanceof Error ? error . message : 'Unknown error' } ` ,
174+ content : { error : error instanceof Error ? error . message : 'Unknown error' } ,
183175 } ) ;
184176 }
185177 return false ;
186178 }
187179 } ,
188180 template : bridgeTemplate ,
189181 validate : async ( runtime : IAgentRuntime ) => {
190- const privateKey = runtime . getSetting ( " EVM_PRIVATE_KEY" ) ;
191- return typeof privateKey === " string" && privateKey . startsWith ( "0x" ) ;
182+ const privateKey = runtime . getSetting ( ' EVM_PRIVATE_KEY' ) ;
183+ return typeof privateKey === ' string' && privateKey . startsWith ( '0x' ) ;
192184 } ,
193185 examples : [
194186 [
195187 {
196- user : "user" ,
188+ name : 'user' ,
189+ user : 'user' ,
197190 content : {
198- text : " Bridge 1 ETH from Ethereum to Base" ,
199- action : " CROSS_CHAIN_TRANSFER" ,
191+ text : ' Bridge 1 ETH from Ethereum to Base' ,
192+ action : ' CROSS_CHAIN_TRANSFER' ,
200193 } ,
201194 } ,
202195 ] ,
203196 ] ,
204- similes : [ " CROSS_CHAIN_TRANSFER" , " CHAIN_BRIDGE" , " MOVE_CROSS_CHAIN" ] ,
197+ similes : [ ' CROSS_CHAIN_TRANSFER' , ' CHAIN_BRIDGE' , ' MOVE_CROSS_CHAIN' ] ,
205198} ;
0 commit comments