Skip to main content

Smart contract storage

Smart contract storage is the persistent data layer of an Ethereum Virtual Machine (EVM) contract. It holds your state variables on chain. Unlike local or in-memory variables, data in contract storage stays on the blockchain and changes only when a contract function writes to it. Storage is public, so anyone can read it. Contracts use it for token balances and allowances, user registries, votes and proposals, and marketplace listings.

Prerequisites

  • Solidity basics: how to write and deploy a simple contract.
  • A Solidity toolchain (Hardhat, Foundry, or Remix).
  • Access to a peaq EVM endpoint, or a local test EVM.
  • An account funded with enough tokens to pay gas.

Instructions

The contract below stores one number and a mapping of addresses to strings.

1. Define the contract

  • storedNumber is a public state variable of type uint256.
  • userData is a public mapping from an address to a string.
  • setNumber overwrites storedNumber.
  • setUserData writes a string for the calling address (msg.sender).

2. Deploy the contract

Point your toolchain at a peaq EVM endpoint (https://quicknode1.peaq.xyz for mainnet) and deploy with the constructor argument _initialNumber.
  • Hardhat: add a peaq network (chainId: 3338) to hardhat.config.js, then deploy with an Ignition module as in Deploy Smart Contract
  • Foundry: forge create --rpc-url https://quicknode1.peaq.xyz --private-key <key> --broadcast src/DataStorage.sol:DataStorage --constructor-args 42
  • Remix: pick the network in the environment settings, then use Deploy.

3. Write to storage

Call setNumber(42) to update the number, and setUserData("Hello, peaq!") to store a string for your address. Each call sends a transaction, costs gas, and persists the new value in contract storage.

Next steps