Skip to content

Commit 12f9209

Browse files
committed
chore(llm): optimize code
1 parent d83ddcd commit 12f9209

File tree

5 files changed

+14
-31
lines changed

5 files changed

+14
-31
lines changed

packages/llm/src/agent/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ export class PolkadotAgentApi implements IPolkadotAgentApi {
5959
}
6060

6161
getNativeBalanceTool(address: string): BalanceTool {
62-
return checkBalanceTool(this.api.getAllApis(), address) as unknown as BalanceTool
62+
return checkBalanceTool(this.api, address) as unknown as BalanceTool
6363
}
6464

6565
transferNativeTool(signer: PolkadotSigner): TransferTool {
66-
return transferNativeTool(this.api.getAllApis(), signer) as unknown as TransferTool
66+
return transferNativeTool(this.api, signer) as unknown as TransferTool
6767
}
6868

6969
xcmTransferNativeTool(signer: PolkadotSigner, sender: string): XcmTransferNativeAssetTool {

packages/llm/src/langchain/balance.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
import { tool } from "@langchain/core/tools"
2-
import type { Api, KnownChainId } from "@polkadot-agent-kit/common"
2+
import type { KnownChainId } from "@polkadot-agent-kit/common"
33
import { formatBalance } from "@polkadot-agent-kit/common"
4+
import type { PolkadotApi } from "@polkadot-agent-kit/core"
45
import { getNativeBalance } from "@polkadot-agent-kit/core"
56
import type { z } from "zod"
67

78
import type { BalanceToolResult, balanceToolSchema } from "../types"
89
import { toolConfigBalance } from "../types/balance"
910
import { ToolNames } from "../types/common"
10-
import { executeTool, getApiForChain, validateAndFormatAddress } from "../utils"
11+
import { executeTool, validateAndFormatAddress } from "../utils"
1112

1213
/**
1314
* Returns a tool that checks the balance of a specific address
1415
* @param apis - Map of chain IDs to API instances
1516
* @param address - The address to check the balance for
1617
* @returns A dynamic structured tool that checks the balance of the specified address
1718
*/
18-
export const checkBalanceTool = (apis: Map<KnownChainId, Api<KnownChainId>>, address: string) => {
19+
export const checkBalanceTool = (polkadotApi: PolkadotApi, address: string) => {
1920
return tool(async ({ chain }: z.infer<typeof balanceToolSchema>) => {
2021
return executeTool<BalanceToolResult>(
2122
ToolNames.CHECK_BALANCE,
2223
async () => {
23-
const api = getApiForChain(apis, chain)
24+
const api = polkadotApi.getApi(chain as KnownChainId)
2425
const formattedAddress = validateAndFormatAddress(address, chain as KnownChainId)
2526
const balanceInfo = await getNativeBalance(api, formattedAddress)
2627
const formattedBalance = formatBalance(balanceInfo.balance, balanceInfo.decimals)

packages/llm/src/langchain/transfer.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
import { tool } from "@langchain/core/tools"
2-
import type { Api, KnownChainId } from "@polkadot-agent-kit/common"
2+
import type { KnownChainId } from "@polkadot-agent-kit/common"
33
import { getDecimalsByChainId, parseUnits } from "@polkadot-agent-kit/common"
4+
import type { PolkadotApi } from "@polkadot-agent-kit/core"
45
import { submitTxWithPolkadotSigner, transferNativeCall } from "@polkadot-agent-kit/core"
56
import type { PolkadotSigner } from "polkadot-api/signer"
67
import type { z } from "zod"
78

89
import type { TransferToolResult, transferToolSchema } from "../types"
910
import { ToolNames } from "../types/common"
1011
import { toolConfigTransferNative } from "../types/transfer"
11-
import { executeTool, getApiForChain, validateAndFormatMultiAddress } from "../utils"
12+
import { executeTool, validateAndFormatMultiAddress } from "../utils"
1213

1314
/**
1415
* Returns a tool that transfers native tokens to a specific address
1516
* @param api - The API instance to use for the transfer
1617
* @returns A dynamic structured tool that transfers native tokens to the specified address
1718
*/
18-
export const transferNativeTool = (
19-
apis: Map<KnownChainId, Api<KnownChainId>>,
20-
signer: PolkadotSigner
21-
) => {
19+
export const transferNativeTool = (polkadotApi: PolkadotApi, signer: PolkadotSigner) => {
2220
return tool(async ({ amount, to, chain }: z.infer<typeof transferToolSchema>) => {
2321
return executeTool<TransferToolResult>(
2422
ToolNames.TRANSFER_NATIVE,
2523
async () => {
26-
const api = getApiForChain(apis, chain)
24+
const api = polkadotApi.getApi(chain as KnownChainId)
2725
const formattedAddress = validateAndFormatMultiAddress(to, chain as KnownChainId)
2826
const parsedAmount = parseUnits(amount, getDecimalsByChainId(chain))
2927
const tx = await submitTxWithPolkadotSigner(

packages/llm/src/utils/tools.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Api, KnownChainId } from "@polkadot-agent-kit/common"
1+
import type { KnownChainId } from "@polkadot-agent-kit/common"
22
import { convertAddress, toMultiAddress } from "@polkadot-agent-kit/core"
33
import type { MultiAddress } from "@polkadot-api/descriptors"
44

@@ -12,18 +12,6 @@ import { ChainNotAvailableError, ErrorCodes, InvalidAddressError, isAnyToolError
1212
*/
1313
const generateToolCallId = (prefix: string): string => `${prefix}_${Date.now()}`
1414

15-
export const getApiForChain = (
16-
apis: Map<KnownChainId, Api<KnownChainId>>,
17-
chain: string
18-
): Api<KnownChainId> => {
19-
const api = apis.get(chain as KnownChainId)
20-
if (!api) {
21-
const availableChains = Array.from(apis.keys())
22-
throw new ChainNotAvailableError(chain, availableChains)
23-
}
24-
return api
25-
}
26-
2715
export const validateAndFormatAddress = (address: string, chain: KnownChainId): string => {
2816
const formattedAddress = convertAddress(address, chain)
2917
if (!formattedAddress) {

packages/sdk/src/api.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import type {
2-
AgentConfig,
3-
Api,
4-
KnownChainId
5-
} from "@polkadot-agent-kit/common"
1+
import type { AgentConfig, Api, KnownChainId } from "@polkadot-agent-kit/common"
62
import { getAllSupportedChains, getChainById } from "@polkadot-agent-kit/common"
73
import type { IPolkadotApi } from "@polkadot-agent-kit/core"
84
import { PolkadotApi } from "@polkadot-agent-kit/core"

0 commit comments

Comments
 (0)