-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
6 changed files
with
234 additions
and
2 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@moonup/moon-react", | ||
"version": "2.0.37", | ||
"version": "2.0.35", | ||
"license": "MIT", | ||
"author": "0xEwhal <[email protected]>", | ||
"type": "module", | ||
|
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
214 changes: 214 additions & 0 deletions
214
packages/moon-react/src/hooks/integrations/useOpensea.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,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<CreateCollectionOfferData> => { | ||
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<CreateOfferData> => { | ||
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<GetCollectionData> => { | ||
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<GetNftData> => { | ||
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<UnwrapWethData> => { | ||
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<WrapEthData> => { | ||
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, | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@moonup/moon-sdk", | ||
"version": "2.0.13", | ||
"version": "2.0.14", | ||
"author": "0xEwhal <[email protected]>", | ||
"type": "commonjs", | ||
"main": "./dist/index.js", | ||
|
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