# Merkle Claim Flow

## Create and Claim Cred Merkle Type Process

This document explains the process of creating and claiming a Cred Merkle type, based on the provided sequence diagrams and smart contract code.

### Creation Process

#### Overview

The creation process involves interaction between four main components:

1. Contract
2. Frontend
3. Backend
4. Arweave (decentralized storage)

<figure><img src="/files/IfFbeA6BIN2aWmfMqFoH" alt=""><figcaption></figcaption></figure>

#### Step-by-Step Creation Process

1. **Request Signature**: Frontend initiates the process by requesting a signature.
2. **Upload CSV**: Backend uploads a CSV file with addresses to Arweave.
3. **Create and Upload Merkle Tree**: Backend creates a Merkle tree from the CSV data and uploads it to Arweave.
4. **Upload Cred Metadata**: Backend uploads the credential metadata to Arweave.
5. **Create and Response Signature**: Backend creates a signature and sends it to the Frontend.
6. **Call "createCred" Method**: Frontend calls the `createCred` method on the Contract.

### Claim Process

#### Overview

The claim process allows eligible users to claim their credentials using Merkle proofs.

<figure><img src="/files/P64mrh8AgLan47E2L3DV" alt=""><figcaption></figcaption></figure>

#### Step-by-Step Claim Process

1. **Request Proof**: Frontend requests a proof for claiming.
2. **Fetch Cred Metadata**: Backend retrieves credential metadata from Arweave.
3. **Fetch Merkle Tree**: Backend fetches the Merkle tree from Arweave.
4. **Load Merkle Tree & Find Proof**: Backend processes the Merkle tree and finds the proof:

   ```javascript
   javascriptCopyconst json = await (await fetch(cred.verification.merkle_tree)).json();
   const tree = loadMerkleTree(json);

   let proof, data;
   for (const [i, v] of tree.entries()) {
     if (v[0].toLowerCase() == minter) {
       proof = tree.getProof(i) as Hex[string][];
       data = v[1];
     }
   }

   if (!proof) {
     return new Response("Not Eligible", { status: 400 });
   }
   ```
5. **Response Proof**: Backend sends the proof back to the Frontend.
6. **Call "claim" Method**: Frontend calls the `merkleClaim` method on the Contract.

### Key Points of the MerkleClaim Function:

1. **Input Parameters**:
   * `proof_`: Merkle proof
   * `encodeData_`: Encoded data containing minter address, referral address, and art ID
   * `mintArgs_`: Minting arguments including quantity and image URI
   * `leafPart_`: Additional data for the Merkle leaf
2. **Verification**:
   * Checks if the minter address is valid and the verification type is "MERKLE"
   * Verifies the Merkle proof using `MerkleProofLib.verifyCalldata`
3. **Claim Processing**:
   * Validates and updates the claim state
   * Processes the claim, which likely includes minting the NFT
4. **Event Emission**:
   * Emits an `ArtClaimedData` event with relevant information

#### Conclusion

The Cred Merkle type creation and claim process provides a secure and efficient way to distribute credentials to eligible addresses. The use of Merkle trees allows for gas-efficient verification on-chain, while storing larger datasets off-chain on Arweave ensures data availability and reduces on-chain storage costs.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.phi.box/explore-phi/developers/merkle-claim-flow.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
