# SDK Reference

## Installation&#x20;

You can install the @denota-labs/denota-sdk package with the following command:

```bash
npm install @denota-labs/denota-sdk
```

## Quickstart

The `setProvider` function initializes the library's internal state by setting up the provider, signer, account, and other necessary contracts for executing transactions. It also retrieves the corresponding contract addresses based on the chain ID. This function must be called before performing any transactions to ensure a smooth and seamless experience.

The function accepts an `ethers.Signer` and `chainId` as its input parameters, providing flexibility for developers.

```javascript
import { ethers } from 'ethers';
import { setProvider } from '@denota-labs/denota-sdk';

async function initializeProvider() {
  const provider = ethers.getDefaultProvider();
  const signer = new ethers.Wallet(YOUR_PRIVATE_KEY, provider);
  const chainId = YOUR_CHAIN_ID;

  try {
    await setProvider({ signer, chainId });
    console.log("Provider has been set successfully");
  } catch (error) {
    console.error("Error setting provider:", error);
  }
}

initializeProvider();
```

Before sending funds, use the `approveToken` function to approve a token for use in writing notas.

```javascript
import { approveToken } from '@denota-labs/denota-sdk';

async function approve() {
  const currency = 'DAI';
  const approvalAmount = 100;
  await approveToken({ currency, approvalAmount });
}

approve();
```

## Interacting with Notas

### Write

The `write` function is used to create a nota based on the provided module data.

```javascript
import { DirectPayData, write } from '@denota-labs/denota-sdk';

const directPayData: DirectPayData = {
  moduleName: "direct",
  type: "invoice",
  creditor: "0x...",
  debitor: "0x...",
};

const writeProps = {
  currency: "DAI",
  amount: 10,
  metadata: { type: "raw", notes: "Example invoice" },
  module: directPayData,
};

const result = await write(writeProps);
console.log(result);
```

Metadata can be attached in two forms. If the metadata has already been uploaded to IFPS, it can be attached to the nota:&#x20;

```typescript
const writeProps = {
  currency: "DAI",
  amount: 10,
  metadata: { type: "uploaded", ipfsHash, imageUrl },
  module: directPayData,
};
```

If the metadata hasn't been uploaded yet, it can be uploaded through the SDK:&#x20;

```typescript
const writeProps = {
  currency: "DAI",
  amount: 10,
  metadata: { 
    type: "raw", 
    notes: "Example invoice", 
    file: exampleFile, 
    tags: "logo" 
  },
  module: directPayData,
};
```

### Fund

The `fund` function is used to fund a nota based on the nota ID.

Usage:

```javascript
import { fund } from '@denota-labs/denota-sdk';

const fundProps = {
  notaId: "12345", // Replace with the actual nota ID
};

const result = await fund(fundProps);
console.log(result);
```

### Cash

The `cash` function is used to move a payment out of escrow based on the nota ID and the action type ("reversal" or "release").

```javascript
import { cash } from '@denota-labs/denota-sdk';

const cashPaymentProps = {
  notaId: "12345", // Replace with the actual nota ID
  type: "release",
};

const result = await cash(cashPaymentProps);
console.log(result);
```

### Transfer

\[Coming soon]

### Approve

\[Coming soon]

## Querying Notas

Denota Protocol provides a subgraph to query nota data using the Graph Protocol. To access Nota data, you can use the `getNotasQueryURL()` function to get the query URL and a GraphQL library like Apollo Client to perform the queries.

#### Setup

To get started, set up the Apollo Client in your project:

```javascript
import { ApolloClient, InMemoryCache, gql } from "@apollo/client";

const client = new ApolloClient({
  uri: getNotasQueryURL(), 
  cache: new InMemoryCache(),
});
```

#### Sample Query

Here's a sample query that fetches notas for a given account:

```javascript
const GET_NOTAS = gql`
  query GetNotas($account: ID!) {
    account(id: $account) {
      cheqs {
        id
        amount
        timestamp
        status
        uri
        erc20 {
          id
        }
        sender {
          id
        }
        receiver {
          id
        }
      }
    }
  }
`;

async function fetchNotas(account) {
  try {
    const { data } = await client.query({
      query: GET_NOTAS,
      variables: { account },
    });
    console.log(data.account.cheqs);
  } catch (error) {
    console.error("Error fetching notas:", error);
  }
}

fetchNotas("0x..."); // Replace with the Ethereum address of the account
```

This query retrieves the following data:

* `id`: Nota ID
* `amount`: Nota amount
* `timestamp`: Nota creation timestamp
* `status`: Nota status
* `uri`: Nota URI
* `erc20`: Token information for the nota currency
* `sender`: Sender's Ethereum address
* `receiver`: Receiver's Ethereum address

Replace `0x...` with the Ethereum address of the account you want to fetch notas for.

#### Exploring the Schema

To explore the full schema and test queries, use the GraphiQL playground at the following URL:

\[Coming Soon]

This playground allows you to explore the available types, fields, and relationships in the Denota subgraph and test your queries before integrating them into your application.
