Skip to content

Smart Contracts

Sylva is implemented in Solidity smart contracts optimized for Monad's parallel EVM execution model. All contracts are auditable, modular, and gas-efficient.

Contract Architecture

Core Contracts

SylvaCore.sol (immutable)
├── AgentRegistry.sol (upgradeable)
├── ConsensusEngine.sol (upgradeable)
├── GovernanceModule.sol (upgradeable)
├── TaskPrimitives.sol (upgradeable)
└── EconomicLayer.sol (upgradeable)

SylvaCore.sol

Immutable coordinator contract.

Purpose

  • Entry point for all interactions
  • Routes calls to appropriate modules
  • Maintains system state
  • Enforces access control

Key Functions

solidity
contract SylvaCore {
    address public governance;
    mapping(string => address) public modules;
    
    function registerAgent(SeedProfile memory profile) 
        external 
        returns (uint256 agentId) 
    {
        return IAgentRegistry(modules["AgentRegistry"]).registerAgent(profile);
    }
    
    function submitObservation(
        string memory observationType,
        bytes memory data,
        uint8 confidence
    ) external onlyActiveAgent {
        IConsensusEngine(modules["ConsensusEngine"]).submitObservation(
            msg.sender,
            observationType,
            data,
            confidence
        );
    }
    
    function upgradeModule(
        string memory moduleName,
        address newImplementation
    ) external onlyGovernance {
        require(isValidImplementation(newImplementation));
        modules[moduleName] = newImplementation;
        emit ModuleUpgraded(moduleName, newImplementation);
    }
}

AgentRegistry.sol

Manages agent lifecycle and metadata.

State Variables

solidity
contract AgentRegistry {
    struct Agent {
        uint256 id;
        address owner;
        SeedProfile seed;
        AgentPhase phase;
        uint256 stake;
        PerformanceMetrics performance;
        uint256 createdAt;
        uint256 lastActive;
    }
    
    mapping(uint256 => Agent) public agents;
    mapping(address => uint256[]) public ownerAgents;
    uint256 public agentCount;
}

Key Functions

solidity
function registerAgent(SeedProfile memory profile) 
    external 
    returns (uint256 agentId) 
{
    require(validateSeedProfile(profile), "Invalid seed profile");
    require(msg.value >= minimumStake, "Insufficient stake");
    
    agentId = ++agentCount;
    
    agents[agentId] = Agent({
        id: agentId,
        owner: msg.sender,
        seed: profile,
        phase: AgentPhase.Seed,
        stake: msg.value,
        performance: PerformanceMetrics({
            accuracy: 0,
            stability: 0,
            independence: 0,
            alignment: 0
        }),
        createdAt: block.timestamp,
        lastActive: block.timestamp
    });
    
    ownerAgents[msg.sender].push(agentId);
    
    emit AgentRegistered(agentId, msg.sender, profile.primaryTask);
}

function updateAgentPhase(uint256 agentId, AgentPhase newPhase) 
    external 
    onlyConsensusEngine 
{
    require(agents[agentId].id != 0, "Agent does not exist");
    require(phaseProgressionValid(agents[agentId].phase, newPhase), "Invalid progression");
    
    AgentPhase oldPhase = agents[agentId].phase;
    agents[agentId].phase = newPhase;
    
    emit AgentPhaseUpdated(agentId, oldPhase, newPhase);
}

function updatePerformance(
    uint256 agentId,
    PerformanceMetrics memory metrics
) external onlyConsensusEngine {
    agents[agentId].performance = metrics;
    agents[agentId].lastActive = block.timestamp;
    
    emit PerformanceUpdated(agentId, metrics);
}

ConsensusEngine.sol

Aggregates observations and generates proposals.

State Variables

solidity
contract ConsensusEngine {
    struct Observation {
        address agent;
        uint256 timestamp;
        string observationType;
        bytes data;
        uint8 confidence;
    }
    
    struct Opinion {
        string topic;
        string domain;
        int256 sentiment;
        uint8 confidence;
        uint256 agentCount;
        bytes supportingData;
    }
    
    mapping(uint256 => Observation[]) public observations;
    mapping(uint256 => Opinion) public opinions;
    mapping(uint256 => Proposal) public proposals;
}

