From 018d6add7f3830b64000881ce7a541525921403a Mon Sep 17 00:00:00 2001 From: Nicky Ru Date: Tue, 17 Dec 2024 10:02:32 +0000 Subject: [PATCH] fix: add lint script for plugin evm and fix lint errors --- packages/plugin-evm/eslint.config.mjs | 3 + packages/plugin-evm/package.json | 43 +++++------ packages/plugin-evm/src/actions/swap.ts | 1 - packages/plugin-evm/src/actions/transfer.ts | 35 +++------ packages/plugin-evm/src/providers/wallet.ts | 4 +- .../plugin-evm/src/tests/transfer.test.ts | 4 +- packages/plugin-evm/src/tests/wallet.test.ts | 74 ++++++++++--------- packages/plugin-evm/src/types/index.ts | 4 +- 8 files changed, 81 insertions(+), 87 deletions(-) create mode 100644 packages/plugin-evm/eslint.config.mjs diff --git a/packages/plugin-evm/eslint.config.mjs b/packages/plugin-evm/eslint.config.mjs new file mode 100644 index 0000000000..92fe5bbebe --- /dev/null +++ b/packages/plugin-evm/eslint.config.mjs @@ -0,0 +1,3 @@ +import eslintGlobalConfig from "../../eslint.config.mjs"; + +export default [...eslintGlobalConfig]; diff --git a/packages/plugin-evm/package.json b/packages/plugin-evm/package.json index c2c2148214..ea3fa885ac 100644 --- a/packages/plugin-evm/package.json +++ b/packages/plugin-evm/package.json @@ -1,23 +1,24 @@ { - "name": "@ai16z/plugin-evm", - "version": "0.1.6-alpha.4", - "main": "dist/index.js", - "type": "module", - "types": "dist/index.d.ts", - "dependencies": { - "@ai16z/eliza": "workspace:*", - "@lifi/data-types": "5.15.5", - "@lifi/sdk": "3.4.1", - "@lifi/types": "16.3.0", - "tsup": "8.3.5", - "viem": "2.21.53" - }, - "scripts": { - "build": "tsup --format esm --dts", - "dev": "tsup --format esm --dts --watch", - "test": "vitest run" - }, - "peerDependencies": { - "whatwg-url": "7.1.0" - } + "name": "@ai16z/plugin-evm", + "version": "0.1.5-alpha.5", + "main": "dist/index.js", + "type": "module", + "types": "dist/index.d.ts", + "dependencies": { + "@ai16z/eliza": "workspace:*", + "@lifi/data-types": "5.15.5", + "@lifi/sdk": "3.4.1", + "@lifi/types": "16.3.0", + "tsup": "8.3.5", + "viem": "2.21.53" + }, + "scripts": { + "build": "tsup --format esm --dts", + "dev": "tsup --format esm --dts --watch", + "test": "vitest run", + "lint": "eslint --fix --cache ." + }, + "peerDependencies": { + "whatwg-url": "7.1.0" + } } diff --git a/packages/plugin-evm/src/actions/swap.ts b/packages/plugin-evm/src/actions/swap.ts index 1c66f43f14..6501dbf878 100644 --- a/packages/plugin-evm/src/actions/swap.ts +++ b/packages/plugin-evm/src/actions/swap.ts @@ -1,6 +1,5 @@ import type { IAgentRuntime, Memory, State } from "@ai16z/eliza"; import { - ChainId, createConfig, executeRoute, ExtendedChain, diff --git a/packages/plugin-evm/src/actions/transfer.ts b/packages/plugin-evm/src/actions/transfer.ts index 61b2bacbbe..3060012e7f 100644 --- a/packages/plugin-evm/src/actions/transfer.ts +++ b/packages/plugin-evm/src/actions/transfer.ts @@ -28,7 +28,7 @@ export class TransferAction { params.data = "0x"; } - await this.walletProvider.switchChain(params.fromChain); + this.walletProvider.switchChain(params.fromChain); const walletClient = this.walletProvider.getWalletClient( params.fromChain @@ -41,12 +41,12 @@ export class TransferAction { value: parseEther(params.amount), data: params.data as Hex, kzg: { - blobToKzgCommitment: function (blob: ByteArray): ByteArray { + blobToKzgCommitment: function (_: ByteArray): ByteArray { throw new Error("Function not implemented."); }, computeBlobKzgProof: function ( - blob: ByteArray, - commitment: ByteArray + _blob: ByteArray, + _commitment: ByteArray ): ByteArray { throw new Error("Function not implemented."); }, @@ -81,7 +81,7 @@ const buildTransferDetails = async ( const contextWithChains = context.replace( "SUPPORTED_CHAINS", - chains.toString() + chains.map((item) => `"${item}"`).join("|") ); const transferDetails = (await generateObjectDeprecated({ @@ -109,9 +109,9 @@ export const transferAction = { description: "Transfer tokens between addresses on the same chain", handler: async ( runtime: IAgentRuntime, - message: Memory, + _message: Memory, state: State, - options: any, + _options: any, callback?: HandlerCallback ) => { console.log("Transfer action handler called"); @@ -119,24 +119,11 @@ export const transferAction = { const action = new TransferAction(walletProvider); // Compose transfer context - const transferContext = composeContext({ + const paramOptions = await buildTransferDetails( state, - template: transferTemplate, - }); - - // Generate transfer content - const content = await generateObjectDeprecated({ runtime, - context: transferContext, - modelClass: ModelClass.LARGE, - }); - - const paramOptions: TransferParams = { - fromChain: content.fromChain, - toAddress: content.toAddress, - amount: content.amount, - data: content.data, - }; + walletProvider + ); try { const transferResp = await action.transfer(paramOptions); @@ -148,7 +135,7 @@ export const transferAction = { hash: transferResp.hash, amount: formatEther(transferResp.value), recipient: transferResp.to, - chain: content.fromChain, + chain: paramOptions.fromChain, }, }); } diff --git a/packages/plugin-evm/src/providers/wallet.ts b/packages/plugin-evm/src/providers/wallet.ts index 2b816c461b..a72e2e9abf 100644 --- a/packages/plugin-evm/src/providers/wallet.ts +++ b/packages/plugin-evm/src/providers/wallet.ts @@ -211,8 +211,8 @@ export const initWalletProvider = (runtime: IAgentRuntime) => { export const evmWalletProvider: Provider = { async get( runtime: IAgentRuntime, - message: Memory, - state?: State + _message: Memory, + _state?: State ): Promise { try { const walletProvider = initWalletProvider(runtime); diff --git a/packages/plugin-evm/src/tests/transfer.test.ts b/packages/plugin-evm/src/tests/transfer.test.ts index d637a6a3d9..a6159db76d 100644 --- a/packages/plugin-evm/src/tests/transfer.test.ts +++ b/packages/plugin-evm/src/tests/transfer.test.ts @@ -17,7 +17,7 @@ describe("Transfer Action", () => { it("should initialize with wallet provider", () => { const ta = new TransferAction(wp); - expect(ta).to.toBeDefined(); + expect(ta).toBeDefined(); }); }); describe("Transfer", () => { @@ -44,7 +44,7 @@ describe("Transfer Action", () => { }); const prepareChains = () => { - let customChains: Record = {}; + const customChains: Record = {}; const chainNames = ["iotexTestnet"]; chainNames.forEach( (chain) => diff --git a/packages/plugin-evm/src/tests/wallet.test.ts b/packages/plugin-evm/src/tests/wallet.test.ts index a8e5a3ee87..a6b227a470 100644 --- a/packages/plugin-evm/src/tests/wallet.test.ts +++ b/packages/plugin-evm/src/tests/wallet.test.ts @@ -13,7 +13,7 @@ const customRpcUrls = { describe("Wallet provider", () => { let walletProvider: WalletProvider; let pk: `0x${string}`; - let customChains: Record = {}; + const customChains: Record = {}; beforeAll(() => { pk = generatePrivateKey(); @@ -32,24 +32,24 @@ describe("Wallet provider", () => { walletProvider = new WalletProvider(pk); - expect(walletProvider.getAddress()).to.be.eq(expectedAddress); + expect(walletProvider.getAddress()).toEqual(expectedAddress); }); it("sets default chain to ethereum mainnet", () => { walletProvider = new WalletProvider(pk); - expect(walletProvider.chains.mainnet.id).to.be.eq(mainnet.id); - expect(walletProvider.getCurrentChain().id).to.be.eq(mainnet.id); + expect(walletProvider.chains.mainnet.id).toEqual(mainnet.id); + expect(walletProvider.getCurrentChain().id).toEqual(mainnet.id); }); it("sets custom chains", () => { walletProvider = new WalletProvider(pk, customChains); - expect(walletProvider.chains.iotex.id).to.be.eq(iotex.id); - expect(walletProvider.chains.arbitrum.id).to.be.eq(arbitrum.id); + expect(walletProvider.chains.iotex.id).toEqual(iotex.id); + expect(walletProvider.chains.arbitrum.id).toEqual(arbitrum.id); }); it("sets the first provided custom chain as current chain", () => { walletProvider = new WalletProvider(pk, customChains); - expect(walletProvider.getCurrentChain().id).to.be.eq(iotex.id); + expect(walletProvider.getCurrentChain().id).toEqual(iotex.id); }); }); describe("Clients", () => { @@ -58,7 +58,7 @@ describe("Wallet provider", () => { }); it("generates public client", () => { const client = walletProvider.getPublicClient("mainnet"); - expect(client.chain.id).to.be.equal(mainnet.id); + expect(client.chain.id).toEqual(mainnet.id); expect(client.transport.url).toEqual( mainnet.rpcUrls.default.http[0] ); @@ -71,11 +71,11 @@ describe("Wallet provider", () => { const wp = new WalletProvider(pk, { ["mainnet"]: chain }); const client = wp.getPublicClient("mainnet"); - expect(client.chain.id).to.be.equal(mainnet.id); - expect(client.chain.rpcUrls.default.http[0]).to.eq( + expect(client.chain.id).toEqual(mainnet.id); + expect(client.chain.rpcUrls.default.http[0]).toEqual( mainnet.rpcUrls.default.http[0] ); - expect(client.chain.rpcUrls.custom.http[0]).to.eq( + expect(client.chain.rpcUrls.custom.http[0]).toEqual( customRpcUrls.mainnet ); expect(client.transport.url).toEqual(customRpcUrls.mainnet); @@ -86,7 +86,7 @@ describe("Wallet provider", () => { const client = walletProvider.getWalletClient("mainnet"); - expect(client.account.address).to.be.equal(expectedAddress); + expect(client.account.address).toEqual(expectedAddress); expect(client.transport.url).toEqual( mainnet.rpcUrls.default.http[0] ); @@ -102,12 +102,12 @@ describe("Wallet provider", () => { const client = wp.getWalletClient("mainnet"); - expect(client.account.address).to.be.equal(expectedAddress); - expect(client.chain.id).to.be.equal(mainnet.id); - expect(client.chain.rpcUrls.default.http[0]).to.eq( + expect(client.account.address).toEqual(expectedAddress); + expect(client.chain.id).toEqual(mainnet.id); + expect(client.chain.rpcUrls.default.http[0]).toEqual( mainnet.rpcUrls.default.http[0] ); - expect(client.chain.rpcUrls.custom.http[0]).to.eq( + expect(client.chain.rpcUrls.custom.http[0]).toEqual( customRpcUrls.mainnet ); expect(client.transport.url).toEqual(customRpcUrls.mainnet); @@ -120,16 +120,16 @@ describe("Wallet provider", () => { it("should fetch balance", async () => { const bal = await walletProvider.getWalletBalance(); - expect(bal).to.be.eq("0"); + expect(bal).toEqual("0"); }); it("should fetch balance for a specific added chain", async () => { const bal = await walletProvider.getWalletBalanceForChain("iotex"); - expect(bal).to.be.eq("0"); + expect(bal).toEqual("0"); }); it("should return null if chain is not added", async () => { const bal = await walletProvider.getWalletBalanceForChain("base"); - expect(bal).to.be.null; + expect(bal).toBeNull(); }); }); describe("Chain", () => { @@ -140,7 +140,7 @@ describe("Wallet provider", () => { const chainName = "iotex"; const chain: Chain = WalletProvider.genChainFromName(chainName); - expect(chain.rpcUrls.default.http[0]).to.eq( + expect(chain.rpcUrls.default.http[0]).toEqual( iotex.rpcUrls.default.http[0] ); }); @@ -152,59 +152,63 @@ describe("Wallet provider", () => { customRpcUrl ); - expect(chain.rpcUrls.default.http[0]).to.eq( + expect(chain.rpcUrls.default.http[0]).toEqual( iotex.rpcUrls.default.http[0] ); - expect(chain.rpcUrls.custom.http[0]).to.eq(customRpcUrl); + expect(chain.rpcUrls.custom.http[0]).toEqual(customRpcUrl); }); it("switches chain", () => { const initialChain = walletProvider.getCurrentChain().id; - expect(initialChain).to.be.eq(iotex.id); + expect(initialChain).toEqual(iotex.id); walletProvider.switchChain("mainnet"); const newChain = walletProvider.getCurrentChain().id; - expect(newChain).to.be.eq(mainnet.id); + expect(newChain).toEqual(mainnet.id); }); it("switches chain (by adding new chain)", () => { const initialChain = walletProvider.getCurrentChain().id; - expect(initialChain).to.be.eq(iotex.id); + expect(initialChain).toEqual(iotex.id); walletProvider.switchChain("arbitrum"); const newChain = walletProvider.getCurrentChain().id; - expect(newChain).to.be.eq(arbitrum.id); + expect(newChain).toEqual(arbitrum.id); }); it("adds chain", () => { const initialChains = walletProvider.chains; - expect(initialChains.base).to.be.undefined; + expect(initialChains.base).toBeUndefined(); const base = WalletProvider.genChainFromName("base"); walletProvider.addChain({ base }); const newChains = walletProvider.chains; - expect(newChains.arbitrum.id).to.be.eq(arbitrum.id); + expect(newChains.arbitrum.id).toEqual(arbitrum.id); }); it("gets chain configs", () => { const chain = walletProvider.getChainConfigs("iotex"); - expect(chain.id).to.eq(iotex.id); + expect(chain.id).toEqual(iotex.id); }); it("throws if tries to switch to an invalid chain", () => { const initialChain = walletProvider.getCurrentChain().id; - expect(initialChain).to.be.eq(iotex.id); + expect(initialChain).toEqual(iotex.id); + // intentionally set incorrect chain, ts will complain + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - expect(() => walletProvider.switchChain("eth")).to.throw(); + expect(() => walletProvider.switchChain("eth")).toThrow(); }); it("throws if unsupported chain name", () => { + // intentionally set incorrect chain, ts will complain + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - expect(() => - WalletProvider.genChainFromName("ethereum") - ).to.throw(); + expect(() => WalletProvider.genChainFromName("ethereum")).toThrow(); }); it("throws if invalid chain name", () => { + // intentionally set incorrect chain, ts will complain + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - expect(() => WalletProvider.genChainFromName("eth")).to.throw(); + expect(() => WalletProvider.genChainFromName("eth")).toThrow(); }); }); }); diff --git a/packages/plugin-evm/src/types/index.ts b/packages/plugin-evm/src/types/index.ts index 8fa8247dcd..c2e7c29acd 100644 --- a/packages/plugin-evm/src/types/index.ts +++ b/packages/plugin-evm/src/types/index.ts @@ -10,8 +10,8 @@ import type { } from "viem"; import * as viemChains from "viem/chains"; -const SupportedChainList = Object.keys(viemChains) as Array; -export type SupportedChain = (typeof SupportedChainList)[number]; +const _SupportedChainList = Object.keys(viemChains) as Array; +export type SupportedChain = (typeof _SupportedChainList)[number]; // Transaction types export interface Transaction {