# Data Sharing

## Overview

This guide introduces how to use the **BAVO NETWORK SDK** with examples to help you get started.

## Preparations

* **Node.js**: Version 18 or above
* **MetaMask**: Required if using Ethereum or Holesky chains
  * Ensure you have enough ETH in your wallet to cover gas and computation fees.
* **EverPay**: Ensure you have enough ETH in EverPay to cover storage fees (see SDK documentation for more details).
* **ArConnect**: Required if using the AO chain
  * Ensure you have enough wAR (Wrapped AR) in your wallet.

## How to Run the Demo

1.  **Clone the repository**:

    ```bash
    bashCopy codegit clone https://github.com/pado-labs/bavo-network-sdk.git
    cd pado-network-sdk
    cd demo
    ```
2.  **Install the packages**:

    ```bash
    bashCopy codenpm install
    ```
3.  **Run the demo**:

    ```bash
    bashCopy codenpm run dev
    ```

    The demo will be accessible at [http://localhost:5173](http://localhost:5173).

#### Usage

**Installation**

Install the BAVO SDK via npm:

```bash
bashCopy codenpm install --save @bavolabs/bavo-network-sdk
```

**Initialize Client**

**For AO Chain:**

Ensure ArConnect is installed in Chrome and that you have enough AR.

```javascript
javascriptCopy codeconst wallet = window.arweaveWallet;
const bavoNetworkClient = new BavoNetworkContractClient('ao', wallet, 'arweave');
```

**For Ethereum or Holesky Chains:**

Ensure MetaMask is installed in Chrome and you have enough ETH for gas and computation fees, as well as ETH in EverPay for storage.

```javascript
javascriptCopy codeconst wallet = window.ethereum;
const bavoNetworkClient = new BavoNetworkContractClient('holesky', wallet, 'arseeding');
```

#### Data Provider

**Upload Data**

1.  **Prepare the data** in Uint8Array format:

    ```javascript
    javascriptCopy codeconst data = new Uint8Array(fileContent);
    ```
2.  **Tag the data** (e.g., filename and suffix):

    ```javascript
    javascriptCopy codeconst dataTag = {'name': 'test', 'suffix': 'txt'};
    ```
3. **Set the price for the data**:
   *   For **Holesky or Ethereum**:

       ```javascript
       javascriptCopy codeconst priceInfo = {
           price: 1_000_000_000_000, // 0.0000001 ETH
           symbol: 'ETH'
       };
       ```
   *   For **AO** (using wAR):

       ```javascript
       javascriptCopy codeconst priceInfo = {
           price: '200000000', // 0.0002 wAR
           symbol: 'wAR'
       };
       ```
4.  **Define data permissions** (optional):

    ```javascript
    javascriptCopy codeconst dataPermissions = [];
    ```
5.  **Set the encryption schema** (optional):

    ```javascript
    javascriptCopy codeconst schema = {
      t: 2, // Minimum number of nodes
      n: 3  // Total number of nodes
    };
    ```
6.  **Upload the data**:

    ```javascript
    javascriptCopy codeconst dataId = await bavoNetworkClient.uploadData(data, dataTag, priceInfo, dataPermissions, schema);
    ```

    If successful, you will receive a `dataId` to query your uploaded data.

**Retrieve Balance and Withdraw Tokens**

Data providers earn token rewards when their data is purchased. You can check and withdraw the balance as follows:

1.  **Get balance**:

    ```javascript
    javascriptCopy codeconst balance = await bavoNetworkClient.getBalance(address, 'ETH');
    console.log(balance.locked.toString());
    console.log(balance.free.toString());
    ```
2.  **Withdraw tokens**:

    ```javascript
    javascriptCopy codeconst transaction = await bavoNetworkClient.withdrawToken(metamaskAddress, 'ETH', balance.free);
    console.log(transaction);
    ```

#### Data User

**Generate Key Pair**

You must generate a public-private key pair for encryption and decryption:

```javascript
javascriptCopy codeimport { Utils } from "@bavolabs/bavo-network-sdk";

const keyInfo = await new Utils().generateKey();
```

**Submit Task**

To submit a task, you need the `dataId` provided by the Data Provider.

```javascript
javascriptCopy codeconst taskId = await bavoNetworkClient.submitTask(TaskType.DATA_SHARING, userDataId, keyInfo.pk);
```

This will return a `taskId` to use when retrieving the result.

**Get Task Result**

Retrieve the task result:

```javascript
javascriptCopy codeconst data = await bavoNetworkClient.getTaskResult(taskId, keyInfo.sk);
```

If successful, you will receive the data in `Uint8Array` format for further processing.

#### Conclusion

Using the **BAVO NETWORK SDK**, developers can easily interact with the network to upload data, submit tasks, retrieve results, and manage balances.
