Getting Started

Add powerful automation to your dApp in under 5 minutes.


Prerequisites

  • Node.js: v16.0.0 or higher
  • Web3.js: v4.0.0 or higher (v1.x is NOT supported)
  • RPC Provider: A WebSocket-enabled RPC provider (e.g., Infura, Alchemy, QuickNode)

1. Installation

Install the ZapChain core plugin and the Web3.js peer dependency:

npm install zapchain@2.0.0

2. Registration & Configuration

Import the plugin and register it with your Web3 instance. You can optionally pass a configuration object to customize storage and logging.

import { Web3 } from 'web3';
import { ZapChainPlugin, LocalStorageAdapter } from 'zapchain';

// 1. Initialize Web3 with a WebSocket Provider
const web3 = new Web3('wss://ethereum-sepolia.publicnode.com');

// 2. Initialize ZapChain with persistence
const zapChain = new ZapChainPlugin({
  storage: new LocalStorageAdapter(), // Persist rules between reloads
  enableLogging: true,                // Show execution logs in console
  useWebSocket: true                  // v2.0: Real-time block monitoring
});

// 3. Register the plugin
web3.registerPlugin(zapChain);

3. Create Your First Rule

Let's create a rule that acts as a "Stop Loss"."If my ETH balance drops below 0.1 ETH, swap 100 USDC to ETH."

const rule = await zapChain.createRule({
  id: 'eth-stop-loss',
  conditions: [{
    type: 'balance',
    tokenAddress: '0x0000000000000000000000000000000000000000', // Native ETH
    operator: '<',
    value: web3.utils.toWei('0.1', 'ether')
  }],
  actions: [{
    type: 'contract_call',
    contractAddress: '0xUniswapRouter...', // Uniswap Router Address
    abi: SWAP_ABI,
    functionName: 'swapExactTokensForETH',
    params: [
      web3.utils.toWei('100', 'mwei'), // AmountIn (100 USDC)
      '0',                             // AmountOutMin (Slippage)
      ['0xA0b8...', '0xC02aa...'],     // Path [USDC, WETH]
      userWalletAddress,               // To
      Math.floor(Date.now() / 1000) + 60 * 20 // Deadline
    ]
  }]
});

// Enable the rule to start watching
await zapChain.enableRule(rule.id);
console.log('Rule active:', rule.id);