Browse DIDLab Docs

Smart contracts · Examples

Four small contracts for public reference

These examples are deliberately compact. They demonstrate common patterns without publishing the deeper exercises, security extensions, or assessment tasks used inside DIDLab Labs.

SimpleStorage

State, write transaction, read call, and event.

SimpleStorage.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract SimpleStorage {
    uint256 private value;
    event ValueChanged(uint256 value, address indexed changedBy);

    function set(uint256 newValue) external {
        value = newValue;
        emit ValueChanged(newValue, msg.sender);
    }

    function get() external view returns (uint256) {
        return value;
    }
}

Counter

Simple state transitions with a guard condition and event.

Counter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract Counter {
    uint256 public count;
    event CountChanged(uint256 count, address indexed changedBy);

    function increment() external {
        count += 1;
        emit CountChanged(count, msg.sender);
    }

    function decrement() external {
        require(count > 0, "Counter already zero");
        count -= 1;
        emit CountChanged(count, msg.sender);
    }
}

StudentRecord

Structs, mappings, append-only records, and evidence-style hashes.

StudentRecord.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract StudentRecord {
    struct Record {
        string course;
        bytes32 artifactHash;
        uint64 recordedAt;
    }

    mapping(address => Record[]) private records;
    event RecordAdded(address indexed student, bytes32 indexed artifactHash);

    function addRecord(address student, string calldata course, bytes32 artifactHash) external {
        records[student].push(Record(course, artifactHash, uint64(block.timestamp)));
        emit RecordAdded(student, artifactHash);
    }

    function recordCount(address student) external view returns (uint256) {
        return records[student].length;
    }
}

DocumentHash

Anchor a document digest and optional IPFS CID on-chain without storing the document itself.

DocumentHash.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract DocumentHash {
    struct Entry {
        bytes32 digest;
        string cid;
        uint64 anchoredAt;
    }

    mapping(bytes32 => Entry) public entries;
    event DocumentAnchored(bytes32 indexed documentId, bytes32 digest, string cid);

    function anchor(bytes32 documentId, bytes32 digest, string calldata cid) external {
        require(entries[documentId].anchoredAt == 0, "Already anchored");
        entries[documentId] = Entry(digest, cid, uint64(block.timestamp));
        emit DocumentAnchored(documentId, digest, cid);
    }
}

Security boundary

These contracts are learning references, not audited production systems. Production contracts need access control, authorization, testing, upgrade/ownership decisions, threat modeling, and independent review.

Deploy one reference contract

Start with SimpleStorage, then move to a contract that matches your application idea.

Validation profile

Network

DIDLab Blockchain

Chain ID

252501

Client

Hyperledger Besu

Consensus

QBFT

Native asset

TRUST

Platform token

TT (ERC-20)

Solidity examples

0.8.24

Last validated

August 2026

Go deeper in DIDLab Labs

Public documentation explains the workflow and expected result. Authenticated DIDLab Labs adds managed workspaces, deeper exercises, checkpoints, evidence capture, and instructor assessment.

Open DIDLab Labs