@chainpearc/sdk 0.0.0-stage → 0.4.0
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.
- package/README.md +84 -2
- package/dist/index.d.ts +440 -0
- package/dist/index.js +1118 -0
- package/dist/index.js.map +1 -0
- package/package.json +59 -4
package/README.md
CHANGED
|
@@ -1,3 +1,85 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @chainpearc/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@chainpearc/sdk)
|
|
4
|
+
|
|
5
|
+
**Let any app or AI agent find, pay for and rate APIs per call, in USDC on Arc.**
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @chainpearc/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
> **Use Arc testnet for now.** It is the default. ChainPe has not been tested with real USDC on Arc mainnet.
|
|
12
|
+
|
|
13
|
+
## Quickstart
|
|
14
|
+
|
|
15
|
+
Get free test USDC at https://faucet.circle.com (pick Arc Testnet). On Arc, gas is also paid in USDC.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { ChainPe } from '@chainpearc/sdk'
|
|
19
|
+
|
|
20
|
+
const cp = new ChainPe({ privateKey: process.env.PRIVATE_KEY! })
|
|
21
|
+
const res = await cp.fetch('https://chainpe-arc-demo-production.up.railway.app/forecast?city=Mumbai')
|
|
22
|
+
const data = await res.json()
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`cp.fetch` works like `fetch`. If the server answers `402 Payment Required`, it checks the price against your limit, signs a USDC payment, retries and returns the final `Response`.
|
|
26
|
+
|
|
27
|
+
## Find, pay and rate
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
const services = await cp.discover({ query: 'weather', maxPrice: '0.01' })
|
|
31
|
+
|
|
32
|
+
const result = await cp.pay('https://chainpe-arc-demo-production.up.railway.app/forecast?city=Mumbai')
|
|
33
|
+
console.log(result.data) // the parsed response
|
|
34
|
+
console.log(result.payment) // { amount, recipient, network, rail, txHash or transferId }
|
|
35
|
+
|
|
36
|
+
await cp.giveFeedback(services[0].agentId!, 90)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
`discover` searches Circle's Discovery API and the `ChainPeAccounts` contract, so it also finds ChainPe providers Circle has not listed yet. A service's `endpoint` is either a full route (Circle) or the provider's base URL (ChainPe). For a base URL, read `<endpoint>/openapi.json` for its paths.
|
|
40
|
+
|
|
41
|
+
## Two ways to pay
|
|
42
|
+
|
|
43
|
+
| Rail | How it works | Needs |
|
|
44
|
+
|---|---|---|
|
|
45
|
+
| `exact` | Moves USDC from your wallet for each call, starting at $0.01. Circle settles it on Arc. | USDC in your wallet |
|
|
46
|
+
| `gateway` | Pays from a prepaid Circle Gateway deposit with no gas, from fractions of a cent. Circle settles in batches. | A deposit: `await cp.depositGateway('1')` |
|
|
47
|
+
|
|
48
|
+
By default ChainPe uses Gateway when your deposit covers the price, and your wallet otherwise. To change that, pass `rails`, in order of preference.
|
|
49
|
+
|
|
50
|
+
## API
|
|
51
|
+
|
|
52
|
+
`new ChainPe(options)`
|
|
53
|
+
|
|
54
|
+
| Option | Default | Purpose |
|
|
55
|
+
|---|---|---|
|
|
56
|
+
| `privateKey` | required | The key that signs payments and ratings |
|
|
57
|
+
| `network` | `'arc-testnet'` | `'arc-testnet'` or `'arc'` |
|
|
58
|
+
| `rails` | `['gateway', 'exact']` | Payment rails, in order of preference |
|
|
59
|
+
| `maxPerCall` | `'1'` | Most USDC to pay for one call. A higher price is refused before signing |
|
|
60
|
+
| `autoFeedback` | `false` | Rate the provider 100 after a paid call. This costs gas |
|
|
61
|
+
| `accountsAddress` | the deployed contract | `ChainPeAccounts`. Also read from `CHAINPE_ACCOUNTS_ADDRESS` |
|
|
62
|
+
| `reputationRegistry` | Arc's registry | ERC-8004 registry. Also read from `ERC8004_REPUTATION_REGISTRY` |
|
|
63
|
+
|
|
64
|
+
| Method | Returns | What it does |
|
|
65
|
+
|---|---|---|
|
|
66
|
+
| `fetch(url, opts?)` | `Response` | Calls a URL, paying a 402 |
|
|
67
|
+
| `pay(url, opts?)` | `PaidResult` | The same, with the parsed body and payment details |
|
|
68
|
+
| `request(url, opts?)` | `{ response, payment, agentId }` | The same, with the unread `Response` |
|
|
69
|
+
| `discover(query?)` | `RankedService[]` | Finds services. Takes a search string or `{ query, tags, maxPrice, category, gatewayOnly, limit }` |
|
|
70
|
+
| `balance()` | `{ usdc, gateway }` | Your wallet and Gateway balances |
|
|
71
|
+
| `depositGateway(amount)` | `{ txHash, amount }` | Deposits USDC into Circle Gateway |
|
|
72
|
+
| `getReputation(agentId)` | `{ count, score }` or `null` | Reads a provider's rating |
|
|
73
|
+
| `giveFeedback(agentId, score, opts?)` | tx hash | Posts a 0 to 100 rating. You can't rate your own agent |
|
|
74
|
+
| `getAddress()` | `string` | Your wallet address |
|
|
75
|
+
|
|
76
|
+
`PolicyVaultClient` is also exported. No `PolicyVault` contract is deployed on Arc yet, so it needs a `vaultAddress` of your own.
|
|
77
|
+
|
|
78
|
+
## Errors
|
|
79
|
+
|
|
80
|
+
- A price above `maxPerCall` throws before anything is signed.
|
|
81
|
+
- A server that refuses a signed payment throws `ChainPePaymentError`, with the cause in `.reason`, such as `insufficient_funds`.
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
import { PaymentPolicy } from '@x402/fetch';
|
|
2
|
+
import { Hex, Chain, PublicClient } from 'viem';
|
|
3
|
+
|
|
4
|
+
/** Arc network the SDK operates on. */
|
|
5
|
+
type ChainPeNetwork = 'arc-testnet' | 'arc';
|
|
6
|
+
/**
|
|
7
|
+
* How a call is paid. `exact` moves USDC from the wallet for each call. `gateway`
|
|
8
|
+
* pays from a prepaid Circle Gateway deposit, with no gas, and is settled in batches.
|
|
9
|
+
*/
|
|
10
|
+
type PaymentRail = 'gateway' | 'exact';
|
|
11
|
+
/**
|
|
12
|
+
* Error thrown when an x402 payment fails (e.g. insufficient USDC, settlement
|
|
13
|
+
* refused). Provides `.txHash` and `.reason` for debugging.
|
|
14
|
+
*/
|
|
15
|
+
declare class ChainPePaymentError extends Error {
|
|
16
|
+
readonly txHash?: string;
|
|
17
|
+
readonly reason?: string;
|
|
18
|
+
constructor(message: string, { txHash, reason }?: {
|
|
19
|
+
txHash?: string;
|
|
20
|
+
reason?: string;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/** Options for constructing a {@link ChainPe} client. */
|
|
24
|
+
interface ChainPeOptions {
|
|
25
|
+
/** EOA private key (0x-prefixed or bare hex) used to sign payments + feedback. */
|
|
26
|
+
privateKey: string;
|
|
27
|
+
/** Arc network. Defaults to `'arc-testnet'`. */
|
|
28
|
+
network?: ChainPeNetwork;
|
|
29
|
+
/**
|
|
30
|
+
* Payment rails to use, in order of preference. Defaults to
|
|
31
|
+
* `['gateway', 'exact']`: Gateway when the deposit covers the price, else the wallet.
|
|
32
|
+
*/
|
|
33
|
+
rails?: PaymentRail[];
|
|
34
|
+
/** ChainPeAccounts address. Defaults to the deployed contract for the network. */
|
|
35
|
+
accountsAddress?: string;
|
|
36
|
+
/** ERC-8004 Reputation Registry address. Defaults to Arc's registry. */
|
|
37
|
+
reputationRegistry?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Max USDC to auto-pay for a single 402 (human units, e.g. `"1.00"`). A higher
|
|
40
|
+
* price is refused before anything is signed. Defaults to `"1"`.
|
|
41
|
+
*/
|
|
42
|
+
maxPerCall?: string;
|
|
43
|
+
/**
|
|
44
|
+
* When true, `.fetch()`/`.pay()` post positive ERC-8004 feedback to the
|
|
45
|
+
* provider's agent after a successful paid call. It costs gas, paid in USDC.
|
|
46
|
+
* Defaults to `false`. `.pay()` returns the feedback result; use
|
|
47
|
+
* `.giveFeedback()` for explicit control.
|
|
48
|
+
*/
|
|
49
|
+
autoFeedback?: boolean;
|
|
50
|
+
}
|
|
51
|
+
/** Per-request options for {@link ChainPe.fetch} / {@link ChainPe.pay}. */
|
|
52
|
+
interface FetchOptions extends RequestInit {
|
|
53
|
+
/** Override the constructor `autoFeedback` for this call. */
|
|
54
|
+
autoFeedback?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Timeout per HTTP attempt in milliseconds. Defaults to 30 000 ms (30 s).
|
|
57
|
+
*/
|
|
58
|
+
timeout?: number;
|
|
59
|
+
}
|
|
60
|
+
/** A service found by {@link ChainPe.discover}. */
|
|
61
|
+
interface ChainPeService {
|
|
62
|
+
/** Stable id: the resource URL (Circle) or the service key (ChainPeAccounts). */
|
|
63
|
+
id: string;
|
|
64
|
+
name: string;
|
|
65
|
+
description: string;
|
|
66
|
+
tags: string[];
|
|
67
|
+
/** The URL to call. */
|
|
68
|
+
endpoint: string;
|
|
69
|
+
/** The HTTP method, when the listing names one. */
|
|
70
|
+
method?: string;
|
|
71
|
+
/** Human USDC units. */
|
|
72
|
+
pricePerRequest: string;
|
|
73
|
+
paymentToken: 'USDC';
|
|
74
|
+
/** Address that receives payment (the 402 `payTo`). */
|
|
75
|
+
walletAddress: string;
|
|
76
|
+
network: ChainPeNetwork;
|
|
77
|
+
/** Payment rails this service accepts on this network. */
|
|
78
|
+
rails: PaymentRail[];
|
|
79
|
+
/** Where the listing came from. */
|
|
80
|
+
source: 'circle' | 'chainpe';
|
|
81
|
+
/** Circle Discovery category, when listed there. */
|
|
82
|
+
category?: string;
|
|
83
|
+
/** The provider's address, for services on ChainPeAccounts. */
|
|
84
|
+
developer?: string;
|
|
85
|
+
/** ERC-8004 agent id, when the service is linked to an identity. */
|
|
86
|
+
agentId?: string;
|
|
87
|
+
createdAt?: string;
|
|
88
|
+
updatedAt?: string;
|
|
89
|
+
}
|
|
90
|
+
/** Filters for {@link ChainPe.discover}. */
|
|
91
|
+
interface DiscoverOptions {
|
|
92
|
+
/** Free-text search. */
|
|
93
|
+
query?: string;
|
|
94
|
+
tags?: string[];
|
|
95
|
+
/** Max price per request (human USDC units). */
|
|
96
|
+
maxPrice?: string;
|
|
97
|
+
/** Circle Discovery category, e.g. `DATA_ENRICHMENT`. */
|
|
98
|
+
category?: string;
|
|
99
|
+
/** Only services that accept Circle Gateway. */
|
|
100
|
+
gatewayOnly?: boolean;
|
|
101
|
+
/** Max results. Defaults to 20. */
|
|
102
|
+
limit?: number;
|
|
103
|
+
}
|
|
104
|
+
/** Aggregated ERC-8004 reputation for an agent. */
|
|
105
|
+
interface ReputationSummary {
|
|
106
|
+
/** Number of feedback entries counted. */
|
|
107
|
+
count: number;
|
|
108
|
+
/** Aggregate score (typically 0–100), or null when unscored. */
|
|
109
|
+
score: number | null;
|
|
110
|
+
}
|
|
111
|
+
/** A service annotated with its reputation (returned by {@link ChainPe.discover}). */
|
|
112
|
+
interface RankedService extends ChainPeService {
|
|
113
|
+
reputation: ReputationSummary | null;
|
|
114
|
+
}
|
|
115
|
+
/** Details of a payment that was made. */
|
|
116
|
+
interface PaymentInfo {
|
|
117
|
+
/** Human USDC units. */
|
|
118
|
+
amount: string;
|
|
119
|
+
recipient: string;
|
|
120
|
+
network: ChainPeNetwork;
|
|
121
|
+
rail: PaymentRail;
|
|
122
|
+
/** On-chain tx hash. Set on the exact rail. */
|
|
123
|
+
txHash?: string;
|
|
124
|
+
/** Circle transfer id. Set on the Gateway rail, which settles in a later batch. */
|
|
125
|
+
transferId?: string;
|
|
126
|
+
/** The provider's decoded settlement response. */
|
|
127
|
+
settlement?: unknown;
|
|
128
|
+
}
|
|
129
|
+
/** Result of {@link ChainPe.request}: a live, unread Response + metadata. */
|
|
130
|
+
interface PaidResponse {
|
|
131
|
+
response: Response;
|
|
132
|
+
/** Set when a payment was actually made. */
|
|
133
|
+
payment?: PaymentInfo;
|
|
134
|
+
/** The provider's ERC-8004 agent id, when it advertises one. */
|
|
135
|
+
agentId?: string;
|
|
136
|
+
}
|
|
137
|
+
/** Result of {@link ChainPe.pay} — a parsed, structured paid response. */
|
|
138
|
+
interface PaidResult<T = unknown> {
|
|
139
|
+
status: number;
|
|
140
|
+
ok: boolean;
|
|
141
|
+
headers: Record<string, string>;
|
|
142
|
+
/** Parsed body (JSON when the content-type is JSON, else text). */
|
|
143
|
+
data: T;
|
|
144
|
+
/** Present when a payment was made + settled. */
|
|
145
|
+
payment?: PaymentInfo;
|
|
146
|
+
/** Present when feedback was posted (autoFeedback). */
|
|
147
|
+
reputation?: {
|
|
148
|
+
agentId: string;
|
|
149
|
+
score: number;
|
|
150
|
+
txHash: string;
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/** Wallet balances (human USDC units). */
|
|
154
|
+
interface Balances {
|
|
155
|
+
/** USDC in the wallet. It also pays gas on Arc. */
|
|
156
|
+
usdc: string;
|
|
157
|
+
/** USDC available in Circle Gateway. `'0'` if it could not be read. */
|
|
158
|
+
gateway: string;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Keeps the options of the first usable rail. Gateway only counts when the
|
|
163
|
+
* deposit covers the price, so a wallet with no deposit falls back to `exact`.
|
|
164
|
+
*/
|
|
165
|
+
declare function railPolicy(rails: PaymentRail[], gatewayAvailable: bigint): PaymentPolicy;
|
|
166
|
+
declare class ChainPe {
|
|
167
|
+
readonly network: ChainPeNetwork;
|
|
168
|
+
readonly rails: PaymentRail[];
|
|
169
|
+
readonly accountsAddress?: `0x${string}`;
|
|
170
|
+
readonly reputationRegistry: `0x${string}`;
|
|
171
|
+
private readonly privateKey;
|
|
172
|
+
private readonly account;
|
|
173
|
+
private readonly maxPerCallAtomic;
|
|
174
|
+
private readonly autoFeedback;
|
|
175
|
+
private gatewayClient?;
|
|
176
|
+
constructor(options: ChainPeOptions);
|
|
177
|
+
/** Echoes the resolved configuration (no secrets). */
|
|
178
|
+
get config(): {
|
|
179
|
+
network: ChainPeNetwork;
|
|
180
|
+
address: `0x${string}`;
|
|
181
|
+
rails: PaymentRail[];
|
|
182
|
+
accountsAddress: `0x${string}` | undefined;
|
|
183
|
+
reputationRegistry: `0x${string}`;
|
|
184
|
+
maxPerCall: string;
|
|
185
|
+
};
|
|
186
|
+
/** The wallet address that signs payments and feedback. */
|
|
187
|
+
getAddress(): string;
|
|
188
|
+
/** Reads the wallet's USDC and the Circle Gateway deposit (human units). */
|
|
189
|
+
balance(): Promise<Balances>;
|
|
190
|
+
/** Deposits USDC into Circle Gateway, to pay on the Gateway rail with no gas. */
|
|
191
|
+
depositGateway(amount: string): Promise<{
|
|
192
|
+
txHash: string;
|
|
193
|
+
amount: string;
|
|
194
|
+
}>;
|
|
195
|
+
/**
|
|
196
|
+
* Drop-in `fetch` that automatically pays an x402 `402 Payment Required` in
|
|
197
|
+
* USDC and returns the final live {@link Response}.
|
|
198
|
+
*/
|
|
199
|
+
fetch(url: string, opts?: FetchOptions): Promise<Response>;
|
|
200
|
+
/**
|
|
201
|
+
* Like {@link fetch}, but returns the live, unread {@link Response} together
|
|
202
|
+
* with the payment details. Never posts feedback.
|
|
203
|
+
*/
|
|
204
|
+
request(url: string, opts?: FetchOptions): Promise<PaidResponse>;
|
|
205
|
+
/**
|
|
206
|
+
* Like {@link fetch}, but returns a structured, body-parsed result with the
|
|
207
|
+
* payment + (optional) reputation details. Convenient for agents.
|
|
208
|
+
*/
|
|
209
|
+
pay<T = unknown>(url: string, opts?: FetchOptions): Promise<PaidResult<T>>;
|
|
210
|
+
/**
|
|
211
|
+
* Finds services through Circle's Discovery API and ChainPeAccounts, annotated
|
|
212
|
+
* with ERC-8004 reputation and ranked. ChainPeAccounts also covers providers
|
|
213
|
+
* Circle has not listed yet. Throws only when neither source can be read.
|
|
214
|
+
*/
|
|
215
|
+
discover(query?: string | DiscoverOptions): Promise<RankedService[]>;
|
|
216
|
+
/** Reads a provider agent's aggregate ERC-8004 reputation. */
|
|
217
|
+
getReputation(agentId: string): Promise<ReputationSummary | null>;
|
|
218
|
+
/** Posts ERC-8004 feedback (0–100) for a provider agent. Returns the tx hash. */
|
|
219
|
+
giveFeedback(agentId: string, score: number, opts?: {
|
|
220
|
+
endpoint?: string;
|
|
221
|
+
tag?: string;
|
|
222
|
+
waitConfirm?: boolean;
|
|
223
|
+
}): Promise<string>;
|
|
224
|
+
private gateway;
|
|
225
|
+
/** A deposit we cannot read counts as empty, so payment falls back to the wallet. */
|
|
226
|
+
private gatewayAvailable;
|
|
227
|
+
/**
|
|
228
|
+
* Runs the x402 flow: request → on 402, cap the price, choose a rail, sign →
|
|
229
|
+
* retry. Spend limits run before anything is signed.
|
|
230
|
+
*/
|
|
231
|
+
private execute;
|
|
232
|
+
private paymentInfo;
|
|
233
|
+
private explainPaymentError;
|
|
234
|
+
/**
|
|
235
|
+
* Best-effort positive feedback for the provider's agent after a paid call.
|
|
236
|
+
* Prefers the agent id the provider advertises; otherwise looks for its `payTo`
|
|
237
|
+
* in ChainPeAccounts. Never throws (self-feedback, no gas, etc. are swallowed).
|
|
238
|
+
*/
|
|
239
|
+
private tryAutoFeedback;
|
|
240
|
+
private parseBody;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
declare const CIRCLE_DISCOVERY_URL = "https://api.circle.com/v2/x402/discovery/resources";
|
|
244
|
+
interface CircleAccept {
|
|
245
|
+
network: string;
|
|
246
|
+
amount: string;
|
|
247
|
+
payTo: string;
|
|
248
|
+
extra?: {
|
|
249
|
+
name?: string;
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
interface CircleItem {
|
|
253
|
+
resource: string;
|
|
254
|
+
lastUpdated?: string;
|
|
255
|
+
accepts?: CircleAccept[];
|
|
256
|
+
metadata?: {
|
|
257
|
+
provider?: {
|
|
258
|
+
name?: string;
|
|
259
|
+
description?: string;
|
|
260
|
+
category?: string;
|
|
261
|
+
tags?: string[];
|
|
262
|
+
};
|
|
263
|
+
method?: string;
|
|
264
|
+
description?: string;
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
/** Maps one listing to a service on this network, or undefined if it has no option here. Pure. */
|
|
268
|
+
declare function mapCircleItem(item: CircleItem, network: ChainPeNetwork): ChainPeService | undefined;
|
|
269
|
+
/** Throws when Circle cannot be reached, so a caller can tell that apart from no results. */
|
|
270
|
+
declare function searchCircle(network: ChainPeNetwork, options: DiscoverOptions): Promise<ChainPeService[]>;
|
|
271
|
+
/**
|
|
272
|
+
* Circle's search can return listings that share no word with the query. Keeps a
|
|
273
|
+
* listing only when one of its words is a query word, or that word plus "s". Pure.
|
|
274
|
+
*/
|
|
275
|
+
declare function isRelevant(service: ChainPeService, query?: string): boolean;
|
|
276
|
+
/** Drops a ChainPe listing when Circle already lists that provider on the same host. Pure. */
|
|
277
|
+
declare function mergeListings(circle: ChainPeService[], chainpe: ChainPeService[]): ChainPeService[];
|
|
278
|
+
/**
|
|
279
|
+
* Finds services through Circle's Discovery API and ChainPeAccounts, annotated
|
|
280
|
+
* with ERC-8004 reputation and ranked. Needs no wallet. ChainPeAccounts also covers
|
|
281
|
+
* providers Circle has not listed yet. Throws only when neither source can be read.
|
|
282
|
+
*/
|
|
283
|
+
declare function discoverServices(network: ChainPeNetwork, query?: string | DiscoverOptions, addresses?: {
|
|
284
|
+
accountsAddress?: string;
|
|
285
|
+
reputationRegistry?: string;
|
|
286
|
+
}): Promise<RankedService[]>;
|
|
287
|
+
|
|
288
|
+
interface RawService {
|
|
289
|
+
key: string;
|
|
290
|
+
provider: string;
|
|
291
|
+
payTo: string;
|
|
292
|
+
agentId: bigint;
|
|
293
|
+
name: string;
|
|
294
|
+
description: string;
|
|
295
|
+
tags: string;
|
|
296
|
+
endpoint: string;
|
|
297
|
+
pricePerRequest: string;
|
|
298
|
+
network: string;
|
|
299
|
+
acceptsExact: boolean;
|
|
300
|
+
acceptsGateway: boolean;
|
|
301
|
+
active: boolean;
|
|
302
|
+
createdAt: bigint;
|
|
303
|
+
updatedAt: bigint;
|
|
304
|
+
}
|
|
305
|
+
/** Decodes an on-chain service tuple into the public {@link ChainPeService}. */
|
|
306
|
+
declare function decodeService(raw: RawService, network: ChainPeNetwork): ChainPeService;
|
|
307
|
+
/** Filters services by free-text query, tags, max price and rail. Pure. */
|
|
308
|
+
declare function filterServices(services: ChainPeService[], options: DiscoverOptions): ChainPeService[];
|
|
309
|
+
/**
|
|
310
|
+
* Ranks services by reputation: scored before unscored, higher score first,
|
|
311
|
+
* then more feedback, then cheaper. Stable for equal keys. Pure.
|
|
312
|
+
*/
|
|
313
|
+
declare function rankByReputation(services: RankedService[]): RankedService[];
|
|
314
|
+
/** Reads the on-chain ChainPeAccounts contract. */
|
|
315
|
+
declare class RegistryClient {
|
|
316
|
+
private readonly network;
|
|
317
|
+
private readonly address;
|
|
318
|
+
constructor(network: ChainPeNetwork, address: `0x${string}`);
|
|
319
|
+
getAddress(): string;
|
|
320
|
+
/** Lists every active service. Deactivated listings stay on-chain, so they are dropped here. */
|
|
321
|
+
listAllServices(): Promise<ChainPeService[]>;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** Converts the on-chain (value, decimals) summary into a human score. Pure. */
|
|
325
|
+
declare function summaryToScore(summaryValue: bigint, decimals: number): number;
|
|
326
|
+
/**
|
|
327
|
+
* Reads a provider agent's aggregate reputation. Returns null when the agent is
|
|
328
|
+
* unlinked (`agentId` 0/empty) or no registry is configured; `count: 0` when the
|
|
329
|
+
* agent exists but has no feedback yet.
|
|
330
|
+
*/
|
|
331
|
+
declare function getReputation(network: ChainPeNetwork, registry: `0x${string}` | undefined, agentId: string): Promise<ReputationSummary | null>;
|
|
332
|
+
interface GiveFeedbackParams {
|
|
333
|
+
agentId: string;
|
|
334
|
+
/** Score, typically 0–100 (valueDecimals 0). */
|
|
335
|
+
value: number;
|
|
336
|
+
endpoint?: string;
|
|
337
|
+
tag?: string;
|
|
338
|
+
/** Await the receipt before returning. Defaults to false. */
|
|
339
|
+
waitConfirm?: boolean;
|
|
340
|
+
}
|
|
341
|
+
/** Posts ERC-8004 feedback for a provider agent. Returns the tx hash. */
|
|
342
|
+
declare function giveFeedback(network: ChainPeNetwork, registry: `0x${string}` | undefined, privateKey: string, params: GiveFeedbackParams): Promise<string>;
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* PolicyVault client — programmable, gasless agent spending.
|
|
346
|
+
*
|
|
347
|
+
* Three roles, one client (whichever key you construct it with):
|
|
348
|
+
* - **owner** funds the vault + sets the policy/allowlist (`deposit`,
|
|
349
|
+
* `setPolicy`, `setAllowlist`, `revokeSession`) — owner pays gas.
|
|
350
|
+
* - **agent** holds the session key and only *signs* spends (`signSpend`) —
|
|
351
|
+
* no gas, no tx.
|
|
352
|
+
* - **relayer** submits a signed spend (`relaySpend`) and pays the gas.
|
|
353
|
+
*
|
|
354
|
+
* The on-chain `PolicyVault` enforces every limit; an over-cap or non-allowlisted
|
|
355
|
+
* spend reverts (the rejection reason is surfaced as the thrown error).
|
|
356
|
+
*/
|
|
357
|
+
|
|
358
|
+
interface PolicyVaultOptions {
|
|
359
|
+
privateKey: string;
|
|
360
|
+
network?: ChainPeNetwork;
|
|
361
|
+
vaultAddress: string;
|
|
362
|
+
}
|
|
363
|
+
interface PolicyParams {
|
|
364
|
+
sessionKey: string;
|
|
365
|
+
/** Human USDC strings. */
|
|
366
|
+
maxPerCall: string;
|
|
367
|
+
dailyCap: string;
|
|
368
|
+
totalBudget: string;
|
|
369
|
+
/** Unix seconds the session is valid until. */
|
|
370
|
+
expiry: number;
|
|
371
|
+
allowlistOnly?: boolean;
|
|
372
|
+
}
|
|
373
|
+
/** A session-signed spend authorization, ready for a relayer to submit. */
|
|
374
|
+
interface SpendAuthorization {
|
|
375
|
+
owner: string;
|
|
376
|
+
to: string;
|
|
377
|
+
/** Human USDC string. */
|
|
378
|
+
amount: string;
|
|
379
|
+
amountAtomic: string;
|
|
380
|
+
nonce: string;
|
|
381
|
+
deadline: number;
|
|
382
|
+
signature: Hex;
|
|
383
|
+
}
|
|
384
|
+
declare class PolicyVaultClient {
|
|
385
|
+
readonly network: ChainPeNetwork;
|
|
386
|
+
readonly vault: `0x${string}`;
|
|
387
|
+
private readonly account;
|
|
388
|
+
constructor(options: PolicyVaultOptions);
|
|
389
|
+
getAddress(): string;
|
|
390
|
+
private wallet;
|
|
391
|
+
private usdc;
|
|
392
|
+
balanceOf(owner: string): Promise<string>;
|
|
393
|
+
dailyRemaining(owner: string): Promise<string>;
|
|
394
|
+
budgetRemaining(owner: string): Promise<string>;
|
|
395
|
+
nonce(owner: string): Promise<bigint>;
|
|
396
|
+
deposit(human: string): Promise<Hex>;
|
|
397
|
+
withdraw(human: string): Promise<Hex>;
|
|
398
|
+
setPolicy(p: PolicyParams): Promise<Hex>;
|
|
399
|
+
setAllowlist(to: string, allowed: boolean): Promise<Hex>;
|
|
400
|
+
revokeSession(): Promise<Hex>;
|
|
401
|
+
/**
|
|
402
|
+
* Signs a spend authorization with this key (the session key). Reads the
|
|
403
|
+
* owner's current nonce so the relayer can submit it. `ttlSeconds` bounds how
|
|
404
|
+
* long the authorization stays valid (default 5 min).
|
|
405
|
+
*/
|
|
406
|
+
signSpend(params: {
|
|
407
|
+
owner: string;
|
|
408
|
+
to: string;
|
|
409
|
+
amount: string;
|
|
410
|
+
ttlSeconds?: number;
|
|
411
|
+
}): Promise<SpendAuthorization>;
|
|
412
|
+
/**
|
|
413
|
+
* Submits a session-signed spend. Throws with the on-chain revert reason if a
|
|
414
|
+
* policy limit is violated (e.g. "over per-call cap").
|
|
415
|
+
*/
|
|
416
|
+
relaySpend(auth: SpendAuthorization): Promise<Hex>;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Network constants for Circle's Arc.
|
|
421
|
+
*
|
|
422
|
+
* Arc pays gas in USDC. The native balance has 18 decimals while the ERC-20
|
|
423
|
+
* interface at the USDC address has 6, so amounts always go through the ERC-20.
|
|
424
|
+
*/
|
|
425
|
+
|
|
426
|
+
declare const USDC_DECIMALS = 6;
|
|
427
|
+
declare function getCaip2(network: ChainPeNetwork): string;
|
|
428
|
+
declare function rpcUrlFor(network: ChainPeNetwork): string;
|
|
429
|
+
declare function explorerFor(network: ChainPeNetwork): string;
|
|
430
|
+
/** Arc's USDC, through its 6-decimal ERC-20 interface. */
|
|
431
|
+
declare function getUsdcAddress(network: ChainPeNetwork): `0x${string}`;
|
|
432
|
+
declare function chainFor(network: ChainPeNetwork): Chain;
|
|
433
|
+
declare function publicClientFor(network: ChainPeNetwork): PublicClient;
|
|
434
|
+
/** ChainPeAccounts: explicit arg → env → built-in. Undefined where it is not deployed. */
|
|
435
|
+
declare function resolveAccountsAddress(network: ChainPeNetwork, explicit?: string): `0x${string}` | undefined;
|
|
436
|
+
/** Arc's ERC-8004 Reputation Registry: explicit arg → env → built-in. */
|
|
437
|
+
declare function resolveReputationRegistry(network: ChainPeNetwork, explicit?: string): `0x${string}`;
|
|
438
|
+
declare function normalizeKey(key: string): `0x${string}`;
|
|
439
|
+
|
|
440
|
+
export { type Balances, CIRCLE_DISCOVERY_URL, ChainPe, type ChainPeNetwork, type ChainPeOptions, ChainPePaymentError, type ChainPeService, type DiscoverOptions, type FetchOptions, type GiveFeedbackParams, type PaidResponse, type PaidResult, type PaymentInfo, type PaymentRail, type PolicyParams, PolicyVaultClient, type PolicyVaultOptions, type RankedService, type RawService, RegistryClient, type ReputationSummary, type SpendAuthorization, USDC_DECIMALS, chainFor, decodeService, discoverServices, explorerFor, filterServices, getCaip2, getReputation, getUsdcAddress, giveFeedback, isRelevant, mapCircleItem, mergeListings, normalizeKey, publicClientFor, railPolicy, rankByReputation, resolveAccountsAddress, resolveReputationRegistry, rpcUrlFor, searchCircle, summaryToScore };
|