From 329cc14ea3a8465ebf0fe26df9711dc0a6919c9f Mon Sep 17 00:00:00 2001 From: AIFlow_ML Date: Mon, 3 Feb 2025 05:29:51 +0700 Subject: [PATCH 1/2] Fixed all the issues and added Biome --- packages/plugin-0x/biome.json | 41 ++++++++++++++ packages/plugin-0x/package.json | 13 ++++- packages/plugin-0x/src/EVMtokenRegistry.ts | 6 +-- .../src/actions/getIndicativePrice.ts | 30 +++++------ packages/plugin-0x/src/actions/getQuote.ts | 54 +++++++++---------- packages/plugin-0x/src/actions/swap.ts | 29 +++++----- packages/plugin-0x/src/constants.ts | 2 +- .../src/hooks.ts/useGetWalletClient.ts | 4 +- packages/plugin-0x/src/index.ts | 2 +- 9 files changed, 115 insertions(+), 66 deletions(-) create mode 100644 packages/plugin-0x/biome.json diff --git a/packages/plugin-0x/biome.json b/packages/plugin-0x/biome.json new file mode 100644 index 0000000000..818716a621 --- /dev/null +++ b/packages/plugin-0x/biome.json @@ -0,0 +1,41 @@ +{ + "$schema": "https://biomejs.dev/schemas/1.5.3/schema.json", + "organizeImports": { + "enabled": false + }, + "linter": { + "enabled": true, + "rules": { + "recommended": true, + "correctness": { + "noUnusedVariables": "error" + }, + "suspicious": { + "noExplicitAny": "error" + }, + "style": { + "useConst": "error", + "useImportType": "off" + } + } + }, + "formatter": { + "enabled": true, + "indentStyle": "space", + "indentWidth": 4, + "lineWidth": 100 + }, + "javascript": { + "formatter": { + "quoteStyle": "single", + "trailingCommas": "es5" + } + }, + "files": { + "ignore": [ + "dist/**/*", + "extra/**/*", + "node_modules/**/*" + ] + } +} \ No newline at end of file diff --git a/packages/plugin-0x/package.json b/packages/plugin-0x/package.json index 517c3f96fe..2949450152 100644 --- a/packages/plugin-0x/package.json +++ b/packages/plugin-0x/package.json @@ -21,13 +21,22 @@ "scripts": { "build": "tsup --format esm --dts", "dev": "tsup --format esm --dts --watch", - "test": "vitest run" + "test": "vitest run", + "lint": "biome check src/", + "lint:fix": "biome check --apply src/", + "format": "biome format src/", + "format:fix": "biome format --write src/" }, "dependencies": { + "@elizaos/core": "workspace:*", + "tsup": "^8.0.1", + "whatwg-url": "7.1.0", "@0x/swap-ts-sdk": "2.1.1" }, "devDependencies": { - "tsup": "^8.0.1" + "tsup": "^8.0.1", + "@biomejs/biome": "1.5.3", + "vitest": "^2.1.5" }, "peerDependencies": { "@elizaos/core": "workspace:*", diff --git a/packages/plugin-0x/src/EVMtokenRegistry.ts b/packages/plugin-0x/src/EVMtokenRegistry.ts index 51a674b941..39075f71a5 100644 --- a/packages/plugin-0x/src/EVMtokenRegistry.ts +++ b/packages/plugin-0x/src/EVMtokenRegistry.ts @@ -1,9 +1,9 @@ import { elizaLogger } from "@elizaos/core"; import { Chains, - TokenMetadata, - TrustWalletGithubJson, - TrustWalletTokenMetadata, + type TokenMetadata, + type TrustWalletGithubJson, + type TrustWalletTokenMetadata, } from "./types"; import { NATIVE_TOKENS } from "./constants"; diff --git a/packages/plugin-0x/src/actions/getIndicativePrice.ts b/packages/plugin-0x/src/actions/getIndicativePrice.ts index b73ceee4ca..7a9a469d46 100644 --- a/packages/plugin-0x/src/actions/getIndicativePrice.ts +++ b/packages/plugin-0x/src/actions/getIndicativePrice.ts @@ -1,9 +1,9 @@ import { - Action, - IAgentRuntime, - Memory, - State, - HandlerCallback, + type Action, + type IAgentRuntime, + type Memory, + type State, + type HandlerCallback, elizaLogger, composeContext, ModelClass, @@ -13,7 +13,7 @@ import { import { createClientV2 } from "@0x/swap-ts-sdk"; import { getIndicativePriceTemplate } from "../templates"; import { z } from "zod"; -import { Chains, GetIndicativePriceResponse, PriceInquiry } from "../types"; +import { Chains, type GetIndicativePriceResponse, type PriceInquiry } from "../types"; import { parseUnits } from "viem"; import { CHAIN_NAMES, ZX_MEMORY } from "../constants"; import { EVMTokenRegistry } from "../EVMtokenRegistry"; @@ -45,17 +45,17 @@ export const getIndicativePrice: Action = { runtime: IAgentRuntime, message: Memory, state: State, - options: Record, + _options: Record, callback: HandlerCallback ) => { const supportedChains = Object.keys(Chains).join(" | "); - state = !state + const localState = !state ? await runtime.composeState(message, { supportedChains }) : await runtime.updateRecentMessageState(state); const context = composeContext({ - state, + state: localState, template: getIndicativePriceTemplate, }); @@ -86,7 +86,7 @@ export const getIndicativePrice: Action = { text: `Unsupported chain: ${chain}. Supported chains are: ${Object.keys( Chains ) - .filter((k) => isNaN(Number(k))) + .filter((k) => !Number.isNaN(Number(k))) .join(", ")}`, }); return; @@ -148,10 +148,10 @@ export const getIndicativePrice: Action = { // Format amounts to human-readable numbers const buyAmount = Number(price.buyAmount) / - Math.pow(10, buyTokenMetadata.decimals); + (10 ** buyTokenMetadata.decimals); const sellAmount = Number(price.sellAmount) / - Math.pow(10, sellTokenMetadata.decimals); + (10 ** sellTokenMetadata.decimals); await storePriceInquiryToMemory(runtime, message, { sellTokenObject: sellTokenMetadata, @@ -163,13 +163,13 @@ export const getIndicativePrice: Action = { // Updated formatted response to include chain const formattedResponse = [ - `💱 Swap Details:`, - `────────────────`, + "💱 Swap Details:", + "────────────────", `📤 Sell: ${sellAmount.toFixed(4)} ${sellTokenMetadata.symbol}`, `📥 Buy: ${buyAmount.toFixed(4)} ${buyTokenMetadata.symbol}`, `📊 Rate: 1 ${sellTokenMetadata.symbol} = ${(buyAmount / sellAmount).toFixed(4)} ${buyTokenMetadata.symbol}`, `🔗 Chain: ${CHAIN_NAMES[chainId]}`, - `────────────────`, + "────────────────", `💫 Happy with the price? Type 'quote' to continue`, ].join("\n"); diff --git a/packages/plugin-0x/src/actions/getQuote.ts b/packages/plugin-0x/src/actions/getQuote.ts index 9d50beb94d..d4df553fe7 100644 --- a/packages/plugin-0x/src/actions/getQuote.ts +++ b/packages/plugin-0x/src/actions/getQuote.ts @@ -1,13 +1,13 @@ import { - Action, - IAgentRuntime, - Memory, - State, - HandlerCallback, + type Action, + type IAgentRuntime, + type Memory, + type State, + type HandlerCallback, elizaLogger, MemoryManager, } from "@elizaos/core"; -import { GetQuoteResponse, PriceInquiry, Quote } from "../types"; +import type { GetQuoteResponse, PriceInquiry, Quote } from "../types"; import { formatTokenAmount } from "../utils"; import { CHAIN_NAMES, NATIVE_TOKENS, ZX_MEMORY } from "../constants"; import { createClientV2 } from "@0x/swap-ts-sdk"; @@ -25,8 +25,8 @@ export const getQuote: Action = { handler: async ( runtime: IAgentRuntime, message: Memory, - state: State, - options: Record, + _state: State, + _options: Record, callback: HandlerCallback ) => { const latestPriceInquiry = await retrieveLatestPriceInquiry( @@ -89,7 +89,7 @@ export const getQuote: Action = { const warnings = []; if (quote.issues?.balance) { warnings.push( - `⚠️ Warnings:`, + "⚠️ Warnings:", ` • Insufficient balance (Have ${formatTokenAmount( quote.issues.balance.actual, quote.issues.balance.token, @@ -99,8 +99,8 @@ export const getQuote: Action = { } const formattedResponse = [ - `🎯 Firm Quote Details:`, - `────────────────`, + "🎯 Firm Quote Details:", + "────────────────", // Basic swap details (same as price) `📤 Sell: ${formatTokenAmount( quote.sellAmount, @@ -125,7 +125,7 @@ export const getQuote: Action = { )}`, // Fee breakdown - `💰 Fees Breakdown:`, + "💰 Fees Breakdown:", ` • 0x Protocol Fee: ${formatTokenAmount( quote.fees.zeroExFee?.amount, quote.fees.zeroExFee?.token, @@ -153,8 +153,8 @@ export const getQuote: Action = { ...(warnings.length > 0 ? warnings : []), - `────────────────`, - `💫 Ready to execute? Type 'execute' to continue`, + "────────────────", + "💫 Ready to execute? Type 'execute' to continue", ] .filter(Boolean) .join("\n"); @@ -223,20 +223,20 @@ export const getQuote: Action = { ], }; -const formatTime = (time: string) => { - const expirationDate = new Date(parseInt(time) * 1000); +// const formatTime = (time: string) => { +// const expirationDate = new Date(parseInt(time) * 1000); - // Format: "Mar 15, 2:30 PM" - const formattedTime = expirationDate.toLocaleString(undefined, { - month: "short", - day: "numeric", - hour: "numeric", - minute: "2-digit", - hour12: true, - }); +// // Format: "Mar 15, 2:30 PM" +// const formattedTime = expirationDate.toLocaleString(undefined, { +// month: "short", +// day: "numeric", +// hour: "numeric", +// minute: "2-digit", +// hour12: true, +// }); - return `${formattedTime}`; -}; +// return `${formattedTime}`; +// }; export const retrieveLatestPriceInquiry = async ( runtime: IAgentRuntime, @@ -260,7 +260,7 @@ export const retrieveLatestPriceInquiry = async ( } return null; } catch (error) { - elizaLogger.error(`Failed to retrieve price inquiry: ${error.message}`); + elizaLogger.error("Failed to retrieve price inquiry:", error.message); return null; } }; diff --git a/packages/plugin-0x/src/actions/swap.ts b/packages/plugin-0x/src/actions/swap.ts index 17da272144..2183d8be3d 100644 --- a/packages/plugin-0x/src/actions/swap.ts +++ b/packages/plugin-0x/src/actions/swap.ts @@ -1,16 +1,16 @@ import { - Action, - IAgentRuntime, - Memory, - State, - HandlerCallback, + type Action, + type IAgentRuntime, + type Memory, + type State, + type HandlerCallback, elizaLogger, MemoryManager, } from "@elizaos/core"; -import { Hex, numberToHex, concat } from "viem"; +import { type Hex, numberToHex, concat } from "viem"; import { CHAIN_EXPLORERS, ZX_MEMORY } from "../constants"; import { getWalletClient } from "../hooks.ts/useGetWalletClient"; -import { Quote } from "../types"; +import type { Quote } from "../types"; export const swap: Action = { name: "EXECUTE_SWAP_0X", @@ -31,8 +31,8 @@ export const swap: Action = { handler: async ( runtime: IAgentRuntime, message: Memory, - state: State, - options: Record, + _state: State, + _options: Record, callback: HandlerCallback ) => { const latestQuote = await retrieveLatestQuote(runtime, message); @@ -99,13 +99,12 @@ export const swap: Action = { content: { hash: txHash, status: "success" }, }); return true; - } else { - callback({ - text: `❌ Swap failed! Check transaction: ${CHAIN_EXPLORERS[chainId]}/tx/${txHash}`, - content: { hash: txHash, status: "failed" }, - }); - return false; } + callback({ + text: `❌ Swap failed! Check transaction: ${CHAIN_EXPLORERS[chainId]}/tx/${txHash}`, + content: { hash: txHash, status: "failed" }, + }); + return false; } catch (error) { elizaLogger.error("Swap execution failed:", error); callback({ diff --git a/packages/plugin-0x/src/constants.ts b/packages/plugin-0x/src/constants.ts index f51220a229..f161bbb072 100644 --- a/packages/plugin-0x/src/constants.ts +++ b/packages/plugin-0x/src/constants.ts @@ -1,4 +1,4 @@ -import { Chains, TokenMetadata } from "./types"; +import { Chains, type TokenMetadata } from "./types"; export const ZX_MEMORY = { price: { diff --git a/packages/plugin-0x/src/hooks.ts/useGetWalletClient.ts b/packages/plugin-0x/src/hooks.ts/useGetWalletClient.ts index 5bc7238bb3..25516a3808 100644 --- a/packages/plugin-0x/src/hooks.ts/useGetWalletClient.ts +++ b/packages/plugin-0x/src/hooks.ts/useGetWalletClient.ts @@ -3,8 +3,8 @@ import { http, publicActions, createTestClient, - WalletClient, - PublicClient, + type WalletClient, + type PublicClient, walletActions, } from "viem"; diff --git a/packages/plugin-0x/src/index.ts b/packages/plugin-0x/src/index.ts index f7f493eae2..a8fd147113 100644 --- a/packages/plugin-0x/src/index.ts +++ b/packages/plugin-0x/src/index.ts @@ -1,4 +1,4 @@ -import { Plugin } from "@elizaos/core"; +import type { Plugin } from "@elizaos/core"; import { getIndicativePrice } from "./actions/getIndicativePrice"; import { getQuote } from "./actions/getQuote"; import { swap } from "./actions/swap"; From 45e58e9faf6e41ff17a5f4c92fbf44d969c3cdca Mon Sep 17 00:00:00 2001 From: Shakker Nerd <165377636+shakkernerd@users.noreply.github.com> Date: Sun, 2 Feb 2025 23:15:19 +0000 Subject: [PATCH 2/2] chore: remove multiple tsup package --- packages/plugin-0x/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/plugin-0x/package.json b/packages/plugin-0x/package.json index 2949450152..975b84a391 100644 --- a/packages/plugin-0x/package.json +++ b/packages/plugin-0x/package.json @@ -29,7 +29,6 @@ }, "dependencies": { "@elizaos/core": "workspace:*", - "tsup": "^8.0.1", "whatwg-url": "7.1.0", "@0x/swap-ts-sdk": "2.1.1" },