Skip to content

ritual-net/infernet-sdk

Repository files navigation

Infernet SDK

Tests

The Infernet SDK is a set of smart contracts from Ritual that enable on-chain smart contracts to subscribe to off-chain compute workloads.

Developers can inherit one of two simple interfaces, CallbackConsumer or SubscriptionConsumer in their smart contracts, and consume one-time or recurring computation.

Important

Smart contract architecture, quick-start guides, and in-depth documentation can be found on the Ritual documentation website

Note

Infernet SDK v1.1.0 has been audited by Trail of Bits and Zellic.

Warning

These smart contracts are being provided as is. No guarantee, representation or warranty is being made, express or implied, as to the safety or correctness of the smart contracts.

Local deployment and usage

First, ensure you have Foundry installed locally. A simple way to install is to run the following command:

# Install foundryup, follow instructions
curl -L https://foundry.paradigm.xyz | bash

Building and running

To build, run, or execute other commands, you can reference the Makefile.

The default target (make) will:

  1. Install all dependencies
  2. Clean existing build outputs
  3. Format code
  4. Build code and copy compiled artifacts
  5. Run test suite

Using within your own contracts

To import Infernet as a library, you can install the code in your repo with forge:

forge install https://github.com/ritual-net/infernet-sdk

To integrate with the contracts, the available interfaces can be imported from ./src/consumer:

import {CallbackConsumer} from "infernet/consumer/Callback.sol";

contract MyContract is CallbackConsumer {
   function requestSomeComputeResponse() {
      // This will create a new one-time callback request for off-chain compute
      _requestCompute(...);
   }

   function _receiveCompute(...) internal override {
      // Here you will receive the off-chain compute response
   }
}
import {SubscriptionConsumer} from "infernet/consumer/Subscription.sol";

contract MyContract is SubscriptionConsumer {
   function scheduleComputeResponse() {
      // This will create a new recurring request for off-chain compute
      _createComputeSubscription(...);
   }

   function cancelScheduledComputeResponse() {
      // This will allow you to cancel scheduled requests
      _cancelComputeSubscription(...);
   }

   function _receiveCompute(...) internal override {
      // Here you will receive the off-chain compute output
   }
}

Pruned repository structure

Ignores self-explanatory files:

.
├── .env.sample # Sample env file (used for deploy scripts)
├── compiled # Seperately compiled contracts (via solc)
│   └── Verifier.sol
│       ├── Halo2Verifier.json
│       └── Verifier.sol
├── scripts
│   └── Deploy.sol # Deploy contracts via LibDeploy
├── src
│   ├── Coordinator.sol # Base coordinator
│   ├── EIP712Coordinator.sol # Coordinator w/ EIP-712 delegation support
│   ├── Inbox.sol # Message inbox
│   ├── Registry.sol # Root registry
│   ├── consumer
│   │   ├── Base.sol
│   │   ├── Callback.sol # CallbackConsumer
│   │   └── Subscription.sol # SubscriptionConsumer
│   ├── pattern
│   │   ├── Allowlist.sol # Responding node allowlist pattern
│   │   └── Delegator.sol # Delegated compute pattern (w/ EIP712Coordinator)
│   ├── payments
│   │   ├── Fee.sol # Protocol fee registry
│   │   ├── IVerifier.sol # Verifier interface (implemented downstream)
│   │   ├── Wallet.sol # Escrow wallet
│   │   └── WalletFactory.sol # Wallet factory
│   └── utility
│       ├── Coordinated.sol # Utility restriction for Coordinator-permissioned fns
│       └── Reader.sol # Off-chain reader
└── test
    ├── Coordinated.t.sol
    ├── Coordinator.t.sol
    ├── E2E.t.sol
    ├── EIP712Coordinator.t.sol
    ├── Fee.t.sol
    ├── Inbox.t.sol
    ├── Reader.t.sol
    ├── Registry.t.sol
    ├── Wallet.t.sol
    ├── WalletFactory.t.sol
    ├── ezkl # E2E tests w/ EZKL-generated proofs
    │   ├── BalanceScale.sol
    │   └── DataAttestor.sol
    ├── lib
    │   ├── LibDeploy.sol # Useful deployment library
    │   └── LibSign.sol # Useful EIP-712 signing library
    └── mocks
        ├── MockCoordinated.sol
        ├── MockNode.sol
        ├── MockProtocol.sol
        ├── MockToken.sol
        ├── consumer
        │   ├── AllowlistDelegatorSubscription.sol
        │   ├── AllowlistSubscription.sol
        │   ├── Base.sol
        │   ├── Callback.sol
        │   ├── DelegatorCallback.sol
        │   ├── DelegatorSubscription.sol
        │   └── Subscription.sol
        └── verifier
            ├── Atomic.sol
            ├── Base.sol
            └── Optimistic.sol

Deploying contracts on EVM networks

To deploy the contracts on an EVM chain:

  1. Set up environment variables: Copy the .env.sample file to .env and fill in your values:

    cp .env.sample .env
    

    Edit .env to set your PRIVATE_KEY and RPC_URL:

    PRIVATE_KEY=your_private_key_here
    RPC_URL=your_rpc_url_here
    
  2. Build the contracts:

    make build
    
  3. Deploy the contracts:

    make deploy
    

This will use the scripts/Deploy.sol script to deploy the contracts to the network specified by your RPC_URL.

Note: Ensure your account has sufficient balance for gas fees on the target network.

License

BSD 3-clause Clear