> ## Documentation Index
> Fetch the complete documentation index at: https://docs.peaq.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Storage Operations

Another basic operation peaq provides is on-chain storage, allowing data to be saved using a `key: value` structure.

## addItem(itemType, item, seed)

Adds an entry pair to the blockchain.

| Parameter    | Type     | EVM      | Substrate | Description                                                                                  |
| ------------ | -------- | -------- | --------- | -------------------------------------------------------------------------------------------- |
| **itemType** | `string` | Required | Required  | Key that will be stored in peaq storage. Max 64 bytes.                                       |
| **item**     | `object` | Required | Required  | Value that will be stored in peaq storage. Max 256 bytes.                                    |
| **seed**     | `string` | N/A      | Required  | If not set at instance creation, allows user to define who sends the Substrate Transactions. |

### Add Item Code Examples

<CodeGroup>
  ```javascript EVM Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
  import { Sdk } from "@peaq-network/sdk";

  const HTTPS_BASE_URL = "https://quicknode.peaq.xyz";
  const EVM_PRIVATE = process.env["EVM_PRIVATE"];

  const sdk = await Sdk.createInstance({
      baseUrl: HTTPS_BASE_URL,
      chainType: Sdk.ChainType.EVM
  });

  const key = "my_key";
  const value = "my_value";

  const tx = await sdk.storage.addItem({
      itemType: key,
      item: value
  });

  const receipt = await Sdk.sendEvmTx({
      tx: tx,
      baseUrl: HTTPS_BASE_URL,
      seed: EVM_PRIVATE
  });
  ```

  ```javascript EVM Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
  // sdk.storage.addItem({}) return object
  {
    to: '0x0000000000000000000000000000000000000801',
    data: '0x257c3c030000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000066d795f6b6579000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000086d795f76616c7565000000000000000000000000000000000000000000000000'
  }

  // sendEvmTx({}) return object
  TransactionReceipt {
    provider: JsonRpcProvider {},
    to: '0x0000000000000000000000000000000000000801',
    from: '0x9Eeab1aCcb1A701aEfAB00F3b8a275a39646641C',
    contractAddress: null,
    hash: '0x6fec47eadcba1d94e28a6d08b997aa717b69cd3c4319adb7459d21018866602a',
    index: 0,
    blockHash: '0x46a7dae54300988b4fdd42957e033d32219c2f09d683c932f00cd96d312f4f54',
    blockNumber: 4488814,
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000800000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000010000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    gasUsed: 43014n,
    blobGasUsed: null,
    cumulativeGasUsed: 43014n,
    gasPrice: 100000000000n,
    blobGasPrice: null,
    type: 2,
    status: 1,
    root: undefined
  }
  ```

  ```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
  import { Sdk } from "@peaq-network/sdk";

  const WSS_BASE_URL = "wss://quicknode.peaq.xyz";
  const SUBSTRATE_SEED = process.env["SUBSTRATE_SEED"];

  const sdk = await Sdk.createInstance({
      baseUrl: WSS_BASE_URL,
      chainType: Sdk.ChainType.SUBSTRATE,
      seed: SUBSTRATE_SEED
  });

  const key = "my_key";
  const value = "my_value";

  const result = await sdk.storage.addItem({
      itemType: key,
      item: value
  });

  // disconnect when finished
  await sdk.disconnect();
  ```

  ```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
  // sdk.storage.addItem({}) return object
  {
    message: 'Successfully added the storage item type my_key with item my_value for the address 5Df42mkztLtkksgQuLy4YV6hmhzdjYvDknoxHv1QBkaY12Pg',
    block_hash: '0xafe84e572ad2ca33cbdad683dfcf2c8ac65bb9944c34304a81010212973739fa',
    unsubscribe: [Function (anonymous)]
  }
  ```
</CodeGroup>

## readItem(itemType, address, wssBaseUrl)

Returns the `key: value` pair of what is stored on-chain.

