mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-16 07:36:45 +00:00
feat: get tps
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
||||
registerDomain,
|
||||
lendAsset,
|
||||
getLendingDetails,
|
||||
getTPS,
|
||||
} from "../tools";
|
||||
import { CollectionOptions, LuloDepositAssetMint } from "../types";
|
||||
import { DEFAULT_OPTIONS } from "../constants";
|
||||
@@ -94,4 +95,8 @@ export class SolanaAgentKit {
|
||||
async fetchLendingDetails(LULO_API_KEY: string) {
|
||||
return getLendingDetails(this, LULO_API_KEY);
|
||||
}
|
||||
|
||||
async getTPS() {
|
||||
return getTPS(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,6 +239,24 @@ export class SolanaFetchLendingDetailsTool extends Tool {
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaTPSCalculatorTool extends Tool {
|
||||
name = "solana_get_tps";
|
||||
description = "Get the current TPS of the Solana network";
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
async _call(_input: string): Promise<string> {
|
||||
try {
|
||||
const tps = await this.solanaKit.getTPS();
|
||||
return `Solana (mainnet-beta) current transactions per second: ${tps}`;
|
||||
} catch (error: any) {
|
||||
return `Error fetching TPS: ${error.message}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Updated createSolanaTools function
|
||||
export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
return [
|
||||
@@ -253,5 +271,6 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
new SolanaGetWalletAddressTool(solanaKit),
|
||||
new SolanaLendAssetTool(solanaKit),
|
||||
new SolanaFetchLendingDetailsTool(solanaKit),
|
||||
new SolanaTPSCalculatorTool(solanaKit),
|
||||
];
|
||||
}
|
||||
|
||||
17
src/tools/get_tps.ts
Normal file
17
src/tools/get_tps.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { SolanaAgentKit } from "../index";
|
||||
|
||||
export async function getTPS(agent: SolanaAgentKit): Promise<number> {
|
||||
const perfSamples = await agent.connection.getRecentPerformanceSamples();
|
||||
|
||||
if (
|
||||
!perfSamples.length ||
|
||||
!perfSamples[0]?.numTransactions ||
|
||||
!perfSamples[0]?.samplePeriodSecs
|
||||
) {
|
||||
throw new Error("No performance samples available");
|
||||
}
|
||||
|
||||
const tps = perfSamples[0].numTransactions / perfSamples[0].samplePeriodSecs;
|
||||
|
||||
return tps;
|
||||
}
|
||||
@@ -7,3 +7,4 @@ export * from "./transfer";
|
||||
export * from "./trade";
|
||||
export * from "./register_domain";
|
||||
export * from "./lend";
|
||||
export * from "./get_tps";
|
||||
|
||||
Reference in New Issue
Block a user