Skip to content

โœ… Smart Contract Best Practices

Zaphenath is designed with minimalism, modularity, and auditability in mind. This page outlines best practices for writing, auditing, and integrating contracts similar to Zaphenath in production environments.

๐Ÿ”’ Code Structure Guidelines

  • Use explicit access control โ€” Every mutating function is guarded by onlyRoleOrAbove().
  • Avoid nested mappings inside structs โ€” Keep data layout readable and storage-efficient.
  • Immutable logic flow โ€” Avoid external calls, unbounded loops, or delegatecall.
  • Event coverage โ€” Emit events on all mutating actions (create, update, delete, assign).

๐Ÿงช Testing Strategy

Layer Tool Purpose
Unit Tests Foundry (forge) Validate isolated contract behavior
Integration Foundry + scripts End-to-end flow across roles and timeouts
Fuzzing Foundry Randomized role/path combinations
Time Simulation vm.warp() Timeout verification
Revert Checks expectRevert() Ensure security fails when expected

Suggested Coverage

  • Ping behavior & logging
  • Role enforcement and misassignment
  • Timeout boundary enforcement
  • Unauthorized access attempts

๐Ÿ” Audit Considerations

  • Storage collisions โ€” Ensure mappings are unique per key
  • Gas analysis โ€” Measure costs of createKey, ping, readKey, and deleteKey
  • Re-entrancy โ€” Not possible here, but audit hooks before adding external integrations
  • Invariant checks โ€” For example: a Writer must not access keys unless timeout passed

โš™๏ธ Deployment & Upgrade Strategy

  • Immutable core โ€” Core Zaphenath contracts are designed for permanence
  • Proxy support (optional) โ€” Wrap in upgradeable proxy pattern only if needed
  • Scripted deployment โ€” Use forge script to avoid manual steps
  • Post-deploy tests โ€” Immediately test timeout logic after deployment

๐Ÿ” Operational Best Practices

  • Monitor for excessive gas on specific keys
  • Alert on CustodianUpdated or readKey usage
  • Rotate test keys in staging environments
  • Maintain off-chain documentation of key purposes and access levels

โžก๏ธ Next: Deploying on Private Chains