npm package: @primuslabs/network-js-sdk
Description: Primus Network js sdk
Author: Primus Labs
Latest version: 0.1.9

# PrimusNetwork SDK Documentation

## Overview

The PrimusNetwork SDK is a TypeScript library designed for blockchain network interaction, enabling developers to submit tasks, perform attestations, and query task statuses. It abstracts smart contract interactions through a developer-friendly API.

## Key Features

1. **SDK Initialization**: Connect to specified blockchain networks
2. **Task Submission**: Submit tasks requiring attestation to the network
3. **Attestation Execution**: Perform verification using selected nodes
4. **Status Polling**: Continuously check task status and results
5. **Balance Withdrawal**: Claim rewards from the contract

## Installation

```bash
npm install @primuslabs/network-js-sdk
# or
yarn add @primuslabs/network-js-sdk
```

## Getting Started

### 1. Initialize the SDK

```typescript
import { PrimusNetwork } from 'primus-network-sdk';
import { ethers } from 'ethers';

// Create provider (Metamask, WalletConnect, or custom node)
const provider = window.ethereum;
const primusNetwork = new PrimusNetwork();

async function initializeSdk() {
  try {
    const chainId = 84532; // Base Sepolia example
    await primusNetwork.init(provider, chainId);
    console.log('SDK initialized successfully');
  } catch (error) {
    console.error('Initialization failed:', error);
  }
}

initializeSdk();
```

### 2. Submit Tasks

```typescript
async function submitTask() {
  try {
    const params = {
      templateId: 'your_template_id', // Task template identifier
      address: '0x...', // User address
    };
    
    const result = await primusNetwork.submitTask(params);
    console.log('Task submitted:', result);
    // Result includes taskId, taskTxHash for subsequent operations
  } catch (error) {
    console.error('Submission failed:', error);
  }
}
```

### 3. Perform Attestation

```typescript
async function attestTask(taskInfo: any) {
  try {
    const params = {
      templateId: 'your_template_id', // Task template identifier
      address: '0x...', // User address
      taskId: taskInfo.taskId, // Obtain it from the return value of the submitTask method.
      taskTxHash: taskInfo.taskTxHash, // Obtain it from the return value of the submitTask method.
      attestors: taskInfo.taskAttestors, // Obtain it from the return value of the submitTask method.
      // Additional attestation parameters...
    }; // Using taskId/taskTxHash/attestors from submitTask response
    
    const results = await primusNetwork.attest(params);
    console.log('Attestation results:', results);
  } catch (error) {
    console.error('Attestation failed:', error);
  }
}
```

### 4. Verify & Poll Task Result

```typescript
async function monitorTask(taskId: string) {
  try {
    const taskResult = await primusNetwork.verifyAndPollTaskResult({
      taskId, // Obtain it from the return value of the submitTask method or the attest method.
    });
    
    console.log('TaskResult :', taskResult);
    // Process results...
  } catch (error) {
    console.error('Polling failed:', error);
  }
}
```

### 5. Withdraw Funds

```typescript
async function withdrawRewards() {
  try {
    const settledTasks = await primusNetwork.withdrawBalance();
    console.log('Withdrawal successful. Settled tasks:', settledTasks);
  } catch (error) {
    console.error('Withdrawal failed:', error);
  }
}
```

## Complete Example

```typescript
import { PrimusNetwork } from "@primuslabs/network-js-sdk";

async function main() {
  const provider = window.ethereum;
  const primusNetwork = new PrimusNetwork();
  console.log(primusNetwork.supportedChainIds); // [84532, 8453]

  try {
    // 1. Initialize
    await primusNetwork.init(provider, 84532); // The Base chain changes 84532 to 8453
    console.log("SDK initialized");

    // 2. Submit task, set TemplateID and user address.
    const submitTaskParams = {
      templateId: "YOUR_TEMPLATEID",
      address: "YOUR_USER_ADDRESS",
    };
    const submitTaskResult = await primusNetwork.submitTask(submitTaskParams);
    console.log("Task submitted:", submitTaskResult);


    // 3. Perform attestation
    const attestParams = {
      ...submitTaskParams,
      ...submitTaskResult,
      // extendedParams: JSON.stringify({ attUrlOptimization: true }), //Optional,optimization the url of attestation.
    };
    const attestResult = await primusNetwork.attest(attestParams);
    console.log('Attestation completed:', attestResult);

    // 4. Verify & poll task result
    const verifyAndPollTaskResultParams = {
      taskId: attestResult[0].taskId,
      reportTxHash: attestResult[0].reportTxHash,
    }
    const taskResult = await primusNetwork.verifyAndPollTaskResult(verifyAndPollTaskResultParams);
    console.log("Final result:", taskResult);

    // Optional withdrawal
    // const settledTaskIds = await primusNetwork.withdrawBalance();
    // console.log('Withdrawn:', settledTaskIds  );
  } catch (error) {
    console.error("Main flow error:", error);
  }
}

main();
```

## Important Notes

1. Verify `chainId` is supported by checking `SUPPORTEDCHAINIDS`
2. Initialize SDK before submitting tasks
3. Set appropriate timeouts for attestation operations
4. Implement comprehensive error handling

## Supported Networks

Check `SUPPORTEDCHAINIDS` for available networks:
- Base Mainnet
- Base Sepolia