Skip to content

Commit 0bcf50d

Browse files
authored
Merge pull request #1456 from pythonberg1997/fix
fix: swap and bridge actions of plugin-evm
2 parents e0c4c14 + 314626c commit 0bcf50d

File tree

3 files changed

+106
-22
lines changed

3 files changed

+106
-22
lines changed

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

+56-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import type { IAgentRuntime, Memory, State } from "@elizaos/core";
2+
import {
3+
composeContext,
4+
generateObjectDeprecated,
5+
ModelClass,
6+
} from "@elizaos/core";
27
import {
38
createConfig,
49
executeRoute,
510
ExtendedChain,
611
getRoutes,
712
} from "@lifi/sdk";
8-
import { WalletProvider } from "../providers/wallet";
13+
14+
import { initWalletProvider, WalletProvider } from "../providers/wallet";
915
import { bridgeTemplate } from "../templates";
1016
import type { BridgeParams, Transaction } from "../types";
17+
import { parseEther } from "viem";
1118

1219
export { bridgeTemplate };
1320

@@ -54,7 +61,7 @@ export class BridgeAction {
5461
toChainId: this.walletProvider.getChainConfigs(params.toChain).id,
5562
fromTokenAddress: params.fromToken,
5663
toTokenAddress: params.toToken,
57-
fromAmount: params.amount,
64+
fromAmount: parseEther(params.amount).toString(),
5865
fromAddress: fromAddress,
5966
toAddress: params.toAddress || fromAddress,
6067
});
@@ -84,16 +91,56 @@ export const bridgeAction = {
8491
description: "Bridge tokens between different chains",
8592
handler: async (
8693
runtime: IAgentRuntime,
87-
message: Memory,
94+
_message: Memory,
8895
state: State,
89-
options: any
96+
_options: any,
97+
callback?: any
9098
) => {
91-
const privateKey = runtime.getSetting(
92-
"EVM_PRIVATE_KEY"
93-
) as `0x${string}`;
94-
const walletProvider = new WalletProvider(privateKey);
99+
console.log("Bridge action handler called");
100+
const walletProvider = initWalletProvider(runtime);
95101
const action = new BridgeAction(walletProvider);
96-
return action.bridge(options);
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+
}
97144
},
98145
template: bridgeTemplate,
99146
validate: async (runtime: IAgentRuntime) => {

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

+48-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import type { IAgentRuntime, Memory, State } from "@elizaos/core";
2+
import {
3+
composeContext,
4+
generateObjectDeprecated,
5+
ModelClass,
6+
} from "@elizaos/core";
27
import {
38
createConfig,
49
executeRoute,
510
ExtendedChain,
611
getRoutes,
712
} from "@lifi/sdk";
8-
import { WalletProvider } from "../providers/wallet";
13+
14+
import { initWalletProvider, WalletProvider } from "../providers/wallet";
915
import { swapTemplate } from "../templates";
1016
import type { SwapParams, Transaction } from "../types";
17+
import { parseEther } from "viem";
1118

1219
export { swapTemplate };
1320

@@ -60,7 +67,7 @@ export class SwapAction {
6067
toChainId: this.walletProvider.getChainConfigs(params.chain).id,
6168
fromTokenAddress: params.fromToken,
6269
toTokenAddress: params.toToken,
63-
fromAmount: params.amount,
70+
fromAmount: parseEther(params.amount).toString(),
6471
fromAddress: fromAddress,
6572
options: {
6673
slippage: params.slippage || 0.5,
@@ -82,7 +89,7 @@ export class SwapAction {
8289
from: fromAddress,
8390
to: routes.routes[0].steps[0].estimate
8491
.approvalAddress as `0x${string}`,
85-
value: BigInt(params.amount),
92+
value: 0n,
8693
data: process.data as `0x${string}`,
8794
chainId: this.walletProvider.getChainConfigs(params.chain).id,
8895
};
@@ -94,18 +101,48 @@ export const swapAction = {
94101
description: "Swap tokens on the same chain",
95102
handler: async (
96103
runtime: IAgentRuntime,
97-
message: Memory,
104+
_message: Memory,
98105
state: State,
99-
options: any,
106+
_options: any,
100107
callback?: any
101108
) => {
109+
console.log("Swap action handler called");
110+
const walletProvider = initWalletProvider(runtime);
111+
const action = new SwapAction(walletProvider);
112+
113+
// Compose swap context
114+
const swapContext = composeContext({
115+
state,
116+
template: swapTemplate,
117+
});
118+
const content = await generateObjectDeprecated({
119+
runtime,
120+
context: swapContext,
121+
modelClass: ModelClass.LARGE,
122+
});
123+
124+
const swapOptions: SwapParams = {
125+
chain: content.chain,
126+
fromToken: content.inputToken,
127+
toToken: content.outputToken,
128+
amount: content.amount,
129+
slippage: content.slippage,
130+
};
131+
102132
try {
103-
const privateKey = runtime.getSetting(
104-
"EVM_PRIVATE_KEY"
105-
) as `0x${string}`;
106-
const walletProvider = new WalletProvider(privateKey);
107-
const action = new SwapAction(walletProvider);
108-
return await action.swap(options);
133+
const swapResp = await action.swap(swapOptions);
134+
if (callback) {
135+
callback({
136+
text: `Successfully swap ${swapOptions.amount} ${swapOptions.fromToken} tokens to ${swapOptions.toToken}\nTransaction Hash: ${swapResp.hash}`,
137+
content: {
138+
success: true,
139+
hash: swapResp.hash,
140+
recipient: swapResp.to,
141+
chain: content.chain,
142+
},
143+
});
144+
}
145+
return true;
109146
} catch (error) {
110147
console.error("Error in swap handler:", error.message);
111148
if (callback) {

packages/plugin-evm/src/templates/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Extract the following information about the requested token bridge:
3232
- Token symbol or address to bridge
3333
- Source chain
3434
- Destination chain
35-
- Amount to bridge
35+
- Amount to bridge: Must be a string representing the amount in ether (only number without coin symbol, e.g., "0.1")
3636
- Destination address (if specified)
3737
3838
Respond with a JSON markdown block containing only the extracted values:
@@ -57,7 +57,7 @@ export const swapTemplate = `Given the recent messages and wallet information be
5757
Extract the following information about the requested token swap:
5858
- Input token symbol or address (the token being sold)
5959
- Output token symbol or address (the token being bought)
60-
- Amount to swap
60+
- Amount to swap: Must be a string representing the amount in ether (only number without coin symbol, e.g., "0.1")
6161
- Chain to execute on
6262
6363
Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined:

0 commit comments

Comments
 (0)