mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-22 07:36:44 +00:00
Implement getAssetsByAuthority tool
This commit is contained in:
@@ -60,6 +60,7 @@ import driftUserAccountInfoAction from "./drift/driftUserAccountInfo";
|
||||
import deriveDriftVaultAddressAction from "./drift/deriveVaultAddress";
|
||||
import updateDriftVaultDelegateAction from "./drift/updateDriftVaultDelegate";
|
||||
import getAssetAction from "./metaplex/getAsset";
|
||||
import getAssetByAuthorityAction from "./metaplex/getAssetByAuthority";
|
||||
|
||||
export const ACTIONS = {
|
||||
WALLET_ADDRESS_ACTION: getWalletAddressAction,
|
||||
@@ -125,6 +126,7 @@ export const ACTIONS = {
|
||||
DERIVE_DRIFT_VAULT_ADDRESS_ACTION: deriveDriftVaultAddressAction,
|
||||
UPDATE_DRIFT_VAULT_DELEGATE_ACTION: updateDriftVaultDelegateAction,
|
||||
GET_ASSET_ACTION: getAssetAction,
|
||||
GET_ASSET_BY_AUTHORITY_ACTION: getAssetByAuthorityAction,
|
||||
};
|
||||
|
||||
export type { Action, ActionExample, Handler } from "../types/action";
|
||||
|
||||
70
src/actions/metaplex/getAssetByAuthority.ts
Normal file
70
src/actions/metaplex/getAssetByAuthority.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { Action } from "../../types/action";
|
||||
import { SolanaAgentKit } from "../../agent";
|
||||
import { z } from "zod";
|
||||
import { get_assets_by_authority } from "../../tools/metaplex";
|
||||
|
||||
const getAssetByAuthorityAction: Action = {
|
||||
name: "GET_ASSET_BY_AUTHORITY",
|
||||
similes: [
|
||||
"fetch assets by authority",
|
||||
"retrieve assets by authority",
|
||||
"get assets by authority address",
|
||||
"fetch authority assets",
|
||||
],
|
||||
description: `Fetch a list of assets owned by a specific address using the Metaplex DAS API.`,
|
||||
examples: [
|
||||
[
|
||||
{
|
||||
input: {
|
||||
authorityAddress: "mRdta4rc2RtsxEUDYuvKLamMZAdW6qHcwuq866Skxxv",
|
||||
limit: 10,
|
||||
},
|
||||
output: {
|
||||
status: "success",
|
||||
message: "Assets retrieved successfully",
|
||||
result: [
|
||||
// Example asset details
|
||||
{
|
||||
name: "Example Asset 1",
|
||||
symbol: "EXA1",
|
||||
uri: "https://example.com/asset1.json",
|
||||
},
|
||||
{
|
||||
name: "Example Asset 2",
|
||||
symbol: "EXA2",
|
||||
uri: "https://example.com/asset2.json",
|
||||
},
|
||||
],
|
||||
},
|
||||
explanation: "Fetch a list of assets owned by a specific address",
|
||||
},
|
||||
],
|
||||
],
|
||||
schema: z.object({
|
||||
authorityAddress: z.string().min(1, "Authority address is required"),
|
||||
sortBy: z
|
||||
.object({
|
||||
sortBy: z.enum(["created", "updated", "recentAction", "none"]),
|
||||
sortDirection: z.enum(["asc", "desc"]),
|
||||
})
|
||||
.optional(),
|
||||
limit: z.number().optional(),
|
||||
page: z.number().optional(),
|
||||
before: z.string().optional(),
|
||||
after: z.string().optional(),
|
||||
}),
|
||||
handler: async (
|
||||
agent: SolanaAgentKit,
|
||||
input: z.infer<typeof getAssetByAuthorityAction.schema>,
|
||||
) => {
|
||||
const result = await get_assets_by_authority(agent, input);
|
||||
|
||||
return {
|
||||
status: "success",
|
||||
message: "Assets retrieved successfully",
|
||||
result,
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
export default getAssetByAuthorityAction;
|
||||
@@ -99,6 +99,7 @@ import {
|
||||
updateVaultDelegate,
|
||||
get_token_balance,
|
||||
get_asset,
|
||||
get_assets_by_authority,
|
||||
} from "../tools";
|
||||
import {
|
||||
Config,
|
||||
@@ -116,7 +117,10 @@ import {
|
||||
HeliusWebhookIdResponse,
|
||||
HeliusWebhookResponse,
|
||||
} from "../types";
|
||||
import { DasApiAsset } from "@metaplex-foundation/digital-asset-standard-api";
|
||||
import {
|
||||
DasApiAsset,
|
||||
GetAssetsByAuthorityRpcInput,
|
||||
} from "@metaplex-foundation/digital-asset-standard-api";
|
||||
|
||||
/**
|
||||
* Main class for interacting with Solana blockchain
|
||||
@@ -827,4 +831,9 @@ export class SolanaAgentKit {
|
||||
async getAsset(assetId: string): Promise<DasApiAsset> {
|
||||
return get_asset(this, assetId);
|
||||
}
|
||||
async getAssetsByAuthority(
|
||||
params: GetAssetsByAuthorityRpcInput,
|
||||
): Promise<DasApiAsset[]> {
|
||||
return get_assets_by_authority(this, params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,8 +114,9 @@ import {
|
||||
SolanaUpdateDriftVaultTool,
|
||||
SolanaWithdrawFromDriftAccountTool,
|
||||
SolanaWithdrawFromDriftVaultTool,
|
||||
SolanaGetAssetTool,
|
||||
SolanaGetAssetsByAuthorityTool,
|
||||
} from "./index";
|
||||
import { SolanaGetAssetTool } from "./metaplex/get_asset";
|
||||
|
||||
export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
return [
|
||||
@@ -210,5 +211,6 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
new SolanaWithdrawFromDriftAccountTool(solanaKit),
|
||||
new SolanaWithdrawFromDriftVaultTool(solanaKit),
|
||||
new SolanaGetAssetTool(solanaKit),
|
||||
new SolanaGetAssetsByAuthorityTool(solanaKit),
|
||||
];
|
||||
}
|
||||
|
||||
39
src/langchain/metaplex/get_asset_by_authority.ts
Normal file
39
src/langchain/metaplex/get_asset_by_authority.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { Tool } from "langchain/tools";
|
||||
import { SolanaAgentKit } from "../../agent";
|
||||
|
||||
export class SolanaGetAssetsByAuthorityTool extends Tool {
|
||||
name = "solana_get_assets_by_authority";
|
||||
description = `Fetch a list of assets owned by a specific address using the Metaplex DAS API.
|
||||
|
||||
Inputs (input is a JSON string):
|
||||
authorityAddress: string, eg "N4f6zftYsuu4yT7icsjLwh4i6pB1zvvKbseHj2NmSQw" (required)
|
||||
sortBy: { sortBy: "created" | "updated" | "recentAction" | "none", sortDirection: "asc" | "desc" } (optional)
|
||||
limit: number (optional)
|
||||
page: number (optional)
|
||||
before: string (optional)
|
||||
after: string (optional)`;
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
protected async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
const result = await this.solanaKit.getAssetsByAuthority(parsedInput);
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "Assets retrieved successfully",
|
||||
result,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
export * from "./deploy_collection";
|
||||
export * from "./mint_nft";
|
||||
export * from "./deploy_token";
|
||||
export * from "./get_asset";
|
||||
export * from "./get_asset_by_authority";
|
||||
|
||||
@@ -19,11 +19,8 @@ export async function get_asset(
|
||||
try {
|
||||
const endpoint = agent.connection.rpcEndpoint;
|
||||
const umi = createUmi(endpoint).use(dasApi());
|
||||
const assetPublicKey = publicKey(assetId);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
return await umi.rpc.getAsset(assetPublicKey);
|
||||
return await umi.rpc.getAsset(publicKey(assetId));
|
||||
} catch (error: any) {
|
||||
console.error("Error retrieving asset: ", error.message);
|
||||
throw new Error(`Asset retrieval failed: ${error.message}`);
|
||||
|
||||
15
src/tools/metaplex/get_assets_by_authority.ts
Normal file
15
src/tools/metaplex/get_assets_by_authority.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { SolanaAgentKit } from "../../agent";
|
||||
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
|
||||
import {
|
||||
dasApi,
|
||||
GetAssetsByAuthorityRpcInput,
|
||||
} from "@metaplex-foundation/digital-asset-standard-api";
|
||||
|
||||
export async function get_assets_by_authority(
|
||||
agent: SolanaAgentKit,
|
||||
params: GetAssetsByAuthorityRpcInput,
|
||||
) {
|
||||
const umi = createUmi(agent.connection.rpcEndpoint).use(dasApi());
|
||||
const assets = await umi.rpc.getAssetsByAuthority(params);
|
||||
return assets.items;
|
||||
}
|
||||
@@ -2,3 +2,4 @@ export * from "./deploy_collection";
|
||||
export * from "./mint_nft";
|
||||
export * from "./deploy_token";
|
||||
export * from "./get_asset";
|
||||
export * from "./get_assets_by_authority";
|
||||
|
||||
Reference in New Issue
Block a user