Skip to content
LogoLogo

Check Token Role

token.hasRole

Checks if an account has a specific role for a TIP-20 token. Learn more about token roles

Usage

import {  } from './viem.config'
 
const  = await ..({
  : '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb',
  : 'issuer',
  : '0x20c0000000000000000000000000000000000001',
})
 
.('Has issuer role:', )
Has issuer role: true

Recipes

Skip Redundant Role Grants When Provisioning

Combine hasRole with token.grantRolesSync to keep operator provisioning idempotent: grant the issuer role only when the operator does not already hold it.

import {  } from './viem.config'
 
const  = '0x8ba1f109551bD432803012645Ac136ddd64DBA72'
const  = '0x20c0000000000000000000000000000000000001'
 
const  = await ..({
  : ,
  : 'issuer',
  ,
})
 
if (!)
  await ..({
    : ['issuer'],
    : ,
    ,
  })

Verify Pause Authority Before an Incident Response

Omit account to check the Client's own account, for example verifying an incident-response key holds the pause role before it halts transfers with token.pauseSync.

import {  } from './viem.config'
 
const  = '0x20c0000000000000000000000000000000000001'
 
const  = await ..({
  : 'pause',
  ,
})
 
if (!)
  throw new ('incident key is missing the pause role')
 
await ..({  })

Return Value

type ReturnType = boolean // Whether the account has the role

Parameters

account

  • Type: Account | Address
  • Default: client.account

Account (or address) to check.

role

  • Type: "defaultAdmin" | "pause" | "unpause" | "issuer" | "burnBlocked"

Role to check.

token

  • Type: Address | bigint

Token to operate on: a TIP-20 token id or a contract address.

blockNumber (optional)

  • Type: bigint

Block number to read the state from.

blockOverrides (optional)

  • Type: BlockOverrides

Block overrides to apply to the state.

blockTag (optional)

  • Type: BlockTag

Block tag to read the state from.

stateOverride (optional)

  • Type: StateOverride

State override to apply.