From 403a64bfc984f7bc07cfe1babf2dd8580c6f71b1 Mon Sep 17 00:00:00 2001 From: fukaoi Date: Wed, 23 Oct 2024 10:44:43 +0900 Subject: [PATCH] updated --- app/components/parts/ImageFileUploadUI.tsx | 3 ++ app/routes/token.tsx | 11 +++-- app/types/index.ts | 2 +- app/utils/mint-token.ts | 55 ++++++++++++---------- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/app/components/parts/ImageFileUploadUI.tsx b/app/components/parts/ImageFileUploadUI.tsx index 432bf96..2351161 100644 --- a/app/components/parts/ImageFileUploadUI.tsx +++ b/app/components/parts/ImageFileUploadUI.tsx @@ -53,6 +53,9 @@ const ImageFileUploadUI: FC = ({ const reader = new FileReader(); const file = e.target.files?.[0]; + if (!file) { + throw new Error("Not found file object"); + } file.arrayBuffer().then((buffer) => { const genericFile = createGenericFile(new Uint8Array(buffer), file.name); setGenericFileBuffer(genericFile); diff --git a/app/routes/token.tsx b/app/routes/token.tsx index e430166..f6cfe61 100644 --- a/app/routes/token.tsx +++ b/app/routes/token.tsx @@ -105,9 +105,6 @@ const Token = () => { cluster: data.cluster, customClusterUrl: data.customClusterUrl, }); - if (!genericFile) { - setErrorModal({ open: true, message: "Please Image Upload" }); - } try { data.file = genericFile!; @@ -118,7 +115,11 @@ const Token = () => { } catch (error) { setBtnState({ title: "Submit", isDisabled: false }); setLoading({ isLoading: false, message: "" }); - setErrorModal({ open: true, message: (error as Error).message }); + const errorMessage = + (error as Error).message.length <= 1 + ? "An error occurred" + : (error as Error).message; + setErrorModal({ open: true, message: errorMessage }); console.error("# mintToken: ", error); } }; @@ -187,7 +188,7 @@ const Token = () => { )} diff --git a/app/types/index.ts b/app/types/index.ts index 71b0e3b..0d55834 100644 --- a/app/types/index.ts +++ b/app/types/index.ts @@ -9,7 +9,7 @@ export type TokenMetadata = { symbol: string; imagePreview?: string; metadataJsonUrl?: string; - file: GenericFile; + file?: GenericFile; totalSupply: number; decimals: number; }; diff --git a/app/utils/mint-token.ts b/app/utils/mint-token.ts index 7dd2a5b..462d1e6 100644 --- a/app/utils/mint-token.ts +++ b/app/utils/mint-token.ts @@ -41,7 +41,7 @@ export const mintToken = async ( console.debug("# rpc url: ", rpcUrl); umi.use(walletAdapterIdentity(walletAdapter)); umi.use(mplTokenMetadata()); - umi.use(irysUploader({ timeout: 60000, priceMultiplier: 1.2 })); + umi.use(irysUploader({ timeout: 60000, priceMultiplier: 1.1 })); const mint = generateSigner(umi); const token = findAssociatedTokenPda(umi, { mint: mint.publicKey, @@ -49,35 +49,40 @@ export const mintToken = async ( tokenProgramId: SPL_TOKEN_2022_PROGRAM_ID, }); - console.log("#umi debug: ", umi); + let jsonUrl = ""; + if (metadata.file) { + callbackHandle?.("Image Uploading"); - callbackHandle?.("Image Uploading"); + const genericFile = createGenericFile( + metadata.file.buffer, + metadata.file.displayName, + { + contentType: metadata.file.contentType + ? metadata.file.contentType + : "image/png", + } + ); + const uploadedImageUrl = await umi.uploader.upload([genericFile]); + const imageUrl = fetchGatewayUrl(metadata.cluster, uploadedImageUrl[0]); + console.debug("# image url: ", imageUrl); + callbackHandle?.("Metadata Uploading"); - const genericFile = createGenericFile( - metadata.file.buffer, - metadata.file.displayName, - { - contentType: metadata.file.contentType - ? metadata.file.contentType - : "image/png", - } - ); - const uploadedImageUrl = await umi.uploader.upload([genericFile]); - const imageUrl = fetchGatewayUrl(metadata.cluster, uploadedImageUrl[0]); - console.debug("# image url: ", imageUrl); - callbackHandle?.("Metadata Uploading"); + const uploadedJsonUrl = await umi.uploader.uploadJson({ + name: metadata.name, + symbol: metadata.symbol, + image: imageUrl, + }); + jsonUrl = fetchGatewayUrl(metadata.cluster, uploadedJsonUrl); + console.debug("# json url: ", jsonUrl); + } else if (metadata.metadataJsonUrl) { + jsonUrl = metadata.metadataJsonUrl; + } else { + throw new Error("No file or metadataJsonUrl provided"); + } - const uploadedJsonUrl = await umi.uploader.uploadJson({ - name: metadata.name, - symbol: metadata.symbol, - image: imageUrl, - }); - const jsonUrl = fetchGatewayUrl(metadata.cluster, uploadedJsonUrl); - console.debug("# json url: ", jsonUrl); callbackHandle?.("Minting SPL Token"); const transaction = transactionBuilder() - .add(setComputeUnitLimit(umi, { units: 80000 })) .add(setComputeUnitPrice(umi, { microLamports: 50000 })) .add( createV1(umi, { @@ -107,7 +112,7 @@ export const mintToken = async ( const { blockhash, lastValidBlockHeight } = await umi.rpc.getLatestBlockhash(); const res = await transaction.sendAndConfirm(umi, { - send: { maxRetries: 5, commitment: "finalized", skipPreflight: true }, + send: { maxRetries: 5, commitment: "confirmed", skipPreflight: true }, confirm: { strategy: { type: "blockhash",