@moonbeam-foundation/sdk 0.7.0 → 0.11.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 +78 -432
- package/bin/moonbeam.mjs +15 -2
- package/dist/acp/moonbeam-acp.generated.d.ts +7 -2
- package/dist/acp/moonbeam-acp.generated.d.ts.map +1 -1
- package/dist/acp/moonbeam-acp.generated.js +6 -3
- package/dist/acp/moonbeam-acp.generated.js.map +1 -1
- package/dist/acp/outcome.d.ts +5 -1
- package/dist/acp/outcome.d.ts.map +1 -1
- package/dist/acp/outcome.js +3 -2
- package/dist/acp/outcome.js.map +1 -1
- package/dist/adapters/stamp.d.ts +1 -1
- package/dist/adapters/stamp.d.ts.map +1 -1
- package/dist/adapters/stamp.js +6 -6
- package/dist/adapters/stamp.js.map +1 -1
- package/dist/assurance/assurance-shared.d.ts.map +1 -1
- package/dist/assurance/assurance-shared.js +3 -1
- package/dist/assurance/assurance-shared.js.map +1 -1
- package/dist/cover/index.d.ts +65 -0
- package/dist/cover/index.d.ts.map +1 -0
- package/dist/cover/index.js +61 -0
- package/dist/cover/index.js.map +1 -0
- package/dist/devnet/edge.d.ts +207 -0
- package/dist/devnet/edge.d.ts.map +1 -0
- package/dist/devnet/edge.js +209 -0
- package/dist/devnet/edge.js.map +1 -0
- package/dist/devnet/index.d.ts +403 -0
- package/dist/devnet/index.d.ts.map +1 -0
- package/dist/devnet/index.js +420 -0
- package/dist/devnet/index.js.map +1 -0
- package/dist/devnet/profile.d.ts +110 -0
- package/dist/devnet/profile.d.ts.map +1 -0
- package/dist/devnet/profile.js +145 -0
- package/dist/devnet/profile.js.map +1 -0
- package/dist/erc8004/registry.generated.d.ts.map +1 -1
- package/dist/erc8004/registry.generated.js +0 -1
- package/dist/erc8004/registry.generated.js.map +1 -1
- package/dist/errors/index.d.ts +51 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +63 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/jobs/index.d.ts +62 -0
- package/dist/jobs/index.d.ts.map +1 -0
- package/dist/jobs/index.js +59 -0
- package/dist/jobs/index.js.map +1 -0
- package/dist/judge/index.d.ts +109 -1
- package/dist/judge/index.d.ts.map +1 -1
- package/dist/judge/index.js +97 -3
- package/dist/judge/index.js.map +1 -1
- package/dist/market/index.d.ts +281 -0
- package/dist/market/index.d.ts.map +1 -0
- package/dist/market/index.js +211 -0
- package/dist/market/index.js.map +1 -0
- package/dist/onboard/index.d.ts +4 -4
- package/dist/onboard/index.d.ts.map +1 -1
- package/dist/onboard/index.js +9 -15
- package/dist/onboard/index.js.map +1 -1
- package/dist/pools/index.d.ts +29 -5
- package/dist/pools/index.d.ts.map +1 -1
- package/dist/pools/index.js +34 -15
- package/dist/pools/index.js.map +1 -1
- package/dist/proof/index.d.ts +8 -4
- package/dist/proof/index.d.ts.map +1 -1
- package/dist/proof/index.js +10 -7
- package/dist/proof/index.js.map +1 -1
- package/dist/resale/index.d.ts +22 -6
- package/dist/resale/index.d.ts.map +1 -1
- package/dist/resale/index.js +29 -15
- package/dist/resale/index.js.map +1 -1
- package/dist/resale/payment-scheme.pin.json +1 -1
- package/dist/resale/settle-rule.d.ts +49 -0
- package/dist/resale/settle-rule.d.ts.map +1 -0
- package/dist/resale/settle-rule.js +53 -0
- package/dist/resale/settle-rule.js.map +1 -0
- package/dist/workflows/data.d.ts +3 -0
- package/dist/workflows/data.d.ts.map +1 -0
- package/dist/workflows/data.js +829 -0
- package/dist/workflows/data.js.map +1 -0
- package/dist/workflows/index.d.ts +40 -0
- package/dist/workflows/index.d.ts.map +1 -0
- package/dist/workflows/index.js +79 -0
- package/dist/workflows/index.js.map +1 -0
- package/dist/workflows/steps.d.ts +48 -0
- package/dist/workflows/steps.d.ts.map +1 -0
- package/dist/workflows/steps.js +62 -0
- package/dist/workflows/steps.js.map +1 -0
- package/dist/workflows/types.d.ts +26 -0
- package/dist/workflows/types.d.ts.map +1 -0
- package/dist/workflows/types.js +2 -0
- package/dist/workflows/types.js.map +1 -0
- package/dist/workflows/verified.d.ts +17 -0
- package/dist/workflows/verified.d.ts.map +1 -0
- package/dist/workflows/verified.js +219 -0
- package/dist/workflows/verified.js.map +1 -0
- package/dist/x402/index.d.ts +80 -17
- package/dist/x402/index.d.ts.map +1 -1
- package/dist/x402/index.js +95 -27
- package/dist/x402/index.js.map +1 -1
- package/docs/REFERENCE.md +511 -0
- package/docs/WORKFLOWS.md +164 -0
- package/docs/studio/hire-from-chat.png +0 -0
- package/docs/studio/list-business.png +0 -0
- package/docs/studio/onboard-batch.png +0 -0
- package/docs/studio/onboard.png +0 -0
- package/docs/studio/publish.png +0 -0
- package/docs/studio/resell.png +0 -0
- package/docs/studio/sell-edge.png +0 -0
- package/docs/workflows/hire-from-chat.png +0 -0
- package/docs/workflows/hire-from-chat.svg +102 -0
- package/docs/workflows/list-business.png +0 -0
- package/docs/workflows/list-business.svg +116 -0
- package/docs/workflows/onboard-batch.png +0 -0
- package/docs/workflows/onboard-batch.svg +66 -0
- package/docs/workflows/onboard.png +0 -0
- package/docs/workflows/onboard.svg +76 -0
- package/docs/workflows/publish.png +0 -0
- package/docs/workflows/publish.svg +86 -0
- package/docs/workflows/resell.png +0 -0
- package/docs/workflows/resell.svg +116 -0
- package/docs/workflows/sell-edge.png +0 -0
- package/docs/workflows/sell-edge.svg +46 -0
- package/examples/40-the-article.mjs +1 -1
- package/examples/50-x402-pay-a-wall.mjs +22 -11
- package/examples/51-read-a-proof.mjs +5 -4
- package/examples/60-cross-list-a-seller.mjs +36 -0
- package/examples/README.md +11 -2
- package/examples/data/cross-list.recorded.json +10 -0
- package/examples/data/x402-wall-402.json +85 -0
- package/package.json +56 -15
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
# Moonbeam SDK reference
|
|
2
|
+
|
|
3
|
+
The full reference of `@moonbeam-foundation/sdk`: assurance, the four outcomes, reading the chain, building transactions,
|
|
4
|
+
grading, reselling, x402, proofs, arbitration and pool economics. The [README](../README.md) covers agentic commerce and the
|
|
5
|
+
Moonbeam Studio's workflows.
|
|
6
|
+
|
|
7
|
+
## Assurance: a price, a judge and a deadline, settled against proof
|
|
8
|
+
|
|
9
|
+
- **What:** Moonbeam's assurance layer on Base, modelled end to end: the seller's deposit, the buyer's premium, the
|
|
10
|
+
backers' pool, the verdict, and a settlement in which every ledger sums to zero.
|
|
11
|
+
- **Why:** agent escrow refunds the price of a failed job, never the cost of the failure, and the judge has no capital
|
|
12
|
+
behind its verdict. Moonbeam adds the deposit, the pool and a grader (Jev by TypeSafe).
|
|
13
|
+
- **Today:** cover is not sold on Base, because no arbitrator is registered on the Base hook; jobs are graded and
|
|
14
|
+
recorded, and the model below is what the hook settles.
|
|
15
|
+
|
|
16
|
+