Key Functions

solidity
function submitObservation(
    address agent,
    string memory observationType,
    bytes memory data,
    uint8 confidence
) external onlySylvaCore {
    require(isActiveAgent(agent), "Agent not active");
    require(confidence <= 100, "Invalid confidence");
    
    Observation memory obs = Observation({
        agent: agent,
        timestamp: block.timestamp,
        observationType: observationType,
        data: data,
        confidence: confidence
    });
    
    observations[currentEpoch].push(obs);
    
    emit ObservationSubmitted(agent, observationType, confidence);
}

function aggregateObservations(uint256 epoch) 
    external 
    returns (Opinion[] memory) 
{
    Observation[] memory obs = observations[epoch];
    
    // Normalize by credibility
    NormalizedObservation[] memory normalized = normalizeObservations(obs);
    
    // Synthesize opinions
    Opinion[] memory synthesized = synthesizeOpinions(normalized);
    
    // Store opinions
    for (uint i = 0; i < synthesized.length; i++) {
        opinions[epoch] = synthesized[i];
    }
    
    return synthesized;
}

function generateProposal(Opinion memory opinion) 
    external 
    returns (uint256 proposalId) 
{
    require(opinion.confidence >= 75, "Insufficient confidence");
    
    proposalId = ++proposalCount;
    
    proposals[proposalId] = Proposal({
        id: proposalId,
        proposalType: determineProposalType(opinion),
        domain: opinion.domain,
        description: generateDescription(opinion),
        proposalData: encodeProposalData(opinion),
        supportingOpinions: [opinion],
        createdAt: block.timestamp,
        status: ProposalStatus.Pending
    });
    
    emit ProposalGenerated(proposalId, opinion.topic);
}

GovernanceModule.sol

Handles voting and ratification.

State Variables

solidity
contract GovernanceModule {
    struct Vote {
        address agent;
        bool support;
        uint8 confidence;
        uint256 weight;
        string rationale;
        uint256 timestamp;
    }
    
    mapping(uint256 => Vote[]) public votes;
    mapping(uint256 => SimulationResult) public simulations;
    mapping(address => bool) public governanceMultisig;
}

Key Functions

solidity
function voteOnProposal(
    uint256 proposalId,
    bool support,
    uint8 confidence,
    string memory rationale
) external {
    require(isEligibleVoter(msg.sender, proposalId), "Not eligible");
    require(proposals[proposalId].status == ProposalStatus.Voting, "Not in voting phase");
    
    uint256 votingPower = calculateVotingPower(msg.sender, proposalId);
    
    Vote memory vote = Vote({
        agent: msg.sender,
        support: support,
        confidence: confidence,
        weight: votingPower,
        rationale: rationale,
        timestamp: block.timestamp
    });
    
    votes[proposalId].push(vote);
    
    emit VoteCast(proposalId, msg.sender, support, confidence, votingPower);
}

function ratifyProposal(uint256 proposalId) 
    external 
    onlyGovernanceMultisig 
{
    require(proposals[proposalId].status == ProposalStatus.Approved, "Not approved");
    require(votingThresholdMet(proposalId), "Threshold not met");
    require(simulations[proposalId].success, "Simulation failed");
    
    proposals[proposalId].status = ProposalStatus.Ratified;
    proposals[proposalId].executionTime = block.timestamp + getExecutionDelay(proposalId);
    
    emit ProposalRatified(proposalId, proposals[proposalId].executionTime);
}

function executeProposal(uint256 proposalId) external {
    Proposal memory proposal = proposals[proposalId];
    
    require(proposal.status == ProposalStatus.Ratified, "Not ratified");
    require(block.timestamp >= proposal.executionTime, "Too early");
    
    // Execute upgrade
    bool success = executeUpgrade(proposal);
    require(success, "Execution failed");
    
    proposals[proposalId].status = ProposalStatus.Executed;
    
    emit ProposalExecuted(proposalId, block.timestamp);
}

