Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazeeh21 committed Nov 29, 2024
1 parent 75729cc commit 2556487
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
29 changes: 10 additions & 19 deletions airdrop/src/hooks/useGetAirdropData.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

import { useQuery } from "@tanstack/react-query";
import { AbstractAddress } from "fuels";
import { useUploadAirdropDataParams } from "./useUploadAirdropData";
import { PinataSDK } from "pinata";
import { PINATA_JWT } from "../lib";
import { useUploadAirdropDataParams } from "./useUploadAirdropData";

export type ContractIdData = Array<useUploadAirdropDataParams>;

Expand All @@ -15,20 +13,8 @@ export const pinata = new PinataSDK({
export const useGetAirdropData = () => {
return useQuery<ContractIdData>({
queryKey: ["airdropData"],
// @ts-expect-error - revisit this later
queryFn: async () => {
queryFn: async (): Promise<ContractIdData> => {
try {
const options = {
method: "GET",
headers: {
Authorization: `Bearer ${PINATA_JWT}`,
},
};

const metaData = JSON.stringify({
name: "AirdropData",
});

const ipfsData = (await pinata.listFiles()).filter(
(file) => file.metadata.name === "AirdropData"
);
Expand All @@ -46,11 +32,16 @@ export const useGetAirdropData = () => {
{ airdropData }
);

if (airdropData) return airdropData;
if (airdropData) {
return airdropData as unknown as ContractIdData;
}
return [];
} catch (error) {
console.error("Error while fetching contractIds from ipfs in useGetAirdropContractId: ", error);
return [];
console.error(
"Error while fetching contractIds from ipfs in useGetAirdropContractId: ",
error
);
throw error;
}
},
});
Expand Down
20 changes: 14 additions & 6 deletions airdrop/src/hooks/useUploadAirdropData.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import { PINATA_JWT } from "../lib";
import { AbstractAddress } from "fuels";
import { getTruncatedAddress } from "@/lib/utils";
import { useMutation } from "@tanstack/react-query";
import { AbstractAddress } from "fuels";
import toast from "react-hot-toast";
import { getTruncatedAddress } from "@/lib/utils";
import { PINATA_JWT } from "../lib";


export type useUploadAirdropDataParams = {
contractId: AbstractAddress | string;
recipients: Array<{ address: string; amount: bigint }>;
};

interface PinataFetchOptions {
method: string;
body: FormData;
headers: {
pinata_api_key?: string;
pinata_secret_api_key?: string;
Authorization: string;
};
}

export const useUploadAirdropData = () => {
const mutation = useMutation({
mutationFn: async ({
contractId,
recipients,
}: useUploadAirdropDataParams) => {
// Custom replacer function to handle BigInt values
// @ts-expect-error
const replacer = (key: string, value: any) => {
return typeof value === "bigint" ? value.toString() : value;
};
Expand All @@ -37,7 +46,7 @@ export const useUploadAirdropData = () => {
const blob = new Blob([airdropData], { type: "application/json" });
formData.append("file", blob, "airdropData.json");

const fetchOptions = {
const fetchOptions: PinataFetchOptions = {
method: "POST",
body: formData,
headers: {
Expand All @@ -49,7 +58,6 @@ export const useUploadAirdropData = () => {

const res = await fetch(
"https://api.pinata.cloud/pinning/pinFileToIPFS",
// @ts-expect-error will fix it once the build succeeds
fetchOptions
);
const resData = await res.json();
Expand Down

0 comments on commit 2556487

Please sign in to comment.