Smart Contracts API

REST API for contract management, deployment, monitoring, and disaster recovery.

Base URL

https://api.yourdomain.com/v1/contracts

Authentication

All requests require API key:

curl https://api.yourdomain.com/v1/contracts/contracts \
  -H "Authorization: Bearer YOUR_API_KEY"

Contracts

Create Contract

Register new smart contract.

Endpoint: POST /contracts

Request:

{
  "name": "TokenSale",
  "description": "ERC20 token sale contract",
  "abi": [...],
  "bytecode": "0x606060...",
  "source_code": "contract TokenSale { ... }",
  "network": "ethereum_mainnet",
  "template_id": "template_erc20"
}

Response:

{
  "id": "contract_abc123",
  "name": "TokenSale",
  "version": "1.0.0",
  "status": "created",
  "created_at": "2024-01-15T10:30:00Z"
}

Deploy Contract

Deploy contract to blockchain network.

Endpoint: POST /contracts/:id/deploy

Request:

{
  "network": "ethereum_mainnet",
  "constructor_args": [
    "1000000",
    "0x742d35Cc6634C0532925a3b844Bc9e7595f0DeE4"
  ],
  "gas_limit": 3000000,
  "gas_price": "50",
  "confirmations_required": 3
}

Response:

{
  "deployment_id": "deploy_xyz789",
  "status": "pending",
  "transaction_hash": "0x8a3f...",
  "estimated_cost": {
    "gas_limit": 3000000,
    "gas_price_gwei": 50,
    "total_eth": 0.15,
    "total_usd": 245.50
  }
}

Get Contract Status

Endpoint: GET /contracts/:id

Response:

{
  "id": "contract_abc123",
  "name": "TokenSale",
  "version": "1.0.0",
  "status": "deployed",
  "deployments": [
    {
      "network": "ethereum_mainnet",
      "address": "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984",
      "transaction_hash": "0x8a3f...",
      "block_number": 18542340,
      "deployed_at": "2024-01-15T10:35:22Z",
      "status": "active"
    }
  ],
  "health": {
    "status": "healthy",
    "last_execution": "2024-01-15T14:20:15Z",
    "execution_count": 142,
    "failure_rate": 0.014
  }
}

List Contracts

Endpoint: GET /contracts

Query Parameters:

  • network - Filter by network
  • status - Filter by status
  • template_id - Filter by template
  • limit - Results per page

Versions

Create Version

Create new contract version.

Endpoint: POST /contracts/:id/versions

Request:

{
  "version": "1.1.0",
  "changes": "Added pausable functionality",
  "abi": [...],
  "bytecode": "0x606060...",
  "source_code": "contract TokenSale { ... }"
}

Rollback Version

Rollback to previous version.

Endpoint: POST /contracts/:id/rollback

Request:

{
  "target_version": "1.0.0",
  "reason": "Critical bug in v1.1.0"
}

Execution

Execute Contract Function

Call contract function.

Endpoint: POST /contracts/:id/execute

Request:

{
  "network": "ethereum_mainnet",
  "function": "transfer",
  "args": [
    "0x742d35Cc6634C0532925a3b844Bc9e7595f0DeE4",
    "1000000"
  ],
  "gas_limit": 100000,
  "value": "0",
  "idempotency_key": "tx_unique_123"
}

Response:

{
  "execution_id": "exec_abc",
  "status": "pending",
  "transaction_hash": "0x5b2f...",
  "estimated_confirmation": "2024-01-15T10:32:00Z",
  "idempotency_key": "tx_unique_123"
}

Get Execution Status

Endpoint: GET /executions/:id

Response:

{
  "execution_id": "exec_abc",
  "status": "confirmed",
  "transaction_hash": "0x5b2f...",
  "block_number": 18542355,
  "gas_used": 82450,
  "gas_price_gwei": 50,
  "cost_eth": 0.0041225,
  "cost_usd": 6.75,
  "confirmed_at": "2024-01-15T10:31:45Z",
  "confirmations": 12
}

Monitoring

Get Health Metrics

Endpoint: GET /contracts/:id/health

Response:

{
  "contract_id": "contract_abc123",
  "status": "healthy",
  "metrics": {
    "execution_count": 1420,
    "success_count": 1398,
    "failure_count": 22,
    "success_rate": 0.9845,
    "avg_gas_used": 85420,
    "avg_execution_time_ms": 1250
  },
  "alerts": []
}

Configure Alert Rules

Endpoint: POST /contracts/:id/alerts

Request:

{
  "rule_name": "High Gas Usage",
  "condition": {
    "metric": "gas_used",
    "operator": "greater_than",
    "threshold": 500000
  },
  "actions": [
    {
      "type": "webhook",
      "url": "https://your-app.com/alerts"
    },
    {
      "type": "email",
      "recipients": ["ops@example.com"]
    }
  ]
}

Get Transaction History

Endpoint: GET /contracts/:id/transactions

Query Parameters:

  • from - Start timestamp
  • to - End timestamp
  • status - Filter by status
  • limit - Results per page

Response:

