Guides

Check a claim from another program

Require a claim receipt in your own instruction, read a poll’s tally, or check a nullifier off chain.

Every claim leaves a receipt account at an address anyone can derive from the scope and the scope nullifier n. A program can require that receipt in its own accounts, so an instruction runs only for a scope nullifier that has claimed; and it can read a scope's count, its tally and whether it has closed. Off chain, the SDK, the CLI and the API answer the same questions from the scope's ledger.

A program that is handed an n learns from the receipt that an enrolled person claimed with it; the receipt holds no wallet and no leaf.

From a program

Anchor's declare_program! reads Once's IDL and gives your program Once's account types and address. Save once.json into your crate's idls/ folder.

rust
use anchor_lang::prelude::*;

declare_program!(once);
use once::accounts::{ClaimReceipt, Scope};

#[program]
pub mod caller {
    use super::*;

    /// Succeeds only for a scope nullifier that has claimed in `scope`.
    pub fn only_claimed(ctx: Context<OnlyClaimed>, n: [u8; 32]) -> Result<()> {
        let receipt = &ctx.accounts.receipt;
        msg!("claimed at {}", receipt.claimed_at);
        let _ = n;
        Ok(())
    }

    /// Reads a closed poll's winning option.
    pub fn read_poll(ctx: Context<ReadPoll>) -> Result<()> {
        let poll = &ctx.accounts.poll;
        require!(poll.closed, CallerError::PollOpen);
        let options = poll.options.len();
        let (winner, votes) = poll.tally[..options]
            .iter()
            .enumerate()
            .max_by_key(|(_, votes)| **votes)
            .unwrap();
        msg!("option {} won with {} votes", winner, votes);
        Ok(())
    }
}

#[derive(Accounts)]
#[instruction(n: [u8; 32])]
pub struct OnlyClaimed<'info> {
    pub scope: Account<'info, Scope>,
    #[account(
        seeds = [b"claim", scope.key().as_ref(), n.as_ref()],
        bump,
        seeds::program = once::ID,
    )]
    pub receipt: Account<'info, ClaimReceipt>,
}

#[derive(Accounts)]
pub struct ReadPoll<'info> {
    pub poll: Account<'info, Scope>,
}

#[error_code]
pub enum CallerError {
    #[msg("The poll is still open.")]
    PollOpen,
}

Account<'info, ClaimReceipt> checks that the account is owned by Once and carries the receipt's discriminator, and the seeds constraint checks it is the receipt for this scope and this n. When no one has claimed with n, the account is empty and the instruction fails before your handler runs. The scope account is checked the same way, so a caller can't pass a lookalike. The accounts reference has every field of both.

The program's address on the localnet is ByJWamQ4FndMu5Kio7mFZHKuvgavZz5bYJZKBLMziojN; declare_program! takes it from the IDL's address, so the IDL you copy decides the cluster.

While the scope is open

After a scope closes, anyone can call close_claims to close its receipts and return their rent to the relayers that paid it. A check that has to hold after the scope's window reads the receipt while the scope is open, or reads the scope's count and tally, which stay on the scope account for good.

From the SDK

createSdk reads a scope and its ledger through the API. hasClaimed pages through the scope's ledger on your side, so the API never learns which nullifier you asked about.

ts
import { createSdk } from '@once/sdk'

const once = createSdk(process.env.ONCE_API!)

// Has this scope nullifier claimed in scope 1?
const n = '036b4a2960740da25020d41c7d8ce211cb8ddf11267e53cf9e1a088225ebe700'
console.log(await once.hasClaimed('1', n))

// A poll's tally
const poll = await once.scope('3')
for (const option of poll.options) console.log(option.label, option.tally)
text
true
Slot history 51
Account diffs 24
Faster subscriptions 12

A person's own device computes its n for a scope with scopeNullifier(s, scopeId), exported by the SDK, and checks it the same way.

From the CLI

bash
$ once nullifier check 1 036b4a2960740da25020d41c7d8ce211cb8ddf11267e53cf9e1a088225ebe700
{
  "scopeId": "1",
  "claimed": true
}

once claims <scope> prints the whole ledger: each nullifier with its time, transaction signature and, for a poll, its option. The CLI reads the API at ONCE_API_URL.

From the API

bash
$ curl "$ONCE_API/v1/scopes/1/claims?limit=100"

The ledger comes in pages of up to 100; follow page.nextCursor until it is null.

What comes next

Gates, build 1 on Once's road, add a third kind of scope owned by a program. That program calls Once inside its own instruction with a person's claim proof, bound to a value it chooses such as its user's wallet, and Once records the scope nullifier or refuses a person it has already seen in that scope.