From 9311fd3bce3087b20d3ca5c8165f08fd47e9a7bb Mon Sep 17 00:00:00 2001 From: ewhal Date: Tue, 7 Jan 2025 13:47:04 +1000 Subject: [PATCH] fix: increment package versions for moon-react to 2.0.35 and moon-sdk to 2.0.14; add useOpensea hook for integration with Opensea SDK --- packages/moon-react/package.json | 2 +- .../src/hooks/integrations/index.ts | 1 + .../src/hooks/integrations/useOpensea.ts | 214 ++++++++++++++++++ packages/moon-sdk/package.json | 2 +- packages/moon-sdk/src/moon.ts | 8 + .../moon-sdk/src/services/MoonAPIService.ts | 9 + 6 files changed, 234 insertions(+), 2 deletions(-) create mode 100644 packages/moon-react/src/hooks/integrations/useOpensea.ts diff --git a/packages/moon-react/package.json b/packages/moon-react/package.json index b9cc0b2..ae1a50e 100644 --- a/packages/moon-react/package.json +++ b/packages/moon-react/package.json @@ -1,6 +1,6 @@ { "name": "@moonup/moon-react", - "version": "2.0.37", + "version": "2.0.35", "license": "MIT", "author": "0xEwhal ", "type": "module", diff --git a/packages/moon-react/src/hooks/integrations/index.ts b/packages/moon-react/src/hooks/integrations/index.ts index 1097b5f..969c9e5 100644 --- a/packages/moon-react/src/hooks/integrations/index.ts +++ b/packages/moon-react/src/hooks/integrations/index.ts @@ -14,3 +14,4 @@ export * from "./useAllora"; export * from "./useCookiefun"; export * from "./useMakeFun"; export * from "./useFinancialDatasets"; +export * from "./useOpensea"; diff --git a/packages/moon-react/src/hooks/integrations/useOpensea.ts b/packages/moon-react/src/hooks/integrations/useOpensea.ts new file mode 100644 index 0000000..26fc746 --- /dev/null +++ b/packages/moon-react/src/hooks/integrations/useOpensea.ts @@ -0,0 +1,214 @@ +import { useMoonAuth } from "@/context"; +import { useMoonTransaction } from "@/hooks"; +import type { + CreateCollectionOfferData, + CreateOfferData, + GetCollectionData, + GetCollectionParams, + GetNftData, + GetNftParams, + OpenSeaCollectionInputBody, + OpenSeaNFTInputBody, + UnwrapWethData, + UnwrapWethPayload, + WrapEthData, + WrapEthPayload, +} from "@moonup/moon-api"; + +import { useQuery } from "@tanstack/react-query"; +import { useCallback } from "react"; +import { + useAccount, + useChainId, + useSendTransaction, + useSwitchChain, +} from "wagmi"; + +export const useOpensea = () => { + const { handleTransaction } = useMoonTransaction(); + const { moon } = useMoonAuth(); + const chainId = useChainId(); + const { switchChain } = useSwitchChain(); + const { isConnected, address } = useAccount(); + const { sendTransactionAsync } = useSendTransaction(); + + const getOpenseaSDK = () => { + const openseaSDK = moon?.getOpenseaSDK(); + if (!openseaSDK) throw new Error("Moon Opensea SDK not initialized"); + return openseaSDK; + }; + + const prepareTransaction = (account: string, transaction: any) => { + if (isConnected && address === account) { + return { + ...transaction, + broadcast: false, + dryrun: true, + }; + } + return transaction; + }; + + const handleWagmiTransaction = async (transactionData: any) => { + try { + if (isConnected && address === transactionData.transaction.from) { + if (chainId !== Number.parseInt(transactionData.transaction.chainId)) { + await switchChain({ + chainId: Number.parseInt(transactionData.transaction.chainId), + }); + } + await sendTransactionAsync({ + to: transactionData.transaction.to, + data: transactionData.transaction.data, + value: BigInt(transactionData.transaction.value), + chainId: Number.parseInt(transactionData.transaction.chain_id), + }); + } + return transactionData; + } catch (error) { + console.error("handleWagmiTransaction: Error: ", error); + return transactionData; + } + }; + + const createCollectionOffer = useCallback( + async (payload: { + accountName: string; + data: OpenSeaCollectionInputBody; + }): Promise => { + return handleTransaction("createCollectionOffer", async () => { + const openseaSDK = getOpenseaSDK(); + const preparedTransaction = prepareTransaction( + payload.accountName, + payload.data, + ); + const response = await openseaSDK.createCollectionOffer( + payload.accountName, + preparedTransaction, + ); + return handleWagmiTransaction(response.data); + }); + }, + [moon, isConnected, address, chainId, switchChain, sendTransactionAsync], + ); + + const createOffer = useCallback( + async (payload: { + accountName: string; + data: OpenSeaNFTInputBody; + }): Promise => { + return handleTransaction("createOffer", async () => { + const openseaSDK = getOpenseaSDK(); + const preparedTransaction = prepareTransaction( + payload.accountName, + payload.data, + ); + const response = await openseaSDK.createOffer( + payload.accountName, + preparedTransaction, + ); + return handleWagmiTransaction(response.data); + }); + }, + [moon, isConnected, address, chainId, switchChain, sendTransactionAsync], + ); + + const getCollection = useCallback( + async (params: GetCollectionParams): Promise => { + return handleTransaction("getCollection", async () => { + const openseaSDK = getOpenseaSDK(); + const response = await openseaSDK.getCollection(params); + return response.data; + }); + }, + [moon, isConnected, address, chainId, switchChain, sendTransactionAsync], + ); + + const getNft = useCallback( + async (params: GetNftParams): Promise => { + return handleTransaction("getNft", async () => { + const openseaSDK = getOpenseaSDK(); + const response = await openseaSDK.getNft(params); + return response.data; + }); + }, + [moon, isConnected, address, chainId, switchChain, sendTransactionAsync], + ); + + const useGetCollection = (params: GetCollectionParams) => { + return useQuery({ + queryKey: ["openseaGetCollection", params], + queryFn: async () => { + const openseaSDK = getOpenseaSDK(); + const response = await openseaSDK.getCollection(params); + return response.data; + }, + enabled: !!moon, + }); + }; + + const useGetNft = (params: GetNftParams) => { + return useQuery({ + queryKey: ["openseaGetNft", params], + queryFn: async () => { + const openseaSDK = getOpenseaSDK(); + const response = await openseaSDK.getNft(params); + return response.data; + }, + enabled: !!moon, + }); + }; + + const unwrapWeth = useCallback( + async (payload: { + accountName: string; + data: UnwrapWethPayload; + }): Promise => { + return handleTransaction("unwrapWeth", async () => { + const openseaSDK = getOpenseaSDK(); + const preparedTransaction = prepareTransaction( + payload.accountName, + payload.data, + ); + const response = await openseaSDK.unwrapWeth( + payload.accountName, + preparedTransaction, + ); + return handleWagmiTransaction(response.data); + }); + }, + [moon, isConnected, address, chainId, switchChain, sendTransactionAsync], + ); + + const wrapEth = useCallback( + async (payload: { + accountName: string; + data: WrapEthPayload; + }): Promise => { + return handleTransaction("wrapEth", async () => { + const openseaSDK = getOpenseaSDK(); + const preparedTransaction = prepareTransaction( + payload.accountName, + payload.data, + ); + const response = await openseaSDK.wrapEth( + payload.accountName, + preparedTransaction, + ); + return handleWagmiTransaction(response.data); + }); + }, + [moon, isConnected, address, chainId, switchChain, sendTransactionAsync], + ); + + return { + createCollectionOffer, + createOffer, + useGetCollection, + useGetNft, + unwrapWeth, + wrapEth, + getCollection, + getNft, + }; +}; diff --git a/packages/moon-sdk/package.json b/packages/moon-sdk/package.json index c9d0d95..5c3d551 100644 --- a/packages/moon-sdk/package.json +++ b/packages/moon-sdk/package.json @@ -1,6 +1,6 @@ { "name": "@moonup/moon-sdk", - "version": "2.0.13", + "version": "2.0.14", "author": "0xEwhal ", "type": "commonjs", "main": "./dist/index.js", diff --git a/packages/moon-sdk/src/moon.ts b/packages/moon-sdk/src/moon.ts index 5331f49..772fae6 100644 --- a/packages/moon-sdk/src/moon.ts +++ b/packages/moon-sdk/src/moon.ts @@ -1538,6 +1538,14 @@ export class MoonSDK extends EventEmitter implements IMoonSDK { return this.moonAPIService.getFinancialDatasetsSDK(); } + /** + * Retrieves the Opensea SDK instance from the Moon API service. + * @returns {MoonAPI.Opensea} The Opensea SDK instance. + */ + getOpenseaSDK(): MoonAPI.Opensea { + return this.moonAPIService.getOpenseaSDK(); + } + // Event listener methods /** * Registers an event listener for the specified event. diff --git a/packages/moon-sdk/src/services/MoonAPIService.ts b/packages/moon-sdk/src/services/MoonAPIService.ts index ca721fc..b679a73 100644 --- a/packages/moon-sdk/src/services/MoonAPIService.ts +++ b/packages/moon-sdk/src/services/MoonAPIService.ts @@ -72,6 +72,7 @@ export class MoonAPIService { "Leverager", "Lifi", "Odos", + "Opensea", "Onramper", "Thorswap", "Aave", @@ -432,4 +433,12 @@ export class MoonAPIService { public getFinancialDatasetsSDK(): MoonAPI.FinancialDatasets { return this.getSDK("FinancialDatasets") as MoonAPI.FinancialDatasets; } + + /** + * Retrieves the Opensea SDK instance. + * @returns {MoonAPI.Opensea} The Opensea SDK instance. + */ + public getOpenseaSDK(): MoonAPI.Opensea { + return this.getSDK("Opensea") as MoonAPI.Opensea; + } }