# 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:

```bash
npm install @goodsdks/citizen-sdk
```

Or with Yarn:

```bash
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.

```typescript
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.

```typescript
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](https://github.com/GoodDollar).

### References

* [Viem Documentation](https://viem.sh/)
* [Wagmi Documentation](https://wagmi.sh/)
* [OpenZeppelin Contracts](https://docs.openzeppelin.com/contracts/)
* [UBISchemeV2 Contract](https://github.com/GoodDollar/GoodProtocol/blob/master/contracts/ubi/UBISchemeV2.sol)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gooddollar.org/for-developers/apis-and-sdks/ubi/claim-ubi-viem-wagmi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
