Skip to content

Commit

Permalink
Merge pull request #308 from fukaoi/feature/checkbox-upload-image
Browse files Browse the repository at this point in the history
updated
  • Loading branch information
fukaoi authored Oct 23, 2024
2 parents 1054d57 + 403a64b commit a6362ef
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
3 changes: 3 additions & 0 deletions app/components/parts/ImageFileUploadUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const ImageFileUploadUI: FC<ImageFileUploadUIProps> = ({

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);
Expand Down
11 changes: 6 additions & 5 deletions app/routes/token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand All @@ -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);
}
};
Expand Down Expand Up @@ -187,7 +188,7 @@ const Token = () => {
<MetadataJsonUrlTextField
control={control}
name="metadataJsonUrl"
rules={validationRules.url}
// rules={validationRules.description}
/>
)}
</Paper>
Expand Down
2 changes: 1 addition & 1 deletion app/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type TokenMetadata = {
symbol: string;
imagePreview?: string;
metadataJsonUrl?: string;
file: GenericFile;
file?: GenericFile;
totalSupply: number;
decimals: number;
};
Expand Down
55 changes: 30 additions & 25 deletions app/utils/mint-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,48 @@ 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,
owner: umi.identity.publicKey,
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, {
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit a6362ef

Please sign in to comment.