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

Plugin-evm tests and multichain support #864

Closed
wants to merge 19 commits into from
Closed
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
3 changes: 2 additions & 1 deletion packages/plugin-evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch"
"dev": "tsup --format esm --dts --watch",
"test": "vitest run"
},
"peerDependencies": {
"whatwg-url": "7.1.0"
Expand Down
38 changes: 17 additions & 21 deletions packages/plugin-evm/src/actions/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type { IAgentRuntime, Memory, State } from "@ai16z/eliza";
import {
ChainId,
createConfig,
executeRoute,
ExtendedChain,
getRoutes,
} from "@lifi/sdk";
import { WalletProvider } from "../providers/wallet";
import { getChainConfigs } from "../providers/chainConfigs";
import { bridgeTemplate } from "../templates";
import type { BridgeParams, Transaction } from "../types";

Expand All @@ -19,25 +17,23 @@ export class BridgeAction {
constructor(private walletProvider: WalletProvider) {
this.config = createConfig({
integrator: "eliza",
chains: Object.values(
getChainConfigs(this.walletProvider.runtime)
).map((config) => ({
id: config.chainId,
chains: Object.values(this.walletProvider.chains).map((config) => ({
id: config.id,
name: config.name,
key: config.name.toLowerCase(),
chainType: "EVM",
nativeToken: {
...config.nativeCurrency,
chainId: config.chainId,
chainId: config.id,
address: "0x0000000000000000000000000000000000000000",
coinKey: config.nativeCurrency.symbol,
},
metamask: {
chainId: `0x${config.chainId.toString(16)}`,
chainId: `0x${config.id.toString(16)}`,
chainName: config.name,
nativeCurrency: config.nativeCurrency,
rpcUrls: [config.rpcUrl],
blockExplorerUrls: [config.blockExplorerUrl],
rpcUrls: [config.rpcUrls.default.http[0]],
blockExplorerUrls: [config.blockExplorers.default.url],
},
diamondAddress: "0x0000000000000000000000000000000000000000",
coin: config.nativeCurrency.symbol,
Expand All @@ -47,16 +43,15 @@ export class BridgeAction {
}

async bridge(params: BridgeParams): Promise<Transaction> {
const walletClient = this.walletProvider.getWalletClient();
const walletClient = this.walletProvider.getWalletClient(
params.fromChain
);
const [fromAddress] = await walletClient.getAddresses();

const routes = await getRoutes({
fromChainId: getChainConfigs(this.walletProvider.runtime)[
params.fromChain
].chainId as ChainId,
toChainId: getChainConfigs(this.walletProvider.runtime)[
params.toChain
].chainId as ChainId,
fromChainId: this.walletProvider.getChainConfigs(params.fromChain)
.id,
toChainId: this.walletProvider.getChainConfigs(params.toChain).id,
fromTokenAddress: params.fromToken,
toTokenAddress: params.toToken,
fromAmount: params.amount,
Expand All @@ -79,9 +74,7 @@ export class BridgeAction {
to: routes.routes[0].steps[0].estimate
.approvalAddress as `0x${string}`,
value: BigInt(params.amount),
chainId: getChainConfigs(this.walletProvider.runtime)[
params.fromChain
].chainId,
chainId: this.walletProvider.getChainConfigs(params.fromChain).id,
};
}
}
Expand All @@ -95,7 +88,10 @@ export const bridgeAction = {
state: State,
options: any
) => {
const walletProvider = new WalletProvider(runtime);
const privateKey = runtime.getSetting(
"EVM_PRIVATE_KEY"
) as `0x${string}`;
const walletProvider = new WalletProvider(privateKey);
const action = new BridgeAction(walletProvider);
return action.bridge(options);
},
Expand Down
37 changes: 16 additions & 21 deletions packages/plugin-evm/src/actions/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
getRoutes,
} from "@lifi/sdk";
import { WalletProvider } from "../providers/wallet";
import { getChainConfigs } from "../providers/chainConfigs";
import { swapTemplate } from "../templates";
import type { SwapParams, Transaction } from "../types";

Expand All @@ -19,16 +18,14 @@ export class SwapAction {
constructor(private walletProvider: WalletProvider) {
this.config = createConfig({
integrator: "eliza",
chains: Object.values(
getChainConfigs(this.walletProvider.runtime)
).map((config) => ({
id: config.chainId,
chains: Object.values(this.walletProvider.chains).map((config) => ({
id: config.id,
name: config.name,
key: config.name.toLowerCase(),
chainType: "EVM" as const,
nativeToken: {
...config.nativeCurrency,
chainId: config.chainId,
chainId: config.id,
address: "0x0000000000000000000000000000000000000000",
coinKey: config.nativeCurrency.symbol,
priceUSD: "0",
Expand All @@ -38,15 +35,15 @@ export class SwapAction {
name: config.nativeCurrency.name,
},
rpcUrls: {
public: { http: [config.rpcUrl] },
public: { http: [config.rpcUrls.default.http[0]] },
},
blockExplorerUrls: [config.blockExplorerUrl],
blockExplorerUrls: [config.blockExplorers.default.url],
metamask: {
chainId: `0x${config.chainId.toString(16)}`,
chainId: `0x${config.id.toString(16)}`,
chainName: config.name,
nativeCurrency: config.nativeCurrency,
rpcUrls: [config.rpcUrl],
blockExplorerUrls: [config.blockExplorerUrl],
rpcUrls: [config.rpcUrls.default.http[0]],
blockExplorerUrls: [config.blockExplorers.default.url],
},
coin: config.nativeCurrency.symbol,
mainnet: true,
Expand All @@ -56,16 +53,12 @@ export class SwapAction {
}

async swap(params: SwapParams): Promise<Transaction> {
const walletClient = this.walletProvider.getWalletClient();
const walletClient = this.walletProvider.getWalletClient(params.chain);
const [fromAddress] = await walletClient.getAddresses();

const routes = await getRoutes({
fromChainId: getChainConfigs(this.walletProvider.runtime)[
params.chain
].chainId as ChainId,
toChainId: getChainConfigs(this.walletProvider.runtime)[
params.chain
].chainId as ChainId,
fromChainId: this.walletProvider.getChainConfigs(params.chain).id,
toChainId: this.walletProvider.getChainConfigs(params.chain).id,
fromTokenAddress: params.fromToken,
toTokenAddress: params.toToken,
fromAmount: params.amount,
Expand All @@ -92,8 +85,7 @@ export class SwapAction {
.approvalAddress as `0x${string}`,
value: BigInt(params.amount),
data: process.data as `0x${string}`,
chainId: getChainConfigs(this.walletProvider.runtime)[params.chain]
.chainId,
chainId: this.walletProvider.getChainConfigs(params.chain).id,
};
}
}
Expand All @@ -109,7 +101,10 @@ export const swapAction = {
callback?: any
) => {
try {
const walletProvider = new WalletProvider(runtime);
const privateKey = runtime.getSetting(
"EVM_PRIVATE_KEY"
) as `0x${string}`;
const walletProvider = new WalletProvider(privateKey);
const action = new SwapAction(walletProvider);
return await action.swap(options);
} catch (error) {
Expand Down
110 changes: 94 additions & 16 deletions packages/plugin-evm/src/actions/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import { ByteArray, parseEther, type Hex } from "viem";
import { WalletProvider } from "../providers/wallet";
import { ByteArray, formatEther, parseEther, type Hex } from "viem";
import {
composeContext,
generateObjectDEPRECATED,
HandlerCallback,
ModelClass,
type IAgentRuntime,
type Memory,
type State,
} from "@ai16z/eliza";

import { initWalletProvider, WalletProvider } from "../providers/wallet";
import type { Transaction, TransferParams } from "../types";
import { transferTemplate } from "../templates";
import type { IAgentRuntime, Memory, State } from "@ai16z/eliza";

export { transferTemplate };
export class TransferAction {
constructor(private walletProvider: WalletProvider) {}

async transfer(
runtime: IAgentRuntime,
params: TransferParams
): Promise<Transaction> {
const walletClient = this.walletProvider.getWalletClient();
const [fromAddress] = await walletClient.getAddresses();
async transfer(params: TransferParams): Promise<Transaction> {
console.log(
`Transferring: ${params.amount} tokens to (${params.toAddress} on ${params.fromChain})`
);

await this.walletProvider.switchChain(runtime, params.fromChain);
const walletClient = this.walletProvider.getWalletClient(
params.fromChain
);

try {
const hash = await walletClient.sendTransaction({
account: fromAddress,
account: walletClient.account,
to: params.toAddress,
value: parseEther(params.amount),
data: params.data as Hex,
Expand All @@ -39,7 +48,7 @@ export class TransferAction {

return {
hash,
from: fromAddress,
from: walletClient.account.address,
to: params.toAddress,
value: parseEther(params.amount),
data: params.data as Hex,
Expand All @@ -50,18 +59,87 @@ export class TransferAction {
}
}

const buildTransferDetails = async (
state: State,
runtime: IAgentRuntime,
wp: WalletProvider
): Promise<TransferParams> => {
const context = composeContext({
state,
template: transferTemplate,
});

const chains = Object.keys(wp.chains);

const contextWithChains = context.replace(
"SUPPORTED_CHAINS",
chains.toString()
);

const transferDetails = (await generateObjectDEPRECATED({
runtime,
context: contextWithChains,
modelClass: ModelClass.SMALL,
})) as TransferParams;

const existingChain = wp.chains[transferDetails.fromChain];

if (!existingChain) {
throw new Error(
"The chain " +
transferDetails.fromChain +
" not configured yet. Add the chain or choose one from configured: " +
chains.toString()
);
}

return transferDetails;
};

export const transferAction = {
name: "transfer",
description: "Transfer tokens between addresses on the same chain",
handler: async (
runtime: IAgentRuntime,
message: Memory,
state: State,
options: any
options: any,
callback?: HandlerCallback
) => {
const walletProvider = new WalletProvider(runtime);
const action = new TransferAction(walletProvider);
return action.transfer(runtime, options);
try {
const walletProvider = initWalletProvider(runtime);
const action = new TransferAction(walletProvider);
const transferDetails = await buildTransferDetails(
state,
runtime,
walletProvider
);
const tx = await action.transfer(transferDetails);

if (callback) {
callback({
text: `Successfully transferred ${formatEther(tx.value)} tokens to ${tx.to}\nTransaction hash: ${tx.hash}\nChain: ${transferDetails.fromChain}`,
content: {
success: true,
hash: tx.hash,
amount: formatEther(tx.value),
recipient: tx.to,
chain: transferDetails.fromChain,
},
});
}

return true;
} catch (error) {
console.error("Error during token transfer:", error);
if (callback) {
callback({
text: `Error transferring tokens: ${error.message}`,
content: { error: error.message },
});
}
return false;
}
},
template: transferTemplate,
validate: async (runtime: IAgentRuntime) => {
Expand Down
Loading
Loading