Cortensor
  • Home
  • Abstract
    • Value Proposition
    • Whitepaper
      • Page 1: Introduction and Vision
      • Page 2: Architecture and Technical Overview
      • Page 3: Incentive Structure and Tokenomics
      • Page4: Development Roadmap and Phases
      • Page5: Summary
    • Pitch Memo
  • Introduction
    • What is Cortensor?
    • Key Features & Benefits
    • Vision & Mission
    • Team
  • Getting Started
    • Quick Start Guide
    • System Requirements
    • Installation & Setup
      • Getting Test ETH
      • Setup Own RPC Endpoint
      • Router Node Setup
        • Dedicated Ephemeral Node Setup
        • Reverse Proxy Setup
    • Web2 API Reference
    • Web3 SDK Reference
  • Core Concepts
    • Decentralized AI Inference
      • Community-Powered Network
      • Gamification and Quality Control
      • Incentive Structure
    • Universal AI Accessibility
    • Multi-layer Blockchain Architecture
  • Technical Architecture
    • Design Principles
    • Node Roles
    • Node Lifecycle
      • Ephemeral Node State
    • Node Reputation
    • Network & Flow
    • Type of Services
    • Coordination & Orchestration
      • Multi-Oracle Node Reliability & Leadership Rotation
    • AI Inference
      • Open Source Models
        • Centralized vs Decentralized Models
      • Quantization
      • Performance and Scalability
      • LLM Memory
    • Consensus & Validation
      • Proof of Inference (PoI) & Proof of Useful Work (PoUW
      • aka Mining
      • Proof of Useful Work (PoUW)
      • Proof of Useful Work (PoUW) State Machine
        • Miner & Oracle Nodes in PoUW State Machine
      • Sampling in Large Distributed Systems
      • Parallel Processing
      • Embedding Vector Distance
    • Multi-Layered Blockchain Architecture
    • Modular Architecture and Smart Contract Interactions
      • Session Queue
      • Node Pool
      • Session Payment
    • Mining Overview
    • User Interaction & Node Communication
      • Session, Session Queue, Router, and Miner in Cortensor
    • Data Management
      • IPFS Integration
    • Security & Privacy
    • Dashboard
    • Development Previews
      • Multiple Miners Collaboration with Oracle Node
      • Web3 SDK Client & Session/Session Queue Interaction
    • Technical Threads
      • AI Agents and Cortensor's Decentralized AI Inference
    • Infographic Archive
  • Community & Ecosystem
    • Tokenomics
      • Network Incentive Allocation
      • Token Allocations & Safe Wallet Management
    • Ecosystem Flow
    • Developer Ecosystem
    • Staking Pool Overview
    • Contributing to Cortensor
    • Incentives & Reward System
    • Governance & Compliance
    • Safety Measures and Restricted Addresses
    • Mini Internal Hackathon
    • Buyback Program
    • Liquidity Additions
    • Partnerships
      • Partnership Offering for Demand-Side Partnerships
    • Community Testing
      • Closed Alpha Testing Phase #1
        • Closed Alpha Testing Phase #1 Contest: Closing & Winners Announcement
      • Closed Alpha Testing Phase #2
      • Closed Alpha Testing Phase #3
      • Closed Alpha Testing Phase #4
      • Discord Roles & Mainnet Privileges
      • DevNet Mapping
      • DevNet Modules & Parameters
    • Jobs
      • Technical Writer
      • Communication & Social Media Manager
      • Web3 Frontend Developer
      • Distributed Systems Engineer
  • Integration Guide
    • Web2
      • REST API
      • WebSocket
      • Client SDK
    • Web3
      • Web3 SDK
  • Use Cases
  • Roadmap
    • Technical Roadmap: Launch to Next 365 Days Breakdown
    • Long-term Vision: Beyond Inference
  • Glossary
  • Legal
    • Terms of Use
    • Privacy Policy
    • Disclaimer
    • Agreement for Sale of Tokens
Powered by GitBook
On this page
  • Setup
  • Session Management
  • Task Management
  • Events
  • Listening to Events
  • Session Modes
  • Production Notes
  • Error Handling
  1. Getting Started

Web3 SDK Reference

Status: WIP — Early Draft

This document provides a comprehensive reference for interacting with Cortensor smart contracts using the ethers.js library. It covers session and task management, along with contract event listening for dApp integrations.


Setup

Dependencies

npm install ethers

Configuration

const SESSION_V2_ADDRESS = "0x...";        // SessionV2 contract address
const SESSION_QUEUE_V2_ADDRESS = "0x...";  // SessionQueueV2 contract address
const PUBLIC_RPC_URL = "https://...";      // Public RPC endpoint

ABIs Required

  • SessionV2.json

  • SessionQueueV2.json


Session Management

Create Session

const sessionContract = new ethers.Contract(SESSION_V2_ADDRESS, SessionV2ABI, signer);
const tx = await sessionContract.create(
  "My Session",
  "Metadata",
  await signer.getAddress(),
  1, 3, 1, 0, 0,
  false
);
await tx.wait();

List Sessions by Address

const sessions = await sessionContract.getSessions("0xYourAddress");

Get Session Details

const session = await sessionContract.getSession(0);
const miners = await sessionContract.getSessionMiners(0);

Update Session

await sessionContract.updateSession(
  0,
  "Updated Name",
  "Updated Metadata",
  2, 4, 2
);

Task Management

Submit Task

const queueContract = new ethers.Contract(SESSION_QUEUE_V2_ADDRESS, SessionQueueV2ABI, signer);
const tx = await queueContract.submitTask(0, JSON.stringify({ type: "chat", message: "Hello" }));
await tx.wait();

Get Tasks by Session

const tasks = await queueContract.getTasksBySessionId(0);
const [miners, results] = await queueContract.getTaskResults(0, 0);

Events

SessionV2 Contract Events

Event
Description
Parameters

SessionCreated

New session created

uint256 sessionId, bytes32 sid, address owner, address[] miners

SessionUpdated

Session updated

uint256 indexed sessionId, address indexed updater, uint256 minNumOfNodes, uint256 maxNumOfNodes, uint256 redundant

SessionDeactivated

Session deactivated

uint256 indexed sessionId, address indexed deactivator

TaskSubmitted

Task submitted

uint256 sessionId, string taskData

TaskAssigned

Task assigned to nodes

uint256 sessionId, uint256 taskId, bytes32[] nodeIds

NodeReleased

Node released from session

bytes32 nodeId, uint256 sessionId

RouterAdded

Router added

address routerAddress, string routerInfo

RouterUpdated

Router updated

address routerAddress, string routerInfo

RouterRemoved

Router removed

address routerAddress

AllRoutersRemoved

All routers removed from a session

uint256 indexed sessionId

DedicatedNodeAdded

Dedicated node added

address indexed nodeAddress

DedicatedNodeRemoved

Dedicated node removed

address indexed nodeAddress

DepositToSession

Funds deposited

uint256 indexed sessionId, address indexed from, uint256 amount

WithdrawFromSession

Funds withdrawn

uint256 indexed sessionId, address indexed to, uint256 amount

SessionQueueV2 Contract Events

Event
Description
Parameters

TaskQueued

Task queued

uint256 sessionId, uint256 taskId, uint256 globalId, string taskData

TaskAssigned

Task assigned to miners

uint256 sessionId, uint256 taskId, address[] miners

TaskAcked

Miner acknowledged task

uint256 sessionId, uint256 taskId, address miner

TaskPrecommitted

Miner precommitted

uint256 sessionId, uint256 taskId, address miner

TaskAllPrecommitted

All miners precommitted

uint256 sessionId, uint256 taskId, address[] miners

TaskPrecommitEnded

Precommit phase ended

uint256 sessionId, uint256 taskId

TaskCommitted

Miner committed

uint256 sessionId, uint256 taskId, address miner

TaskAllCommitted

All miners committed

uint256 sessionId, uint256 taskId, address[] miners

TaskCommitEnded

Commit phase ended

uint256 sessionId, uint256 taskId

TaskEnded

Task completed

uint256 sessionId, uint256 taskId, address[] miners


Listening to Events

const provider = new ethers.WebSocketProvider(PUBLIC_RPC_URL.replace('https', 'wss'));
const queueContract = new ethers.Contract(SESSION_QUEUE_V2_ADDRESS, SessionQueueV2ABI, provider);

queueContract.on("TaskEnded", (sessionId, taskId, miners, event) => {
  console.log(`Task ${taskId} in session ${sessionId} completed`);
});

Session Modes

Mode
Description

0

Ephemeral session

1

Hybrid session

2

Dedicated session


Production Notes

  • Use secure RPC providers

  • Wrap all calls in try/catch

  • Monitor gas costs and transaction status

  • Use hardware wallets for critical ops


Error Handling

Use robust logging and retry mechanisms:

try {
  const tx = await contract.fn();
  await tx.wait();
} catch (error) {
  console.error("Contract call failed:", error);
}

This SDK complements the Web2 REST API by enabling full smart contract integration for decentralized apps using Cortensor.

PreviousWeb2 API ReferenceNextCore Concepts

Last updated 5 hours ago