{"id":24539659,"url":"https://github.com/marcusmolchany/tokengate","last_synced_at":"2025-09-03T22:33:05.063Z","repository":{"id":41869616,"uuid":"454602969","full_name":"marcusmolchany/tokengate","owner":"marcusmolchany","description":"js module for token gating on ethereum","archived":false,"fork":false,"pushed_at":"2022-04-25T16:56:46.000Z","size":173,"stargazers_count":29,"open_issues_count":2,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-08-05T06:18:38.322Z","etag":null,"topics":["crypto","ethereum","ethers","gate","token","tokengate","web3"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/tokengate","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marcusmolchany.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-02T00:56:07.000Z","updated_at":"2024-04-26T14:01:49.000Z","dependencies_parsed_at":"2022-08-11T19:50:13.322Z","dependency_job_id":null,"html_url":"https://github.com/marcusmolchany/tokengate","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/marcusmolchany/tokengate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcusmolchany%2Ftokengate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcusmolchany%2Ftokengate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcusmolchany%2Ftokengate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcusmolchany%2Ftokengate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcusmolchany","download_url":"https://codeload.github.com/marcusmolchany/tokengate/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcusmolchany%2Ftokengate/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273523047,"owners_count":25120859,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-03T02:00:09.631Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crypto","ethereum","ethers","gate","token","tokengate","web3"],"created_at":"2025-01-22T17:15:12.759Z","updated_at":"2025-09-03T22:33:05.013Z","avatar_url":"https://github.com/marcusmolchany.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Token Gate\n\n[![NPM version][npm-image]][npm-url]\n[![License][license-image]][license-url]\n[![Downloads][downloads-image]][downloads-url]\n\njs module for token gating on ethereum\n\n## Usage\n\nExported methods\n\n```ts\n// unsecure client-side method to check if an address meets a token threshold.\n// example usage: enable/disable a button based on a connected wallet's address.\nasync unsecureClientSideTokenGate({\n  balanceOfThreshold, // {number} balance of tokens that define token gate threshold\n  contractAddress, // {string} erc20, erc721, or erc1155 contract address\n  signerOrProvider, // {Provider} wallet web3 provider\n  userAddress, // {string} user address\n});\n\n// sign an arbitrary message with a connected wallet\nasync clientSideSignMessage({\n  messageToSign, // {string} text message to be signed by user's connected wallet\n  signer, // {Signer} wallet web3 signer\n});\n\n// THIS IS NOT DEFINED YET. IT WILL ERROR IF YOU TRY TO USE IT.\nasync clientSideSignTypedData({});\n\n// server-side code for a secure client-side/server-side token gate flow.\n// takes a message signed by a client, recovers the address, and checks\n// if that address meets a token threshold.\nasync secureServerSideTokenGate({\n  address, // {string} address to compare to address recovered from signedMessage\n  balanceOfThreshold, // {number} balance of tokens that define token gate threshold\n  contractAddress, // {string} erc20, erc721, or erc1155 contract address\n  message, // {string} clear text message that was signed by user's wallet\n  provider, // {Provider} server-side web3 provider\n  signedMessage, // {string} signed message by user's wallet\n});\n```\n\nClient-side / Server-side example for erc20/721/777\n\n```ts\n////////////////////////////////////////////////////////////////////\n//////////////////////// client-side react /////////////////////////\n////////////////////////////////////////////////////////////////////\nimport axios from \"axios\";\nimport { useEffect, useState } from \"react\";\nimport { clientSideSignMessage, unsecureClientSideTokenGate } from \"tokengate\";\n\nconst balanceOfThreshold = 1; /* require 1 blitmap nft */\nconst contractAddress =\n  \"0x8d04a8c79ceb0889bdd12acdf3fa9d207ed3ff63\"; /* blitmap */\n\n// this must match the message that is signed on the server-side.\n// ideally the server-side issues this message as a challange.\nconst message = \"sign this secret message\";\n\nfunction TokenGateButton({ signer }) {\n  const [isAllowed, setIsAllowed] = useState\u003cboolean\u003e(false);\n\n  useEffect(() =\u003e {\n    const asyncEffect = async () =\u003e {\n      const userAddress = await signer.getAddress();\n      const _isAllowed = await unsecureClientSideTokenGate({\n        balanceOfThreshold,\n        contractAddress,\n        signerOrProvider: signer,\n        userAddress,\n      });\n\n      setIsAllowed(_isAllowed);\n    };\n\n    asyncEffect();\n  }, [signer]);\n\n  const onClick = async () =\u003e {\n    const userAddress = await signer.getAddress();\n    const signedMessage = await clientSideSignMessage({\n      messageToSign: message,\n      signer,\n    });\n\n    try {\n      const resp = await axios.post(\"/api/token-gate\", {\n        address: userAddress,\n        signedMessage,\n      });\n      setIsAllowed(resp.data.isAllowed);\n    } catch (e) {\n      console.error(\"something went wrong\");\n      setIsAllowed(false);\n    }\n  };\n\n  return (\n    \u003cbutton disabled={!isAllowed} onClick={onClick}\u003e\n      Access token gated content\n    \u003c/button\u003e\n  );\n}\n\n////////////////////////////////////////////////////////////////////\n///////////////////////// server-side api //////////////////////////\n////////////////////////////////////////////////////////////////////\nimport { ethers } from \"ethers\";\nimport { secureServerSideTokenGate } from \"tokengate\";\n\nconst balanceOfThreshold = 1; /* require 1 blitmap nft */\nconst contractAddress =\n  \"0x8d04a8c79ceb0889bdd12acdf3fa9d207ed3ff63\"; /* blitmap */\n\n// this must match the message that is signed on the client-side.\n// ideally the backend issues this message as a challange including a\n// human-readable message, nonce, timestamp, domain and chain information.\nconst message = \"sign this secret message\";\n\napp.post(\"/api/token-gate\", (req, res) =\u003e {\n  // optional `networkId` param so that you can use other networks.\n  // defaults to `1` which is mainnet\n  const { address, networkId = 1, signedMessage } = req.body;\n\n  // create a web3 provider\n  const provider = new ethers.providers.InfuraProvider(networkId);\n\n  const isAllowed = await secureServerSideTokenGate({\n    address,\n    balanceOfThreshold,\n    contractAddress,\n    message,\n    provider,\n    signedMessage,\n  });\n\n  // handle the success/failure case however you want\n\n  return res.json({ isAllowed });\n});\n```\n\nClient-side / Server-side example for erc1555\n\n```ts\n// the balanceOf method for erc1155 has a different api than other tokens. it allows you to\n// check the balance of a particular user for a particular token id. for tokengate, you must\n// provide two new arguments: `tokenId`, and `tokenStandard`.\n// `tokenId` will be the number id of the token you are checking the balance of\n// `tokenStandard` will be `erc1155`\n\n// client-side\n// pass `tokenId` and `tokenStandard` when doing a client-side token gate check\nconst _isAllowed = await unsecureClientSideTokenGate({\n  balanceOfThreshold,\n  contractAddress,\n  signerOrProvider: signer,\n  tokenId: 2,\n  tokenStandard: \"erc1155\",\n  userAddress,\n});\n\n// server-side\n// pass `tokenId` and `tokenStandard` to your api handler\nconst isAllowed = await secureServerSideTokenGate({\n  address,\n  balanceOfThreshold,\n  contractAddress,\n  message,\n  provider,\n  signedMessage,\n  tokenId: 2,\n  tokenStandard: \"erc1155\",\n});\n```\n\n[npm-image]: https://img.shields.io/npm/v/tokengate.svg?style=for-the-badge\u0026labelColor=161c22\n[npm-url]: https://www.npmjs.com/package/tokengate\n[license-image]: https://img.shields.io/npm/l/tokengate.svg?style=for-the-badge\u0026labelColor=161c22\n[license-url]: /LICENSE\n[downloads-image]: https://img.shields.io/npm/dm/tokengate.svg?style=for-the-badge\u0026labelColor=161c22\n[downloads-url]: https://www.npmjs.com/package/tokengate\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcusmolchany%2Ftokengate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcusmolchany%2Ftokengate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcusmolchany%2Ftokengate/lists"}