| Parameter      | Type     | EVM      | Substrate | Description                                                                                                       |
| -------------- | -------- | -------- | --------- | ----------------------------------------------------------------------------------------------------------------- |
| **itemType**   | `string` | Required | Required  | Key that will be use to search peaq storage at the address.                                                       |
| **address**    | `string` | Required | Optional  | Wallet address where the is key stored.                                                                           |
| **wssBaseUrl** | `string` | Required | N/A       | WSS URL must be used to read from storage. EVM must explicitly set WSS (Substrate has it stored during creation). |

### Get Item Code Examples

<CodeGroup>
  ```javascript EVM Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
  import { Sdk } from "@peaq-network/sdk";

  const HTTPS_BASE_URL = "https://quicknode.peaq.xyz";
  const WSS_BASE_URL = "wss://quicknode.peaq.xyz";
  const EVM_ADDRESS = process.env["EVM_ADDRESS"];

  const sdk = await Sdk.createInstance({
      baseUrl: HTTPS_BASE_URL,
      chainType: Sdk.ChainType.EVM
  });

  const key = "my_key";
  const pair = await sdk.storage.getItem({
      itemType: key,
      address: EVM_ADDRESS,
      wssBaseUrl: WSS_BASE_URL
  });
  ```

  ```javascript EVM Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
  // sdk.storage.getItem({}) return object
  { my_key: 'my_value' }
  ```

  ```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
  import { Sdk } from "@peaq-network/sdk";

  const WSS_BASE_URL = "wss://quicknode.peaq.xyz";
  const SUBSTRATE_SEED = process.env["SUBSTRATE_SEED"];

  const sdk = await Sdk.createInstance({
      baseUrl: WSS_BASE_URL,
      chainType: Sdk.ChainType.SUBSTRATE,
      seed: SUBSTRATE_SEED
  });

  const key = "my_key";
  const pair = await sdk.storage.getItem({
      itemType: key
  });

  // disconnect when finished
  await sdk.disconnect();
  ```

  ```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
  // sdk.storage.getItem({}) return object
  { my_key: 'my_value' }
  ```
</CodeGroup>

## updateItem(itemType, item, seed)

Updates the value in storage for the key passed.

| Parameter    | Type     | EVM      | Substrate | Description                                                                                  |
| ------------ | -------- | -------- | --------- | -------------------------------------------------------------------------------------------- |
| **itemType** | `string` | Required | Required  | Key that is stored in peaq storage whose value will be updated.                              |
| **item**     | `string` | Required | Required  | New value that will be stored in peaq storage.                                               |
| **seed**     | `string` | N/A      | Optional  | If not set at instance creation, allows user to define who sends the Substrate Transactions. |

### Update Item Code Examples

