Skip to content

Commit

Permalink
fix: increment package versions for moon-react to 2.0.35 and moon-sdk…
Browse files Browse the repository at this point in the history
… to 2.0.14; add useOpensea hook for integration with Opensea SDK
  • Loading branch information
ewhal committed Jan 7, 2025
1 parent e50f48f commit 9311fd3
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/moon-react/package.json
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",
Expand Down
1 change: 1 addition & 0 deletions packages/moon-react/src/hooks/integrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from "./useAllora";
export * from "./useCookiefun";
export * from "./useMakeFun";
export * from "./useFinancialDatasets";
export * from "./useOpensea";
214 changes: 214 additions & 0 deletions packages/moon-react/src/hooks/integrations/useOpensea.ts
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,
};
};
2 changes: 1 addition & 1 deletion packages/moon-sdk/package.json
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",
Expand Down
8 changes: 8 additions & 0 deletions packages/moon-sdk/src/moon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions packages/moon-sdk/src/services/MoonAPIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class MoonAPIService {
"Leverager",
"Lifi",
"Odos",
"Opensea",
"Onramper",
"Thorswap",
"Aave",
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 9311fd3

Please sign in to comment.