# How to use the libraries

## Deployments

List of deployed Registries per network can be found [here](/zerolabs-tokenization-module/domain-definitions/smart-contracts/certificate-registry/deployments.md).

## Installation

```bash
npm i @zero-labs/smart-contracts
```

## Setup

```typescript
import { CertificateContracts } from '@zero-labs/smart-contracts';

...

const provider = new providers.JsonRpcProvider('https://volta-rpc.energyweb.org'); // Replace with your RPC node of choice
const signer = new Wallet('0xabc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc1', provider); // Replace with your private key for deployment


const certificateContracts = new CertificateContracts(signer);
await certificateContracts.deploy(
    1 // (OPTIONAL): Replace with your desired topic of choice, otherwise the next available topic will be assigned to you
);

const {
    registry, // Registry smart contract
    batches, // Batches smart contract
    topic // Topic under which the certificates would be minted
} = certificateContracts;    
```

## Usage

### Creating an empty batch

```typescript
await batches.createBatches(1);
```

### Setting an redemption statement for a batch

```typescript
await batches.setRedemptionStatement(
    batchId,
    "REDEMPTION_STATEMENT_ID-123",
    "https://test.uri/1"
)
```

### Minting tokens

```typescript
import { encodeMetadata } from '@zero-labs/smart-contracts';
import { BigNumber } from 'ethers';
import { DateTime } from 'luxon';

await batches.mint(
    '1', // Batch ID
    ['0xd46aC0Bc23dB5e8AfDAAB9Ad35E9A3bA05E092E8'], // Mint TO
    [1e6], // Amount
    [encodeMetadata({
        generator: {
            id: "123",
            name: "Ime",
            energySource: "SOLAR",
            region: "EU",
            country: "HR",
            capacity: BigNumber.from(1e9),
            commissioningDate: BigNumber.from(
                DateTime.fromISO("2017-05-15").valueOf()
            ),
        },
        generationStartTime: BigNumber.from(
            DateTime.fromISO("2022-01-01").valueOf()
        ),
        generationEndTime: BigNumber.from(DateTime.fromISO("2022-01-31").valueOf()),
        productType: "I-REC",
        data: '',
    })]
);
```

### Transferring the tokens

```typescript
import { utils } from 'ethers';

await registry.safeTransferFrom(
    '0xD173313A51f8fc37BcF67569b463abd89d81844f', // FROM
    '0xd46aC0Bc23dB5e8AfDAAB9Ad35E9A3bA05E092E8', // TO
    '1', // CERTIFICATE ID
    1e6, // AMOUNT
    utils.solidityPack(['uint256'], [TransactionType.TRANSFER]), // Needed as metadata to differentiate the call as a transfer
);
```

### Claiming the tokens

```typescript
import { encodeClaimData } from '@zero-labs/smart-contracts';

await registry.safeTransferAndClaimFrom(
    '0xD173313A51f8fc37BcF67569b463abd89d81844f', // FROM
    '0xd46aC0Bc23dB5e8AfDAAB9Ad35E9A3bA05E092E8', // TO
    '1', // CERTIFICATE ID
    1e6, // AMOUNT
    '', // Additional claiming metadata, unused for now
    encodeClaimData({
        beneficiary: "Test beneficiary",
        region: "string",
        countryCode: "string",
        periodStartDate: "string",
        periodEndDate: "string",
        purpose: "string",
        consumptionEntityID: "string",
        proofID: "string",
    })
);
```


---

# 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.zerolabs.green/zerolabs-tokenization-module/developer-tools/how-to-use-the-libraries.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.
