Claim UBI (Viem/Wagmi)

The Claim SDK enables developers to integrate Universal Basic Income (UBI) claiming functionality into their applications, allowing users to claim GoodDollars (G$) on supported blockchain networks like Celo and Fuse. Built to work seamlessly with the GoodDollar protocol, it relies on the Identity SDK for whitelisting checks and supports both Wagmi (for React) and Viem (for non-React) environments.

Installation

To use the Claim SDK, install the @goodsdks/citizen-sdk package, which includes the necessary components:

npm install @goodsdks/citizen-sdk

Or with Yarn:

yarn add @goodsdks/citizen-sdk

Available Methods

The ClaimSDK class provides the following methods:

  • checkEntitlement(pClient?: PublicClient): Promise<bigint>

  • claim(): Promise<TransactionReceipt | any>

  • nextClaimTime(): Promise<Date>

  • getDailyStats(): Promise<{ claimers: bigint; amount: bigint }>

Refer to the package documentation for detailed information on each method, including parameters and return types. <todo: add link after PR merge>

Using the Wagmi SDK

The Claim SDK integrates with Wagmi for React applications, leveraging the useIdentitySDK hook for easy setup. Below is an example of initializing the SDK and performing basic operations like checking eligibility and claiming UBI.

import { useAccount, usePublicClient, useWalletClient } from 'wagmi';
import { useIdentitySDK } from '@goodsdks/identity-sdk/wagmi-sdk';
import { ClaimSDK } from '@goodsdks/identity-sdk/viem-claim-sdk';

const ClaimComponent = () => {
  const { address } = useAccount();
  const publicClient = usePublicClient();
  const { data: walletClient } = useWalletClient();
  const identitySDK = useIdentitySDK('production');

  if (!address || !publicClient || !walletClient || !identitySDK) {
    return <div>Loading...</div>;
  }

  const claimSDK = new ClaimSDK({
    account: address,
    publicClient,
    walletClient,
    identitySDK,
    env: 'production',
  });

  const checkEntitlement = async () => {
    try {
      const entitlement = await claimSDK.checkEntitlement();
      console.log('Entitlement:', entitlement.toString());
    } catch (error) {
      console.error('Entitlement check failed:', error);
    }
  };

  const claimUBI = async () => {
    try {
      await claimSDK.claim();
      console.log('Claim successful');
    } catch (error) {
      console.error('Claim failed:', error);
    }
  };

  return (
    <div>
      <button onClick={checkEntitlement}>Check Entitlement</button>
      <button onClick={claimUBI}>Claim UBI</button>
    </div>
  );
};

For a more comprehensive example, including state management and user feedback, see <link to demo app>

Using the Viem SDK

For non-React environments or backend services, the Viem-based Claim SDK offers a straightforward way to interact with the UBI Scheme Contract. The ClaimSDK.init the method simplifies initialization.

import { PublicClient, WalletClient } from 'viem';
import { IdentitySDK } from '@goodsdks/identity-sdk/viem-identity-sdk';
import { ClaimSDK } from '@goodsdks/identity-sdk/viem-claim-sdk';

const publicClient = new PublicClient({ /* configuration */ });
const walletClient = new WalletClient({ /* configuration */ });
const identitySDK = new IdentitySDK(publicClient, walletClient, 'production');

const claimSDK = await ClaimSDK.init({
  publicClient,
  walletClient,
  identitySDK,
  env: 'production',
});

try {
  const entitlement = await claimSDK.checkEntitlement();
  console.log('Entitlement:', entitlement.toString());
} catch (error) {
  console.error('Entitlement check failed:', error);
}

try {
  await claimSDK.claim();
  console.log('Claim successful');
} catch (error) {
  console.error('Claim failed:', error);
}

Additional methods like nextClaimTime and getDailyStats are detailed in the GitHub README.

References

Last updated

Was this helpful?