|
| 1 | +# @cfxdevkit/executor |
| 2 | + |
| 3 | +On-chain strategy execution engine for Conflux DevKit — the runtime primitives for building keepers, bots, or AI agents that execute limit orders, DCA, TWAP, and spot swaps on Conflux eSpace. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **`Executor`** — Orchestrator that evaluates jobs on a tick interval, checks price conditions, enforces safety limits, and submits on-chain transactions. |
| 8 | +- **`KeeperClientImpl`** — viem-based client that wraps the `AutomationManager` contract for job creation, cancellation, and forceful execution. |
| 9 | +- **`SafetyGuard`** — Circuit-breaker with configurable per-tick swap caps, per-job retry caps, and a global circuit-breaker on consecutive failures. |
| 10 | +- **`RetryQueue`** — Exponential backoff with jitter for failed job retries. |
| 11 | +- **`PriceChecker`** — Pluggable price source interface + condition evaluator (`gte`/`lte` against a target price). |
| 12 | +- **Full type system** — `Job` union type with per-strategy params (`LimitOrderJob`, `DCAJob`, `TWAPJob`, `SwapJob`) and lifecycle statuses. |
| 13 | +- **Zero framework coupling** — Injectable `AutomationLogger` interface; no React, no HTTP framework required. |
| 14 | + |
| 15 | +## Supported strategies |
| 16 | + |
| 17 | +| Strategy | Status | |
| 18 | +|---|---| |
| 19 | +| Limit order | ✅ Implemented | |
| 20 | +| DCA (Dollar-Cost Averaging) | ✅ Implemented | |
| 21 | +| TWAP | 🔜 Types only (contract support pending) | |
| 22 | +| Spot swap | 🔜 Types only (contract support pending) | |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +```bash |
| 27 | +pnpm add @cfxdevkit/executor |
| 28 | +# or |
| 29 | +npm install @cfxdevkit/executor |
| 30 | +``` |
| 31 | + |
| 32 | +## Peer dependencies |
| 33 | + |
| 34 | +```json |
| 35 | +{ |
| 36 | + "viem": ">=2.0.0" |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +### Basic keeper setup |
| 43 | + |
| 44 | +```ts |
| 45 | +import { Executor, KeeperClientImpl, SafetyGuard, PriceChecker, noopLogger } from '@cfxdevkit/executor'; |
| 46 | +import { createWalletClient, http } from 'viem'; |
| 47 | +import { confluxESpace } from 'viem/chains'; |
| 48 | +import { privateKeyToAccount } from 'viem/accounts'; |
| 49 | + |
| 50 | +const account = privateKeyToAccount('0x...'); |
| 51 | +const walletClient = createWalletClient({ account, chain: confluxESpace, transport: http() }); |
| 52 | + |
| 53 | +const keeperClient = new KeeperClientImpl({ |
| 54 | + walletClient, |
| 55 | + automationManagerAddress: '0x...', |
| 56 | +}); |
| 57 | + |
| 58 | +const safetyGuard = new SafetyGuard({ |
| 59 | + maxSwapsPerTick: 3, |
| 60 | + maxConsecutiveFailures: 5, |
| 61 | + maxRetriesPerJob: 4, |
| 62 | +}); |
| 63 | + |
| 64 | +const priceChecker = new PriceChecker({ |
| 65 | + priceSource: myPriceSource, // implement PriceSource interface |
| 66 | + decimalsResolver: myResolver, // implement DecimalsResolver interface |
| 67 | +}); |
| 68 | + |
| 69 | +const executor = new Executor({ |
| 70 | + keeperClient, |
| 71 | + safetyGuard, |
| 72 | + priceChecker, |
| 73 | + jobStore: myJobStore, // implement JobStore interface |
| 74 | + logger: noopLogger, // or inject your own AutomationLogger |
| 75 | + tickIntervalMs: 15_000, // evaluate jobs every 15 s |
| 76 | +}); |
| 77 | + |
| 78 | +executor.start(); |
| 79 | +``` |
| 80 | + |
| 81 | +### Creating a limit order job |
| 82 | + |
| 83 | +```ts |
| 84 | +import type { LimitOrderStrategy } from '@cfxdevkit/executor'; |
| 85 | + |
| 86 | +const strategy: LimitOrderStrategy = { |
| 87 | + kind: 'limit_order', |
| 88 | + tokenIn: '0xCFX...', |
| 89 | + tokenOut: '0xUSDT...', |
| 90 | + amountIn: '100.0', // human-readable |
| 91 | + targetPrice: '0.38', // trigger when price >= target |
| 92 | + direction: 'gte', |
| 93 | + slippageBps: 50, // 0.5% |
| 94 | + expiresInDays: 7, |
| 95 | +}; |
| 96 | + |
| 97 | +await executor.createJob(strategy, ownerAddress); |
| 98 | +``` |
| 99 | + |
| 100 | +### Creating a DCA job |
| 101 | + |
| 102 | +```ts |
| 103 | +import type { DCAStrategy } from '@cfxdevkit/executor'; |
| 104 | + |
| 105 | +const strategy: DCAStrategy = { |
| 106 | + kind: 'dca', |
| 107 | + tokenIn: '0xUSDT...', |
| 108 | + tokenOut: '0xCFX...', |
| 109 | + amountPerSwap: '50.0', // human-readable, per interval |
| 110 | + intervalHours: 24, |
| 111 | + totalSwaps: 30, |
| 112 | + slippageBps: 100, // 1% |
| 113 | +}; |
| 114 | + |
| 115 | +await executor.createJob(strategy, ownerAddress); |
| 116 | +``` |
| 117 | + |
| 118 | +## Job lifecycle |
| 119 | + |
| 120 | +``` |
| 121 | +pending → active → executed |
| 122 | + ↘ failed (exhausted retries) |
| 123 | + → cancelled (user-cancelled) |
| 124 | + → paused (SafetyGuard circuit-breaker) |
| 125 | +``` |
| 126 | + |
| 127 | +## Conflux Compatibility |
| 128 | + |
| 129 | +| Network | Chain ID | Support | |
| 130 | +|---|---|---| |
| 131 | +| Conflux eSpace Mainnet | 1030 | ✅ | |
| 132 | +| Conflux eSpace Testnet | 71 | ✅ | |
0 commit comments