“
As a CNFT creator on Blockchain Solana, metadata updating can be a simple process. However, meeting an error that involves re-computing a Merkle Tree has left many frustrated creators. In this article,
Error
Metadata for a CNFT on Blockchain Solana. The “UpdateMatata” method is called proof. This can lead to an inconsistent state on the network and cause problems with CNFT functionality.
Here are the reference materials you use:
- [API Solana documentation] (
- [CNFT documentation on Solana] (
Code example
For demonstrative purposes, suppose we have a CNFT contract with the following code:
`Solana
Import {program} from "solar-program";
Import {updatemetAdata} from "../lib/update-metadata";
Async function Main () {
// initializes metadata
Consist metadate = wait for the GetmetAdata ();
Try {
// update the metadata
Wait for updatemetata (metadata);
} Catch (error) {
IF (error instance error && error.message.includes ("invalid root") {
// manage the error by re-calculating the root from a proof
Consist of proof = waiting for getproofforupdate ();
Const root = waiting for computeremerkletree (proof);
Try {
// update the metadata with the new root
Wait for updatemetata (metadata, root);
} Catch (error) {
// Manage any errors that appear during the metadata update
Console.error (error);
}
} Else {
Throwing error;
}
}
}
The ASYNC GETMETATAd () function {
// returns a few sample of data metadata
Return [
"
"Other information about metadata",
];
}
// help functions to generate proof and calculation of Merkle tree
GETPROFFORUPdate function () {
// generates random proof for updating metadata
Returns the new promise (resolve => settimeout (() => resolves (New Uint8array (32)), 100);
}
ASYNC FUNCT COMPATHERKLETE (proof) {
// Calculate the Merkle shaft from proof
Returns the new promise (resolve => settimeout (() => resolves (New Uint8array (256)), 200));
}
Slender ();
Solution
The root from a proof only when updating the metadata. This can be obtained by introducing a new variable that keeps track of if an update is in progress
` Solana
Import {program} from “solar-program”;
Import {updatemetAdata} from “../lib/update-metadata”;
CLASS CNTF {
private updateinprogress = false;
Private Proferatindex = 0;
async gitmetata () {
IF (this.updateinprogress) returns null; // update is in progress, do not proceed
this.proofindex ++;
Consist metadate = waiting for GetmetAdatAfromromroof ();
Return metadata;
}
async updatemetAdata (metadata) {
IF (this.updateinprogress) throws a new error (“Invalid root re -evidence”);
This.Updateinprogress = True;
Try {
// update metadata using metadata
Wait for updatemetata (metadata);
} Catch (error) {
IF (error instance error && error.message.includes (“invalid root”) {
// Manage the error by re-calculating the root from a proof
Const protectshindex = this.proofindex;
Const protect = wait getprooffromindex (proofleindex);
Try {
this.updateinprogress = false; // Reset the update flag
this.