Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .agent/rules/solidity_zksync.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Solidity & ZkSync Development Standards

## Toolchain & Environment
- **Primary Tool**: `forge` (ZkSync fork). Use for compilation, testing, and generic scripting.
- **Secondary Tool**: `hardhat`. Use only when `forge` encounters compatibility issues (e.g., complex deployments, specific plugin needs).
- **Network Target**: ZkSync Era (Layer 2).
- **Solidity Version**: `^0.8.20` (or `0.8.24` if strictly supported by the zk-compiler).

## Modern Solidity Best Practices
- **Safety First**:
- **Checks-Effects-Interactions (CEI)** pattern must be strictly followed.
- Use `Ownable2Step` over `Ownable` for privileged access.
- Prefer `ReentrancyGuard` for external calls where appropriate.
- **Gas & Efficiency**:
- Use **Custom Errors** (`error MyError();`) instead of `require` strings.
- Use `mapping` over arrays for membership checks where possible.
- Minimize on-chain storage; use events for off-chain indexing.

## Testing Standards
- **Framework**: Foundry (Forge).
- **Methodology**:
- **Unit Tests**: Comprehensive coverage for all functions.
- **Fuzz Testing**: Required for arithmetic and purely functional logic.
- **Invariant Testing**: Define invariants for stateful system properties.
- **Naming Convention**:
- `test_Description`
- `testFuzz_Description`
- `test_RevertIf_Condition`

## ZkSync Specifics
- **System Contracts**: Be aware of ZkSync system contracts (e.g., `ContractDeployer`, `L2EthToken`) when interacting with low-level features.
- **Gas Model**: Account for ZkSync's different gas metering if performing low-level optimization.
- **Compiler Differences**: Be mindful of differences between `solc` and `zksolc` (e.g., `create2` address derivation).
11 changes: 10 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@
"Frontends",
"testuser",
"testhandle",
"douglasacost"
"douglasacost",
"IBEACON",
"AABBCCDD",
"SSTORE",
"Permissionless",
"Reentrancy",
"SFID",
"EXTCODECOPY",
"solady",
"SLOAD"
]
}
33 changes: 33 additions & 0 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Solidity & ZkSync Development Standards

## Toolchain & Environment
- **Primary Tool**: `forge` (ZkSync fork). Use for compilation, testing, and generic scripting.
- **Secondary Tool**: `hardhat`. Use only when `forge` encounters compatibility issues (e.g., complex deployments, specific plugin needs).
- **Network Target**: ZkSync Era (Layer 2).
- **Solidity Version**: `^0.8.20` (or `0.8.24` if strictly supported by the zk-compiler).

## Modern Solidity Best Practices
- **Safety First**:
- **Checks-Effects-Interactions (CEI)** pattern must be strictly followed.
- Use `Ownable2Step` over `Ownable` for privileged access.
- Prefer `ReentrancyGuard` for external calls where appropriate.
- **Gas & Efficiency**:
- Use **Custom Errors** (`error MyError();`) instead of `require` strings.
- Use `mapping` over arrays for membership checks where possible.
- Minimize on-chain storage; use events for off-chain indexing.

## Testing Standards
- **Framework**: Foundry (Forge).
- **Methodology**:
- **Unit Tests**: Comprehensive coverage for all functions.
- **Fuzz Testing**: Required for arithmetic and purely functional logic.
- **Invariant Testing**: Define invariants for stateful system properties.
- **Naming Convention**:
- `test_Description`
- `testFuzz_Description`
- `test_RevertIf_Condition`

## ZkSync Specifics
- **System Contracts**: Be aware of ZkSync system contracts (e.g., `ContractDeployer`, `L2EthToken`) when interacting with low-level features.
- **Gas Model**: Account for ZkSync's different gas metering if performing low-level optimization.
- **Compiler Differences**: Be mindful of differences between `solc` and `zksolc` (e.g., `create2` address derivation).
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "lib/era-contracts"]
path = lib/era-contracts
url = https://github.com/matter-labs/era-contracts
[submodule "lib/solady"]
path = lib/solady
url = https://github.com/vectorized/solady
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"editor.formatOnSave": true,
"[solidity]": {
"editor.defaultFormatter": "JuanBlanco.solidity"
},
"chat.tools.terminal.autoApprove": {
"forge": true
}
}
20 changes: 20 additions & 0 deletions foundry.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"lib/zksync-storage-proofs": {
"rev": "4b20401ce44c1ec966a29d893694f65db885304b"
},
"lib/openzeppelin-contracts": {
"rev": "e4f70216d759d8e6a64144a9e1f7bbeed78e7079"
},
"lib/solady": {
"tag": {
"name": "v0.1.26",
"rev": "acd959aa4bd04720d640bf4e6a5c71037510cc4b"
}
},
"lib/forge-std": {
"rev": "1eea5bae12ae557d589f9f0f0edae2faa47cb262"
},
"lib/era-contracts": {
"rev": "84d5e3716f645909e8144c7d50af9dd6dd9ded62"
}
}
1 change: 1 addition & 0 deletions lib/solady
Submodule solady added at acd959
3 changes: 2 additions & 1 deletion remappings.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
@openzeppelin=lib/openzeppelin-contracts/
@openzeppelin=lib/openzeppelin-contracts/
solady/=lib/solady/src/
92 changes: 92 additions & 0 deletions src/swarms/FleetIdentity.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";

