Check Proof of Identity (POI) Status

This guide provides an example of how to check if a user is POI active by verifying that the user's ProofOfIdentity balance is more than 0 and that the user is not suspended.

Example Code

import { ProofOfIdentityContract as ProofOfIdentityContractSDK } from '@haven1/blockchain-sdk/contract';
import { useReadContracts } from 'wagmi';
import { Address } from 'viem';

type CheckPOIStatusParameters = {
  address: Address;
  contractAddress: Address;
  chainId: number;
};

export const checkPOIStatus = async ({
  address,
  contractAddress,
  chainId,
}: CheckPOIStatusParameters) => {
  const { data: poiResult, isLoading, isError } = useReadContracts({
    contracts: [
      {
        address: contractAddress,
        chainId,
        abi: ProofOfIdentityContractSDK.abi,
        functionName: 'balanceOf',
        args: [address],
      },
      {
        address: contractAddress,
        chainId,
        abi: ProofOfIdentityContractSDK.abi,
        functionName: 'isSuspended',
        args: [address],
      },
    ],
  });

  if (isLoading) {
    console.log('Loading POI status...');
    return;
  }

  if (isError) {
    console.error('Error fetching POI status');
    return;
  }

  const balance = poiResult?.[0]?.result;
  const isSuspended = poiResult?.[1]?.result;

  if (balance > 0 && !isSuspended) {
    console.log('User is POI active');
  } else {
    console.log('User is not POI active');
  }
};

Usage

const params = {
  address: '0xUserAddress',
  contractAddress: '0xContractAddress',
  chainId: Haven1Testnet,
};

checkPOIStatus(params);

This example demonstrates how to use the checkPOIStatus function to verify if a user is POI active by checking their balance and suspension status.

Example Code using useProofOfIdentity wagmi wrap Hook

import { useProofOfIdentity } from '@haven1/wagmi-sdk/hook';
import { useEffect } from 'react';

const CheckPOIStatusWithHook = ({ address, contractAddress, chainId }) => {
  const { data, isLoading, isError } = useProofOfIdentity({
    address,
    contractAddress,
    chainId,
  });

  useEffect(() => {
    if (isLoading) {
      console.log('Loading POI status...');
      return;
    }

    if (isError) {
      console.error('Error fetching POI status');
      return;
    }

    const { balance, isSuspended } = data;

    if (balance > 0 && !isSuspended) {
      console.log('User is POI active');
    } else {
      console.log('User is not POI active');
    }
  }, [data, isLoading, isError]);

  return null;
};

Usage

const params = {
  address: '0xUserAddress',
  contractAddress: '0xContractAddress',
  chainId: Haven1Testnet,
};

<CheckPOIStatusWithHook {...params} />;

This example demonstrates how to use the useProofOfIdentity hook to verify if a user is POI active by checking their balance and suspension status.

Last updated