|
1 |
| -import type { IAgentRuntime, Memory, State } from "@elizaos/core"; |
| 1 | +import { |
| 2 | + IAgentRuntime, |
| 3 | + Memory, |
| 4 | + State, |
| 5 | + composeContext, |
| 6 | + generateObjectDeprecated, |
| 7 | + ModelClass, |
| 8 | +} from "@elizaos/core"; |
2 | 9 | import {
|
3 | 10 | createConfig,
|
4 | 11 | executeRoute,
|
5 | 12 | ExtendedChain,
|
6 | 13 | getRoutes,
|
7 | 14 | } from "@lifi/sdk";
|
| 15 | +import { zeroAddress } from "viem"; |
8 | 16 | import { WalletProvider } from "../providers/wallet";
|
9 | 17 | import { bridgeTemplate } from "../templates";
|
10 | 18 | import type { BridgeParams, Transaction } from "../types";
|
@@ -52,8 +60,9 @@ export class BridgeAction {
|
52 | 60 | fromChainId: this.walletProvider.getChainConfigs(params.fromChain)
|
53 | 61 | .id,
|
54 | 62 | toChainId: this.walletProvider.getChainConfigs(params.toChain).id,
|
55 |
| - fromTokenAddress: params.fromToken, |
56 |
| - toTokenAddress: params.toToken, |
| 63 | + // if user wants to bridge native token, setting fromToken to zero address |
| 64 | + fromTokenAddress: params.fromToken ? params.fromToken : zeroAddress, |
| 65 | + toTokenAddress: params.toToken ? params.toToken : zeroAddress, |
57 | 66 | fromAmount: params.amount,
|
58 | 67 | fromAddress: fromAddress,
|
59 | 68 | toAddress: params.toAddress || fromAddress,
|
@@ -88,12 +97,29 @@ export const bridgeAction = {
|
88 | 97 | state: State,
|
89 | 98 | options: any
|
90 | 99 | ) => {
|
| 100 | + // Option is {} object |
91 | 101 | const privateKey = runtime.getSetting(
|
92 | 102 | "EVM_PRIVATE_KEY"
|
93 | 103 | ) as `0x${string}`;
|
94 | 104 | const walletProvider = new WalletProvider(privateKey);
|
| 105 | + // Get all chains from walletProvider |
| 106 | + const chains = Object.keys(walletProvider.chains); |
| 107 | + const context = composeContext({ |
| 108 | + state, |
| 109 | + template: bridgeTemplate, |
| 110 | + }); |
| 111 | + const contextWithChains = context.replace( |
| 112 | + "SUPPORTED_CHAINS", |
| 113 | + chains.map((item) => `"${item}"`).join("|") |
| 114 | + ); |
| 115 | + // Generate bridge details object |
| 116 | + const bridgeDetails = (await generateObjectDeprecated({ |
| 117 | + runtime, |
| 118 | + context: contextWithChains, |
| 119 | + modelClass: ModelClass.SMALL, |
| 120 | + })) as BridgeParams; |
95 | 121 | const action = new BridgeAction(walletProvider);
|
96 |
| - return action.bridge(options); |
| 122 | + return action.bridge(bridgeDetails); |
97 | 123 | },
|
98 | 124 | template: bridgeTemplate,
|
99 | 125 | validate: async (runtime: IAgentRuntime) => {
|
|
0 commit comments