Skip to main content

Examples of common flows

Here are common architectural flows you may need to integrate into your application. These include methods to gather prerequisite data for each backend functionality described earlier.

Get a campaign's CID

To obtain a campaign's IPFS CID, you can:

  1. Check the create events emitted by the Merkle factory

  2. Use the Sablier indexers to query for that information.

For approach "B", run the following query against the official endpoints:

query getCampaignData($campaignId: String!){
campaign(id: $campaignId){
id
lockup
root
ipfsCID
aggregateAmount
totalRecipients
}
}

Check eligibility for an address

To check if an address is eligible, use the /api/eligibility route provided by the merkle-api backend.

Steps

  1. Get the campaign CID (see the example above)
  2. Call the /api/eligibility route using the CID and wallet address

Read more in the dedicated route documentation within the /api/eligibility section of the functionality page.

Alternatively, you can check eligibility by searching for the address in the list stored within the IPFS file from step 1. You'll still need to download the file first using its CID.

Get the tokenId after a claim

After someone claims, you may want to show them a preview of the stream or its NFT. To do this, you'll need the tokenId (or streamId) related to that user's claim.

To obtain a tokenId linked to a claim, you can:

  1. Listen to the claim method and the Claim event emitted by the Merkle contract instance

  2. Use the Sablier Indexers to query for that information.

For approach "B", run the following query against the official endpoints (ensure the address is lowercase):

query getClaimForRecipient($campaignId: String!, $recipient: String) {
actions(where: { campaign: $campaignId, category: Claim, claimRecipient: $recipient }) {
campaign {
id
lockup
}
claimTokenId
claimRecipient
claimIndex
}
}

Bonus: Stream NFT

To get the Stream NFT, use the same query as option "B" to retrieve the lockup contract (where the Stream NFT is issued) and its tokenId. With these, you can call the tokenURI method, which returns the SVG code of the onchain NFT.

Extract the SVG tags

The actual SVG tags are encoded inside the tokenURI method response. You can feed this blob to an HTML img tag or render the SVG tags directly. To extract this code in plain format (not base64), run the following JavaScript code to decode the response:

const toPart = output.split("data:application/json;base64,").pop();
const toString = Buffer.from(toPart || "", "base64").toString("utf-8");
const toJSON = JSON.parse(toString);

const blob = _.get(toJSON, "image")?.split("data:image/svg+xml;base64,")[1];
const toSVG = Buffer.from(blob || "", "base64").toString("utf-8");

Check if a user claimed their stream

To check if a user has already claimed their stream from a campaign, you can:

  1. Call the hasClaimed method from the Merkle contract instance

  2. Use the Sablier Indexers to query for that information.

Approach A

For approach "A", perform the following actions:

  1. Get the campaign's CID, Lockup contract address, and user address (for the first items, see the Get a campaign's CID example above)
  2. Check user eligibility (see Eligibility example above) and extract their index
  3. Call the hasClaimed method inside the lockup contract using the index from step 2
note

A user's index represents their assigned order number in the eligibility list. We use this value to generate a proof if the recipient is eligible and to register their claim directly in the contract.

Approach B

For approach "B", run the same query as in the Get the tokenId after a claim example. If the query returns results, the user has claimed their stream.

tip

A missing claim could also mean the user wasn't eligible initially. We recommend checking for eligibility alongside any existing claim checks.