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
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.
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
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.
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
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
});
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.
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
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
});
await sdk.disconnect();
EVM delete storage precompile currently in development. When completed will add to the JavaScript SDK.