Skip to main content
deleted 3 characters in body
Source Link
Jimii
  • 7.7k
  • 1
  • 8
  • 22
import {
    createGenericFile,
    keypairIdentity,
    publicKey,
    Umi,
} from "@metaplex-foundation/umi";
import {
    clusterApiUrl,
    Connection,
} from "@solana/web3.js";
import {
    fetchMetadataFromSeeds,
    findMasterEditionPda,
    mplTokenMetadata,
    updateV1,
} from "@metaplex-foundation/mpl-token-metadata";
import fs, { readFileSync } from "fs";
import path from "path";

export const connection = new Connection(clusterApiUrl("devnet"));

const umi = createUmi(connection);

export const keypair = umi.eddsa.createKeypairFromSecretKey(Uint8Array.from([ <UPDATE"UPDATE WITH SECRET KEY>]KEY"));

umi.use(keypairIdentity(keypair)).use(mplTokenMetadata());

export function uint8FileData(pathName: string): Uint8Array {
    const filePath = path.join(__dirname, pathName);

    const data = fs.readFileSync(filePath);
    return data;
}

async function updateSPLTokenMD() {
    const mint = publicKey(""); // update with mint address
    const onChainMetadata = await fetchMetadataFromSeeds(umi, { mint });

    const newName = "Updated Asset Name";

    const offChainMetadata = await (await fetch(onChainMetadata.uri)).json();

    const filePath = "<PATH TO YOUR IMAGE FILE>"; // update this appropriately
    let fileData = uint8FileData(filePath);
    let file = createGenericFile(fileData, filePath, {
        contentType: "image/jpeg", // update the file MIME type if different
    });

    const [image] = await umi.uploader.upload([file]);
    const uri = await umi.uploader.uploadJson({
        ...offChainMetadata,
        image,
        name: newName,
    });

    await updateV1(umi, {
        mint,
        authority: umi.identity,
        data: { ...onChainMetadata, name: newName },
        primarySaleHappened: true,
        isMutable: true,
    }).sendAndConfirm(umi);
}

updateSPLTokenMD().then().catch();
import {
    createGenericFile,
    keypairIdentity,
    publicKey,
    Umi,
} from "@metaplex-foundation/umi";
import {
    clusterApiUrl,
    Connection,
} from "@solana/web3.js";
import {
    fetchMetadataFromSeeds,
    findMasterEditionPda,
    mplTokenMetadata,
    updateV1,
} from "@metaplex-foundation/mpl-token-metadata";
import fs, { readFileSync } from "fs";
import path from "path";

export const connection = new Connection(clusterApiUrl("devnet"));

const umi = createUmi(connection);

export const keypair = umi.eddsa.createKeypairFromSecretKey(Uint8Array.from([ <UPDATE WITH SECRET KEY>]));

umi.use(keypairIdentity(keypair)).use(mplTokenMetadata());

export function uint8FileData(pathName: string): Uint8Array {
    const filePath = path.join(__dirname, pathName);

    const data = fs.readFileSync(filePath);
    return data;
}

async function updateSPLTokenMD() {
    const mint = publicKey(""); // update with mint address
    const onChainMetadata = await fetchMetadataFromSeeds(umi, { mint });

    const newName = "Updated Asset Name";

    const offChainMetadata = await (await fetch(onChainMetadata.uri)).json();

    const filePath = "<PATH TO YOUR IMAGE FILE>"; // update this appropriately
    let fileData = uint8FileData(filePath);
    let file = createGenericFile(fileData, filePath, {
        contentType: "image/jpeg", // update the file MIME type if different
    });

    const [image] = await umi.uploader.upload([file]);
    const uri = await umi.uploader.uploadJson({
        ...offChainMetadata,
        image,
        name: newName,
    });

    await updateV1(umi, {
        mint,
        authority: umi.identity,
        data: { ...onChainMetadata, name: newName },
        primarySaleHappened: true,
        isMutable: true,
    }).sendAndConfirm(umi);
}

updateSPLTokenMD().then().catch();
import {
    createGenericFile,
    keypairIdentity,
    publicKey,
    Umi,
} from "@metaplex-foundation/umi";
import {
    clusterApiUrl,
    Connection,
} from "@solana/web3.js";
import {
    fetchMetadataFromSeeds,
    findMasterEditionPda,
    mplTokenMetadata,
    updateV1,
} from "@metaplex-foundation/mpl-token-metadata";
import fs, { readFileSync } from "fs";
import path from "path";

export const connection = new Connection(clusterApiUrl("devnet"));

const umi = createUmi(connection);

export const keypair = umi.eddsa.createKeypairFromSecretKey(Uint8Array.from("UPDATE WITH SECRET KEY"));

umi.use(keypairIdentity(keypair)).use(mplTokenMetadata());

export function uint8FileData(pathName: string): Uint8Array {
    const filePath = path.join(__dirname, pathName);

    const data = fs.readFileSync(filePath);
    return data;
}

async function updateSPLTokenMD() {
    const mint = publicKey(""); // update with mint address
    const onChainMetadata = await fetchMetadataFromSeeds(umi, { mint });

    const newName = "Updated Asset Name";

    const offChainMetadata = await (await fetch(onChainMetadata.uri)).json();

    const filePath = "<PATH TO YOUR IMAGE FILE>"; // update this appropriately
    let fileData = uint8FileData(filePath);
    let file = createGenericFile(fileData, filePath, {
        contentType: "image/jpeg", // update the file MIME type if different
    });

    const [image] = await umi.uploader.upload([file]);
    const uri = await umi.uploader.uploadJson({
        ...offChainMetadata,
        image,
        name: newName,
    });

    await updateV1(umi, {
        mint,
        authority: umi.identity,
        data: { ...onChainMetadata, name: newName },
        primarySaleHappened: true,
        isMutable: true,
    }).sendAndConfirm(umi);
}

updateSPLTokenMD().then().catch();
add umi guide as pointed out by Mike
Source Link
Jimii
  • 7.7k
  • 1
  • 8
  • 22
Source Link
Jimii
  • 7.7k
  • 1
  • 8
  • 22

The first step is to make sure that you minted the tokens with the isMutable flag set to true else the process below will not work.

To update the image you will need to update the entire off-chain metadata contained in the URI. If you are using a decentralized solution like ipfs, it'll be impossible to update the data of the hash and you'll have to read that data and create new hash.

Here's how to do so using UMI

import {
    createGenericFile,
    keypairIdentity,
    publicKey,
    Umi,
} from "@metaplex-foundation/umi";
import {
    clusterApiUrl,
    Connection,
} from "@solana/web3.js";
import {
    fetchMetadataFromSeeds,
    findMasterEditionPda,
    mplTokenMetadata,
    updateV1,
} from "@metaplex-foundation/mpl-token-metadata";
import fs, { readFileSync } from "fs";
import path from "path";

export const connection = new Connection(clusterApiUrl("devnet"));

const umi = createUmi(connection);

export const keypair = umi.eddsa.createKeypairFromSecretKey(Uint8Array.from([ <UPDATE WITH SECRET KEY>]));

umi.use(keypairIdentity(keypair)).use(mplTokenMetadata());

export function uint8FileData(pathName: string): Uint8Array {
    const filePath = path.join(__dirname, pathName);

    const data = fs.readFileSync(filePath);
    return data;
}

async function updateSPLTokenMD() {
    const mint = publicKey(""); // update with mint address
    const onChainMetadata = await fetchMetadataFromSeeds(umi, { mint });

    const newName = "Updated Asset Name";

    const offChainMetadata = await (await fetch(onChainMetadata.uri)).json();

    const filePath = "<PATH TO YOUR IMAGE FILE>"; // update this appropriately
    let fileData = uint8FileData(filePath);
    let file = createGenericFile(fileData, filePath, {
        contentType: "image/jpeg", // update the file MIME type if different
    });

    const [image] = await umi.uploader.upload([file]);
    const uri = await umi.uploader.uploadJson({
        ...offChainMetadata,
        image,
        name: newName,
    });

    await updateV1(umi, {
        mint,
        authority: umi.identity,
        data: { ...onChainMetadata, name: newName },
        primarySaleHappened: true,
        isMutable: true,
    }).sendAndConfirm(umi);
}

updateSPLTokenMD().then().catch();

update docs: https://developers.metaplex.com/token-metadata/update

umi docs: https://developers.metaplex.com/umi