๐Ÿ”Token-gating using badges

Token gating by Badge spec implies that only holders of Badges belonging to one or more specs are able to get through the token gate.

Finding the badge spec id

First, you need to get the id of the Badge Spec that you used to issue badges to different addresses.

You can find the Badge spec id in the url of the Badge page.

https://beta.otterspace.xyz/badges/bafyreifcjs56sk5gseb75e45rjebh4kwln3trwi6aphhacf727lkshq74e 

Fetching all the holders of a badge spec

You can use our subgraph to fetch all the badge owners of that spec

{
  badgeSpec(id: "bafyreicl3unvw6tvzjfduvrhxbfi74gsob6mpf6ekn3s2nkopqz2phtx7e") {
    id
    metadata {
      name
      description
      image
      expiresAt
    }
    totalBadgesCount
    badges {
      id
      owner
      createdAt
    }
  }
}

Does an address hold a Badge belonging to this spec?

If you already know the address of a user, you can simply check if this address owns a badge with the spec that you intend to gate. For example, see the query below. Here weโ€™re checking if the address 0x77B476429826C5ba77885D08F272d89D8F1Ed0e4 owns a Pioneer badge issued by the Otterspace Raft.

{
  badges(where: {owner: "0x77B476429826C5ba77885D08F272d89D8F1Ed0e4", spec: "bafyreicl3unvw6tvzjfduvrhxbfi74gsob6mpf6ekn3s2nkopqz2phtx7e"}) {
    id
    createdAt
    owner
    createdAt
    spec {
      id
      metadata {
        name
      }
      raft {
        id
        metadata {
          name
        }
      }
    }
  }
}

And if youโ€™d like to check if an address owns one of the badges, simply use spec_in filter

{
  badges(
    where: {owner: "0x77B476429826C5ba77885D08F272d89D8F1Ed0e4", spec_in: ["bafyreicl3unvw6tvzjfduvrhxbfi74gsob6mpf6ekn3s2nkopqz2phtx7e","bafyreia2lnu2jmr6sqqijzd3xt6fw2qriyy2vbq57zikzrkeyxfvlpqp3i"]}
  ) {
    id
    createdAt
    owner
    createdAt
    spec {
      id
      metadata {
				name
			}
      raft {
        id
        metadata {
					name 
				}
      }
    }
  }
}

Filtering out expired and revoked Badges

One thing that makes Badges different from other NFTs is that they can expire. Expired Badges shouldnโ€™t be able to be used to access spaces and resources. You can check if a Badge is expired by looking at the expiry date in the metadata.

Filtering by community

If you are building a token-gating UI, you probably want to allow an account admin to select from the Badge Specs belonging to that account.

The unique identifier of a Raft is the Token Id of an ERC721 token which is airdropped to the community during its formation. The Raft Id can be found in the communityโ€™s profile url.

https://beta.otterspace.xyz/communities/1

Last updated