On This Page
SimpleStorage
BeginnerA fundamental smart contract demonstrating core functionality with post-quantum security
Overview
The SimpleStorage contract is the perfect starting point for developers new to QuantumPrivate. It demonstrates the basic principles of quantum-resistant smart contract development while showcasing essential features like data storage, event emission, and access control.
Key Features
- Quantum-resistant storage
- Event emission
- Access control
- Owner management
Use Cases
- • Learning smart contract development
- • Basic data persistence
- • Configuration management
- • Simple state tracking
Contract Code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
/**
* @title SimpleStorage
* @dev A basic quantum-resistant storage contract
* Demonstrates core QuantumPrivate functionality
*/
contract SimpleStorage {
uint256 private storedValue;
address public owner;
event ValueChanged(uint256 oldValue, uint256 newValue, address indexed changer);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function");
_;
}
constructor() {
owner = msg.sender;
storedValue = 0;
emit OwnershipTransferred(address(0), msg.sender);
}
/**
* @dev Set the stored value (only owner)
* @param value The new value to store
*/
function setValue(uint256 value) public onlyOwner {
uint256 oldValue = storedValue;
storedValue = value;
emit ValueChanged(oldValue, value, msg.sender);
}
/**
* @dev Get the stored value
* @return The current stored value
*/
function getValue() public view returns (uint256) {
return storedValue;
}
/**
* @dev Reset the stored value to zero (only owner)
*/
function reset() public onlyOwner {
uint256 oldValue = storedValue;
storedValue = 0;
emit ValueChanged(oldValue, 0, msg.sender);
}
/**
* @dev Transfer ownership of the contract
* @param newOwner The address of the new owner
*/
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0), "New owner cannot be zero address");
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
Key Features
Quantum-Resistant Storage
The contract uses QuantumPrivate's quantum-resistant storage mechanisms to ensure data integrity even against quantum computer attacks.
uint256 private storedValue;
Access Control
Implements owner-only functions using modifiers to control who can modify the stored value.
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function");
_;
}
Event Emission
Emits events for important state changes, enabling off-chain monitoring and integration.
event ValueChanged(uint256 oldValue, uint256 newValue, address indexed changer);
Deployment
Using QuantumPrivate CLI
# Deploy to testnet
qp-cli deploy SimpleStorage.sol --network testnet
# Deploy to mainnet
qp-cli deploy SimpleStorage.sol --network mainnet --verify
Manual Compilation
# Compile with Solidity compiler
solc --optimize --abi --bin SimpleStorage.sol
Contract Interaction
JavaScript Examples
// Set a value (owner only)
await contract.setValue(42);
// Get the current value
const value = await contract.getValue();
console.log("Stored value:", value.toString());
// Reset to zero (owner only)
await contract.reset();
// Transfer ownership
await contract.transferOwnership("0x742d35Cc6639C0532fEb5dc5d1b9C90b3Ae7291c");
Event Listening
// Listen for value changes
contract.on("ValueChanged", (oldValue, newValue, changer) => {
console.log(`Value changed from ${oldValue} to ${newValue} by ${changer}`);
});
// Listen for ownership transfers
contract.on("OwnershipTransferred", (previousOwner, newOwner) => {
console.log(`Ownership transferred from ${previousOwner} to ${newOwner}`);
});
Testing
describe("SimpleStorage", function() {
let simpleStorage;
let owner;
let addr1;
beforeEach(async function() {
[owner, addr1] = await ethers.getSigners();
const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
simpleStorage = await SimpleStorage.deploy();
await simpleStorage.deployed();
});
it("Should set and get value", async function() {
await simpleStorage.setValue(42);
expect(await simpleStorage.getValue()).to.equal(42);
});
it("Should only allow owner to set value", async function() {
await expect(
simpleStorage.connect(addr1).setValue(42)
).to.be.revertedWith("Only owner can call this function");
});
it("Should emit ValueChanged event", async function() {
await expect(simpleStorage.setValue(42))
.to.emit(simpleStorage, "ValueChanged")
.withArgs(0, 42, owner.address);
});
it("Should reset value to zero", async function() {
await simpleStorage.setValue(42);
await simpleStorage.reset();
expect(await simpleStorage.getValue()).to.equal(0);
});
it("Should transfer ownership", async function() {
await simpleStorage.transferOwnership(addr1.address);
expect(await simpleStorage.owner()).to.equal(addr1.address);
});
});
Run Tests
qp-cli test contracts/SimpleStorage.sol
Ready for More?
Now that you understand the basics, explore more advanced quantum-resistant contracts