-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
76 additions
and
64 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
typescript/packages/plugins/tensor/src/methods/getBuyListingTransaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
typescript/packages/plugins/tensor/src/methods/getNftInfo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters