2026-06-17 01:32:06 +03:00

209 lines
10 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ERC-8183 Agentic Commerce
On-chain job escrows between AI agents on EVM chains. A **client** creates a job, funds an escrow in an ERC-20 payment token, and a **provider** (worker) submits a deliverable. Settlement releases the escrow to the provider or refunds the client. Three contracts work together:
- **Commerce** (`AgenticCommerce`) — jobs, escrow, and the deliverable lifecycle.
- **EvaluatorRouter** — routes settlement/disputes to a policy (optimistic model).
- **OptimisticPolicy** — an optimistic dispute window with reject-voting.
`erc8183` commands default to `--chain bsc`. Only `bsc` and `bsctestnet` have known deployments.
## Deployments
| Contract | `bsc` | `bsctestnet` |
|----------|-------|--------------|
| Commerce | `0xea4daa3100a767e86fded867729ae7446476eba6` | `0xa206c0517b6371c6638cd9e4a42cc9f02a33b0de` |
| EvaluatorRouter | `0x51895229e12f9876011789b04f8698af06ccd6da` | `0xd7d36d66d2f1b608a0f943f722d27e3744f66f25` |
| OptimisticPolicy | `0x9c01845705b3078aa2e8cff7520a6376fd766de5` | `0x4f4678d4439fec812ac7674bb3efb4c8f5fb78a6` |
| Payment token (`U`, 18 dec) | `0xce24439f2d9c6a2289f741120fe202248b666666` | `0xc70B8741B8B07A6d61E54fd4B20f22Fa648E5565` |
Budgets and escrow are denominated in the **payment token** (read it from `status` → `paymentToken`), not the native coin. The client wallet must hold ≥ the budget in that token before `fund`; gas is paid separately in the native coin.
## Job lifecycle
Status enum (from `status`): `Open → Funded → Submitted → Completed | Rejected | Expired`.
There are two settlement models. Choose one at `create-job` time via the `evaluator`:
### A. Direct evaluation (evaluator is a trusted address)
```
create-job → set-budget → fund → submit → complete (or reject)
```
The `evaluator` address decides the outcome: it calls `complete` (releases escrow to the provider) or `reject` (refunds the client). Simplest model; no Router/Policy involved.
### B. Optimistic dispute (evaluator = the EvaluatorRouter)
```
create-job → set-budget → register-job → fund → submit → settle (after the dispute window)
```
For this model the job must be wired to the Router/Policy:
- Set **both `--evaluator` and `--hook` to the EvaluatorRouter address** at `create-job`. A zero hook reverts `HookRequired()`; an evaluator that isn't the Router reverts `RouterNotEvaluator()` at `register-job`.
- **`register-job` MUST run before `fund`** — funding with the Router as hook reverts `PolicyNotSet()` until a policy is registered.
- After `submit`, anyone can `settle` once the dispute window elapses (releases escrow to the provider). The client may `dispute` within the window; voters `vote-reject`; reaching the reject quorum refunds the client. `mark-expired` finalises a past-expiry job to `Expired`.
- In this model `complete`/`reject` are not caller-invoked — settlement runs through the Router/Policy.
### Expiry constraints
`--expires-at` is a unix timestamp (seconds). `create-job` requires it to be in the future and ≤ now + 365 days. Separately, `submit` requires `now ≤ expiredAt − disputeWindow` (the Policy's window: **7 days on `bsc`, 1 day on `bsctestnet`**), so a too-short expiry creates and funds fine but can't be submitted. Use roughly a 30-day expiry. After expiry, the client can `claim-refund`.
## Commerce commands
### Create a job
```bash
twak erc8183 create-job \
--provider <address> \
--evaluator <address> \
--expires-at <unix> \
--description <text> \
--hook <address> \
--json
```
Caller becomes the client. For the optimistic model set `--evaluator` and `--hook` to the EvaluatorRouter address (see model B). Output: `{ jobId, status: "Open", hash, chain, explorer }`.
### Set / change the provider
```bash
twak erc8183 set-provider <jobId> --provider <address> --json
```
Client-only, allowed while the job is still `Open`. Output: `{ jobId, provider, hash, chain, explorer }`.
### Set the budget
```bash
twak erc8183 set-budget <jobId> --amount <atomic> --json
```
`--amount` is in atomic units of the payment token (e.g. `1000000000000000000` = 1.0 of an 18-decimal token). Output: `{ jobId, amount, hash, chain, explorer }`.
### Fund the escrow
```bash
twak erc8183 fund <jobId> [--expected-budget <atomic>] --json
```
Two transactions: approves exactly the budget (the on-chain value, or the `--expected-budget` pin) to the Commerce contract, then deposits it into escrow. The budget must be set first (else `VALIDATION_ERROR`). `--expected-budget` pins the amount: the contract's `BudgetMismatch()` guard reverts if the on-chain budget differs, closing the race between reading the budget and funding; omitted, twak reads the current on-chain budget and funds that. Zero or non-numeric values fail fast with `VALIDATION_ERROR`. Output: `{ jobId, status: "Funded", approveHash, hash, chain, explorer }`.
### Submit a deliverable
```bash
twak erc8183 submit <jobId> --deliverable <bytes32> [--opt-params <hex>] --json
```
`--deliverable` is a `0x`-prefixed 32-byte commitment (e.g. a content hash). In the optimistic model, pass the deliverable manifest in `--opt-params` — the Router forwards it to the policy, which re-emits it in the `JobInitialised` event where off-chain evaluators read it (e.g. hex-encoded `{"deliverable_url":"ipfs://…"}`). A submit without it emits empty optParams, so evaluators cannot locate the deliverable. Output: `{ jobId, status: "Submitted", hash, chain, explorer }`.
### Complete (direct-evaluator model)
```bash
twak erc8183 complete <jobId> --reason <bytes32> --json
```
Evaluator marks a submitted job complete, releasing the escrow to the provider. `--reason` is an optional bytes32 attestation (defaults to zero). Output: `{ jobId, status: "Completed", released, hash, chain, explorer }` (`released` = amount paid out, or `null`).
### Reject (direct-evaluator model)
```bash
twak erc8183 reject <jobId> --reason <bytes32> --json
```
Refunds the client if the job was funded. `--reason` optional bytes32. Output: `{ jobId, status: "Rejected", refunded, hash, chain, explorer }`.
### Claim a refund
```bash
twak erc8183 claim-refund <jobId> --json
```
Client claims the escrow back from an expired job. Output: `{ jobId, status: "Expired", refunded, hash, chain, explorer }`.
### Job status (read-only)
```bash
twak erc8183 status <jobId> --json
```
Output: `{ jobId, status, client, provider, evaluator, description, budget, expiredAt, submittedAt, deliverable, hook, paymentToken, chain }` — `status` is the enum name, `budget`/`expiredAt`/`submittedAt` are decimal strings.
## EvaluatorRouter commands (optimistic model)
### Register a job with a policy
```bash
twak erc8183 register-job <jobId> --policy <address> --json
```
`--policy` defaults to the deployed OptimisticPolicy. Must run **before** `fund` in the optimistic model. Output: `{ jobId, policy, hash, chain, explorer }`.
### Settle through the policy
```bash
twak erc8183 settle <jobId> --evidence <hex> --json
```
Settles a job via its registered policy after the dispute window. `--evidence` is optional opaque bytes (defaults to empty). Output: `{ jobId, hash, chain, explorer }`.
### Mark expired (permissionless)
```bash
twak erc8183 mark-expired <jobId> --json
```
Finalises a registered job past its expiry to `Expired`. Output: `{ jobId, status: "Expired", hash, chain, explorer }`.
## OptimisticPolicy commands
### Raise a dispute (client)
```bash
twak erc8183 dispute <jobId> --json
```
Output: `{ jobId, hash, chain, explorer }`.
### Vote to reject (voter)
```bash
twak erc8183 vote-reject <jobId> --json
```
Output: `{ jobId, hash, chain, explorer }`.
### Policy info (read-only)
```bash
twak erc8183 policy-info <jobId> --json
```
Reads the job's registered policy via the Router (falling back to the deployed OptimisticPolicy before `register-job`, so pre-flight reads like `disputeWindow` still work). Use it to check `settle` timing and dispute state instead of probing with reverting writes. Output: `{ jobId, policy, registered, disputeWindow, submittedAt, disputed, rejectVotes, voteQuorum, quorumSnapshot, settleableAt, chain }` — `registered`/`disputed` are booleans, the numeric fields are decimal strings, and `settleableAt` (`submittedAt + disputeWindow`, the moment optimistic `settle` unlocks) is `null` until a deliverable is submitted.
## Options
All write subcommands accept `--chain <key>` (default `bsc`; `bsctestnet` supported), `--password <pw>` (resolved from OS keychain / `TWAK_WALLET_PASSWORD` if omitted), and `--json`. `status` and `policy-info` are read-only (no `--password`).
The six Commerce writes (`set-provider`, `set-budget`, `fund`, `submit`, `complete`, `reject`) also accept `--opt-params <hex>` — raw bytes passed through to the contract's trailing `optParams` parameter (`0x`-prefixed, even-length hex; defaults to empty). Invalid hex fails fast with `VALIDATION_ERROR` before any transaction. Only `submit` carries protocol-meaningful content (see above); on the others it is an extension point.
## Common reverts
The contracts use custom errors; a revert surfaces the raw 4-byte selector in the error output, e.g. `{"error": "execution reverted: 0x32d53d69", "errorCode": "TX_FAILED"}`. The most common:
- `0x55c45de1` `HookRequired()` — `create-job` with a zero hook (set `--hook` to the Router for the optimistic model)
- `0xec43ea50` `RouterNotEvaluator()` — `register-job` when `evaluator` isn't the Router
- `0x32d53d69` `PolicyNotSet()` — `fund` before `register-job` (optimistic model)
- `0x15e5dd74` `SubmissionTooLate()` — `submit` after `expiredAt − disputeWindow`
- `0xf7a0748c` `ExpiryTooShort()` / `0xb40b2a0e` `ExpiryTooLong()` — `--expires-at` not in `(now, now + 365d]`
- `0x99b0fc87` `BudgetMismatch()` — on-chain budget changed between read and `fund`
## BSC Testnet
Pass `--chain bsctestnet` to use the testnet deployment (chain ID 97; addresses in the table above). Differences from mainnet:
- The Policy `disputeWindow` is **1 day** (vs 7 days), so the submit floor is looser and optimistic `settle` unlocks after 1 day.
- The payment token `U` on testnet has an `Ownable` `mint` — a non-owner wallet cannot self-mint and cannot swap for it on testnet, so the client wallet must be funded with `U` out-of-band before `fund`.