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 all commits
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
24 changes: 18 additions & 6 deletions packages/plugin-evm/src/actions/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { IAgentRuntime, Memory, State } from "@elizaos/core";
import {
IAgentRuntime,
Memory,
State,
composeContext,
generateObjectDeprecated,
ModelClass,
Expand All @@ -10,7 +12,7 @@ import {
ExtendedChain,
getRoutes,
} from "@lifi/sdk";

import { zeroAddress } from "viem";
import { initWalletProvider, WalletProvider } from "../providers/wallet";
import { bridgeTemplate } from "../templates";
import type { BridgeParams, Transaction } from "../types";
Expand Down Expand Up @@ -59,8 +61,9 @@ export class BridgeAction {
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: parseEther(params.amount).toString(),
fromAddress: fromAddress,
toAddress: params.toAddress || fromAddress,
Expand Down Expand Up @@ -100,15 +103,24 @@ export const bridgeAction = {
const walletProvider = initWalletProvider(runtime);
const action = new BridgeAction(walletProvider);

// Get all chains from walletProvider
const chains = Object.keys(walletProvider.chains);

// Compose bridge context
const bridgeContext = composeContext({
state,
template: bridgeTemplate,
});
const contextWithChains = bridgeContext.replace(
"SUPPORTED_CHAINS",
chains.map((item) => `"${item}"`).join("|")
);

// Generate bridge details object
const content = await generateObjectDeprecated({
runtime,
context: bridgeContext,
modelClass: ModelClass.LARGE,
context: contextWithChains,
modelClass: ModelClass.SMALL,
});

const bridgeOptions: BridgeParams = {
Expand Down
52 changes: 30 additions & 22 deletions packages/plugin-evm/src/actions/swap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { IAgentRuntime, Memory, State } from "@elizaos/core";
import {
IAgentRuntime,
Memory,
State,
composeContext,
generateObjectDeprecated,
ModelClass,
Expand All @@ -10,7 +12,6 @@ import {
ExtendedChain,
getRoutes,
} from "@lifi/sdk";

import { initWalletProvider, WalletProvider } from "../providers/wallet";
import { swapTemplate } from "../templates";
import type { SwapParams, Transaction } from "../types";
Expand Down Expand Up @@ -107,29 +108,36 @@ export const swapAction = {
callback?: any
) => {
console.log("Swap action handler called");
const walletProvider = initWalletProvider(runtime);
const action = new SwapAction(walletProvider);

// Compose swap context
const swapContext = composeContext({
state,
template: swapTemplate,
});
const content = await generateObjectDeprecated({
runtime,
context: swapContext,
modelClass: ModelClass.LARGE,
});
try {
const walletProvider = initWalletProvider(runtime);

const swapOptions: SwapParams = {
chain: content.chain,
fromToken: content.inputToken,
toToken: content.outputToken,
amount: content.amount,
slippage: content.slippage,
};
const chains = Object.keys(walletProvider.chains);
const context = composeContext({
state,
template: swapTemplate,
});
const contextWithChains = context.replace(
"SUPPORTED_CHAINS",
chains.map((item) => `"${item}"`).join("|")
);

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

const swapOptions: SwapParams = {
chain: content.chain,
fromToken: content.fromToken,
toToken: content.toToken,
amount: content.amount,
slippage: content.slippage,
};

const action = new SwapAction(walletProvider);
const swapResp = await action.swap(swapOptions);
if (callback) {
callback({
Expand Down
Loading