|
|
17
|
+
|
|
18
|
+
### What problem this solves
|
|
19
|
+
|
|
20
|
+
Agent-to-agent protocols already hold payment in escrow and release it when an
|
|
21
|
+
evaluator approves. If the provider misses the deadline, escrow refunds the
|
|
22
|
+
buyer. That covers the *price of the job*.
|
|
23
|
+
|
|
24
|
+
It does not cover the *cost of the failure* — the campaign that missed its slot,
|
|
25
|
+
the fill that never happened — and it leaves the evaluator with no capital
|
|
26
|
+
behind its verdict. This SDK models the layer that closes both gaps.
|
|
27
|
+
|
|
28
|
+
### Quick start: settle a covered job
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { settle, parseUnits } from '@moonbeam-foundation/sdk';
|
|
32
|
+
|
|
33
|
+
const units = (v: string) => parseUnits(v); // amounts in the token's smallest units
|
|
34
|
+
|
|
35
|
+
const { ledger, locked } = settle({
|
|
36
|
+
terms: {
|
|
37
|
+
pay: units('100'), // what the client pays for the work
|
|
38
|
+
premium: units('1'), // what it pays for cover
|
|
39
|
+
deposit: units('120'), // the provider's bond, sized above the job
|
|
40
|
+
evaluatorBond: units('25'), // the judge's bond
|
|
41
|
+
poolCapital: units('5000'), // backers standing behind the provider
|
|
42
|
+
damage: units('70'), // consequential loss if the work is bad
|
|
43
|
+
},
|
|
44
|
+
verdict: 'cheated',
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
locked; // 221_000000n — pay + premium + deposit, derived not typed
|
|
48
|
+
ledger.client; // +70_000000n — refunded AND compensated
|
|
49
|
+
ledger.provider; // -70_000000n — pays the damage out of its own bond
|
|
50
|
+
ledger.pool; // 0n — the bond covered it; the pool is the tail
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Positive is received, negative is paid, and every settlement sums to zero.
|
|
54
|
+
|
|
55
|
+
The deposit is sized above the job on purpose. Cheating has to cost the
|
|
56
|
+
provider more than the job pays, or the arithmetic argues for cheating; and
|
|
57
|
+
because the bond is first loss, the pool pays only the residual a bond cannot
|
|
58
|
+
cover. Drop the deposit to `40` here and the pool pays `30` on this single
|
|
59
|
+
job, which is the shape a flat premium cannot fund at realistic failure rates.
|
|
60
|
+
|
|
61
|
+
## Examples
|
|
62
|
+
|
|
63
|
+
Twenty-five runnable files in [`examples/`](../examples), built on Base jobs; all run offline except four (17 and 32 read live; 50 and
|
|
64
|
+
51 read live, or run from recorded answers with `OFFLINE=1`). `npm test` runs them.
|
|
65
|
+
|
|
66
|
+
| Start with | If you are |
|
|
67
|
+
|---|---|
|
|
68
|
+
| [`03-end-to-end-offline`](../examples/03-end-to-end-offline.mjs) | new here: one job read from Base, graded, settled |
|
|
69
|
+
| [`20`–`23` onboarding](../examples/README.md#before-you-take-part) | about to pay, work, judge or back: what you put up, what you can lose, what should stop you |
|
|
70
|
+
| [`10`–`17` the grader](../examples/README.md#the-grader-stage-by-stage) | judging jobs: evidence → facts → answers → verdict → record → end the job → verify, then live |
|
|
71
|
+
| [`30`–`33` reading jobs](../examples/README.md#reading-jobs-that-already-ran) | studying jobs that already ran, one or many, offline or from a Base RPC |
|
|
72
|
+
| [`50`–`53`](../examples/README.md) | paying an x402 wall on Base, reading a proof, arbitration, the records every step comes with |
|
|
73
|
+
|
|
74
|
+
```sh
|
|
75
|
+
node examples/03-end-to-end-offline.mjs
|
|
76
|
+
# 1 read the chain says: doneRight (judged by: self)
|
|
77
|
+
# 2 grade an independent grader says: complete · re-verifies: true
|
|
78
|
+
# 3 settle client -101 · provider +100 · pool +1 · sums to zero
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## The four outcomes
|
|
82
|
+
|
|
83
|
+
| Verdict | What happens |
|
|
84
|
+
|---|---|
|
|
85
|
+
| `doneRight` | Provider is paid and gets its deposit back; the pool keeps the premium. |
|
|
86
|
+
| `notDelivered` | Nothing arrived. Everything unwinds, nobody punished. |
|
|
87
|
+
| `rejected` | Work arrived and was graded bad. Refund and deposit returned — **no damage cover**. |
|
|
88
|
+
| `cheated` | Client is made whole *beyond* a refund — from the provider's deposit first, then the pool. |
|
|
89
|
+
|
|
90
|
+
The last two rows carry the design's most important distinction. Refusing to pay
|
|
91
|
+
costs an evaluator nothing and requires no proof, so rejections are ordinary
|
|
92
|
+
events rather than evidence of fraud. If every rejection paid damage cover, a
|
|
93
|
+
pool would be underwriting quality disputes and its losses would track the
|
|
94
|
+
rejection rate rather than the fraud rate. So `cheated` — the only verdict that
|
|
95
|
+
draws on the deposit and the pool — is reserved for an adjudication by a neutral
|
|
96
|
+
party.
|
|
97
|
+
|
|
98
|
+
That is what makes **cheating pay the victim**: being cheated is the client's
|
|
99
|
+
best financial outcome after the fact, and every unit paid out comes from someone
|
|
100
|
+
who did wrong or who sold cover, never from an uninvolved party.
|
|
101
|
+
|
|
102
|
+
## Reading the chain
|
|
103
|
+
|
|
104
|
+
Decode agent-commerce logs into typed events, fold them into job state, and ask
|
|
105
|
+
what actually settled:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { decodeAcpLogs, foldTask, toOutcome, localClock } from '@moonbeam-foundation/sdk';
|
|
109
|
+
|
|
110
|
+
const state = foldTask(decodeAcpLogs(logs)); // one job's logs
|
|
111
|
+
const outcome = toOutcome(state, localClock(now));
|
|
112
|
+
// { verdict: 'cheated', basis: 'adjudicatedForClient' }
|
|
113
|
+
// or { verdict: null, pending: 'awaitingAdjudication' }
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
`toOutcome` returns no verdict whenever the chain has not settled the question —
|
|
117
|
+
a raised dispute, or a close marked "adjudicated" with no named winner. It
|
|
118
|
+
refuses to guess, because a verdict moves money.
|
|
119
|
+
|
|
120
|
+
Decoding is keyed on topic0 and address-agnostic, so it survives redeployment.
|
|
121
|
+
Every pinned constant is re-derived from its signature in the test suite, so a
|
|
122
|
+
typo cannot silently fail to match live traffic. One event is genuinely
|
|
123
|
+
ambiguous — two contracts declare an identical `Disputed` signature — and the
|
|
124
|
+
decoder reports `source: 'ambiguous'` rather than guessing.
|
|
125
|
+
|
|
126
|
+
## Nobody can be locked out
|
|
127
|
+
|
|
128
|
+
A job holds other people's money, so no party may strand another by going quiet:
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
import { livenessOf, recourseFor, waitingOn } from '@moonbeam-foundation/sdk';
|
|
132
|
+
|
|
133
|
+
livenessOf(state, clock); // { noLockout: true, stranded: [] }
|
|
134
|
+
recourseFor('provider', state, clock); // what I can do, and my guaranteed exit
|
|
135
|
+
waitingOn(state, clock); // who is holding this up
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
- A silent **provider** cannot strand the client: past the deadline the refund is
|
|
139
|
+
permissionless — anyone may trigger it.
|
|
140
|
+
- A silent **client** cannot strand the provider: release is the client's alone,
|
|
141
|
+
but the provider can escalate to arbitration.
|
|
142
|
+
- A silent **evaluator** cannot strand either: the deadline resolves the job.
|
|
143
|
+
|
|
144
|
+
These are checked against the chain's state rather than asserted in prose.
|
|
145
|
+
|
|
146
|
+
## Time carries its provenance
|
|
147
|
+
|
|
148
|
+
Deadlines decide money, so a `Clock` always records where "now" came from —
|
|
149
|
+
`local`, `chain`, or `attested` (a signed finality attestation). A proof-checking
|
|
150
|
+
setup narrows what must be trusted; it does not remove trust, so the SDK never
|
|
151
|
+
describes an attested clock as requiring none.
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import { createAttestationSource, clockFromAttestation } from '@moonbeam-foundation/sdk';
|
|
155
|
+
import { recoverMessageAddress } from 'viem';
|
|
156
|
+
|
|
157
|
+
const source = createAttestationSource({ baseUrl: 'https://…' });
|
|
158
|
+
const attestation = await source.latest(8453);
|
|
159
|
+
|
|
160
|
+
// The `signer` field is SELF-REPORTED. Recover it yourself or it proves nothing.
|
|
161
|
+
const ecrecover = (digest: string, signature: string) =>
|
|
162
|
+
recoverMessageAddress({ message: { raw: digest as `0x${string}` }, signature: signature as `0x${string}` });
|
|
163
|
+
|
|
164
|
+
if (attestation.ok) {
|
|
165
|
+
const clock = await clockFromAttestation(attestation.value, ecrecover);
|
|
166
|
+
// Refused outright if the signature does not check out — never downgraded silently.
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Note the recovery scheme: attestations are signed as EIP-191 personal messages
|
|
171
|
+
over the digest, not as a raw hash. Recovery always returns *some* address, so
|
|
172
|
+
the wrong scheme yields a plausible-looking address that simply never matches.
|
|
173
|
+
Verify against a known-good attestation once before relying on it.
|
|
174
|
+
|
|
175
|
+
## Network answers that are not answers
|
|
176
|
+
|
|
177
|
+
Connectors return a `Result` rather than throwing, to keep one distinction the
|
|
178
|
+
SDK considers load-bearing:
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
if (!result.ok && meansAbsent(result.error)) {
|
|
182
|
+
// The source definitively has nothing for this. A fact you can act on.
|
|
183
|
+
} else if (!result.ok) {
|
|
184
|
+
// Unavailable, network failure, malformed: you do not know. Not the same thing.
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
A 404 is honest — the service has no data for that chain. A 500 is an outage.
|
|
189
|
+
Collapsing the two would let a server fault read as "no proof exists", and under
|
|
190
|
+
the rule above, absence of proof must never authorise a payment.
|
|
191
|
+
|
|
192
|
+
The log reader carries the same instinct: some providers answer a valid query
|
|
193
|
+
with an empty set instead of an error, which is indistinguishable from a quiet
|
|
194
|
+
range, so `fetchLogRange` reports emptiness as `noData` unless you explicitly
|
|
195
|
+
vouch for your source. It also pages wide ranges automatically, because
|
|
196
|
+
providers that reject them leave holes in history that later reads never revisit.
|
|
197
|
+
|
|
198
|
+
## Building transactions
|
|
199
|
+
|
|
200
|
+
The SDK builds calls; it never signs or sends them, and never holds a key. The calls target the assurance hook Moonbeam runs
|
|
201
|
+
on Base (`@moonbeam-foundation/sdk/onboard`; the hook, the pool factory and the pools are read from the coordination layer's
|
|
202
|
+
address registry, never typed here):
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
import { fundCalls, sellerDepositCalls, coverPremium, submitCalls, settleCalls, BASE_GLMR, BASE_USDC, MOONBEAM_HOOK } from '@moonbeam-foundation/sdk/onboard';
|
|
206
|
+
|
|
207
|
+
// a covered job is a GLMR job: the hook covers in GLMR only (the USDC route closed on 2026-09-29)
|
|
208
|
+
const premium = coverPremium(terms.price, { k: 19, n: 20 }); // the seller's record → the premium (jev-wilson)
|
|
209
|
+
sellerDepositCalls(terms); // the seller approves exactly its deposit
|
|
210
|
+
const [approve, fund] = fundCalls({ ...terms, token: BASE_GLMR }, { pool, premium, coverCap }); // approve price + premium, then fundJobWithCover
|
|
211
|
+
const [approveU, fundU] = fundCalls({ ...terms, token: BASE_USDC }); // an uncovered USDC job: no pool, premium 0
|
|
212
|
+
submitCalls({ jobId, evidenceDigest, correlator }); // the seller seals its delivery
|
|
213
|
+
settleCalls(jobId, 'completed'); // complete(jobId) · reject(jobId, digest) · expire(jobId)
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
`fundJob`'s last address word is the **cover pool**, not an evaluator: the hook keeps no per-job evaluator (evaluators are the
|
|
217
|
+
owner's allowlist). `premium` is an argument the hook checks against the pool. A USDC job with a pool is refused before any call
|
|
218
|
+
(the hook would revert `PoolAssetMismatch`). `MOONBEAM_HOOK.status` carries the registry's word on the hook row (`live`, or
|
|
219
|
+
`scheduled-change` while a route change is announced). `test/fork/run-fork.mjs` sends these calls on a local fork of Base:
|
|
220
|
+
deposit approval, approve, `fundJob` against a live pool, `submit`, `complete`, then the settle's attestation.
|
|
221
|
+
|
|
222
|
+
Pools open by the council's yes, not by a release: `poolsOpenOn(transport)` (`./pools`) reads `GET /v1/pools/networks` (its
|
|
223
|
+
`supported` lines and `closed_by_policy`) through the site's proxy and returns no chain while the GLMR pools are not open; the
|
|
224
|
+
typed `POOLS_OPEN_ON` is only the fallback. Pass its `chains` to the pool builders as `openOn`. The judge's quota is read the same way (`quotaOf(transport)`).
|
|
225
|
+
|
|
226
|
+
## Judging a job on the Base hook
|
|
227
|
+
|
|
228
|
+
`@moonbeam-foundation/sdk/judge` grades a GLMR job on Moonbeam's assurance hook on Base
|
|
229
|
+
(`0xc0578657Eda85e0a246771aa1839ce79b54eE80d`) and builds the call that ends it. The grade works like this:
|
|
230
|
+
|
|
231
|
+
- Code checks the facts first. A failed check rejects the job, and the model is never asked.
|
|
232
|
+
- Jev, TypeSafe's calibrated decision model, answers four closed questions from the published RUBRIC_v1. Every answer
|
|
233
|
+
comes back as a full probability distribution.
|
|
234
|
+
- Code composes complete, reject or needs_review under fixed thresholds, and returns a receipt that hashes all of it.
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
import { gradeJob, jobFacts, recordGrade, endJob, verifyGrade } from '@moonbeam-foundation/sdk/judge';
|
|
238
|
+
|
|
239
|
+
const receipt = await gradeJob({
|
|
240
|
+
jobId, // the hook's 32-byte job id
|
|
241
|
+
evidence: { task, delivered },
|
|
242
|
+
facts: jobFacts({ delivered: true, checks: { proof_verifies: () => checkProof(delivered) } }),
|
|
243
|
+
key: process.env.TYPESAFE_KEY, // your own TypeSafe key
|
|
244
|
+
});
|
|
245
|
+
const onchain = await recordGrade(receipt); // unsigned calls on Base; { network: 'none' } turns it off
|
|
246
|
+
const end = endJob(jobId, receipt); // complete(jobId) | reject(jobId, digest) | null (needs_review)
|
|
247
|
+
const check = await verifyGrade(receipt, { chain: false });
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
With `key`, Jev is asked over System One: the request `{ model, state, questions }` and its answer are the same at
|
|
251
|
+
TypeSafe (`https://api.typesafe.ai/v1/systemone`, the default) and at a coordination endpoint's System One door. To use the
|
|
252
|
+
door, pass `systemOne: { via: 'coord', base, apiKey }`: your coordination key travels as `X-API-Key`, and your TypeSafe
|
|
253
|
+
key travels in `x-systemone-key` for that call only. `askSystemOne` (also exported as `askJev`) asks once, with any model
|
|
254
|
+
spelling (`jev-1-13-0` → `jev-1.13.0`, `jev-1-14-0` → `jev-1.14.0`, `typesafe-jev` → `jev-latest`).
|
|
255
|
+
|
|
256
|
+
Jev's answers are recorded in two answer logs on Base: JevAnswerLog `0x8e9B9cE86a2d55c10318607b0c815B7B8C66254d` and
|
|
257
|
+
JevAnswerLogV2 `0xC08dfc61d702AEA45d1EF2515d8BDA36d548C75c`. `answerRecordedAt(reader, digest)` looks a digest up in
|
|
258
|
+
both. `readJudgeAdapter(reader)` reads JudgeAdapterV5 `0x5242dE388a846d16641744dEB095eD63990E7be8` on Base: the hook it
|
|
259
|
+
posts verdicts to, the decision log it anchors on, and its release delay.
|
|
260
|
+
|
|
261
|
+
`endJob` is signed by the job's buyer or by an evaluator the hook registered. For needs_review it returns `null`: the
|
|
262
|
+
job stays held until one of those parties decides, or until anyone calls `expire` after the deadline. The tests
|
|
263
|
+
re-encode the hook's `complete` from the covered GLMR hire on Base,
|
|
264
|
+
[`0xc606d00f…3116`](https://basescan.org/tx/0xc606d00fed64e912585bbc93feb15a9289cfcde94289f49f7c74a7a468903116),
|
|
265
|
+
byte for byte.
|
|
266
|
+
|
|
267
|
+
## Reselling on Base
|
|
268
|
+
|
|
269
|
+
`@moonbeam-foundation/sdk/resale` is the Moonbeam profile of the coordination layer's one payment scheme, a byte-identical
|
|
270
|
+
vendored copy pinned by its sha256 (`src/resale/payment-scheme.pin.json`): what a reseller's visitor pays, where every unit of it
|
|
271
|
+
goes, and the reseller's own leg — the same numbers the layer itself charges.
|
|
272
|
+
|
|
273
|
+
```
|
|
274
|
+
price_to_buyer = seller_price + our_fee + venue_fee(in) + venue_fee(out) [+ reseller_markup]
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
```ts
|
|
278
|
+
import { quoteBase, legsByName, markupLeg, childKeyTerms, childKeyPlan, subscribePlan, readSale } from '@moonbeam-foundation/sdk/resale';
|
|
279
|
+
|
|
280
|
+
const q = quoteBase({ venue_in: 'reseller', venue_out: 'erc8183-base', seller_price: 8_000n, reseller: { tenant, markup_bps: 1000 } });
|
|
281
|
+
if (q.ok) legsByName(q); // { our_fee: 2000n, venue_fee: 888n, seller_share: 8000n, reseller_markup: 1000n }; Σ = q.price_to_buyer
|
|
282
|
+
childKeyTerms({ '*': 1000 }); // what a child key is, in the layer's words
|
|
283
|
+
childKeyPlan({ '*': 1000 }); // POST /tenant/keys { action: 'child', markup_bps } — the reseller sends it with its own key
|
|
284
|
+
subscribePlan({ usdc: 1 }); // POST /access/subscribe — the child's top-up door (the x402 quote comes back as a 402)
|
|
285
|
+
await readSale(transport, 'rs_…'); // GET /resale/sales/{id} through the site's proxy
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
A reseller is a tenant of the layer; its visitors buy on child keys from the reseller's balance; the markup is the reseller's
|
|
289
|
+
own leg, never part of the layer's fee. The buyer pays on one of the five Base venues (`x402-base`, `erc8183-base`,
|
|
290
|
+
`memoacp-base`, `bitagent-base`, `olas-base`), and the assurance economy stays in GLMR. The seller is paid where it was hired:
|
|
291
|
+
`quoteSale` takes the seller's own venue (x402 on Solana or Arbitrum, ACP v3 on Arc, and the others the scheme prices), and
|
|
292
|
+
`sellerRailOf(offer)` says which network, token and payTo its leg settles on. A seller's own x402 entry comes first, then its
|
|
293
|
+
venue's rail. A seller with no paid rail of its own is paid in USDC on Base. A network the seller did not publish is never
|
|
294
|
+
chosen for it: its leg is held instead. `quoteBase` keeps both sides on Base. Example:
|
|
295
|
+
[`examples/24-resell-on-base.mjs`](../examples/24-resell-on-base.mjs).
|
|
296
|
+
|
|
297
|
+
## Attested by default
|
|
298
|
+
|
|
299
|
+
Every step this SDK handles comes with its record: a grade (`recordGrade`), an assurance settle (`settleAttested`) and a
|
|
300
|
+
resale step (`attestSale`). Each returns its mark (a digest anyone can recompute from the step's canonical body) and the
|
|
301
|
+
unsigned calls that put the mark on Base, where Moonbeam runs:
|
|
302
|
+
|
|
303
|
+
| contract on Base | call | built for |
|
|
304
|
+
|---|---|---|
|
|
305
|
+
| JevDecisionLog `0x209490d6A0FFC5368A42b0c2208BDCda853f6a92` | `record(subject, kind, digest, confidenceBps, model, uri)` | every step |
|
|
306
|
+
| JevAnswerLog `0x8e9B9cE86a2d55c10318607b0c815B7B8C66254d` | `record(useCase, subject, inputDigest, answersDigest, decisionDigest, model, uri)` | a grade |
|
|
307
|
+
| a grade stamp registry | `stamp(subject, digest, verdict, mode, model, uri)` | a step with a verdict |
|
|
308
|
+
|
|
309
|
+
Base has no grade stamp registry, so the stamp comes back in `missing`, with no call. Pass `networks: [chainId]` to
|
|
310
|
+
aim elsewhere, or `networks: 'none'` (`network: 'none'` for a grade) to turn recording off. The mark is returned
|
|
311
|
+
either way. Your wallet signs the calls; nothing here sends.
|
|
312
|
+
|
|
313
|
+
```ts
|
|
314
|
+
import { settleAttested, hookQuote } from '@moonbeam-foundation/sdk/assurance';
|
|
315
|
+
const s = settleAttested(job, hookQuote({ price, premium, deposit, covered: true }));
|
|
316
|
+
s.legs; // who receives what, from the job's own GLMR amounts
|
|
317
|
+
s.attest.calls; // [JevDecisionLog.record on 8453]
|
|
318
|
+
s.attest.missing; // [{ contract: 'GradeStampRegistry', chainId: 8453, why: … }]
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
## Paying an x402 wall
|
|
322
|
+
|
|
323
|
+
```ts
|
|
324
|
+
import { quoteWall, transferAuthorization, paymentHeaders } from '@moonbeam-foundation/sdk/x402';
|
|
325
|
+
const { accept } = await quoteWall(transport, '/x402/class/mcp.digest', { network: 'eip155:8453' }); // the entry of your network
|
|
326
|
+
const typed = transferAuthorization(accept, { from: myAddress }); // EIP-3009 TransferWithAuthorization, on that chain
|
|
327
|
+
const signature = await wallet.signTypedData(typed); // your wallet signs
|
|
328
|
+
const headers = paymentHeaders(accept, typed.message, signature); // { 'PAYMENT-SIGNATURE': … } (x402 v2)
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
A wall's 402 lists one `exact` entry per network it settles on. `pickAccept(body, { network })` takes the entry of the
|
|
332
|
+
network you pay from: a CAIP-2 id, a v1 name such as `arbitrum`, or a preference list. With no network named, it takes
|
|
333
|
+
Base. Network, token, payTo and amount all come from the entry. A Solana entry is picked like any other, but
|
|
334
|
+
`transferAuthorization` refuses it: your Solana wallet builds that transfer. The x402 v2 payment carries the accepted
|
|
335
|
+
entry whole, in `PAYMENT-SIGNATURE`. `{ legacy: true }` adds `X-PAYMENT` beside it, and `x402Version: 1` sends only
|
|
336
|
+
`X-PAYMENT`. `subscribe()` posts the top-up door and returns its 402 quote. `quoteSale` from `./resale` prices the same
|
|
337
|
+
wall from the seller's price.
|
|
338
|
+
|
|
339
|
+
## Cover quotes
|
|
340
|
+
|
|
341
|
+
```ts
|
|
342
|
+
import { quoteCover, coverQuotePlan } from '@moonbeam-foundation/sdk/cover';
|
|
343
|
+
const q = await quoteCover(transport, { buyer: myAddress, job: 'bitagent:8453:8566' }); // venue bitagent, chain 8453, job 8566
|
|
344
|
+
if (!q.ok) q.why; // why it is not insurable now
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
`GET /v1/cover/quote` requires the venue the job is on. These helpers always send it: `erc8183` (the Virtuals ACP v3
|
|
348
|
+
contract) unless you pass `venue`, or unless the job id names its own. Inputs a venue cannot be quoted from are refused
|
|
349
|
+
before any call.
|
|
350
|
+
|
|
351
|
+
## Job ids and holds
|
|
352
|
+
|
|
353
|
+
`./jobs` reads a job id from any market. A bare number is a Virtuals ACP v3 job on Base. `<label>:<chainId>:<jobId>` names
|
|
354
|
+
the contract the job lives in, as in `bitagent:8453:8566`. A 32-byte id is an assurance-hook job. `readJobs(transport, ids)`
|
|
355
|
+
reads up to 50 of them in one call. A job whose grade came back `needs_review` has its own hold, separate from
|
|
356
|
+
`awaiting_grade`: a person reviews the decision. `toOutcome(state, clock, { verdict: 'needs_review' })` returns
|
|
357
|
+
`pending: 'needsReview'` for such a job.
|
|
358
|
+
|
|
359
|
+
## Refusals
|
|
360
|
+
|
|
361
|
+
Every read throws a `CoordError` on a refusal (`./errors`): `status`, `code`, `type`, `param`, `nextStep` and the words as
|
|
362
|
+
its message. The layer refuses in two shapes, `{ ok: false, code, error: "<words>" }` and the OpenAI error object
|
|
363
|
+
`{ error: { message, type, param, code } }`, and `refusalOf` reads both. An `error` that is an object never surfaces as
|
|
364
|
+
`[object Object]`.
|
|
365
|
+
|
|
366
|
+
## Proofs
|
|
367
|
+
|
|
368
|
+
```ts
|
|
369
|
+
import { readProof, verifyOnBase } from '@moonbeam-foundation/sdk/proof';
|
|
370
|
+
import { baseReader } from '@moonbeam-foundation/sdk/rpc';
|
|
371
|
+
const p = await readProof(transport, txHash); // block, superroot, checks, proven, ref v5:8453:<tx>@<superroot>
|
|
372
|
+
// { chain: 42161 } reads a transaction on another chain the layer proves
|
|
373
|
+
const c = await verifyOnBase(baseReader(), p.blob!); // verify(blob) by eth_call on the V5 verifier 0x2D475a53eAA8F9AC78B69bD2429275a622C644f8
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
`c.verified` is what the verifier returns. `c.anchored` is false while the verifier has no superroot anchor set. The
|
|
377
|
+
block is bound to the snapshot the verifier folds by the inclusion path the layer serves (`c.inclusion`).
|
|
378
|
+
|
|
379
|
+
## Arbitration
|
|
380
|
+
|
|
381
|
+
`./arbitration` builds the calls of the three-tier ArbitratorRouter: `resolveByCode` (tier 0, anyone),
|
|
382
|
+
`secondGraderTypedData` + `resolveBySecondGrader` (tier 1), and `councilApprove`, `pullOracle` and
|
|
383
|
+
`resolveByCouncil` (tier 2). It also decodes `Escalated`, `CouncilApproved` and `Resolved`, and `recordEffectOf`
|
|
384
|
+
says what a ruling writes into the seller's record. The router is not deployed on Base and the hook names no
|
|
385
|
+
arbitrator (`ARBITRATION_ON_BASE`), so every builder takes the router's address.
|
|
386
|
+
|
|
387
|
+
## The Base reader
|
|
388
|
+
|
|
389
|
+
`baseReader()` from `./rpc` walks a measured rotation of Base endpoints: a rate limit or a refused range moves the call
|
|
390
|
+
to the next endpoint, and a revert is thrown at once. `BASE_RPC` (a comma list) goes first. The live examples read
|
|
391
|
+
through it.
|
|
392
|
+
|
|
393
|
+
## Skills for AI agents
|
|
394
|
+
|
|
395
|
+
The package ships one skill per actor under `skills/`, so an agent acting as a
|
|
396
|
+
backer gets the backer's reasoning and not the doubter's:
|
|
397
|
+
|
|
398
|
+
`moonbeam-assurance` (router) · `moonbeam-acp-decoding` · and
|
|
399
|
+
`moonbeam-actor-{client,provider,evaluator,backer,doubter,challenger}`
|
|
400
|
+
|
|
401
|
+
Each states what its actor **risks** before what it earns, keeps to runnable
|
|
402
|
+
calls rather than prose economics, and ends with the deployment boundary. Those
|
|
403
|
+
rules are enforced by tests, not convention.
|
|
404
|
+
|
|
405
|
+
## Hooking up a host protocol
|
|
406
|
+
|
|
407
|
+
The `acp` adapter translates an [Agent Commerce
|
|
408
|
+
Protocol](https://whitepaper.virtuals.io/) job into insurable terms:
|
|
409
|
+
|
|
410
|
+
```ts
|
|
411
|
+
import { acpAdapter, settle, DEFAULT_ACP_POLICY } from '@moonbeam-foundation/sdk';
|
|
412
|
+
|
|
413
|
+
const terms = acpAdapter.toTerms(job, DEFAULT_ACP_POLICY);
|
|
414
|
+
if (!terms) return; // not insurable — see below
|
|
415
|
+
|
|
416
|
+
const verdict = acpAdapter.toVerdict({ approved: false, score: 0.2 });
|
|
417
|
+
const { ledger } = settle({ terms, verdict });
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
A job is **not insurable** when it is unpriced, above the policy cap, past the
|
|
421
|
+
`transaction` phase (the outcome is already being decided), missing a signed
|
|
422
|
+
Proof of Agreement (nothing to grade against), or missing a deadline (nothing
|
|
423
|
+
resolves it if the evaluator goes silent). `toTerms` returns `null` rather than
|
|
424
|
+
throwing, because "not insured" is the ordinary case, not an error.
|
|
425
|
+
|
|
426
|
+
SLA expiry maps to `notDelivered`, never to `cheated` — the host protocol
|
|
427
|
+
already refunds, and nobody should be punished for a job that simply expired.
|
|
428
|
+
|
|
429
|
+
### Writing another adapter
|
|
430
|
+
|
|
431
|
+
Implement `AssuranceAdapter<HostJob, HostOutcome>`: say what a job costs and what
|
|
432
|
+
its outcome means. Adapters translate; they never settle.
|
|
433
|
+
|
|
434
|
+
The interface is deliberately narrow because it currently has **one**
|
|
435
|
+
implementation. One adapter is a hypothetical seam, two is a working one — so it is
|
|
436
|
+
better to keep this small until a second protocol shows what actually needs to
|
|
437
|
+
be shared.
|
|
438
|
+
|
|
439
|
+
## Who must take part, and who may
|
|
440
|
+
|
|
441
|
+
```ts
|
|
442
|
+
import { ACTORS, actorsBy } from '@moonbeam-foundation/sdk';
|
|
443
|
+
|
|
444
|
+
actorsBy('obligatory'); // provider, evaluator
|
|
445
|
+
actorsBy('optional'); // client, pool, doubter, challenger
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
Only the two roles the system takes at their word must post capital: the
|
|
449
|
+
provider doing the work and the evaluator grading it. Everyone else opts in.
|
|
450
|
+
|
|
451
|
+
Worth being precise about backing a pool: choosing to back one is optional, but once
|
|
452
|
+
in, underwriting is not a separate opt-in — **the exposure is the yield**.
|
|
453
|
+
There is no passive tier, because rewards come from fees paid rather than
|
|
454
|
+
inflation.
|
|
455
|
+
|
|
456
|
+
## Pool economics
|
|
457
|
+
|
|
458
|
+
```ts
|
|
459
|
+
import { runSeason, breakEvenFailureRate, clearingFeeIsCoherent } from '@moonbeam-foundation/sdk';
|
|
460
|
+
|
|
461
|
+
// An undersized bond, to show the pool actually paying. A deposit at or above
|
|
462
|
+
// the damage leaves the pool at zero, which is the intended steady state.
|
|
463
|
+
runSeason({
|
|
464
|
+
jobs: 5_000,
|
|
465
|
+
premiumPerJob: units('1'),
|
|
466
|
+
failureRate: 0.006,
|
|
467
|
+
damagePerFailure: units('70'),
|
|
468
|
+
depositPerJob: units('40'),
|
|
469
|
+
});
|
|
470
|
+
// → premium 4_970, absorbed by deposits 1_200, paid by pool 900, net +4_070
|
|
471
|
+
|
|
472
|
+
breakEvenFailureRate(units('1'), units('70'), units('40')); // ≈ 0.032
|
|
473
|
+
clearingFeeIsCoherent(34, units('1'), units('70'), units('40')); // false
|
|
474
|
+
|
|
475
|
+
// Raise the bond above the damage and the residual disappears:
|
|
476
|
+
runSeason({ /* …as above… */ depositPerJob: units('120') });
|
|
477
|
+
// → premium 4_970, absorbed by deposits 2_100, paid by pool 0, net +4_970
|
|
478
|
+
breakEvenFailureRate(units('1'), units('70'), units('120')); // null — no rate breaks it
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
That last call is the useful one. A published clearing fee is a claim about the
|
|
482
|
+
failure rate the market expects; a pool's break-even is the rate it can actually
|
|
483
|
+
survive. If those disagree by an order of magnitude, the two numbers were
|
|
484
|
+
written against different assumptions — which is exactly the kind of drift that
|
|
485
|
+
survives review when the economics live in prose.
|
|
486
|
+
|
|
487
|
+
## Design notes
|
|
488
|
+
|
|
489
|
+
**Money is `bigint` minor units.** No floats anywhere in settlement. Basis-point
|
|
490
|
+
maths rounds *down*, so no unit is ever minted that nobody funded.
|
|
491
|
+
|
|
492
|
+
**The core is pure.** `settle()` takes terms and a verdict and returns a ledger.
|
|
493
|
+
No clock, no chain, no I/O. That is what makes the economics testable:
|
|
494
|
+
conservation of value, the arson cap, and "cheating pays the victim" are
|
|
495
|
+
assertions over a return value rather than claims in a document.
|
|
496
|
+
|
|
497
|
+
**The arson rule.** Doubt payouts are capped at capital already bonded
|
|
498
|
+
(`deposit + poolCapital`), so causing a failure always costs more than betting
|
|
499
|
+
on it can pay.
|
|
500
|
+
|
|
501
|
+
## Development
|
|
502
|
+
|
|
503
|
+
```bash
|
|
504
|
+
npm install
|
|
505
|
+
npm test # every suite, offline
|
|
506
|
+
npm run check # typecheck + build + tests + the two pins (the assurance machine, the payment scheme)
|
|
507
|
+
npm run typecheck # strict, noUncheckedIndexedAccess, exactOptionalPropertyTypes
|
|
508
|
+
npm run build # ESM + .d.ts to dist/
|
|
509
|
+
npm run test:coverage
|
|
510
|
+
```
|
|
511
|
+
|