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.

ParameterTypeEVMSubstrateDescription
itemTypestringRequiredRequiredKey that will be stored in peaq storage. Max 64 bytes.
itemobjectRequiredRequiredValue that will be stored in peaq storage. Max 256 bytes.
seedstringN/ARequiredIf not set at instance creation, allows user to define who sends the Substrate Transactions.

Add Item Code Examples

import { Sdk } from "@peaq-network/sdk";

const HTTPS_BASE_URL = "https://peaq.api.onfinality.io/public";
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
});

readItem(itemType, address, wssBaseUrl)

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

ParameterTypeEVMSubstrateDescription
itemTypestringRequiredRequiredKey that will be use to search peaq storage at the address.
addressstringRequiredOptionalWallet address where the is key stored.
wssBaseUrlstringRequiredN/AWSS URL must be used to read from storage. EVM must explicitly set WSS (Substrate has it stored during creation).

Get Item Code Examples

import { Sdk } from "@peaq-network/sdk";

const HTTPS_BASE_URL = "https://peaq.api.onfinality.io/public";
const WSS_BASE_URL = "wss://peaq.api.onfinality.io/public";
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
});

updateItem(itemType, item, seed)

Updates the value in storage for the key passed.

ParameterTypeEVMSubstrateDescription
itemTypestringRequiredRequiredKey that is stored in peaq storage whose value will be updated.
itemstringRequiredRequiredNew value that will be stored in peaq storage.
seedstringN/AOptionalIf not set at instance creation, allows user to define who sends the Substrate Transactions.

Update Item Code Examples

import { Sdk } from "@peaq-network/sdk";

const HTTPS_BASE_URL = "https://peaq.api.onfinality.io/public";
const WSS_BASE_URL = "wss://peaq.api.onfinality.io/public";
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
});

removeItem(itemType, seed)

Removes the item type and item from peaq storage.

ParameterTypeEVMSubstrateDescription
itemTypestringN/ARequiredThe key representing the pair to be removed.
seedstringN/AOptionalIf not set at instance creation, allows user to define who sends the Substrate Transactions.

Remove Item Code Examples

import { Sdk } from "@peaq-network/sdk";

const WSS_BASE_URL = "wss://peaq.api.onfinality.io/public";
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();
EVM delete storage precompile currently in development. When completed will add to the JavaScript SDK.