/**
* @title FleetIdentity
* @notice Permissionless ERC-721 representing ownership of a BLE fleet.
* @dev TokenID = uint256(uint128(uuid)), guaranteeing one owner per Proximity UUID.
*/
contract FleetIdentity is ERC721 {
error InvalidUUID();
error InvalidPaginationParams();
error NotTokenOwner();

// Array to enable enumeration of all registered fleets (for SDK scanning)
bytes16[] public registeredUUIDs;

// Mapping to quickly check if a UUID is registered (redundant with ownerOf but cheaper for specific checks)
mapping(uint256 => bool) public activeFleets;

event FleetRegistered(address indexed owner, bytes16 indexed uuid, uint256 indexed tokenId);
event FleetBurned(address indexed owner, uint256 indexed tokenId);

constructor() ERC721("Swarm Fleet Identity", "SFID") {}

/// @notice Mints a new fleet NFT for the given Proximity UUID.
/// @param uuid The 16-byte Proximity UUID.
/// @return tokenId The deterministic token ID derived from `uuid`.
function registerFleet(bytes16 uuid) external returns (uint256 tokenId) {
if (uuid == bytes16(0)) {
revert InvalidUUID();
}

tokenId = uint256(uint128(uuid));

_mint(msg.sender, tokenId);

registeredUUIDs.push(uuid);
activeFleets[tokenId] = true;

emit FleetRegistered(msg.sender, uuid, tokenId);
}

/// @notice Burns the fleet NFT. Caller must be the token owner.
/// @param tokenId The fleet token ID to burn.
function burn(uint256 tokenId) external {
if (ownerOf(tokenId) != msg.sender) {
revert NotTokenOwner();
}

activeFleets[tokenId] = false;

_burn(tokenId);

emit FleetBurned(msg.sender, tokenId);
}

/// @notice Returns a paginated slice of all registered UUIDs.
/// @param offset Starting index.
/// @param limit Maximum number of entries to return.
/// @return uuids The requested UUID slice.
function getRegisteredUUIDs(uint256 offset, uint256 limit) external view returns (bytes16[] memory uuids) {
if (limit == 0) {
revert InvalidPaginationParams();
}

if (offset >= registeredUUIDs.length) {
return new bytes16[](0);
}

uint256 end = offset + limit;
if (end > registeredUUIDs.length) {
end = registeredUUIDs.length;
}

uint256 resultLen = end - offset;
uuids = new bytes16[](resultLen);

for (uint256 i = 0; i < resultLen;) {
uuids[i] = registeredUUIDs[offset + i];
unchecked {
++i;
}
}
}

/// @notice Returns the total number of registered fleets (including burned).
function getTotalFleets() external view returns (uint256) {
return registeredUUIDs.length;
}
}
53 changes: 53 additions & 0 deletions src/swarms/ServiceProvider.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";

/**
* @title ServiceProvider
* @notice Permissionless ERC-721 representing ownership of a service endpoint URL.
* @dev TokenID = keccak256(url), guaranteeing one owner per URL.
*/
contract ServiceProvider is ERC721 {
error EmptyURL();
error NotTokenOwner();

// Maps TokenID -> Provider URL
mapping(uint256 => string) public providerUrls;

event ProviderRegistered(address indexed owner, string url, uint256 indexed tokenId);
event ProviderBurned(address indexed owner, uint256 indexed tokenId);

constructor() ERC721("Swarm Service Provider", "SSV") {}

/// @notice Mints a new provider NFT for the given URL.
/// @param url The backend service URL (must be unique).
/// @return tokenId The deterministic token ID derived from `url`.
function registerProvider(string calldata url) external returns (uint256 tokenId) {
if (bytes(url).length == 0) {
revert EmptyURL();
}

tokenId = uint256(keccak256(bytes(url)));

providerUrls[tokenId] = url;

_mint(msg.sender, tokenId);

emit ProviderRegistered(msg.sender, url, tokenId);
}

/// @notice Burns the provider NFT. Caller must be the token owner.
/// @param tokenId The provider token ID to burn.
function burn(uint256 tokenId) external {
if (ownerOf(tokenId) != msg.sender) {
revert NotTokenOwner();
}

delete providerUrls[tokenId];

_burn(tokenId);

emit ProviderBurned(msg.sender, tokenId);
}
}
Loading
Loading