<CodeGroup>
  ```javascript EVM Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
  import { Sdk } from "@peaq-network/sdk";

  const HTTPS_BASE_URL = "https://quicknode.peaq.xyz";
  const WSS_BASE_URL = "wss://quicknode.peaq.xyz";
  const EVM_PRIVATE = process.env["EVM_PRIVATE"];

  const sdk = await Sdk.createInstance({
      baseUrl: HTTPS_BASE_URL,
      chainType: Sdk.ChainType.EVM
  });

  const key = "my_key";
  const value = "my_new_value";

  const tx = await sdk.storage.updateItem({
      itemType: key,
      item: value
  });
  const receipt = await Sdk.sendEvmTx({
      tx: tx,
      baseUrl: HTTPS_BASE_URL,
      seed: EVM_PRIVATE
  });

  // read to see the change
  const pair = await sdk.storage.getItem({
      itemType: key,
      address: EVM_ADDRESS,
      wssBaseUrl: WSS_BASE_URL
  });
  ```

  ```javascript EVM Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
  // sdk.storage.updateItem({}) return object
  {
    to: '0x0000000000000000000000000000000000000801',
    data: '0x1cd4bf090000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000066d795f6b65790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c6d795f6e65775f76616c75650000000000000000000000000000000000000000'
  }

  // sendEvmTx({}) return object
  TransactionReceipt {
    provider: JsonRpcProvider {},
    to: '0x0000000000000000000000000000000000000801',
    from: '0x9Eeab1aCcb1A701aEfAB00F3b8a275a39646641C',
    contractAddress: null,
    hash: '0x2fa941ecd230d6440f2421d80582dc38c53dd0b2ae9b8f55c1980f323bd25f7f',
    index: 18,
    blockHash: '0x7467a4a72e98c69e6cde157e6fa5d56455ecc7d23aae72129c9d4d9f6512ff63',
    blockNumber: 4488965,
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000800000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010',
    gasUsed: 32060n,
    blobGasUsed: null,
    cumulativeGasUsed: 15354515n,
    gasPrice: 100000000000n,
    blobGasPrice: null,
    type: 2,
    status: 1,
    root: undefined
  }
  ```

  ```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
  import { Sdk } from "@peaq-network/sdk";

  const WSS_BASE_URL = "wss://quicknode.peaq.xyz";
  const SUBSTRATE_SEED = process.env["SUBSTRATE_SEED"];

  const sdk = await Sdk.createInstance({
      baseUrl: WSS_BASE_URL,
      chainType: Sdk.ChainType.SUBSTRATE,
      seed: SUBSTRATE_SEED
  });

  const key = "my_key";
  const value = "my_new_value";

  const response = await sdk.storage.updateItem({
      itemType: key,
      item: value
  });

  // read to see it has been changed
  const pair = await sdk.storage.getItem({
      itemType: key
  });
  // disconnect when finished
  await sdk.disconnect();
  ```

  ```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
  // sdk.storage.updateItem({}) return object
  {
    message: 'Successfully updated the storage item type my_key to the new item my_new_value for the address 5Df42mkztLtkksgQuLy4YV6hmhzdjYvDknoxHv1QBkaY12Pg',
    block_hash: '0xb75cf6b47421c585772220bd99df1850a50e59ce506d6302eaba49db6e029288',
    unsubscribe: [Function (anonymous)]
  }
  ```
</CodeGroup>

## removeItem(itemType, seed)

Removes the item type and item from peaq storage.

| Parameter    | Type     | EVM | Substrate | Description                                                                                  |
| ------------ | -------- | --- | --------- | -------------------------------------------------------------------------------------------- |
| **itemType** | `string` | N/A | Required  | The key representing the pair to be removed.                                                 |
| **seed**     | `string` | N/A | Optional  | If not set at instance creation, allows user to define who sends the Substrate Transactions. |

### Remove Item Code Examples

<CodeGroup>
  ```javascript Substrate Request theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
  import { Sdk } from "@peaq-network/sdk";

  const WSS_BASE_URL = "wss://quicknode.peaq.xyz";
  const SUBSTRATE_SEED = process.env["SUBSTRATE_SEED"];

  const sdk = await Sdk.createInstance({
      baseUrl: WSS_BASE_URL,
      chainType: sdk.ChainType.SUBSTRATE,
      seed: SUBSTRATE_SEED
  });

  const key = "my_key";

  const response = await sdk.storage.removeItem({
      itemType: key
  });

  // disconnect when finished
  await sdk.disconnect();
  ```

  ```javascript Substrate Response theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
  // sdk.storage.removeItem({}) return object
  {
    message: 'Successfully removed the storage item type my_key from address 5Df42mkztLtkksgQuLy4YV6hmhzdjYvDknoxHv1QBkaY12Pg',
    block_hash: '0xb40d8934dc69c871b8ee36a34ef63ae5fb0b93c06b5d8d5c5df5244e001d3acb',
    unsubscribe: [Function (anonymous)]
  }
  ```
</CodeGroup>

<Note>EVM delete storage precompile currently in development. When completed will add to the JavaScript SDK.</Note>