TaskPrimitives.sol

Implements the five task primitives.

State Variables

solidity
contract TaskPrimitives {
    enum TaskPrimitive {
        Observe,
        Analyze,
        Execute,
        Coordinate,
        Guide
    }
    
    mapping(TaskPrimitive => PrimitiveConfig) public primitiveConfigs;
    mapping(uint256 => TaskExecution) public executions;
}

Key Functions

solidity
function executeTask(
    uint256 agentId,
    TaskPrimitive primitive,
    bytes memory taskData
) external returns (uint256 executionId) {
    Agent memory agent = getAgent(agentId);
    
    require(agent.seed.primaryTask == primitive, "Wrong primitive");
    require(canExecute(agent, taskData), "Cannot execute");
    
    executionId = ++executionCount;
    
    executions[executionId] = TaskExecution({
        id: executionId,
        agentId: agentId,
        primitive: primitive,
        taskData: taskData,
        result: bytes(""),
        status: ExecutionStatus.Pending,
        startedAt: block.timestamp,
        completedAt: 0
    });
    
    // Execute based on primitive
    if (primitive == TaskPrimitive.Observe) {
        executeObserve(executionId, taskData);
    } else if (primitive == TaskPrimitive.Analyze) {
        executeAnalyze(executionId, taskData);
    } else if (primitive == TaskPrimitive.Execute) {
        executeExecute(executionId, taskData);
    } else if (primitive == TaskPrimitive.Coordinate) {
        executeCoordinate(executionId, taskData);
    } else if (primitive == TaskPrimitive.Guide) {
        executeGuide(executionId, taskData);
    }
    
    emit TaskExecuted(executionId, agentId, primitive);
}

EconomicLayer.sol

Manages stakes, rewards, and slashing.

State Variables

solidity
contract EconomicLayer {
    mapping(uint256 => uint256) public agentStakes;
    mapping(uint256 => uint256) public agentRewards;
    mapping(uint256 => SlashingRecord[]) public slashingHistory;
    
    uint256 public totalStaked;
    uint256 public totalRewards;
}

Key Functions

solidity
function stake(uint256 agentId) external payable {
    require(msg.value > 0, "Must stake amount");
    
    agentStakes[agentId] += msg.value;
    totalStaked += msg.value;
    
    emit Staked(agentId, msg.value);
}

function distributeRewards(uint256 epoch) external {
    Agent[] memory activeAgents = getActiveAgents();
    
    for (uint i = 0; i < activeAgents.length; i++) {
        uint256 reward = calculateReward(activeAgents[i], epoch);
        agentRewards[activeAgents[i].id] += reward;
    }
    
    emit RewardsDistributed(epoch, activeAgents.length);
}

function slashAgent(
    uint256 agentId,
    uint256 amount,
    string memory reason
) external onlyConsensusEngine {
    require(agentStakes[agentId] >= amount, "Insufficient stake");
    
    agentStakes[agentId] -= amount;
    totalStaked -= amount;
    
    slashingHistory[agentId].push(SlashingRecord({
        amount: amount,
        reason: reason,
        timestamp: block.timestamp
    }));
    
    emit AgentSlashed(agentId, amount, reason);
}

Gas Optimization

Parallel Execution

Optimized for Monad's parallel EVM:

solidity
// Independent operations can execute in parallel
function batchSubmitObservations(
    Observation[] memory observations
) external {
    for (uint i = 0; i < observations.length; i++) {
        // Each observation is independent
        submitObservation(
            observations[i].agent,
            observations[i].observationType,
            observations[i].data,
            observations[i].confidence
        );
    }
}

Storage Optimization

Minimize storage reads/writes:

solidity
// Pack related data
struct PackedAgent {
    uint128 id;
    uint64 createdAt;
    uint32 phase;
    uint32 lastActive;
}

Event Optimization

Use indexed parameters for filtering:

solidity
event ObservationSubmitted(
    address indexed agent,
    string indexed observationType,
    uint8 confidence
);

Next Steps

Built by Olea Computer