@moonbeam-foundation/sdk 0.5.2
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/LICENSE +17 -0
- package/README.md +378 -0
- package/bin/moonbeam.mjs +56 -0
- package/dist/acp/coverage.d.ts +36 -0
- package/dist/acp/coverage.d.ts.map +1 -0
- package/dist/acp/coverage.js +51 -0
- package/dist/acp/coverage.js.map +1 -0
- package/dist/acp/events.d.ts +201 -0
- package/dist/acp/events.d.ts.map +1 -0
- package/dist/acp/events.js +201 -0
- package/dist/acp/events.js.map +1 -0
- package/dist/acp/hex.d.ts +29 -0
- package/dist/acp/hex.d.ts.map +1 -0
- package/dist/acp/hex.js +51 -0
- package/dist/acp/hex.js.map +1 -0
- package/dist/acp/lifecycle-types.d.ts +11 -0
- package/dist/acp/lifecycle-types.d.ts.map +1 -0
- package/dist/acp/lifecycle-types.js +8 -0
- package/dist/acp/lifecycle-types.js.map +1 -0
- package/dist/acp/lifecycle.d.ts +78 -0
- package/dist/acp/lifecycle.d.ts.map +1 -0
- package/dist/acp/lifecycle.js +224 -0
- package/dist/acp/lifecycle.js.map +1 -0
- package/dist/acp/outcome.d.ts +45 -0
- package/dist/acp/outcome.d.ts.map +1 -0
- package/dist/acp/outcome.js +104 -0
- package/dist/acp/outcome.js.map +1 -0
- package/dist/acp/pipeline.d.ts +30 -0
- package/dist/acp/pipeline.d.ts.map +1 -0
- package/dist/acp/pipeline.js +25 -0
- package/dist/acp/pipeline.js.map +1 -0
- package/dist/acp/recourse.d.ts +74 -0
- package/dist/acp/recourse.d.ts.map +1 -0
- package/dist/acp/recourse.js +234 -0
- package/dist/acp/recourse.js.map +1 -0
- package/dist/acp/registry.d.ts +62 -0
- package/dist/acp/registry.d.ts.map +1 -0
- package/dist/acp/registry.js +86 -0
- package/dist/acp/registry.js.map +1 -0
- package/dist/acp/transitions.d.ts +82 -0
- package/dist/acp/transitions.d.ts.map +1 -0
- package/dist/acp/transitions.js +106 -0
- package/dist/acp/transitions.js.map +1 -0
- package/dist/actors/index.d.ts +84 -0
- package/dist/actors/index.d.ts.map +1 -0
- package/dist/actors/index.js +133 -0
- package/dist/actors/index.js.map +1 -0
- package/dist/actors/track-record.d.ts +79 -0
- package/dist/actors/track-record.d.ts.map +1 -0
- package/dist/actors/track-record.js +105 -0
- package/dist/actors/track-record.js.map +1 -0
- package/dist/adapters/acp.d.ts +48 -0
- package/dist/adapters/acp.d.ts.map +1 -0
- package/dist/adapters/acp.js +74 -0
- package/dist/adapters/acp.js.map +1 -0
- package/dist/adapters/port.d.ts +53 -0
- package/dist/adapters/port.d.ts.map +1 -0
- package/dist/adapters/port.js +2 -0
- package/dist/adapters/port.js.map +1 -0
- package/dist/adapters/stamp.d.ts +57 -0
- package/dist/adapters/stamp.d.ts.map +1 -0
- package/dist/adapters/stamp.js +114 -0
- package/dist/adapters/stamp.js.map +1 -0
- package/dist/client/index.d.ts +123 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +78 -0
- package/dist/client/index.js.map +1 -0
- package/dist/connect/attestation.d.ts +64 -0
- package/dist/connect/attestation.d.ts.map +1 -0
- package/dist/connect/attestation.js +62 -0
- package/dist/connect/attestation.js.map +1 -0
- package/dist/connect/http.d.ts +36 -0
- package/dist/connect/http.d.ts.map +1 -0
- package/dist/connect/http.js +51 -0
- package/dist/connect/http.js.map +1 -0
- package/dist/connect/logs.d.ts +53 -0
- package/dist/connect/logs.d.ts.map +1 -0
- package/dist/connect/logs.js +42 -0
- package/dist/connect/logs.js.map +1 -0
- package/dist/connect/result.d.ts +49 -0
- package/dist/connect/result.d.ts.map +1 -0
- package/dist/connect/result.js +41 -0
- package/dist/connect/result.js.map +1 -0
- package/dist/core/actors.d.ts +13 -0
- package/dist/core/actors.d.ts.map +1 -0
- package/dist/core/actors.js +57 -0
- package/dist/core/actors.js.map +1 -0
- package/dist/core/clock.d.ts +55 -0
- package/dist/core/clock.d.ts.map +1 -0
- package/dist/core/clock.js +36 -0
- package/dist/core/clock.js.map +1 -0
- package/dist/core/money.d.ts +32 -0
- package/dist/core/money.d.ts.map +1 -0
- package/dist/core/money.js +57 -0
- package/dist/core/money.js.map +1 -0
- package/dist/core/pool.d.ts +71 -0
- package/dist/core/pool.d.ts.map +1 -0
- package/dist/core/pool.js +98 -0
- package/dist/core/pool.js.map +1 -0
- package/dist/core/settle.d.ts +50 -0
- package/dist/core/settle.d.ts.map +1 -0
- package/dist/core/settle.js +175 -0
- package/dist/core/settle.js.map +1 -0
- package/dist/core/types.d.ts +109 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +6 -0
- package/dist/core/types.js.map +1 -0
- package/dist/erc8004/index.d.ts +94 -0
- package/dist/erc8004/index.d.ts.map +1 -0
- package/dist/erc8004/index.js +118 -0
- package/dist/erc8004/index.js.map +1 -0
- package/dist/erc8004/registry.generated.d.ts +17 -0
- package/dist/erc8004/registry.generated.d.ts.map +1 -0
- package/dist/erc8004/registry.generated.js +20 -0
- package/dist/erc8004/registry.generated.js.map +1 -0
- package/dist/erc8183/index.d.ts +114 -0
- package/dist/erc8183/index.d.ts.map +1 -0
- package/dist/erc8183/index.js +109 -0
- package/dist/erc8183/index.js.map +1 -0
- package/dist/index.d.ts +81 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +73 -0
- package/dist/index.js.map +1 -0
- package/dist/judge/index.d.ts +64 -0
- package/dist/judge/index.d.ts.map +1 -0
- package/dist/judge/index.js +64 -0
- package/dist/judge/index.js.map +1 -0
- package/dist/launcher/index.d.ts +109 -0
- package/dist/launcher/index.d.ts.map +1 -0
- package/dist/launcher/index.js +144 -0
- package/dist/launcher/index.js.map +1 -0
- package/dist/negotiate/index.d.ts +166 -0
- package/dist/negotiate/index.d.ts.map +1 -0
- package/dist/negotiate/index.js +150 -0
- package/dist/negotiate/index.js.map +1 -0
- package/dist/onboard/index.d.ts +533 -0
- package/dist/onboard/index.d.ts.map +1 -0
- package/dist/onboard/index.js +509 -0
- package/dist/onboard/index.js.map +1 -0
- package/dist/onboarding/index.d.ts +177 -0
- package/dist/onboarding/index.d.ts.map +1 -0
- package/dist/onboarding/index.js +390 -0
- package/dist/onboarding/index.js.map +1 -0
- package/dist/pools/index.d.ts +191 -0
- package/dist/pools/index.d.ts.map +1 -0
- package/dist/pools/index.js +244 -0
- package/dist/pools/index.js.map +1 -0
- package/docs/demo.gif +0 -0
- package/docs/demo.tape +14 -0
- package/examples/01-settle-a-covered-job.mjs +18 -0
- package/examples/02-negotiate-a-hire.mjs +21 -0
- package/examples/03-end-to-end-offline.mjs +27 -0
- package/examples/10-grade-evidence.mjs +12 -0
- package/examples/11-grade-facts.mjs +10 -0
- package/examples/12-grade-answers.mjs +8 -0
- package/examples/13-grade-verdict.mjs +10 -0
- package/examples/14-grade-record.mjs +11 -0
- package/examples/15-grade-end-the-job.mjs +11 -0
- package/examples/16-grade-verify.mjs +9 -0
- package/examples/17-grade-live.mjs +14 -0
- package/examples/20-onboard-client.mjs +8 -0
- package/examples/21-onboard-provider.mjs +9 -0
- package/examples/22-onboard-evaluator.mjs +7 -0
- package/examples/23-onboard-backer.mjs +8 -0
- package/examples/30-read-a-real-job.mjs +19 -0
- package/examples/31-read-the-ending.mjs +15 -0
- package/examples/32-read-live-from-base.mjs +33 -0
- package/examples/33-read-many-jobs.mjs +12 -0
- package/examples/40-the-article.mjs +78 -0
- package/examples/README.md +93 -0
- package/examples/data/answers.mjs +8 -0
- package/examples/data/base-grade.recorded.json +72 -0
- package/examples/data/pilot-grade.recorded.json +78 -0
- package/examples/data/pilot-hire.json +35 -0
- package/examples/data/terms.mjs +11 -0
- package/examples/data/virtuals-job-74926.json +74 -0
- package/examples/data/virtuals-job-rejected.json +74 -0
- package/package.json +124 -0
- package/skills/moonbeam-acp-decoding/SKILL.md +80 -0
- package/skills/moonbeam-actor-backer/SKILL.md +81 -0
- package/skills/moonbeam-actor-challenger/SKILL.md +62 -0
- package/skills/moonbeam-actor-client/SKILL.md +67 -0
- package/skills/moonbeam-actor-doubter/SKILL.md +55 -0
- package/skills/moonbeam-actor-evaluator/SKILL.md +67 -0
- package/skills/moonbeam-actor-provider/SKILL.md +63 -0
- package/skills/moonbeam-assurance/SKILL.md +58 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { clampToZero, min, ZERO } from './money.js';
|
|
2
|
+
import { poolLossPerFailure } from './settle.js';
|
|
3
|
+
function assertRate(rate, label) {
|
|
4
|
+
if (!Number.isFinite(rate) || rate < 0 || rate > 1) {
|
|
5
|
+
throw new RangeError(`${label} must be a rate between 0 and 1, got ${rate}`);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function runSeason(input) {
|
|
9
|
+
assertRate(input.failureRate, 'failureRate');
|
|
10
|
+
if (!Number.isInteger(input.jobs) || input.jobs < 0) {
|
|
11
|
+
throw new RangeError(`jobs must be a non-negative integer, got ${input.jobs}`);
|
|
12
|
+
}
|
|
13
|
+
const failures = Math.round(input.jobs * input.failureRate);
|
|
14
|
+
const good = input.jobs - failures;
|
|
15
|
+
const premiumEarned = input.premiumPerJob * BigInt(good);
|
|
16
|
+
const perFailureFromDeposit = min(input.damagePerFailure, input.depositPerJob);
|
|
17
|
+
// Derived through the settlement core rather than restated here. This line
|
|
18
|
+
// used to be a bare `clampToZero(damage - deposit)`, which ignored the pool
|
|
19
|
+
// capital ceiling that `settle` enforces — so a season could project a loss
|
|
20
|
+
// the protocol cannot actually pay.
|
|
21
|
+
// A sentinel "infinite capital" value would make the uncapped path go through
|
|
22
|
+
// the same call and look tidier, but it would also mean a real caller passing
|
|
23
|
+
// a very large capital silently took the uncapped branch. The two cases are
|
|
24
|
+
// genuinely different questions, so they are two visible branches.
|
|
25
|
+
const capped = input.poolCapital !== undefined;
|
|
26
|
+
const perFailureFromPool = capped
|
|
27
|
+
? poolLossPerFailure({
|
|
28
|
+
pay: ZERO,
|
|
29
|
+
premium: input.premiumPerJob,
|
|
30
|
+
deposit: input.depositPerJob,
|
|
31
|
+
evaluatorBond: ZERO,
|
|
32
|
+
poolCapital: input.poolCapital,
|
|
33
|
+
damage: input.damagePerFailure,
|
|
34
|
+
})
|
|
35
|
+
: clampToZero(input.damagePerFailure - input.depositPerJob);
|
|
36
|
+
const absorbedByDeposits = perFailureFromDeposit * BigInt(failures);
|
|
37
|
+
const paidByPool = perFailureFromPool * BigInt(failures);
|
|
38
|
+
const net = premiumEarned - paidByPool;
|
|
39
|
+
return {
|
|
40
|
+
failures,
|
|
41
|
+
premiumEarned,
|
|
42
|
+
absorbedByDeposits,
|
|
43
|
+
paidByPool,
|
|
44
|
+
net,
|
|
45
|
+
profitable: net > ZERO,
|
|
46
|
+
capped,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The failure rate at which a pool exactly breaks even.
|
|
51
|
+
*
|
|
52
|
+
* Returns `null` when the pool cannot lose (the provider deposit always covers
|
|
53
|
+
* the damage in full), because there is then no rate at which it goes negative.
|
|
54
|
+
*/
|
|
55
|
+
export function breakEvenFailureRate(premiumPerJob, damagePerFailure, depositPerJob, poolCapital) {
|
|
56
|
+
// Named `lossPerFailure`, not `poolLossPerFailure`: the latter shadowed the
|
|
57
|
+
// imported function of that name, so the shared definition was invisible here
|
|
58
|
+
// even after it existed.
|
|
59
|
+
const lossPerFailure = poolCapital === undefined
|
|
60
|
+
? clampToZero(damagePerFailure - depositPerJob)
|
|
61
|
+
: poolLossPerFailure({
|
|
62
|
+
pay: ZERO,
|
|
63
|
+
premium: premiumPerJob,
|
|
64
|
+
deposit: depositPerJob,
|
|
65
|
+
evaluatorBond: ZERO,
|
|
66
|
+
poolCapital,
|
|
67
|
+
damage: damagePerFailure,
|
|
68
|
+
});
|
|
69
|
+
if (lossPerFailure === ZERO)
|
|
70
|
+
return null;
|
|
71
|
+
// premium * (1 - r) = loss * r -> r = premium / (premium + loss)
|
|
72
|
+
const premium = Number(premiumPerJob);
|
|
73
|
+
const loss = Number(lossPerFailure);
|
|
74
|
+
return premium / (premium + loss);
|
|
75
|
+
}
|
|
76
|
+
/** A clearing fee in bps read as the failure rate the market is pricing. */
|
|
77
|
+
export function impliedFailureRate(clearingFeeBps) {
|
|
78
|
+
return clearingFeeBps / 10_000;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Does a published clearing fee agree with what the pool actually needs?
|
|
82
|
+
*
|
|
83
|
+
* `tolerance` is a ratio: 0.5 accepts the implied rate landing within half an
|
|
84
|
+
* order of magnitude of break-even. A mismatch is not necessarily a bug — a
|
|
85
|
+
* pool may deliberately price in a margin — but an *order-of-magnitude* gap
|
|
86
|
+
* means the two figures were authored against different assumptions.
|
|
87
|
+
*/
|
|
88
|
+
export function clearingFeeIsCoherent(clearingFeeBps, premiumPerJob, damagePerFailure, depositPerJob, tolerance = 0.5) {
|
|
89
|
+
const breakEven = breakEvenFailureRate(premiumPerJob, damagePerFailure, depositPerJob);
|
|
90
|
+
if (breakEven === null)
|
|
91
|
+
return true;
|
|
92
|
+
const implied = impliedFailureRate(clearingFeeBps);
|
|
93
|
+
if (implied === 0)
|
|
94
|
+
return false;
|
|
95
|
+
const ratio = implied / breakEven;
|
|
96
|
+
return ratio >= tolerance && ratio <= 1 / tolerance;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=pool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pool.js","sourceRoot":"","sources":["../../src/core/pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAEpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAuDjD,SAAS,UAAU,CAAC,IAAY,EAAE,KAAa;IAC7C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC;QACnD,MAAM,IAAI,UAAU,CAAC,GAAG,KAAK,wCAAw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AG;IAEf,MAAM,SAAS,GAAG,oBAAoB,CAAC,aAAa,EAAE,gBAAgB,EAAE,aAAa,CAAC,CAAC;IACvF,IAAI,SAAS,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IACpC,MAAM,OAAO,GAAG,kBAAkB,CAAC,cAAc,CAAC,CAAC;IACnD,IAAI,OAAO,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAChC,MAAM,KAAK,GAAG,OAAO,GAAG,SAAS,CAAC;IAClC,OAAO,KAAK,IAAI,SAAS,IAAI,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC;AACtD,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Money } from './money.js';
|
|
2
|
+
import type { JobTerms, Ledger, Settlement, SettlementInput } from './types.js';
|
|
3
|
+
/** Escrowed for the duration of the job: the client's money plus the provider's bond. */
|
|
4
|
+
export declare function lockedValue(terms: JobTerms): Money;
|
|
5
|
+
/**
|
|
6
|
+
* The most a doubt position can ever pay out — "the arson rule".
|
|
7
|
+
*
|
|
8
|
+
* Doubt is capped by the capital already bonded to the job. Without this cap,
|
|
9
|
+
* a party could take a large doubt position and then cause the failure it bet
|
|
10
|
+
* on. With it, causing a failure always costs more than doubting it can pay.
|
|
11
|
+
*/
|
|
12
|
+
export declare function maxDoubtPayout(terms: JobTerms): Money;
|
|
13
|
+
/**
|
|
14
|
+
* What the pool actually loses on one established cheating.
|
|
15
|
+
*
|
|
16
|
+
* The provider's deposit is consumed first, and the pool can never be charged
|
|
17
|
+
* more than it put up — so the loss is the damage minus the deposit, capped at
|
|
18
|
+
* pool capital. Both bounds matter and dropping either one changes the answer.
|
|
19
|
+
*
|
|
20
|
+
* This exists because four modules were each computing this quantity and two of
|
|
21
|
+
* them omitted the cap: `runSeason` and the backer's readiness check both used
|
|
22
|
+
* a bare `damage - deposit`. On a thin pool (damage 900, deposit 40, capital 5)
|
|
23
|
+
* that reported a loss of 860 where `settle` pays 5 — a 172x overstatement of
|
|
24
|
+
* the number a backer sizes their capital against. The models disagreed because
|
|
25
|
+
* the quantity had four definitions rather than one.
|
|
26
|
+
*
|
|
27
|
+
* `settle` is the authority on what money moves, so the definition lives here
|
|
28
|
+
* and everyone else calls it. Same reasoning as `maxDoubtPayout` above, which
|
|
29
|
+
* has been shared from the start and has never drifted.
|
|
30
|
+
*/
|
|
31
|
+
export declare function poolLossPerFailure(terms: JobTerms): Money;
|
|
32
|
+
/**
|
|
33
|
+
* Settle one insured job and return every actor's net position.
|
|
34
|
+
*
|
|
35
|
+
* The function is total and pure: same input, same output, no clock, no chain,
|
|
36
|
+
* no I/O. That is what makes the economic invariants testable — conservation of
|
|
37
|
+
* value, the arson cap, and "cheating pays the victim" are assertions over this
|
|
38
|
+
* return value rather than claims in prose.
|
|
39
|
+
*
|
|
40
|
+
* Sign convention: positive is received, negative is paid.
|
|
41
|
+
*/
|
|
42
|
+
export declare function settle(input: SettlementInput): Settlement;
|
|
43
|
+
/**
|
|
44
|
+
* Total of every position. Must always be zero: settlement moves value between
|
|
45
|
+
* actors, it never creates or destroys it.
|
|
46
|
+
*/
|
|
47
|
+
export declare function ledgerTotal(ledger: Ledger): Money;
|
|
48
|
+
/** True when the ledger conserves value. Every settlement must satisfy this. */
|
|
49
|
+
export declare function conservesValue(ledger: Ledger): boolean;
|
|
50
|
+
//# sourceMappingURL=settle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"settle.d.ts","sourceRoot":"","sources":["../../src/core/settle.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,KAAK,EACV,QAAQ,EACR,MAAM,EAEN,UAAU,EACV,eAAe,EAChB,MAAM,YAAY,CAAC;AAyBpB,yFAAyF;AACzF,wBAAgB,WAAW,CAAC,KAAK,EAAE,QAAQ,GAAG,KAAK,CAElD;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,QAAQ,GAAG,KAAK,CAErD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,QAAQ,GAAG,KAAK,CAEzD;AAgBD;;;;;;;;;GASG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,eAAe,GAAG,UAAU,CA0FzD;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,CAEjD;AAED,gFAAgF;AAChF,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAEtD"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { applyBps, clampToZero, min, sum, ZERO } from './money.js';
|
|
2
|
+
const EMPTY_LEDGER = Object.freeze({
|
|
3
|
+
client: ZERO,
|
|
4
|
+
provider: ZERO,
|
|
5
|
+
evaluator: ZERO,
|
|
6
|
+
pool: ZERO,
|
|
7
|
+
doubter: ZERO,
|
|
8
|
+
challenger: ZERO,
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* Every role, derived rather than listed.
|
|
12
|
+
*
|
|
13
|
+
* This used to be a hand-written array typed `readonly Role[]`, which meant a
|
|
14
|
+
* *missing* entry compiled cleanly — and `ledgerTotal` would then sum a subset,
|
|
15
|
+
* so `conservesValue` returned true for a ledger that did not conserve. The
|
|
16
|
+
* package's headline invariant could pass on a broken ledger.
|
|
17
|
+
*
|
|
18
|
+
* `EMPTY_LEDGER` is `Record<Role, Money>`, so it cannot omit a role without
|
|
19
|
+
* failing to compile. Reading the keys from it makes that protection cover the
|
|
20
|
+
* conservation check too.
|
|
21
|
+
*/
|
|
22
|
+
const ROLES = Object.keys(EMPTY_LEDGER);
|
|
23
|
+
/** Escrowed for the duration of the job: the client's money plus the provider's bond. */
|
|
24
|
+
export function lockedValue(terms) {
|
|
25
|
+
return terms.pay + terms.premium + terms.deposit;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The most a doubt position can ever pay out — "the arson rule".
|
|
29
|
+
*
|
|
30
|
+
* Doubt is capped by the capital already bonded to the job. Without this cap,
|
|
31
|
+
* a party could take a large doubt position and then cause the failure it bet
|
|
32
|
+
* on. With it, causing a failure always costs more than doubting it can pay.
|
|
33
|
+
*/
|
|
34
|
+
export function maxDoubtPayout(terms) {
|
|
35
|
+
return terms.deposit + terms.poolCapital;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* What the pool actually loses on one established cheating.
|
|
39
|
+
*
|
|
40
|
+
* The provider's deposit is consumed first, and the pool can never be charged
|
|
41
|
+
* more than it put up — so the loss is the damage minus the deposit, capped at
|
|
42
|
+
* pool capital. Both bounds matter and dropping either one changes the answer.
|
|
43
|
+
*
|
|
44
|
+
* This exists because four modules were each computing this quantity and two of
|
|
45
|
+
* them omitted the cap: `runSeason` and the backer's readiness check both used
|
|
46
|
+
* a bare `damage - deposit`. On a thin pool (damage 900, deposit 40, capital 5)
|
|
47
|
+
* that reported a loss of 860 where `settle` pays 5 — a 172x overstatement of
|
|
48
|
+
* the number a backer sizes their capital against. The models disagreed because
|
|
49
|
+
* the quantity had four definitions rather than one.
|
|
50
|
+
*
|
|
51
|
+
* `settle` is the authority on what money moves, so the definition lives here
|
|
52
|
+
* and everyone else calls it. Same reasoning as `maxDoubtPayout` above, which
|
|
53
|
+
* has been shared from the start and has never drifted.
|
|
54
|
+
*/
|
|
55
|
+
export function poolLossPerFailure(terms) {
|
|
56
|
+
return min(clampToZero(terms.damage - terms.deposit), terms.poolCapital);
|
|
57
|
+
}
|
|
58
|
+
function assertNonNegative(terms) {
|
|
59
|
+
const entries = [
|
|
60
|
+
['pay', terms.pay],
|
|
61
|
+
['premium', terms.premium],
|
|
62
|
+
['deposit', terms.deposit],
|
|
63
|
+
['evaluatorBond', terms.evaluatorBond],
|
|
64
|
+
['poolCapital', terms.poolCapital],
|
|
65
|
+
['damage', terms.damage],
|
|
66
|
+
];
|
|
67
|
+
for (const [name, value] of entries) {
|
|
68
|
+
if (value < 0n)
|
|
69
|
+
throw new RangeError(`${name} must not be negative`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Settle one insured job and return every actor's net position.
|
|
74
|
+
*
|
|
75
|
+
* The function is total and pure: same input, same output, no clock, no chain,
|
|
76
|
+
* no I/O. That is what makes the economic invariants testable — conservation of
|
|
77
|
+
* value, the arson cap, and "cheating pays the victim" are assertions over this
|
|
78
|
+
* return value rather than claims in prose.
|
|
79
|
+
*
|
|
80
|
+
* Sign convention: positive is received, negative is paid.
|
|
81
|
+
*/
|
|
82
|
+
export function settle(input) {
|
|
83
|
+
const { terms, verdict, doubt, challenge } = input;
|
|
84
|
+
assertNonNegative(terms);
|
|
85
|
+
const ledger = { ...EMPTY_LEDGER };
|
|
86
|
+
const explain = [];
|
|
87
|
+
const credit = (role, amount, why) => {
|
|
88
|
+
if (amount === ZERO)
|
|
89
|
+
return;
|
|
90
|
+
ledger[role] += amount;
|
|
91
|
+
explain.push(why);
|
|
92
|
+
};
|
|
93
|
+
// ── Doubt premium is paid up front, whatever the outcome ──
|
|
94
|
+
// The doubter buys the right to be paid on failure; the pool sells it.
|
|
95
|
+
if (doubt && doubt.notional > ZERO) {
|
|
96
|
+
const fee = applyBps(doubt.notional, doubt.feeBps);
|
|
97
|
+
credit('doubter', -fee, `doubter pays ${fee} fee on ${doubt.notional} notional at ${doubt.feeBps}bps`);
|
|
98
|
+
credit('pool', fee, `pool receives ${fee} doubt fee`);
|
|
99
|
+
}
|
|
100
|
+
let poolShortfall = ZERO;
|
|
101
|
+
switch (verdict) {
|
|
102
|
+
case 'doneRight': {
|
|
103
|
+
// Work delivered and graded good: the client pays, the provider is paid,
|
|
104
|
+
// the pool keeps the premium for cover it did not have to honour.
|
|
105
|
+
credit('client', -(terms.pay + terms.premium), `client pays ${terms.pay} for work and ${terms.premium} for cover`);
|
|
106
|
+
credit('provider', terms.pay, `provider receives ${terms.pay}; deposit returned`);
|
|
107
|
+
credit('pool', terms.premium, `pool earns the ${terms.premium} premium`);
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
case 'notDelivered': {
|
|
111
|
+
// ACP's SLA-expiry path: escrow unwinds. Nobody is punished, nothing is
|
|
112
|
+
// stuck, and the pool earns nothing because it covered nothing.
|
|
113
|
+
explain.push('nobody delivered: escrow unwinds, client refunded in full, deposit returned, no penalty');
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
case 'rejected': {
|
|
117
|
+
// Work arrived and was graded bad. Numerically identical to notDelivered
|
|
118
|
+
// today — everything walks back — but kept distinct because the two are
|
|
119
|
+
// different facts about the world, and a pool that wants to price
|
|
120
|
+
// rejection rate separately from fraud rate needs them separable.
|
|
121
|
+
// Crucially: NO damage cover. An evaluator's refusal costs it nothing and
|
|
122
|
+
// requires no proof, so it cannot on its own be grounds to charge a pool.
|
|
123
|
+
explain.push('graded bad: escrow unwinds, client refunded, deposit returned, no damage cover');
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
case 'cheated': {
|
|
127
|
+
// The case a bare escrow cannot answer. The client is refunded (net zero
|
|
128
|
+
// on the job price) and additionally compensated: first from the
|
|
129
|
+
// provider's deposit, then from the pool for whatever the deposit cannot
|
|
130
|
+
// cover. Every unit paid to the client comes from someone who did wrong
|
|
131
|
+
// or who sold cover — never from an uninvolved party.
|
|
132
|
+
const fromDeposit = min(terms.damage, terms.deposit);
|
|
133
|
+
credit('provider', -fromDeposit, `provider forfeits ${fromDeposit} of its deposit`);
|
|
134
|
+
credit('client', fromDeposit, `client receives ${fromDeposit} from the provider's deposit`);
|
|
135
|
+
poolShortfall = poolLossPerFailure(terms);
|
|
136
|
+
credit('pool', -poolShortfall, `pool covers ${poolShortfall} the deposit could not`);
|
|
137
|
+
credit('client', poolShortfall, `client receives ${poolShortfall} from the pool`);
|
|
138
|
+
if (doubt && doubt.notional > ZERO) {
|
|
139
|
+
const payout = min(doubt.notional, maxDoubtPayout(terms));
|
|
140
|
+
credit('doubter', payout, `doubter paid ${payout} (capped at bonded capital)`);
|
|
141
|
+
credit('pool', -payout, `pool pays ${payout} to doubt`);
|
|
142
|
+
}
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
// ── A challenge is a separate, bonded claim about the verdict itself ──
|
|
147
|
+
if (challenge && (challenge.bond > ZERO || terms.evaluatorBond > ZERO)) {
|
|
148
|
+
if (challenge.upheld) {
|
|
149
|
+
credit('evaluator', -terms.evaluatorBond, `evaluator's call overturned: forfeits ${terms.evaluatorBond}`);
|
|
150
|
+
credit('challenger', terms.evaluatorBond, `challenger paid ${terms.evaluatorBond} from the evaluator's bond`);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
credit('challenger', -challenge.bond, `challenge failed: challenger forfeits ${challenge.bond}`);
|
|
154
|
+
credit('evaluator', challenge.bond, `evaluator keeps its bond and takes ${challenge.bond}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return {
|
|
158
|
+
ledger: Object.freeze({ ...ledger }),
|
|
159
|
+
locked: lockedValue(terms),
|
|
160
|
+
poolShortfall,
|
|
161
|
+
explain: Object.freeze(explain),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Total of every position. Must always be zero: settlement moves value between
|
|
166
|
+
* actors, it never creates or destroys it.
|
|
167
|
+
*/
|
|
168
|
+
export function ledgerTotal(ledger) {
|
|
169
|
+
return sum(ROLES.map((role) => ledger[role]));
|
|
170
|
+
}
|
|
171
|
+
/** True when the ledger conserves value. Every settlement must satisfy this. */
|
|
172
|
+
export function conservesValue(ledger) {
|
|
173
|
+
return ledgerTotal(ledger) === ZERO;
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=settle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"settle.js","sourceRoot":"","sources":["../../src/core/settle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAA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AC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,MAAc;IACxC,OAAO,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAChD,CAAC;AAED,gFAAgF;AAChF,MAAM,UAAU,cAAc,CAAC,MAAc;IAC3C,OAAO,WAAW,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { Money, Bps } from './money.js';
|
|
2
|
+
/**
|
|
3
|
+
* Roles in an insured job.
|
|
4
|
+
*
|
|
5
|
+
* These are ACP's names (Client / Provider / Evaluator) plus the three roles
|
|
6
|
+
* the assurance layer adds. Keeping ACP's vocabulary means an integration does
|
|
7
|
+
* not have to translate on the way in.
|
|
8
|
+
*/
|
|
9
|
+
export type Role = 'client' | 'provider' | 'evaluator' | 'pool' | 'doubter' | 'challenger';
|
|
10
|
+
/**
|
|
11
|
+
* Everyone who can act on a job, including those who never hold a position.
|
|
12
|
+
*
|
|
13
|
+
* `Role` is deliberately narrower: it keys the settlement ledger, so a member
|
|
14
|
+
* of it is somebody money can move to or from. The arbitrator resolves
|
|
15
|
+
* disputes and unresolvable states but takes no share of the escrow, so
|
|
16
|
+
* putting it in `Role` would give it a ledger line that must always be zero —
|
|
17
|
+
* and would let a caller ask what an arbitrator *earned*, which is not a
|
|
18
|
+
* question this system answers.
|
|
19
|
+
*
|
|
20
|
+
* It still has to be nameable: it is the only party who can end three of the
|
|
21
|
+
* four states nobody else can, and a report on who is stranded cannot omit the
|
|
22
|
+
* party who unsticks them.
|
|
23
|
+
*/
|
|
24
|
+
export type Participant = Role | 'arbitrator';
|
|
25
|
+
export declare const ARBITRATOR: "arbitrator";
|
|
26
|
+
/** Does this participant hold a position in the settlement ledger? */
|
|
27
|
+
export declare function holdsPosition(participant: Participant): participant is Role;
|
|
28
|
+
/** Whether a role can participate without putting capital at risk. */
|
|
29
|
+
export type Obligation = 'obligatory' | 'optional';
|
|
30
|
+
export interface ActorSpec {
|
|
31
|
+
readonly role: Role;
|
|
32
|
+
/** Whether taking part in an insured job requires posting capital. */
|
|
33
|
+
readonly obligation: Obligation;
|
|
34
|
+
/** What the role must bond to participate at all. */
|
|
35
|
+
readonly bonds: string;
|
|
36
|
+
/** What it earns when things go right. */
|
|
37
|
+
readonly earns: string;
|
|
38
|
+
/** What it loses when things go wrong. Never empty — every earner risks something. */
|
|
39
|
+
readonly risks: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The terms of one insured job. All amounts in minor units of the settlement
|
|
43
|
+
* asset (see money.ts).
|
|
44
|
+
*/
|
|
45
|
+
export interface JobTerms {
|
|
46
|
+
/** What the client pays the provider for the work itself. */
|
|
47
|
+
readonly pay: Money;
|
|
48
|
+
/** What the client pays for cover, on top of `pay`. */
|
|
49
|
+
readonly premium: Money;
|
|
50
|
+
/** The provider's own bond. First capital consumed when it cheats. */
|
|
51
|
+
readonly deposit: Money;
|
|
52
|
+
/** The evaluator's bond. Paid to a challenger if its verdict is overturned. */
|
|
53
|
+
readonly evaluatorBond: Money;
|
|
54
|
+
/** Capital standing behind the provider, available to cover a shortfall. */
|
|
55
|
+
readonly poolCapital: Money;
|
|
56
|
+
/**
|
|
57
|
+
* Consequential loss to the client when cheating is established — the cost of
|
|
58
|
+
* the failure, not just the price of the job. Covering this is the whole
|
|
59
|
+
* reason the assurance layer exists: a plain escrow refund does not.
|
|
60
|
+
*/
|
|
61
|
+
readonly damage: Money;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* How a job ended.
|
|
65
|
+
*
|
|
66
|
+
* The split between `rejected` and `cheated` is the one that protects a pool.
|
|
67
|
+
* Refusing to pay is free and safe for an evaluator — it requires no proof —
|
|
68
|
+
* so rejections are ordinary events, not evidence of fraud. If every rejection
|
|
69
|
+
* paid out damage cover, a pool would be underwriting quality disputes rather
|
|
70
|
+
* than dishonesty, and its loss rate would track the rejection rate instead of
|
|
71
|
+
* the fraud rate.
|
|
72
|
+
*
|
|
73
|
+
* - `doneRight` the work was delivered and graded good.
|
|
74
|
+
* - `notDelivered` nothing arrived: the deadline passed, or the job expired.
|
|
75
|
+
* - `rejected` work arrived and was graded bad. Everything walks back —
|
|
76
|
+
* no damage cover, because no neutral party found dishonesty.
|
|
77
|
+
* - `cheated` a neutral third party found against the provider. The only
|
|
78
|
+
* verdict that draws on the deposit and the coverage pool.
|
|
79
|
+
*/
|
|
80
|
+
export type Verdict = 'doneRight' | 'notDelivered' | 'rejected' | 'cheated';
|
|
81
|
+
/** An optional, opt-in position taken against a pool. */
|
|
82
|
+
export interface DoubtPosition {
|
|
83
|
+
readonly notional: Money;
|
|
84
|
+
readonly feeBps: Bps;
|
|
85
|
+
}
|
|
86
|
+
/** An optional, opt-in bonded claim that a specific verdict was wrong. */
|
|
87
|
+
export interface ChallengePosition {
|
|
88
|
+
readonly bond: Money;
|
|
89
|
+
/** Whether the challenge succeeded — i.e. the evaluator's call was overturned. */
|
|
90
|
+
readonly upheld: boolean;
|
|
91
|
+
}
|
|
92
|
+
export interface SettlementInput {
|
|
93
|
+
readonly terms: JobTerms;
|
|
94
|
+
readonly verdict: Verdict;
|
|
95
|
+
readonly doubt?: DoubtPosition;
|
|
96
|
+
readonly challenge?: ChallengePosition;
|
|
97
|
+
}
|
|
98
|
+
/** Net position per role. Positive is received, negative is paid. */
|
|
99
|
+
export type Ledger = Readonly<Record<Role, Money>>;
|
|
100
|
+
export interface Settlement {
|
|
101
|
+
readonly ledger: Ledger;
|
|
102
|
+
/** Value locked in escrow for the duration: pay + premium + deposit. */
|
|
103
|
+
readonly locked: Money;
|
|
104
|
+
/** Loss the provider's own deposit could not cover, met by the pool. */
|
|
105
|
+
readonly poolShortfall: Money;
|
|
106
|
+
/** Human-readable trace of each transfer, in order. Useful in tests and UIs. */
|
|
107
|
+
readonly explain: readonly string[];
|
|
108
|
+
}
|
|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Aa,CAAC,WAAwB;IACpD,OAAO,WAAW,KAAK,YAAY,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ERC-8004, Trustless Agents: the identity, reputation and validation
|
|
3
|
+
* registries.
|
|
4
|
+
*
|
|
5
|
+
* The job standard (ERC-8183) says what happened to a piece of work. It says
|
|
6
|
+
* nothing about who did it. That is this standard's job, across three separate
|
|
7
|
+
* registries, and a decoder that reads jobs without reading identity can tell
|
|
8
|
+
* you a job settled but not who settled it.
|
|
9
|
+
*
|
|
10
|
+
* Every topic0 below was re-derived from its signature rather than copied:
|
|
11
|
+
* `EVENT_SIGNATURES` is the source of truth, the tests hash each one and
|
|
12
|
+
* assert it matches, so a mistyped constant fails the build instead of
|
|
13
|
+
* silently never matching a live log.
|
|
14
|
+
*
|
|
15
|
+
* Deliberately address-agnostic, like the 8183 module. The canonical
|
|
16
|
+
* identity and reputation registries live at the same addresses on Base and
|
|
17
|
+
* Arc (no validation registry is recorded on either), but decoding keys on
|
|
18
|
+
* topic0 alone, so a fork or a redeployment reads the same. The addresses
|
|
19
|
+
* come from `registry.generated.ts`, which is generated from the address
|
|
20
|
+
* registry and never hand-edited.
|
|
21
|
+
*
|
|
22
|
+
* One caution this module encodes rather than hides: these registries are
|
|
23
|
+
* upgradeable and owned by a single EOA. Reading them is useful; treating them
|
|
24
|
+
* as ground truth is not, which is why nothing here returns a verdict.
|
|
25
|
+
*/
|
|
26
|
+
/** The three registries the standard defines (not every one is deployed; see REGISTRY_ADDRESSES). */
|
|
27
|
+
export type Erc8004Registry = 'identity' | 'reputation' | 'validation';
|
|
28
|
+
export { ERC8004_DEPLOYMENTS, ERC8004_CANONICAL } from './registry.generated.js';
|
|
29
|
+
export type { Erc8004ChainDeployment } from './registry.generated.js';
|
|
30
|
+
/**
|
|
31
|
+
* Canonical deployments, identical on Base (8453) and Arc (5042). Only the
|
|
32
|
+
* registries actually deployed are present: there is no canonical validation
|
|
33
|
+
* registry, so `REGISTRY_ADDRESSES.validation` is undefined.
|
|
34
|
+
*/
|
|
35
|
+
export declare const REGISTRY_ADDRESSES: Readonly<Partial<Record<Erc8004Registry, string>>>;
|
|
36
|
+
/** The address of one registry on one chain, or undefined when none is recorded there. */
|
|
37
|
+
export declare function registryAddress(chainId: number, registry: Erc8004Registry): string | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* The event signatures, exactly as the contracts declare them. The topic
|
|
40
|
+
* hashes are derived from these strings; see the test that proves it.
|
|
41
|
+
*/
|
|
42
|
+
export declare const EVENT_SIGNATURES: {
|
|
43
|
+
readonly Registered: "Registered(uint256,string,address)";
|
|
44
|
+
readonly URIUpdated: "URIUpdated(uint256,string,address)";
|
|
45
|
+
readonly MetadataSet: "MetadataSet(uint256,string,string,bytes)";
|
|
46
|
+
readonly Transfer: "Transfer(address,address,uint256)";
|
|
47
|
+
readonly NewFeedback: "NewFeedback(uint256,address,uint64,int128,uint8,string,string,string,string,string,bytes32)";
|
|
48
|
+
readonly FeedbackRevoked: "FeedbackRevoked(uint256,address,uint64)";
|
|
49
|
+
readonly ResponseAppended: "ResponseAppended(uint256,address,uint64,address,string,bytes32)";
|
|
50
|
+
readonly ValidationRequest: "ValidationRequest(address,uint256,string,bytes32)";
|
|
51
|
+
readonly ValidationResponse: "ValidationResponse(address,uint256,bytes32,uint8,string,bytes32,string)";
|
|
52
|
+
};
|
|
53
|
+
export type Erc8004Event = keyof typeof EVENT_SIGNATURES;
|
|
54
|
+
/** topic0 per event, keccak256 of the signature above. Verified in tests. */
|
|
55
|
+
export declare const EVENT_TOPICS: Readonly<Record<Erc8004Event, string>>;
|
|
56
|
+
/** Which registry emits each event. */
|
|
57
|
+
export declare const EVENT_REGISTRY: Readonly<Record<Erc8004Event, Erc8004Registry>>;
|
|
58
|
+
/** Reverse lookup: what does this topic0 mean? Unknown topics return undefined. */
|
|
59
|
+
export declare function eventForTopic(topic0: string): Erc8004Event | undefined;
|
|
60
|
+
/** Which registry a topic belongs to, or undefined if we do not read it. */
|
|
61
|
+
export declare function registryForTopic(topic0: string): Erc8004Registry | undefined;
|
|
62
|
+
/** Every topic this module decodes, for an indexer's watch list. */
|
|
63
|
+
export declare function watchedTopics(): readonly string[];
|
|
64
|
+
/**
|
|
65
|
+
* What an identity event tells you about an agent.
|
|
66
|
+
*
|
|
67
|
+
* `Transfer` is the ERC-721 event: these identities are NFTs, so a transfer is
|
|
68
|
+
* a change of owner. It matters for assurance because the party that answers
|
|
69
|
+
* for a worker's behaviour can change without the worker's URI changing at
|
|
70
|
+
* all, and a record that ignored transfers would attribute old work to a new
|
|
71
|
+
* owner.
|
|
72
|
+
*/
|
|
73
|
+
export type IdentityChange = 'minted' | 'uri-changed' | 'metadata-set' | 'owner-changed';
|
|
74
|
+
export declare function identityChangeOf(event: Erc8004Event): IdentityChange | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Reputation, in the standard's own terms.
|
|
77
|
+
*
|
|
78
|
+
* Feedback here is signed by whoever left it and can be revoked by them. That
|
|
79
|
+
* makes it an assertion, not a verdict: useful as a signal, never as the basis
|
|
80
|
+
* for paying out a guarantee. `isRevocable` states which way each event cuts,
|
|
81
|
+
* so a caller cannot accidentally treat a retractable claim as settled fact.
|
|
82
|
+
*/
|
|
83
|
+
export declare function isRevocable(event: Erc8004Event): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Validation: a request for someone to check a piece of work, and the answer.
|
|
86
|
+
*
|
|
87
|
+
* The response carries a `uint8` response value the standard leaves to the
|
|
88
|
+
* validator to define. We deliberately do not map it to an outcome: an
|
|
89
|
+
* assurance verdict is recomputed from sealed evidence, never adopted from a
|
|
90
|
+
* third party's score.
|
|
91
|
+
*/
|
|
92
|
+
export type ValidationStage = 'requested' | 'answered';
|
|
93
|
+
export declare function validationStageOf(event: Erc8004Event): ValidationStage | undefined;
|
|
94
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.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