aimarket-channels 2.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AI-Factory Project Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,239 @@
1
+ Metadata-Version: 2.4
2
+ Name: aimarket-channels
3
+ Version: 2.0.0
4
+ Summary: Pre-funded payment channels — off-chain ledger, on-chain settlement
5
+ License: MIT
6
+ Requires-Python: >=3.11
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: aimarket-hub>=3.0.0
10
+ Provides-Extra: dev
11
+ Requires-Dist: pytest>=8; extra == "dev"
12
+ Dynamic: license-file
13
+
14
+ # aimarket-channels
15
+
16
+ ## Documentation
17
+
18
+ | Document | Description |
19
+ |----------|-------------|
20
+ | [User guide](docs/user-guide.md) | Install, configure, verify plugin is loaded |
21
+ | [User cases](docs/user-cases.md) | Personas and cross-plugin workflows |
22
+ | [SDK integration](docs/sdk-integration.md) | Code examples and hook behavior |
23
+
24
+ ---
25
+
26
+ **Pre-funded payment channels for off-chain capability invocation.**
27
+ Open a channel with one on-chain deposit, invoke multiple capabilities off-chain, settle once. Reduces per-call blockchain transactions from N to 1.
28
+
29
+ ---
30
+
31
+ ## When to Use
32
+
33
+ | Scenario | Why this plugin |
34
+ |----------|----------------|
35
+ | Multi-step AI workflows (translate → review → summarize) | One deposit, 3 invocations, 1 settlement — not 3 separate on-chain transactions |
36
+ | High-frequency invocation (100+ calls/hour) | Per-call on-chain TX would cost more in gas than the capability itself |
37
+ | Agent-to-agent orchestration | External AI agents open a channel and run autonomous multi-step plans |
38
+ | Consumer with fixed budget | Pre-fund $5.00, let the orchestrator spend up to that, get refund of remainder |
39
+ | Demo/staging without real on-chain TX | Accept `demo-*` tx hashes for development (configurable) |
40
+
41
+ ---
42
+
43
+ ## Installation
44
+
45
+ ```bash
46
+ pip install aimarket-channels
47
+ ```
48
+
49
+ No configuration required — channels work immediately. Verify:
50
+
51
+ ```bash
52
+ curl -X POST http://localhost:9083/ai-market/v2/channel/open \
53
+ -H "Content-Type: application/json" \
54
+ -d '{"deposit_usd": 3.00}' | jq '.channel.channel_id'
55
+ ```
56
+
57
+ ---
58
+
59
+ ## API Endpoints
60
+
61
+ | Method | Path | Description |
62
+ |--------|------|-------------|
63
+ | `POST` | `/ai-market/v2/channel/open` | Open a pre-funded payment channel |
64
+ | `POST` | `/ai-market/v2/channel/close` | Close channel, compute settlement, refund remainder |
65
+
66
+ ### Open Channel
67
+
68
+ ```bash
69
+ curl -X POST https://modelmarket.dev/ai-market/v2/channel/open \
70
+ -H "Content-Type: application/json" \
71
+ -d '{
72
+ "deposit_usd": 3.00,
73
+ "token": "USDT",
74
+ "chain": "base",
75
+ "wallet": "0x...",
76
+ "tx_hash": "0xabc123..."
77
+ }'
78
+ ```
79
+
80
+ Response:
81
+ ```json
82
+ {
83
+ "channel": {
84
+ "channel_id": "ch_a8f3b2c1d4e5",
85
+ "balance_usd": 3.00,
86
+ "original_deposit_usd": 3.00,
87
+ "used_usd": 0.00,
88
+ "token": "USDT",
89
+ "chain": "base",
90
+ "status": "open",
91
+ "opened_at": "2026-05-22T12:00:00Z",
92
+ "expires_at": "2026-05-23T12:00:00Z"
93
+ }
94
+ }
95
+ ```
96
+
97
+ ### Close Channel
98
+
99
+ ```bash
100
+ curl -X POST https://modelmarket.dev/ai-market/v2/channel/close \
101
+ -H "Content-Type: application/json" \
102
+ -d '{
103
+ "channel_id": "ch_a8f3b2c1d4e5",
104
+ "settle_tx_hash": "0xsettle..."
105
+ }'
106
+ ```
107
+
108
+ Response:
109
+ ```json
110
+ {
111
+ "settlement": {
112
+ "channel_id": "ch_a8f3b2c1d4e5",
113
+ "used_usd": 1.60,
114
+ "refund_usd": 1.40,
115
+ "original_deposit_usd": 3.00,
116
+ "status": "settled"
117
+ }
118
+ }
119
+ ```
120
+
121
+ ---
122
+
123
+ ## End-to-End Example
124
+
125
+ ```python
126
+ import requests
127
+
128
+ HUB = "https://modelmarket.dev"
129
+
130
+ # 1. Open channel — one on-chain TX
131
+ ch = requests.post(f"{HUB}/ai-market/v2/channel/open", json={
132
+ "deposit_usd": 3.00, "tx_hash": "0x_on_chain_deposit"
133
+ }).json()
134
+ ch_id = ch["channel"]["channel_id"]
135
+ print(f"Channel: {ch_id}, balance: $3.00")
136
+
137
+ # 2. Invoke multiple capabilities — all off-chain
138
+ headers = {"X-Payment-Channel": ch_id}
139
+ plan = [
140
+ ("prod-translate", "translate.multi@v2", {"text": "Hello world", "locales": ["ru", "fr"]}),
141
+ ("prod-legal", "legal.review@v1", {"documents": {"main": "..."}}),
142
+ ("prod-summarize", "summarize@v1", {"text": "..."}),
143
+ ]
144
+
145
+ total = 0.0
146
+ for pid, cid, inp in plan:
147
+ r = requests.post(f"{HUB}/ai-market/v2/invoke", json={
148
+ "product_id": pid, "capability_id": cid,
149
+ "source_hub": "local", "input": inp
150
+ }, headers=headers)
151
+ result = r.json()
152
+ total += result.get("price_usd", 0)
153
+ print(f" {cid}: ${result.get('price_usd', 0):.2f} {'OK' if result.get('success') else 'FAIL'}")
154
+
155
+ # 3. Close channel — one on-chain TX
156
+ settle = requests.post(f"{HUB}/ai-market/v2/channel/close", json={
157
+ "channel_id": ch_id, "settle_tx_hash": "0x_on_chain_settlement"
158
+ }).json()
159
+
160
+ print(f"Used: ${settle['settlement']['used_usd']:.2f}")
161
+ print(f"Refund: ${settle['settlement']['refund_usd']:.2f}")
162
+ print(f"Saved {(len(plan) - 1)} on-chain transactions")
163
+ ```
164
+
165
+ ---
166
+
167
+ ## Channel Lifecycle
168
+
169
+ ```
170
+ OPEN (on-chain deposit)
171
+
172
+ ├── INVOKE (off-chain debit) ──┐
173
+ ├── INVOKE (off-chain debit) ──┤ N invocations
174
+ ├── INVOKE (off-chain debit) ──┘
175
+
176
+ ├── SAFETY ABORT → auto-refund to channel
177
+
178
+ CLOSE (on-chain settlement)
179
+ → used_usd sent to providers
180
+ → refund_usd returned to consumer
181
+ ```
182
+
183
+ **Properties:**
184
+ - **24h auto-expiry** — channels close automatically after 24 hours
185
+ - **Atomic safety refund** — blocked invocations refund to channel immediately
186
+ - **Single-writer ledger** — no double-spend possible (hub is sole writer)
187
+ - **Demo mode** — `demo-*` tx hashes accepted for development (configurable)
188
+
189
+ ---
190
+
191
+ ## Configuration
192
+
193
+ | Env Variable | Default | Description |
194
+ |-------------|---------|-------------|
195
+ | `AIMARKET_CHANNEL_MAX_DEPOSIT` | `10000` | Maximum deposit in USD |
196
+ | `AIMARKET_CHANNEL_EXPIRY_HOURS` | `24` | Channel auto-expiry time |
197
+ | `AIFACTORY_PAYMENT_VERIFY_STUB` | `1` | Accept `demo-*` tx hashes (dev) |
198
+
199
+ ---
200
+
201
+ ## Recommended Deployment
202
+
203
+ | Environment | Recommendation |
204
+ |-------------|---------------|
205
+ | Development | Stub verification on, demo tx hashes accepted |
206
+ | Staging | Testnet RPC (Base Sepolia), real tx verification |
207
+ | Production | Mainnet RPC, real on-chain deposits required |
208
+
209
+ **Combine with:**
210
+ - `aimarket-safety` — safety blocks auto-refund to channel
211
+ - `aimarket-orchestrator` — orchestrator opens one channel per task, runs multi-step plan
212
+ - `aimarket-streaming` — per-chunk billing debits the channel incrementally
213
+
214
+ ---
215
+
216
+ ## Performance
217
+
218
+ | Metric | Value |
219
+ |--------|-------|
220
+ | Channel open latency | < 5ms (in-memory ledger) |
221
+ | Debit latency | < 1ms |
222
+ | Close + settlement computation | < 2ms |
223
+ | Concurrent channels | Unlimited (in-memory, single-process) |
224
+ | Production upgrade path | Replace `ChannelLedger` with PostgreSQL for multi-process |
225
+
226
+ ---
227
+
228
+ ## Security Considerations
229
+
230
+ - **No custody** — the protocol never holds funds. Channels are simulated constructs for the reference implementation. Production uses on-chain escrow contracts
231
+ - **Sequential ledger** — single-writer (hub process), no double-spend vector
232
+ - **Nonce replay protection** — each channel is bound to a single session
233
+ - **Demo tx hashes must be disabled in production** — set `AIFACTORY_PAYMENT_VERIFY_STUB=0`
234
+
235
+ ---
236
+
237
+ ## License
238
+
239
+ MIT · Maintained by AI-Factory · [GitHub](https://github.com/ai-factory/aimarket-channels)
@@ -0,0 +1,226 @@
1
+ # aimarket-channels
2
+
3
+ ## Documentation
4
+
5
+ | Document | Description |
6
+ |----------|-------------|
7
+ | [User guide](docs/user-guide.md) | Install, configure, verify plugin is loaded |
8
+ | [User cases](docs/user-cases.md) | Personas and cross-plugin workflows |
9
+ | [SDK integration](docs/sdk-integration.md) | Code examples and hook behavior |
10
+
11
+ ---
12
+
13
+ **Pre-funded payment channels for off-chain capability invocation.**
14
+ Open a channel with one on-chain deposit, invoke multiple capabilities off-chain, settle once. Reduces per-call blockchain transactions from N to 1.
15
+
16
+ ---
17
+
18
+ ## When to Use
19
+
20
+ | Scenario | Why this plugin |
21
+ |----------|----------------|
22
+ | Multi-step AI workflows (translate → review → summarize) | One deposit, 3 invocations, 1 settlement — not 3 separate on-chain transactions |
23
+ | High-frequency invocation (100+ calls/hour) | Per-call on-chain TX would cost more in gas than the capability itself |
24
+ | Agent-to-agent orchestration | External AI agents open a channel and run autonomous multi-step plans |
25
+ | Consumer with fixed budget | Pre-fund $5.00, let the orchestrator spend up to that, get refund of remainder |
26
+ | Demo/staging without real on-chain TX | Accept `demo-*` tx hashes for development (configurable) |
27
+
28
+ ---
29
+
30
+ ## Installation
31
+
32
+ ```bash
33
+ pip install aimarket-channels
34
+ ```
35
+
36
+ No configuration required — channels work immediately. Verify:
37
+
38
+ ```bash
39
+ curl -X POST http://localhost:9083/ai-market/v2/channel/open \
40
+ -H "Content-Type: application/json" \
41
+ -d '{"deposit_usd": 3.00}' | jq '.channel.channel_id'
42
+ ```
43
+
44
+ ---
45
+
46
+ ## API Endpoints
47
+
48
+ | Method | Path | Description |
49
+ |--------|------|-------------|
50
+ | `POST` | `/ai-market/v2/channel/open` | Open a pre-funded payment channel |
51
+ | `POST` | `/ai-market/v2/channel/close` | Close channel, compute settlement, refund remainder |
52
+
53
+ ### Open Channel
54
+
55
+ ```bash
56
+ curl -X POST https://modelmarket.dev/ai-market/v2/channel/open \
57
+ -H "Content-Type: application/json" \
58
+ -d '{
59
+ "deposit_usd": 3.00,
60
+ "token": "USDT",
61
+ "chain": "base",
62
+ "wallet": "0x...",
63
+ "tx_hash": "0xabc123..."
64
+ }'
65
+ ```
66
+
67
+ Response:
68
+ ```json
69
+ {
70
+ "channel": {
71
+ "channel_id": "ch_a8f3b2c1d4e5",
72
+ "balance_usd": 3.00,
73
+ "original_deposit_usd": 3.00,
74
+ "used_usd": 0.00,
75
+ "token": "USDT",
76
+ "chain": "base",
77
+ "status": "open",
78
+ "opened_at": "2026-05-22T12:00:00Z",
79
+ "expires_at": "2026-05-23T12:00:00Z"
80
+ }
81
+ }
82
+ ```
83
+
84
+ ### Close Channel
85
+
86
+ ```bash
87
+ curl -X POST https://modelmarket.dev/ai-market/v2/channel/close \
88
+ -H "Content-Type: application/json" \
89
+ -d '{
90
+ "channel_id": "ch_a8f3b2c1d4e5",
91
+ "settle_tx_hash": "0xsettle..."
92
+ }'
93
+ ```
94
+
95
+ Response:
96
+ ```json
97
+ {
98
+ "settlement": {
99
+ "channel_id": "ch_a8f3b2c1d4e5",
100
+ "used_usd": 1.60,
101
+ "refund_usd": 1.40,
102
+ "original_deposit_usd": 3.00,
103
+ "status": "settled"
104
+ }
105
+ }
106
+ ```
107
+
108
+ ---
109
+
110
+ ## End-to-End Example
111
+
112
+ ```python
113
+ import requests
114
+
115
+ HUB = "https://modelmarket.dev"
116
+
117
+ # 1. Open channel — one on-chain TX
118
+ ch = requests.post(f"{HUB}/ai-market/v2/channel/open", json={
119
+ "deposit_usd": 3.00, "tx_hash": "0x_on_chain_deposit"
120
+ }).json()
121
+ ch_id = ch["channel"]["channel_id"]
122
+ print(f"Channel: {ch_id}, balance: $3.00")
123
+
124
+ # 2. Invoke multiple capabilities — all off-chain
125
+ headers = {"X-Payment-Channel": ch_id}
126
+ plan = [
127
+ ("prod-translate", "translate.multi@v2", {"text": "Hello world", "locales": ["ru", "fr"]}),
128
+ ("prod-legal", "legal.review@v1", {"documents": {"main": "..."}}),
129
+ ("prod-summarize", "summarize@v1", {"text": "..."}),
130
+ ]
131
+
132
+ total = 0.0
133
+ for pid, cid, inp in plan:
134
+ r = requests.post(f"{HUB}/ai-market/v2/invoke", json={
135
+ "product_id": pid, "capability_id": cid,
136
+ "source_hub": "local", "input": inp
137
+ }, headers=headers)
138
+ result = r.json()
139
+ total += result.get("price_usd", 0)
140
+ print(f" {cid}: ${result.get('price_usd', 0):.2f} {'OK' if result.get('success') else 'FAIL'}")
141
+
142
+ # 3. Close channel — one on-chain TX
143
+ settle = requests.post(f"{HUB}/ai-market/v2/channel/close", json={
144
+ "channel_id": ch_id, "settle_tx_hash": "0x_on_chain_settlement"
145
+ }).json()
146
+
147
+ print(f"Used: ${settle['settlement']['used_usd']:.2f}")
148
+ print(f"Refund: ${settle['settlement']['refund_usd']:.2f}")
149
+ print(f"Saved {(len(plan) - 1)} on-chain transactions")
150
+ ```
151
+
152
+ ---
153
+
154
+ ## Channel Lifecycle
155
+
156
+ ```
157
+ OPEN (on-chain deposit)
158
+
159
+ ├── INVOKE (off-chain debit) ──┐
160
+ ├── INVOKE (off-chain debit) ──┤ N invocations
161
+ ├── INVOKE (off-chain debit) ──┘
162
+
163
+ ├── SAFETY ABORT → auto-refund to channel
164
+
165
+ CLOSE (on-chain settlement)
166
+ → used_usd sent to providers
167
+ → refund_usd returned to consumer
168
+ ```
169
+
170
+ **Properties:**
171
+ - **24h auto-expiry** — channels close automatically after 24 hours
172
+ - **Atomic safety refund** — blocked invocations refund to channel immediately
173
+ - **Single-writer ledger** — no double-spend possible (hub is sole writer)
174
+ - **Demo mode** — `demo-*` tx hashes accepted for development (configurable)
175
+
176
+ ---
177
+
178
+ ## Configuration
179
+
180
+ | Env Variable | Default | Description |
181
+ |-------------|---------|-------------|
182
+ | `AIMARKET_CHANNEL_MAX_DEPOSIT` | `10000` | Maximum deposit in USD |
183
+ | `AIMARKET_CHANNEL_EXPIRY_HOURS` | `24` | Channel auto-expiry time |
184
+ | `AIFACTORY_PAYMENT_VERIFY_STUB` | `1` | Accept `demo-*` tx hashes (dev) |
185
+
186
+ ---
187
+
188
+ ## Recommended Deployment
189
+
190
+ | Environment | Recommendation |
191
+ |-------------|---------------|
192
+ | Development | Stub verification on, demo tx hashes accepted |
193
+ | Staging | Testnet RPC (Base Sepolia), real tx verification |
194
+ | Production | Mainnet RPC, real on-chain deposits required |
195
+
196
+ **Combine with:**
197
+ - `aimarket-safety` — safety blocks auto-refund to channel
198
+ - `aimarket-orchestrator` — orchestrator opens one channel per task, runs multi-step plan
199
+ - `aimarket-streaming` — per-chunk billing debits the channel incrementally
200
+
201
+ ---
202
+
203
+ ## Performance
204
+
205
+ | Metric | Value |
206
+ |--------|-------|
207
+ | Channel open latency | < 5ms (in-memory ledger) |
208
+ | Debit latency | < 1ms |
209
+ | Close + settlement computation | < 2ms |
210
+ | Concurrent channels | Unlimited (in-memory, single-process) |
211
+ | Production upgrade path | Replace `ChannelLedger` with PostgreSQL for multi-process |
212
+
213
+ ---
214
+
215
+ ## Security Considerations
216
+
217
+ - **No custody** — the protocol never holds funds. Channels are simulated constructs for the reference implementation. Production uses on-chain escrow contracts
218
+ - **Sequential ledger** — single-writer (hub process), no double-spend vector
219
+ - **Nonce replay protection** — each channel is bound to a single session
220
+ - **Demo tx hashes must be disabled in production** — set `AIFACTORY_PAYMENT_VERIFY_STUB=0`
221
+
222
+ ---
223
+
224
+ ## License
225
+
226
+ MIT · Maintained by AI-Factory · [GitHub](https://github.com/ai-factory/aimarket-channels)
@@ -0,0 +1 @@
1
+ # aimarket-channels plugin
@@ -0,0 +1,8 @@
1
+ """Channels — re-export shim from aimarket_hub.channels (canonical).
2
+
3
+ The hub-core module (SQLite-backed, integer cents, rate limiting, background
4
+ sweep) is the single source of truth. This plugin-level module is kept as a
5
+ re-export shim for the plugin entry_point manifest.
6
+ """
7
+
8
+ from aimarket_hub.channels import * # noqa: F401, F403
@@ -0,0 +1,25 @@
1
+ """channels plugin — payment channel infrastructure."""
2
+
3
+ from aimarket_hub.plugin import HubPlugin
4
+
5
+
6
+ class ChannelsPlugin(HubPlugin):
7
+ name = "aimarket-channels"
8
+ version = "2.0.0"
9
+ description = "Pre-funded payment channels — off-chain ledger, on-chain settlement"
10
+ homepage = "https://github.com/ai-factory/aimarket-channels"
11
+ category = "infrastructure"
12
+
13
+ def get_manifest_extension(self):
14
+ return {
15
+ "channels": {
16
+ "enabled": True,
17
+ "endpoints": {
18
+ "open": "/ai-market/v2/channel/open",
19
+ "close": "/ai-market/v2/channel/close",
20
+ },
21
+ "max_deposit_usd": 10_000,
22
+ "expiry_hours": 24,
23
+ "demo_mode": True,
24
+ }
25
+ }
@@ -0,0 +1,239 @@
1
+ Metadata-Version: 2.4
2
+ Name: aimarket-channels
3
+ Version: 2.0.0
4
+ Summary: Pre-funded payment channels — off-chain ledger, on-chain settlement
5
+ License: MIT
6
+ Requires-Python: >=3.11
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: aimarket-hub>=3.0.0
10
+ Provides-Extra: dev
11
+ Requires-Dist: pytest>=8; extra == "dev"
12
+ Dynamic: license-file
13
+
14
+ # aimarket-channels
15
+
16
+ ## Documentation
17
+
18
+ | Document | Description |
19
+ |----------|-------------|
20
+ | [User guide](docs/user-guide.md) | Install, configure, verify plugin is loaded |
21
+ | [User cases](docs/user-cases.md) | Personas and cross-plugin workflows |
22
+ | [SDK integration](docs/sdk-integration.md) | Code examples and hook behavior |
23
+
24
+ ---
25
+
26
+ **Pre-funded payment channels for off-chain capability invocation.**
27
+ Open a channel with one on-chain deposit, invoke multiple capabilities off-chain, settle once. Reduces per-call blockchain transactions from N to 1.
28
+
29
+ ---
30
+
31
+ ## When to Use
32
+
33
+ | Scenario | Why this plugin |
34
+ |----------|----------------|
35
+ | Multi-step AI workflows (translate → review → summarize) | One deposit, 3 invocations, 1 settlement — not 3 separate on-chain transactions |
36
+ | High-frequency invocation (100+ calls/hour) | Per-call on-chain TX would cost more in gas than the capability itself |
37
+ | Agent-to-agent orchestration | External AI agents open a channel and run autonomous multi-step plans |
38
+ | Consumer with fixed budget | Pre-fund $5.00, let the orchestrator spend up to that, get refund of remainder |
39
+ | Demo/staging without real on-chain TX | Accept `demo-*` tx hashes for development (configurable) |
40
+
41
+ ---
42
+
43
+ ## Installation
44
+
45
+ ```bash
46
+ pip install aimarket-channels
47
+ ```
48
+
49
+ No configuration required — channels work immediately. Verify:
50
+
51
+ ```bash
52
+ curl -X POST http://localhost:9083/ai-market/v2/channel/open \
53
+ -H "Content-Type: application/json" \
54
+ -d '{"deposit_usd": 3.00}' | jq '.channel.channel_id'
55
+ ```
56
+
57
+ ---
58
+
59
+ ## API Endpoints
60
+
61
+ | Method | Path | Description |
62
+ |--------|------|-------------|
63
+ | `POST` | `/ai-market/v2/channel/open` | Open a pre-funded payment channel |
64
+ | `POST` | `/ai-market/v2/channel/close` | Close channel, compute settlement, refund remainder |
65
+
66
+ ### Open Channel
67
+
68
+ ```bash
69
+ curl -X POST https://modelmarket.dev/ai-market/v2/channel/open \
70
+ -H "Content-Type: application/json" \
71
+ -d '{
72
+ "deposit_usd": 3.00,
73
+ "token": "USDT",
74
+ "chain": "base",
75
+ "wallet": "0x...",
76
+ "tx_hash": "0xabc123..."
77
+ }'
78
+ ```
79
+
80
+ Response:
81
+ ```json
82
+ {
83
+ "channel": {
84
+ "channel_id": "ch_a8f3b2c1d4e5",
85
+ "balance_usd": 3.00,
86
+ "original_deposit_usd": 3.00,
87
+ "used_usd": 0.00,
88
+ "token": "USDT",
89
+ "chain": "base",
90
+ "status": "open",
91
+ "opened_at": "2026-05-22T12:00:00Z",
92
+ "expires_at": "2026-05-23T12:00:00Z"
93
+ }
94
+ }
95
+ ```
96
+
97
+ ### Close Channel
98
+
99
+ ```bash
100
+ curl -X POST https://modelmarket.dev/ai-market/v2/channel/close \
101
+ -H "Content-Type: application/json" \
102
+ -d '{
103
+ "channel_id": "ch_a8f3b2c1d4e5",
104
+ "settle_tx_hash": "0xsettle..."
105
+ }'
106
+ ```
107
+
108
+ Response:
109
+ ```json
110
+ {
111
+ "settlement": {
112
+ "channel_id": "ch_a8f3b2c1d4e5",
113
+ "used_usd": 1.60,
114
+ "refund_usd": 1.40,
115
+ "original_deposit_usd": 3.00,
116
+ "status": "settled"
117
+ }
118
+ }
119
+ ```
120
+
121
+ ---
122
+
123
+ ## End-to-End Example
124
+
125
+ ```python
126
+ import requests
127
+
128
+ HUB = "https://modelmarket.dev"
129
+
130
+ # 1. Open channel — one on-chain TX
131
+ ch = requests.post(f"{HUB}/ai-market/v2/channel/open", json={
132
+ "deposit_usd": 3.00, "tx_hash": "0x_on_chain_deposit"
133
+ }).json()
134
+ ch_id = ch["channel"]["channel_id"]
135
+ print(f"Channel: {ch_id}, balance: $3.00")
136
+
137
+ # 2. Invoke multiple capabilities — all off-chain
138
+ headers = {"X-Payment-Channel": ch_id}
139
+ plan = [
140
+ ("prod-translate", "translate.multi@v2", {"text": "Hello world", "locales": ["ru", "fr"]}),
141
+ ("prod-legal", "legal.review@v1", {"documents": {"main": "..."}}),
142
+ ("prod-summarize", "summarize@v1", {"text": "..."}),
143
+ ]
144
+
145
+ total = 0.0
146
+ for pid, cid, inp in plan:
147
+ r = requests.post(f"{HUB}/ai-market/v2/invoke", json={
148
+ "product_id": pid, "capability_id": cid,
149
+ "source_hub": "local", "input": inp
150
+ }, headers=headers)
151
+ result = r.json()
152
+ total += result.get("price_usd", 0)
153
+ print(f" {cid}: ${result.get('price_usd', 0):.2f} {'OK' if result.get('success') else 'FAIL'}")
154
+
155
+ # 3. Close channel — one on-chain TX
156
+ settle = requests.post(f"{HUB}/ai-market/v2/channel/close", json={
157
+ "channel_id": ch_id, "settle_tx_hash": "0x_on_chain_settlement"
158
+ }).json()
159
+
160
+ print(f"Used: ${settle['settlement']['used_usd']:.2f}")
161
+ print(f"Refund: ${settle['settlement']['refund_usd']:.2f}")
162
+ print(f"Saved {(len(plan) - 1)} on-chain transactions")
163
+ ```
164
+
165
+ ---
166
+
167
+ ## Channel Lifecycle
168
+
169
+ ```
170
+ OPEN (on-chain deposit)
171
+
172
+ ├── INVOKE (off-chain debit) ──┐
173
+ ├── INVOKE (off-chain debit) ──┤ N invocations
174
+ ├── INVOKE (off-chain debit) ──┘
175
+
176
+ ├── SAFETY ABORT → auto-refund to channel
177
+
178
+ CLOSE (on-chain settlement)
179
+ → used_usd sent to providers
180
+ → refund_usd returned to consumer
181
+ ```
182
+
183
+ **Properties:**
184
+ - **24h auto-expiry** — channels close automatically after 24 hours
185
+ - **Atomic safety refund** — blocked invocations refund to channel immediately
186
+ - **Single-writer ledger** — no double-spend possible (hub is sole writer)
187
+ - **Demo mode** — `demo-*` tx hashes accepted for development (configurable)
188
+
189
+ ---
190
+
191
+ ## Configuration
192
+
193
+ | Env Variable | Default | Description |
194
+ |-------------|---------|-------------|
195
+ | `AIMARKET_CHANNEL_MAX_DEPOSIT` | `10000` | Maximum deposit in USD |
196
+ | `AIMARKET_CHANNEL_EXPIRY_HOURS` | `24` | Channel auto-expiry time |
197
+ | `AIFACTORY_PAYMENT_VERIFY_STUB` | `1` | Accept `demo-*` tx hashes (dev) |
198
+
199
+ ---
200
+
201
+ ## Recommended Deployment
202
+
203
+ | Environment | Recommendation |
204
+ |-------------|---------------|
205
+ | Development | Stub verification on, demo tx hashes accepted |
206
+ | Staging | Testnet RPC (Base Sepolia), real tx verification |
207
+ | Production | Mainnet RPC, real on-chain deposits required |
208
+
209
+ **Combine with:**
210
+ - `aimarket-safety` — safety blocks auto-refund to channel
211
+ - `aimarket-orchestrator` — orchestrator opens one channel per task, runs multi-step plan
212
+ - `aimarket-streaming` — per-chunk billing debits the channel incrementally
213
+
214
+ ---
215
+
216
+ ## Performance
217
+
218
+ | Metric | Value |
219
+ |--------|-------|
220
+ | Channel open latency | < 5ms (in-memory ledger) |
221
+ | Debit latency | < 1ms |
222
+ | Close + settlement computation | < 2ms |
223
+ | Concurrent channels | Unlimited (in-memory, single-process) |
224
+ | Production upgrade path | Replace `ChannelLedger` with PostgreSQL for multi-process |
225
+
226
+ ---
227
+
228
+ ## Security Considerations
229
+
230
+ - **No custody** — the protocol never holds funds. Channels are simulated constructs for the reference implementation. Production uses on-chain escrow contracts
231
+ - **Sequential ledger** — single-writer (hub process), no double-spend vector
232
+ - **Nonce replay protection** — each channel is bound to a single session
233
+ - **Demo tx hashes must be disabled in production** — set `AIFACTORY_PAYMENT_VERIFY_STUB=0`
234
+
235
+ ---
236
+
237
+ ## License
238
+
239
+ MIT · Maintained by AI-Factory · [GitHub](https://github.com/ai-factory/aimarket-channels)
@@ -0,0 +1,12 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ aimarket_channels/__init__.py
5
+ aimarket_channels/channels.py
6
+ aimarket_channels/plugin.py
7
+ aimarket_channels.egg-info/PKG-INFO
8
+ aimarket_channels.egg-info/SOURCES.txt
9
+ aimarket_channels.egg-info/dependency_links.txt
10
+ aimarket_channels.egg-info/entry_points.txt
11
+ aimarket_channels.egg-info/requires.txt
12
+ aimarket_channels.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [aimarket.plugins]
2
+ channels = aimarket_channels.plugin:ChannelsPlugin
@@ -0,0 +1,4 @@
1
+ aimarket-hub>=3.0.0
2
+
3
+ [dev]
4
+ pytest>=8
@@ -0,0 +1 @@
1
+ aimarket_channels
@@ -0,0 +1,21 @@
1
+ [build-system]
2
+ requires = ["setuptools>=75", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "aimarket-channels"
7
+ version = "2.0.0"
8
+ description = "Pre-funded payment channels — off-chain ledger, on-chain settlement"
9
+ license = {text = "MIT"}
10
+ readme = "README.md"
11
+ requires-python = ">=3.11"
12
+ dependencies = ["aimarket-hub>=3.0.0"]
13
+
14
+ [project.optional-dependencies]
15
+ dev = ["pytest>=8"]
16
+
17
+ [project.entry-points."aimarket.plugins"]
18
+ channels = "aimarket_channels.plugin:ChannelsPlugin"
19
+
20
+ [tool.setuptools.packages.find]
21
+ include = ["aimarket_channels*"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+