Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bridge and swap actions context params generation #1524

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions packages/plugin-evm/src/actions/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import type { IAgentRuntime, Memory, State } from "@elizaos/core";
import {
IAgentRuntime,
Memory,
State,
composeContext,
generateObjectDeprecated,
ModelClass,
} from "@elizaos/core";
import {
createConfig,
executeRoute,
ExtendedChain,
getRoutes,
} from "@lifi/sdk";
import { zeroAddress } from "viem";
import { WalletProvider } from "../providers/wallet";
import { bridgeTemplate } from "../templates";
import type { BridgeParams, Transaction } from "../types";
Expand Down Expand Up @@ -52,8 +60,9 @@
fromChainId: this.walletProvider.getChainConfigs(params.fromChain)
.id,
toChainId: this.walletProvider.getChainConfigs(params.toChain).id,
fromTokenAddress: params.fromToken,
toTokenAddress: params.toToken,
// if user wants to bridge native token, setting fromToken to zero address
fromTokenAddress: params.fromToken ? params.fromToken : zeroAddress,
toTokenAddress: params.toToken ? params.toToken : zeroAddress,
fromAmount: params.amount,
fromAddress: fromAddress,
toAddress: params.toAddress || fromAddress,
Expand Down Expand Up @@ -86,14 +95,31 @@
runtime: IAgentRuntime,
message: Memory,
state: State,
options: any

Check failure on line 98 in packages/plugin-evm/src/actions/bridge.ts

View workflow job for this annotation

GitHub Actions / check

'options' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 98 in packages/plugin-evm/src/actions/bridge.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
) => {
// Option is {} object
const privateKey = runtime.getSetting(
"EVM_PRIVATE_KEY"
) as `0x${string}`;
const walletProvider = new WalletProvider(privateKey);
// Get all chains from walletProvider
const chains = Object.keys(walletProvider.chains);
const context = composeContext({
state,
template: bridgeTemplate,
});
const contextWithChains = context.replace(
"SUPPORTED_CHAINS",
chains.map((item) => `"${item}"`).join("|")
);
// Generate bridge details object
const bridgeDetails = (await generateObjectDeprecated({
runtime,
context: contextWithChains,
modelClass: ModelClass.SMALL,
})) as BridgeParams;
const action = new BridgeAction(walletProvider);
return action.bridge(options);
return action.bridge(bridgeDetails);
},
template: bridgeTemplate,
validate: async (runtime: IAgentRuntime) => {
Expand Down
30 changes: 28 additions & 2 deletions packages/plugin-evm/src/actions/swap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import type { IAgentRuntime, Memory, State } from "@elizaos/core";
import {
IAgentRuntime,
Memory,
State,
composeContext,
generateObjectDeprecated,
ModelClass,
} from "@elizaos/core";
import {
createConfig,
executeRoute,
ExtendedChain,
getRoutes,
} from "@lifi/sdk";
import { zeroAddress } from "viem";

Check failure on line 15 in packages/plugin-evm/src/actions/swap.ts

View workflow job for this annotation

GitHub Actions / check

'zeroAddress' is defined but never used. Allowed unused vars must match /^_/u
import { WalletProvider } from "../providers/wallet";
import { swapTemplate } from "../templates";
import type { SwapParams, Transaction } from "../types";
Expand Down Expand Up @@ -96,16 +104,34 @@
runtime: IAgentRuntime,
message: Memory,
state: State,
options: any,

Check warning on line 107 in packages/plugin-evm/src/actions/swap.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
callback?: any

Check warning on line 108 in packages/plugin-evm/src/actions/swap.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
) => {
try {
const privateKey = runtime.getSetting(
"EVM_PRIVATE_KEY"
) as `0x${string}`;
const walletProvider = new WalletProvider(privateKey);

const chains = Object.keys(walletProvider.chains);
const context = composeContext({
state,
template: swapTemplate,
});
const contextWithChains = context.replace(
"SUPPORTED_CHAINS",
chains.map((item) => `"${item}"`).join("|")
);

// Generate swap details object
const swapDetails = (await generateObjectDeprecated({
runtime,
context: contextWithChains,
modelClass: ModelClass.SMALL,
})) as SwapParams;

const action = new SwapAction(walletProvider);
return await action.swap(options);
return await action.swap(swapDetails);
} catch (error) {
console.error("Error in swap handler:", error.message);
if (callback) {
Expand Down
Loading