{
  "transactions": [
    {
      "transaction_hash": "0x8a3f...",
      "function": "transfer",
      "from": "0x742d...",
      "to": "0x1f98...",
      "value": "0.5",
      "gas_used": 82450,
      "status": "confirmed",
      "timestamp": "2024-01-15T10:31:45Z"
    }
  ],
  "total": 1420,
  "has_more": true
}

Data Ingestion

Create Ingestion Job

Submit data for processing and contract execution.

Endpoint: POST /ingestion/jobs

Request:

{
  "source_id": "source_oracle",
  "data": {
    "price": 45.23,
    "timestamp": "2024-01-15T10:30:00Z"
  },
  "transformation_rules": ["rule_normalize", "rule_validate"],
  "trigger_contract": "contract_abc123",
  "priority": "high"
}

Response:

{
  "job_id": "job_xyz789",
  "status": "queued",
  "position": 3,
  "estimated_start": "2024-01-15T10:30:15Z"
}

Get Job Status

Endpoint: GET /ingestion/jobs/:id

Response:

{
  "job_id": "job_xyz789",
  "status": "completed",
  "started_at": "2024-01-15T10:30:15Z",
  "completed_at": "2024-01-15T10:30:18Z",
  "result": {
    "transformed_data": {...},
    "contract_triggered": true,
    "execution_id": "exec_abc"
  }
}

Disaster Recovery

Create Backup

Endpoint: POST /backups

Request:

{
  "backup_type": "full",
  "encryption": true,
  "compression": true,
  "retention_days": 90
}

Response:

{
  "backup_id": "backup_abc123",
  "status": "in_progress",
  "backup_type": "full",
  "started_at": "2024-01-15T10:30:00Z",
  "estimated_size_mb": 450
}

List Backups

Endpoint: GET /backups

Response:

{
  "backups": [
    {
      "backup_id": "backup_abc123",
      "backup_type": "full",
      "status": "completed",
      "size_mb": 423,
      "created_at": "2024-01-15T10:30:00Z",
      "expires_at": "2024-04-15T10:30:00Z",
      "checksum": "sha256:a3f2..."
    }
  ]
}

Restore from Backup

Endpoint: POST /backups/:id/restore

Request:

{
  "target_environment": "staging",
  "point_in_time": "2024-01-14T12:00:00Z",
  "verify_integrity": true
}

Response:

{
  "restore_id": "restore_xyz",
  "status": "in_progress",
  "estimated_duration_minutes": 15
}

Horizontal Scaling

Get Cluster Status

Endpoint: GET /scaling/cluster

Response:

{
  "nodes": [
    {
      "node_id": "node_1",
      "status": "active",
      "health": "healthy",
      "metrics": {
        "cpu_utilization": 45,
        "memory_utilization": 62,
        "active_connections": 142,
        "queue_size": 12
      },
      "last_heartbeat": "2024-01-15T10:35:00Z"
    }
  ],
  "total_nodes": 4,
  "active_nodes": 4,
  "load_distribution": "balanced"
}

Configure Auto-Scaling

Endpoint: POST /scaling/policies

Request:

{
  "min_nodes": 2,
  "max_nodes": 10,
  "target_cpu": 70,
  "target_memory": 80,
  "scale_up_threshold": 75,
  "scale_down_threshold": 30,
  "cooldown_minutes": 5
}

Manual Scale Operation

Endpoint: POST /scaling/scale

Request:

{
  "action": "scale_up",
  "node_count": 2
}

Rate Limits

  • Contract Operations: 100/hour
  • Deployments: 10/hour
  • Executions: 1000/hour
  • API Queries: 10000/hour

Error Codes

| Code | Status | Description | |------|--------|-------------| | invalid_abi | 400 | ABI format invalid | | invalid_bytecode | 400 | Bytecode validation failed | | insufficient_gas | 400 | Gas limit too low | | deployment_failed | 500 | Contract deployment failed | | execution_failed | 500 | Contract execution failed | | network_unavailable | 503 | Blockchain network unreachable |

Webhooks

Webhook Events

  • contract.created - Contract registered
  • contract.deployed - Deployment completed
  • execution.started - Function execution started
  • execution.completed - Execution finished
  • execution.failed - Execution failed
  • alert.triggered - Alert rule triggered
  • backup.completed - Backup finished
  • scaling.event - Scaling operation occurred

Webhook Payload

{
  "event": "execution.completed",
  "timestamp": "2024-01-15T10:31:45Z",
  "data": {
    "execution_id": "exec_abc",
    "contract_id": "contract_abc123",
    "transaction_hash": "0x5b2f...",
    "status": "confirmed",
    "gas_used": 82450
  },
  "signature": "sha256=..."
}

SDK

npm install @mesh-software/contracts-sdk
import { SmartContracts } from '@mesh-software/contracts-sdk';

const client = new SmartContracts({ apiKey: 'YOUR_API_KEY' });

// Create and deploy contract
const contract = await client.contracts.create({
  name: 'TokenSale',
  abi: [...],
  bytecode: '0x606060...'
});

const deployment = await client.contracts.deploy(contract.id, {
  network: 'ethereum_mainnet',
  constructor_args: ['1000000']
});

// Execute function
const execution = await client.contracts.execute(contract.id, {
  function: 'transfer',
  args: ['0x742d...', '1000000']
});