Skip to content

Commit

Permalink
tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
mPaella committed Dec 10, 2024
1 parent 05e85c5 commit f9a06d9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -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<typeof getBuyListingTransactionResponseSchema>;
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<typeof getBuyListingTransactionResponseSchema>;
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 };
}
20 changes: 20 additions & 0 deletions typescript/packages/plugins/tensor/src/methods/getNftInfo.ts
Original file line number Diff line number Diff line change
@@ -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<typeof getNftInfoResponseSchema>;
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<typeof getNftInfoResponseSchema>;
} catch (error) {
throw new Error(`Failed to get NFT info: ${error}`);
}

return nftInfo[0];
}
6 changes: 0 additions & 6 deletions typescript/packages/plugins/tensor/src/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
65 changes: 7 additions & 58 deletions typescript/packages/plugins/tensor/src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<typeof getNftInfoResponseSchema>;
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<typeof getNftInfoResponseSchema>;
} 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<SolanaWalletClient> = {
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<typeof getBuyListingTransactionResponseSchema>;

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<typeof getBuyListingTransactionResponseSchema>;
} 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];
Expand Down

0 comments on commit f9a06d9

Please sign in to comment.