Skip to content

Commit

Permalink
fix: add lint script for plugin evm and fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nicky-ru committed Dec 17, 2024
1 parent 68b02e6 commit 6cfb828
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 67 deletions.
3 changes: 3 additions & 0 deletions packages/plugin-evm/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import eslintGlobalConfig from "../../eslint.config.mjs";

export default [...eslintGlobalConfig];
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",
"test": "vitest run"
"test": "vitest run",
"lint": "eslint --fix --cache ."
},
"peerDependencies": {
"whatwg-url": "7.1.0"
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-evm/src/actions/swap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { IAgentRuntime, Memory, State } from "@ai16z/eliza";
import {
ChainId,
createConfig,
executeRoute,
ExtendedChain,
Expand Down
35 changes: 11 additions & 24 deletions packages/plugin-evm/src/actions/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.");
},
Expand Down Expand Up @@ -81,7 +81,7 @@ const buildTransferDetails = async (

const contextWithChains = context.replace(
"SUPPORTED_CHAINS",
chains.toString()
chains.map((item) => `"${item}"`).join("|")
);

const transferDetails = (await generateObjectDeprecated({
Expand Down Expand Up @@ -109,34 +109,21 @@ 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");
const walletProvider = initWalletProvider(runtime);
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);
Expand All @@ -148,7 +135,7 @@ export const transferAction = {
hash: transferResp.hash,
amount: formatEther(transferResp.value),
recipient: transferResp.to,
chain: content.fromChain,
chain: paramOptions.fromChain,
},
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-evm/src/providers/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | null> {
try {
const walletProvider = initWalletProvider(runtime);
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-evm/src/tests/transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -44,7 +44,7 @@ describe("Transfer Action", () => {
});

const prepareChains = () => {
let customChains: Record<string, Chain> = {};
const customChains: Record<string, Chain> = {};
const chainNames = ["iotexTestnet"];
chainNames.forEach(
(chain) =>
Expand Down
74 changes: 39 additions & 35 deletions packages/plugin-evm/src/tests/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const customRpcUrls = {
describe("Wallet provider", () => {
let walletProvider: WalletProvider;
let pk: `0x${string}`;
let customChains: Record<string, Chain> = {};
const customChains: Record<string, Chain> = {};

beforeAll(() => {
pk = generatePrivateKey();
Expand All @@ -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", () => {
Expand All @@ -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]
);
Expand All @@ -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);
Expand All @@ -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]
);
Expand All @@ -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);
Expand All @@ -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", () => {
Expand All @@ -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]
);
});
Expand All @@ -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();
});
});
});
4 changes: 2 additions & 2 deletions packages/plugin-evm/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type {
} from "viem";
import * as viemChains from "viem/chains";

const SupportedChainList = Object.keys(viemChains) as Array<keyof typeof viemChains>;
export type SupportedChain = (typeof SupportedChainList)[number];
const _SupportedChainList = Object.keys(viemChains) as Array<keyof typeof viemChains>;
export type SupportedChain = (typeof _SupportedChainList)[number];

// Transaction types
export interface Transaction {
Expand Down

0 comments on commit 6cfb828

Please sign in to comment.