|
| 1 | +import type { IAgentRuntime, Memory, State } from "@elizaos/core"; |
| 2 | +import { |
| 3 | + composeContext, |
| 4 | + generateObjectDeprecated, |
| 5 | + ModelClass, |
| 6 | +} from "@elizaos/core"; |
| 7 | +import { |
| 8 | + createConfig, |
| 9 | + executeRoute, |
| 10 | + ExtendedChain, |
| 11 | + getRoutes, |
| 12 | +} from "@lifi/sdk"; |
| 13 | + |
| 14 | +import { initWalletProvider, WalletProvider } from "../providers/wallet"; |
| 15 | +import { bridgeTemplate } from "../templates"; |
| 16 | +import type { BridgeParams, Transaction } from "../types"; |
| 17 | +import { parseEther } from "viem"; |
| 18 | + |
| 19 | +export { bridgeTemplate }; |
| 20 | + |
| 21 | +export class BridgeAction { |
| 22 | + private config; |
| 23 | + |
| 24 | + constructor(private walletProvider: WalletProvider) { |
| 25 | + this.config = createConfig({ |
| 26 | + integrator: "eliza", |
| 27 | + chains: Object.values(this.walletProvider.chains).map((config) => ({ |
| 28 | + id: config.id, |
| 29 | + name: config.name, |
| 30 | + key: config.name.toLowerCase(), |
| 31 | + chainType: "EVM", |
| 32 | + nativeToken: { |
| 33 | + ...config.nativeCurrency, |
| 34 | + chainId: config.id, |
| 35 | + address: "0x0000000000000000000000000000000000000000", |
| 36 | + coinKey: config.nativeCurrency.symbol, |
| 37 | + }, |
| 38 | + metamask: { |
| 39 | + chainId: `0x${config.id.toString(16)}`, |
| 40 | + chainName: config.name, |
| 41 | + nativeCurrency: config.nativeCurrency, |
| 42 | + rpcUrls: [config.rpcUrls.default.http[0]], |
| 43 | + blockExplorerUrls: [config.blockExplorers.default.url], |
| 44 | + }, |
| 45 | + diamondAddress: "0x0000000000000000000000000000000000000000", |
| 46 | + coin: config.nativeCurrency.symbol, |
| 47 | + mainnet: true, |
| 48 | + })) as ExtendedChain[], |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + async bridge(params: BridgeParams): Promise<Transaction> { |
| 53 | + const walletClient = this.walletProvider.getWalletClient( |
| 54 | + params.fromChain |
| 55 | + ); |
| 56 | + const [fromAddress] = await walletClient.getAddresses(); |
| 57 | + |
| 58 | + const routes = await getRoutes({ |
| 59 | + fromChainId: this.walletProvider.getChainConfigs(params.fromChain) |
| 60 | + .id, |
| 61 | + toChainId: this.walletProvider.getChainConfigs(params.toChain).id, |
| 62 | + fromTokenAddress: params.fromToken, |
| 63 | + toTokenAddress: params.toToken, |
| 64 | + fromAmount: parseEther(params.amount).toString(), |
| 65 | + fromAddress: fromAddress, |
| 66 | + toAddress: params.toAddress || fromAddress, |
| 67 | + }); |
| 68 | + |
| 69 | + if (!routes.routes.length) throw new Error("No routes found"); |
| 70 | + |
| 71 | + const execution = await executeRoute(routes.routes[0], this.config); |
| 72 | + const process = execution.steps[0]?.execution?.process[0]; |
| 73 | + |
| 74 | + if (!process?.status || process.status === "FAILED") { |
| 75 | + throw new Error("Transaction failed"); |
| 76 | + } |
| 77 | + |
| 78 | + return { |
| 79 | + hash: process.txHash as `0x${string}`, |
| 80 | + from: fromAddress, |
| 81 | + to: routes.routes[0].steps[0].estimate |
| 82 | + .approvalAddress as `0x${string}`, |
| 83 | + value: BigInt(params.amount), |
| 84 | + chainId: this.walletProvider.getChainConfigs(params.fromChain).id, |
| 85 | + }; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export const bridgeAction = { |
| 90 | + name: "bridge", |
| 91 | + description: "Bridge tokens between different chains", |
| 92 | + handler: async ( |
| 93 | + runtime: IAgentRuntime, |
| 94 | + _message: Memory, |
| 95 | + state: State, |
| 96 | + _options: any, |
| 97 | + callback?: any |
| 98 | + ) => { |
| 99 | + console.log("Bridge action handler called"); |
| 100 | + const walletProvider = initWalletProvider(runtime); |
| 101 | + const action = new BridgeAction(walletProvider); |
| 102 | + |
| 103 | + // Compose bridge context |
| 104 | + const bridgeContext = composeContext({ |
| 105 | + state, |
| 106 | + template: bridgeTemplate, |
| 107 | + }); |
| 108 | + const content = await generateObjectDeprecated({ |
| 109 | + runtime, |
| 110 | + context: bridgeContext, |
| 111 | + modelClass: ModelClass.LARGE, |
| 112 | + }); |
| 113 | + |
| 114 | + const bridgeOptions: BridgeParams = { |
| 115 | + fromChain: content.fromChain, |
| 116 | + toChain: content.toChain, |
| 117 | + fromToken: content.token, |
| 118 | + toToken: content.token, |
| 119 | + toAddress: content.toAddress, |
| 120 | + amount: content.amount, |
| 121 | + }; |
| 122 | + |
| 123 | + try { |
| 124 | + const bridgeResp = await action.bridge(bridgeOptions); |
| 125 | + if (callback) { |
| 126 | + callback({ |
| 127 | + text: `Successfully bridge ${bridgeOptions.amount} ${bridgeOptions.fromToken} tokens from ${bridgeOptions.fromChain} to ${bridgeOptions.toChain}\nTransaction Hash: ${bridgeResp.hash}`, |
| 128 | + content: { |
| 129 | + success: true, |
| 130 | + hash: bridgeResp.hash, |
| 131 | + recipient: bridgeResp.to, |
| 132 | + chain: bridgeOptions.fromChain, |
| 133 | + }, |
| 134 | + }); |
| 135 | + } |
| 136 | + return true; |
| 137 | + } catch (error) { |
| 138 | + console.error("Error in bridge handler:", error.message); |
| 139 | + if (callback) { |
| 140 | + callback({ text: `Error: ${error.message}` }); |
| 141 | + } |
| 142 | + return false; |
| 143 | + } |
| 144 | + }, |
| 145 | + template: bridgeTemplate, |
| 146 | + validate: async (runtime: IAgentRuntime) => { |
| 147 | + const privateKey = runtime.getSetting("EVM_PRIVATE_KEY"); |
| 148 | + return typeof privateKey === "string" && privateKey.startsWith("0x"); |
| 149 | + }, |
| 150 | + examples: [ |
| 151 | + [ |
| 152 | + { |
| 153 | + user: "user", |
| 154 | + content: { |
| 155 | + text: "Bridge 1 ETH from Ethereum to Base", |
| 156 | + action: "CROSS_CHAIN_TRANSFER", |
| 157 | + }, |
| 158 | + }, |
| 159 | + ], |
| 160 | + ], |
| 161 | + similes: ["CROSS_CHAIN_TRANSFER", "CHAIN_BRIDGE", "MOVE_CROSS_CHAIN"], |
| 162 | +}; // TODO: add more examples / similies |
0 commit comments