Skip to content

Commit 8157f4c

Browse files
committed
fix: bridge and swap actions context params generation
1 parent a930c84 commit 8157f4c

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

packages/plugin-evm/src/actions/bridge.ts

+30-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
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";
29
import {
310
createConfig,
411
executeRoute,
512
ExtendedChain,
613
getRoutes,
714
} from "@lifi/sdk";
15+
import { zeroAddress } from "viem";
816
import { WalletProvider } from "../providers/wallet";
917
import { bridgeTemplate } from "../templates";
1018
import type { BridgeParams, Transaction } from "../types";
@@ -52,8 +60,9 @@ export class BridgeAction {
5260
fromChainId: this.walletProvider.getChainConfigs(params.fromChain)
5361
.id,
5462
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,
5766
fromAmount: params.amount,
5867
fromAddress: fromAddress,
5968
toAddress: params.toAddress || fromAddress,
@@ -88,12 +97,29 @@ export const bridgeAction = {
8897
state: State,
8998
options: any
9099
) => {
100+
// Option is {} object
91101
const privateKey = runtime.getSetting(
92102
"EVM_PRIVATE_KEY"
93103
) as `0x${string}`;
94104
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;
95121
const action = new BridgeAction(walletProvider);
96-
return action.bridge(options);
122+
return action.bridge(bridgeDetails);
97123
},
98124
template: bridgeTemplate,
99125
validate: async (runtime: IAgentRuntime) => {

packages/plugin-evm/src/actions/swap.ts

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
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";
29
import {
310
createConfig,
411
executeRoute,
512
ExtendedChain,
613
getRoutes,
714
} from "@lifi/sdk";
15+
import { zeroAddress } from "viem";
816
import { WalletProvider } from "../providers/wallet";
917
import { swapTemplate } from "../templates";
1018
import type { SwapParams, Transaction } from "../types";
@@ -104,8 +112,26 @@ export const swapAction = {
104112
"EVM_PRIVATE_KEY"
105113
) as `0x${string}`;
106114
const walletProvider = new WalletProvider(privateKey);
115+
116+
const chains = Object.keys(walletProvider.chains);
117+
const context = composeContext({
118+
state,
119+
template: swapTemplate,
120+
});
121+
const contextWithChains = context.replace(
122+
"SUPPORTED_CHAINS",
123+
chains.map((item) => `"${item}"`).join("|")
124+
);
125+
126+
// Generate swap details object
127+
const swapDetails = (await generateObjectDeprecated({
128+
runtime,
129+
context: contextWithChains,
130+
modelClass: ModelClass.SMALL,
131+
})) as SwapParams;
132+
107133
const action = new SwapAction(walletProvider);
108-
return await action.swap(options);
134+
return await action.swap(swapDetails);
109135
} catch (error) {
110136
console.error("Error in swap handler:", error.message);
111137
if (callback) {

0 commit comments

Comments
 (0)