From f9a06d9ae6e85c297d1a071c6b53ae23a28bace9 Mon Sep 17 00:00:00 2001 From: Maxwell Fortney Date: Tue, 10 Dec 2024 00:35:09 -0500 Subject: [PATCH] tweak --- .../src/methods/getBuyListingTransaction.ts | 49 ++++++++++++++ .../plugins/tensor/src/methods/getNftInfo.ts | 20 ++++++ .../packages/plugins/tensor/src/parameters.ts | 6 -- .../packages/plugins/tensor/src/tools.ts | 65 ++----------------- 4 files changed, 76 insertions(+), 64 deletions(-) create mode 100644 typescript/packages/plugins/tensor/src/methods/getBuyListingTransaction.ts create mode 100644 typescript/packages/plugins/tensor/src/methods/getNftInfo.ts diff --git a/typescript/packages/plugins/tensor/src/methods/getBuyListingTransaction.ts b/typescript/packages/plugins/tensor/src/methods/getBuyListingTransaction.ts new file mode 100644 index 000000000..3ed04964c --- /dev/null +++ b/typescript/packages/plugins/tensor/src/methods/getBuyListingTransaction.ts @@ -0,0 +1,49 @@ +import type { SolanaWalletClient } from "@goat-sdk/core"; +import { getNftInfo } from "./getNftInfo"; +import type { getBuyListingTransactionResponseSchema } from "../parameters"; +import type { z } from "zod"; +import { deserializeTxResponseToInstructions } from "../utils/deserializeTxResponseToInstructions"; +import type { Connection } from "@solana/web3.js"; + +export async function getBuyListingTransaction({ + walletClient, + mintHash, + apiKey, + connection, +}: { walletClient: SolanaWalletClient; mintHash: string; apiKey: string; connection: Connection }) { + const nftInfo = await getNftInfo({ mintHash, apiKey }); + + const price = nftInfo.listing?.price; + const owner = nftInfo.owner; + + if (!price || !owner) { + throw new Error(`No listing found for ${mintHash}`); + } + + const queryParams = new URLSearchParams({ + buyer: walletClient.getAddress(), + mint: mintHash, + owner, + maxPrice: price, + blockhash: "11111111111111111111111111111111", + }); + + let data: z.infer; + try { + const response = await fetch(`https://api.mainnet.tensordev.io/api/v1/tx/buy?${queryParams.toString()}`, { + headers: { + "Content-Type": "application/json", + "x-tensor-api-key": apiKey, + }, + }); + + data = (await response.json()) as z.infer; + console.log(data); + } catch (error) { + throw new Error(`Failed to get buy listing transaction: ${error}`); + } + + const { versionedTransaction, instructions } = await deserializeTxResponseToInstructions(connection, data); + const lookupTableAddresses = versionedTransaction.message.addressTableLookups.map((lookup) => lookup.accountKey); + return { versionedTransaction, instructions, lookupTableAddresses }; +} diff --git a/typescript/packages/plugins/tensor/src/methods/getNftInfo.ts b/typescript/packages/plugins/tensor/src/methods/getNftInfo.ts new file mode 100644 index 000000000..02ca754e1 --- /dev/null +++ b/typescript/packages/plugins/tensor/src/methods/getNftInfo.ts @@ -0,0 +1,20 @@ +import type { z } from "zod"; +import type { getNftInfoResponseSchema } from "../parameters"; + +export async function getNftInfo({ mintHash, apiKey }: { mintHash: string; apiKey: string }) { + let nftInfo: z.infer; + try { + const response = await fetch(`https://api.mainnet.tensordev.io/api/v1/mint?mints=${mintHash}`, { + headers: { + "Content-Type": "application/json", + "x-tensor-api-key": apiKey, + }, + }); + + nftInfo = (await response.json()) as z.infer; + } catch (error) { + throw new Error(`Failed to get NFT info: ${error}`); + } + + return nftInfo[0]; +} diff --git a/typescript/packages/plugins/tensor/src/parameters.ts b/typescript/packages/plugins/tensor/src/parameters.ts index 0d19a5c08..d406fd0f9 100644 --- a/typescript/packages/plugins/tensor/src/parameters.ts +++ b/typescript/packages/plugins/tensor/src/parameters.ts @@ -49,12 +49,6 @@ export const getNftInfoResponseSchema = z.array( }), ); -export const getBuyListingTransactionParametersSchema = z.object({ - mintHash: z.string(), - owner: z.string(), - maxPrice: z.string(), -}); - export const getBuyListingTransactionResponseSchema = z.object({ txs: z.array( z.object({ diff --git a/typescript/packages/plugins/tensor/src/tools.ts b/typescript/packages/plugins/tensor/src/tools.ts index 0698b5b3c..44ea84753 100644 --- a/typescript/packages/plugins/tensor/src/tools.ts +++ b/typescript/packages/plugins/tensor/src/tools.ts @@ -2,14 +2,9 @@ import type { SolanaWalletClient } from "@goat-sdk/core"; import type { DeferredTool } from "@goat-sdk/core"; import type { Connection } from "@solana/web3.js"; -import type { z } from "zod"; -import { - getBuyListingTransactionParametersSchema, - type getBuyListingTransactionResponseSchema, - getNftInfoParametersSchema, - type getNftInfoResponseSchema, -} from "./parameters"; -import { deserializeTxResponseToInstructions } from "./utils/deserializeTxResponseToInstructions"; +import { getNftInfoParametersSchema } from "./parameters"; +import { getNftInfo } from "./methods/getNftInfo"; +import { getBuyListingTransaction } from "./methods/getBuyListingTransaction"; export function getTools({ apiKey, @@ -19,61 +14,15 @@ export function getTools({ name: "get_nft_info", description: "Gets information about a Solana NFT, from the Tensor API", parameters: getNftInfoParametersSchema, - method: async (walletClient, parameters) => { - let nftInfo: z.infer; - try { - const response = await fetch( - `https://api.mainnet.tensordev.io/api/v1/mint?mints=${parameters.mintHash}`, - { - headers: { - "Content-Type": "application/json", - "x-tensor-api-key": apiKey, - }, - }, - ); - - nftInfo = (await response.json()) as z.infer; - } catch (error) { - throw new Error(`Failed to get NFT info: ${error}`); - } - - return nftInfo[0]; - }, + method: async (walletClient, parameters) => getNftInfo({ mintHash: parameters.mintHash, apiKey }), }; const buyListingTool: DeferredTool = { name: "get_buy_listing_transaction", description: "Gets a transaction to buy an NFT from a listing from the Tensor API", - parameters: getBuyListingTransactionParametersSchema, - method: async (walletClient, parameters) => { - let data: z.infer; - - const queryParams = new URLSearchParams({ - buyer: walletClient.getAddress(), - mintHash: parameters.mintHash, - owner: parameters.owner, - maxPrice: parameters.maxPrice, - }); - - try { - const response = await fetch(`https://api.mainnet.tensordev.io/api/v1/buy?${queryParams.toString()}`, { - headers: { - "Content-Type": "application/json", - "x-tensor-api-key": apiKey, - }, - }); - - data = (await response.json()) as z.infer; - } catch (error) { - throw new Error(`Failed to get buy listing transaction: ${error}`); - } - - const { versionedTransaction, instructions } = await deserializeTxResponseToInstructions(connection, data); - const lookupTableAddresses = versionedTransaction.message.addressTableLookups.map( - (lookup) => lookup.accountKey, - ); - return { versionedTransaction, instructions, lookupTableAddresses }; - }, + parameters: getNftInfoParametersSchema, + method: async (walletClient, parameters) => + getBuyListingTransaction({ walletClient, mintHash: parameters.mintHash, apiKey, connection }), }; return [getNftInfoTool, buyListingTool];