event-contract-builder 0.1.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/DISCLAIMER.md +22 -0
- package/LICENSE +201 -0
- package/NOTICE +2 -0
- package/README.md +99 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +221 -0
- package/dist/src/cnl.d.ts +33 -0
- package/dist/src/cnl.d.ts.map +1 -0
- package/dist/src/cnl.js +59 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +221 -0
- package/dist/src/schema/event-contract.d.ts +99 -0
- package/dist/src/schema/event-contract.d.ts.map +1 -0
- package/dist/src/schema/event-contract.js +200 -0
- package/dist/src/schema/outcome.d.ts +11 -0
- package/dist/src/schema/outcome.d.ts.map +1 -0
- package/dist/src/schema/resolution.d.ts +203 -0
- package/dist/src/schema/resolution.d.ts.map +1 -0
- package/package.json +59 -0
package/DISCLAIMER.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Disclaimer
|
|
2
|
+
|
|
3
|
+
The event-contract-builder tooling and all schema definitions, templates, field
|
|
4
|
+
descriptions, and generated outputs in this repository are provided for
|
|
5
|
+
informational and structural purposes only.
|
|
6
|
+
|
|
7
|
+
Nothing in this repository constitutes legal advice, regulatory advice,
|
|
8
|
+
financial advice, or a complete or compliant event-contract submission.
|
|
9
|
+
Outputs generated by this tooling are drafts intended to support — not
|
|
10
|
+
replace — qualified legal, compliance, and regulatory review.
|
|
11
|
+
|
|
12
|
+
Event contracts may be subject to regulation under the Commodity Exchange
|
|
13
|
+
Act, CFTC rules, applicable exchange rules, and other laws depending on
|
|
14
|
+
jurisdiction and structure. You are solely responsible for ensuring that
|
|
15
|
+
any contract you design, submit, or list complies with all applicable laws
|
|
16
|
+
and regulations.
|
|
17
|
+
|
|
18
|
+
The author makes no representation that any schema, template, or output
|
|
19
|
+
produced by this tooling satisfies the requirements of any designated
|
|
20
|
+
contract market, swap execution facility, or regulatory body.
|
|
21
|
+
|
|
22
|
+
Use at your own risk.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# event-contract-builder
|
|
2
|
+
A TypeScript library, CLI, and MCP server for generating, validating, and converting YAML and JSON event contract specifications for prediction markets.
|
|
3
|
+
|
|
4
|
+
## Status
|
|
5
|
+
|
|
6
|
+
This package is in early development. The public API may change before v1.0.0.
|
|
7
|
+
|
|
8
|
+
The library entry point is available through the package root after building:
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
bun run build
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
npm install event-contract-builder
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
Render a canonical statement from structured resolution fields:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import {
|
|
24
|
+
EventContractSpec,
|
|
25
|
+
renderCanonicalStatement,
|
|
26
|
+
type EventContractSpecT,
|
|
27
|
+
} from "event-contract-builder";
|
|
28
|
+
|
|
29
|
+
const spec: EventContractSpecT = {
|
|
30
|
+
dsl: "event-contract-cnl/0.1",
|
|
31
|
+
resolution: {
|
|
32
|
+
criterion: {
|
|
33
|
+
kind: "threshold",
|
|
34
|
+
metric: {
|
|
35
|
+
name: "US CPI year-over-year rate",
|
|
36
|
+
unit: "percent",
|
|
37
|
+
extraction: "Read the annual percent change from the CPI publication.",
|
|
38
|
+
revisionPolicy: "first-published-value",
|
|
39
|
+
},
|
|
40
|
+
comparator: "greater-than-or-equal",
|
|
41
|
+
threshold: 3,
|
|
42
|
+
},
|
|
43
|
+
canonicalStatement: "",
|
|
44
|
+
observationWindow: {
|
|
45
|
+
start: "2026-01-01T00:00:00Z",
|
|
46
|
+
end: "2026-12-31T23:59:59Z",
|
|
47
|
+
timezone: "UTC",
|
|
48
|
+
},
|
|
49
|
+
sources: [
|
|
50
|
+
{
|
|
51
|
+
id: "bls-cpi",
|
|
52
|
+
name: "Consumer Price Index",
|
|
53
|
+
publisher: "U.S. Bureau of Labor Statistics",
|
|
54
|
+
url: "https://www.bls.gov/cpi/",
|
|
55
|
+
publicationSchedule: "Published monthly by the BLS.",
|
|
56
|
+
publiclyAccessible: true,
|
|
57
|
+
independenceNote: "The publisher is a government statistics agency.",
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
primarySourceId: "bls-cpi",
|
|
61
|
+
fallbacks: [],
|
|
62
|
+
resolutionDeadline: "2027-01-31T23:59:59Z",
|
|
63
|
+
earlyResolution: { allowed: false },
|
|
64
|
+
terminalAmbiguityPolicy: "void-and-refund",
|
|
65
|
+
edgeCases: [
|
|
66
|
+
{
|
|
67
|
+
scenario: "If the source retracts the first value before the deadline.",
|
|
68
|
+
disposition: "Use the corrected value published before the deadline.",
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
scenario: "If no value is published before the deadline.",
|
|
72
|
+
disposition: "Apply the terminal ambiguity policy.",
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
scenario: "If the dataset is renamed without method changes.",
|
|
76
|
+
disposition: "Use the renamed successor dataset.",
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
disputeWindowHours: 24,
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const canonicalStatement = renderCanonicalStatement(spec);
|
|
84
|
+
const validatedSpec = EventContractSpec.parse({
|
|
85
|
+
...spec,
|
|
86
|
+
resolution: {
|
|
87
|
+
...spec.resolution,
|
|
88
|
+
canonicalStatement,
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
console.log(validatedSpec.resolution.canonicalStatement);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Disclaimer
|
|
96
|
+
See [DISCLAIMER.md](DISCLAIMER.md) for important information about the legal and regulatory status of outputs produced by this tooling.
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
Copyright © 2026 Christopher Maximilian Altmann. Licensed under the Apache License, Version 2.0.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// src/cnl.ts
|
|
2
|
+
var COMPARATOR_PHRASES = {
|
|
3
|
+
"greater-than": "is greater than",
|
|
4
|
+
"greater-than-or-equal": "is greater than or equal to",
|
|
5
|
+
"less-than": "is less than",
|
|
6
|
+
"less-than-or-equal": "is less than or equal to",
|
|
7
|
+
"equal-to": "is exactly equal to",
|
|
8
|
+
"between-inclusive": "is greater than or equal to the lower bound and less than or equal to the upper bound of",
|
|
9
|
+
occurs: "occurs",
|
|
10
|
+
"does-not-occur": "does not occur"
|
|
11
|
+
};
|
|
12
|
+
var TEMPLATES = {
|
|
13
|
+
binaryThreshold: "This contract resolves YES if {METRIC}, as published by {PUBLISHER} ({SOURCE}), measured over {WINDOW}, {COMPARATOR_PHRASE} {THRESHOLD} {UNIT}, applying the {REVISION_POLICY} as of the resolution deadline; otherwise it resolves NO.",
|
|
14
|
+
binaryOccurrence: "This contract resolves YES if the following event {COMPARATOR_PHRASE} within {WINDOW}: {EVENT_CLAUSE} Otherwise it resolves NO.",
|
|
15
|
+
bucketMembership: "This contract resolves to the single bucket outcome listed in section payout whose range contains the value of {METRIC}, as published by {PUBLISHER} ({SOURCE}), measured over {WINDOW}, applying the {REVISION_POLICY}; bucket lower bounds are inclusive and upper bounds are exclusive."
|
|
16
|
+
};
|
|
17
|
+
var UNMET_DISPOSITION_PHRASES = {
|
|
18
|
+
"void-and-refund": "is voided and all positions are refunded at acquisition price",
|
|
19
|
+
"resolve-no": "resolves NO",
|
|
20
|
+
"resolve-to-floor": "settles at the floor value stated in section payout",
|
|
21
|
+
"exchange-determination-per-rulebook": "is resolved by exchange determination under the rulebook"
|
|
22
|
+
};
|
|
23
|
+
var REVISION_POLICY_PHRASES = {
|
|
24
|
+
"first-published-value": "first published value",
|
|
25
|
+
"value-as-of-observation-time": "value displayed at the stated observation time",
|
|
26
|
+
"final-revised-value": "final revised value"
|
|
27
|
+
};
|
|
28
|
+
function phraseFor(phrases, key, label) {
|
|
29
|
+
const phrase = phrases[key];
|
|
30
|
+
if (phrase === undefined) {
|
|
31
|
+
throw new Error(`Unsupported ${label}: ${key}`);
|
|
32
|
+
}
|
|
33
|
+
return phrase;
|
|
34
|
+
}
|
|
35
|
+
function windowPhrase(spec) {
|
|
36
|
+
const w = spec.resolution.observationWindow;
|
|
37
|
+
return `the period from ${w.start} to ${w.end} (${w.timezone})`;
|
|
38
|
+
}
|
|
39
|
+
function renderCanonicalStatement(spec) {
|
|
40
|
+
const c = spec.resolution.criterion;
|
|
41
|
+
const primary = spec.resolution.sources.find((s) => s.id === spec.resolution.primarySourceId);
|
|
42
|
+
const W = windowPhrase(spec);
|
|
43
|
+
switch (c.kind) {
|
|
44
|
+
case "threshold": {
|
|
45
|
+
const threshold = c.comparator === "between-inclusive" ? `the range [${c.threshold}, ${c.thresholdUpper}]` : String(c.threshold);
|
|
46
|
+
return TEMPLATES.binaryThreshold.replace("{METRIC}", c.metric.name).replace("{PUBLISHER}", primary.publisher).replace("{SOURCE}", primary.name).replace("{WINDOW}", W).replace("{COMPARATOR_PHRASE}", phraseFor(COMPARATOR_PHRASES, c.comparator, "comparator")).replace("{THRESHOLD}", threshold).replace("{UNIT}", c.metric.unit).replace("{REVISION_POLICY}", phraseFor(REVISION_POLICY_PHRASES, c.metric.revisionPolicy, "revision policy"));
|
|
47
|
+
}
|
|
48
|
+
case "occurrence":
|
|
49
|
+
return TEMPLATES.binaryOccurrence.replace("{COMPARATOR_PHRASE}", phraseFor(COMPARATOR_PHRASES, c.comparator, "comparator")).replace("{WINDOW}", W).replace("{EVENT_CLAUSE}", c.eventClause);
|
|
50
|
+
case "bucket-membership":
|
|
51
|
+
return TEMPLATES.bucketMembership.replace("{METRIC}", c.metric.name).replace("{PUBLISHER}", primary.publisher).replace("{SOURCE}", primary.name).replace("{WINDOW}", W).replace("{REVISION_POLICY}", phraseFor(REVISION_POLICY_PHRASES, c.metric.revisionPolicy, "revision policy"));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/schema/event-contract.ts
|
|
56
|
+
import { z as z2 } from "zod";
|
|
57
|
+
|
|
58
|
+
// src/schema/resolution.ts
|
|
59
|
+
import { z } from "zod";
|
|
60
|
+
var IsoDateTime = z.string().regex(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2})?(\.\d+)?(Z|[+-]\d{2}:\d{2})$/, "Must be ISO 8601 date-time with explicit offset (e.g. 2026-07-15T16:00:00-04:00)").describe("ISO 8601 date-time with mandatory UTC offset");
|
|
61
|
+
var IanaTimezone = z.string().regex(/^[A-Za-z]+\/[A-Za-z_+\-]+(\/[A-Za-z_+\-]+)?$|^UTC$/, "Must be an IANA timezone or UTC").describe("IANA timezone identifier (e.g. America/New_York) or UTC");
|
|
62
|
+
var Slug = z.string().regex(/^[a-z0-9]+(-[a-z0-9]+)*$/, "Must be lowercase kebab-case").describe("Stable lowercase kebab-case identifier");
|
|
63
|
+
var CnlSentence = z.string().min(20).max(600).regex(/^[A-Z].*\.$/s, "Must be a complete sentence (capitalized, ending in a period)").refine((s) => !/\b(approximately|roughly|about|around|reasonable|significant|materially|generally|etc\.?)\b/i.test(s), "CNL sentences must not contain hedging/vague terms (approximately, roughly, significant, etc.)").describe("A single precise sentence in the controlled vocabulary; vague terms are rejected");
|
|
64
|
+
var Metric = z.object({
|
|
65
|
+
name: z.string().min(3).describe("Exact name of the data point as the source publishes it"),
|
|
66
|
+
unit: z.string().min(1),
|
|
67
|
+
extraction: CnlSentence.describe("Exactly which table/field/series ID the value is read from"),
|
|
68
|
+
revisionPolicy: z.enum([
|
|
69
|
+
"first-published-value",
|
|
70
|
+
"value-as-of-observation-time",
|
|
71
|
+
"final-revised-value"
|
|
72
|
+
])
|
|
73
|
+
});
|
|
74
|
+
var ThresholdCriterion = z.object({
|
|
75
|
+
kind: z.literal("threshold"),
|
|
76
|
+
metric: Metric,
|
|
77
|
+
comparator: z.enum([
|
|
78
|
+
"greater-than",
|
|
79
|
+
"greater-than-or-equal",
|
|
80
|
+
"less-than",
|
|
81
|
+
"less-than-or-equal",
|
|
82
|
+
"equal-to",
|
|
83
|
+
"between-inclusive"
|
|
84
|
+
]),
|
|
85
|
+
threshold: z.number(),
|
|
86
|
+
thresholdUpper: z.number().optional()
|
|
87
|
+
});
|
|
88
|
+
var OccurrenceCriterion = z.object({
|
|
89
|
+
kind: z.literal("occurrence"),
|
|
90
|
+
comparator: z.enum(["occurs", "does-not-occur"]),
|
|
91
|
+
eventClause: CnlSentence,
|
|
92
|
+
evidenceStandard: CnlSentence
|
|
93
|
+
});
|
|
94
|
+
var BucketMembershipCriterion = z.object({
|
|
95
|
+
kind: z.literal("bucket-membership"),
|
|
96
|
+
metric: Metric
|
|
97
|
+
});
|
|
98
|
+
var Criterion = z.discriminatedUnion("kind", [
|
|
99
|
+
ThresholdCriterion,
|
|
100
|
+
OccurrenceCriterion,
|
|
101
|
+
BucketMembershipCriterion
|
|
102
|
+
]).describe("Structured resolution criterion; canonicalStatement is rendered from this");
|
|
103
|
+
var DataSource = z.object({
|
|
104
|
+
id: Slug,
|
|
105
|
+
name: z.string().min(3),
|
|
106
|
+
publisher: z.string().min(2).describe("Organization that produces the data"),
|
|
107
|
+
url: z.url(),
|
|
108
|
+
datasetId: z.string().optional(),
|
|
109
|
+
publicationSchedule: z.string().min(10).describe("When and how often the value is published"),
|
|
110
|
+
publiclyAccessible: z.boolean(),
|
|
111
|
+
independenceNote: z.string().min(30).describe("Publisher's independence from market participants")
|
|
112
|
+
});
|
|
113
|
+
var Fallback = z.object({
|
|
114
|
+
trigger: z.enum([
|
|
115
|
+
"primary-not-published-by-deadline",
|
|
116
|
+
"primary-discontinued",
|
|
117
|
+
"primary-methodology-materially-changed",
|
|
118
|
+
"primary-retracted-or-corrected"
|
|
119
|
+
]),
|
|
120
|
+
sourceId: Slug.describe("id of a source in resolution.sources"),
|
|
121
|
+
procedure: CnlSentence
|
|
122
|
+
});
|
|
123
|
+
var TerminalAmbiguityPolicy = z.enum([
|
|
124
|
+
"resolve-no",
|
|
125
|
+
"resolve-to-floor",
|
|
126
|
+
"void-and-refund",
|
|
127
|
+
"exchange-determination-per-rulebook"
|
|
128
|
+
]).describe("Pre-committed disposition when no source/criterion can determine the outcome");
|
|
129
|
+
var Resolution = z.object({
|
|
130
|
+
criterion: Criterion,
|
|
131
|
+
canonicalStatement: CnlSentence,
|
|
132
|
+
observationWindow: z.object({
|
|
133
|
+
start: IsoDateTime,
|
|
134
|
+
end: IsoDateTime,
|
|
135
|
+
timezone: IanaTimezone.describe("Governing timezone for any date words in CNL sentences")
|
|
136
|
+
}),
|
|
137
|
+
sources: z.array(DataSource).min(1).describe("All sources; first usable one per fallback order governs"),
|
|
138
|
+
primarySourceId: Slug,
|
|
139
|
+
fallbacks: z.array(Fallback).max(5).default([]),
|
|
140
|
+
resolutionDeadline: IsoDateTime,
|
|
141
|
+
earlyResolution: z.discriminatedUnion("allowed", [
|
|
142
|
+
z.object({ allowed: z.literal(false) }),
|
|
143
|
+
z.object({
|
|
144
|
+
allowed: z.literal(true),
|
|
145
|
+
condition: CnlSentence.describe("CNL condition under which the outcome is irreversibly determined early")
|
|
146
|
+
})
|
|
147
|
+
]),
|
|
148
|
+
terminalAmbiguityPolicy: TerminalAmbiguityPolicy,
|
|
149
|
+
edgeCases: z.array(z.object({
|
|
150
|
+
scenario: CnlSentence,
|
|
151
|
+
disposition: CnlSentence
|
|
152
|
+
})).min(3).max(40).describe("At least 3 pre-decided edge cases (revisions, ties, postponements, source outages...)"),
|
|
153
|
+
disputeWindowHours: z.number().int().min(0).max(168)
|
|
154
|
+
}).describe("Complete resolution mechanics: criterion, sources, fallbacks, deadline, edge cases").superRefine((r, ctx) => {
|
|
155
|
+
const ids = new Set(r.sources.map((s) => s.id));
|
|
156
|
+
if (!ids.has(r.primarySourceId)) {
|
|
157
|
+
ctx.addIssue({
|
|
158
|
+
code: "custom",
|
|
159
|
+
path: ["primarySourceId"],
|
|
160
|
+
message: "primarySourceId must match a source id"
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
for (const [i, f] of r.fallbacks.entries()) {
|
|
164
|
+
if (!ids.has(f.sourceId)) {
|
|
165
|
+
ctx.addIssue({
|
|
166
|
+
code: "custom",
|
|
167
|
+
path: ["fallbacks", i, "sourceId"],
|
|
168
|
+
message: "fallback sourceId must match a source id"
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (r.criterion.kind === "threshold") {
|
|
173
|
+
const hasUpper = r.criterion.thresholdUpper !== undefined;
|
|
174
|
+
const needsUpper = r.criterion.comparator === "between-inclusive";
|
|
175
|
+
if (needsUpper !== hasUpper) {
|
|
176
|
+
ctx.addIssue({
|
|
177
|
+
code: "custom",
|
|
178
|
+
path: ["criterion", "thresholdUpper"],
|
|
179
|
+
message: "thresholdUpper is required iff comparator is between-inclusive"
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const phrase = "comparator" in r.criterion ? COMPARATOR_PHRASES[r.criterion.comparator] : undefined;
|
|
184
|
+
if (phrase && !r.canonicalStatement.includes(phrase)) {
|
|
185
|
+
ctx.addIssue({
|
|
186
|
+
code: "custom",
|
|
187
|
+
path: ["canonicalStatement"],
|
|
188
|
+
message: `canonicalStatement must contain the fixed CNL phrase "${phrase}"`
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
// src/schema/event-contract.ts
|
|
194
|
+
var EventContractSpec = z2.object({
|
|
195
|
+
dsl: z2.literal("event-contract-cnl/0.1"),
|
|
196
|
+
resolution: Resolution
|
|
197
|
+
}).describe("Draft event-contract specification (CNL DSL v0.1) for pre-DCM review");
|
|
198
|
+
// src/schema/outcome.ts
|
|
199
|
+
import { z as z3 } from "zod";
|
|
200
|
+
var OutcomeSchema = z3.object({
|
|
201
|
+
type: z3.literal("binary").describe("Outcome type discriminator."),
|
|
202
|
+
values: z3.tuple([z3.literal("Yes"), z3.literal("No")]).describe("Binary outcome values.")
|
|
203
|
+
});
|
|
204
|
+
export {
|
|
205
|
+
renderCanonicalStatement,
|
|
206
|
+
UNMET_DISPOSITION_PHRASES,
|
|
207
|
+
TerminalAmbiguityPolicy,
|
|
208
|
+
TEMPLATES,
|
|
209
|
+
Slug,
|
|
210
|
+
Resolution,
|
|
211
|
+
OutcomeSchema,
|
|
212
|
+
Metric,
|
|
213
|
+
IsoDateTime,
|
|
214
|
+
IanaTimezone,
|
|
215
|
+
Fallback,
|
|
216
|
+
EventContractSpec,
|
|
217
|
+
DataSource,
|
|
218
|
+
Criterion,
|
|
219
|
+
CnlSentence,
|
|
220
|
+
COMPARATOR_PHRASES
|
|
221
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Controlled Natural Language (CNL) layer
|
|
3
|
+
* =======================================
|
|
4
|
+
*
|
|
5
|
+
* The DSL's CNL has three rules (full grammar in docs/cnl-grammar.md):
|
|
6
|
+
*
|
|
7
|
+
* R1. Closed comparator vocabulary — every comparison uses exactly one of
|
|
8
|
+
* the fixed phrases in COMPARATOR_PHRASES. No synonyms.
|
|
9
|
+
* R2. No hedging lexicon — sentences containing vague terms
|
|
10
|
+
* ("approximately", "significant", "reasonable", ...) are rejected
|
|
11
|
+
* at parse time by the CnlSentence schema.
|
|
12
|
+
* R3. Render, don't write — the trader-facing `canonicalStatement` is
|
|
13
|
+
* produced by `renderCanonicalStatement()` from structured fields.
|
|
14
|
+
* A hand-written statement that drifts from the structured terms
|
|
15
|
+
* fails validation.
|
|
16
|
+
*/
|
|
17
|
+
import type { EventContractSpecT } from "./schema/event-contract";
|
|
18
|
+
/** R1: the only legal English realizations of each comparator. */
|
|
19
|
+
export declare const COMPARATOR_PHRASES: Record<string, string>;
|
|
20
|
+
/** Sentence templates (placeholders in braces). */
|
|
21
|
+
export declare const TEMPLATES: {
|
|
22
|
+
readonly binaryThreshold: "This contract resolves YES if {METRIC}, as published by {PUBLISHER} ({SOURCE}), measured over {WINDOW}, {COMPARATOR_PHRASE} {THRESHOLD} {UNIT}, applying the {REVISION_POLICY} as of the resolution deadline; otherwise it resolves NO.";
|
|
23
|
+
readonly binaryOccurrence: "This contract resolves YES if the following event {COMPARATOR_PHRASE} within {WINDOW}: {EVENT_CLAUSE} Otherwise it resolves NO.";
|
|
24
|
+
readonly bucketMembership: "This contract resolves to the single bucket outcome listed in section payout whose range contains the value of {METRIC}, as published by {PUBLISHER} ({SOURCE}), measured over {WINDOW}, applying the {REVISION_POLICY}; bucket lower bounds are inclusive and upper bounds are exclusive.";
|
|
25
|
+
};
|
|
26
|
+
/** Fixed English realizations of contingency unmet-dispositions. */
|
|
27
|
+
export declare const UNMET_DISPOSITION_PHRASES: Record<string, string>;
|
|
28
|
+
/**
|
|
29
|
+
* Deterministically render the canonical CNL statement for a spec.
|
|
30
|
+
* Validators compare this to `resolution.canonicalStatement`.
|
|
31
|
+
*/
|
|
32
|
+
export declare function renderCanonicalStatement(spec: EventContractSpecT): string;
|
|
33
|
+
//# sourceMappingURL=cnl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cnl.d.ts","sourceRoot":"","sources":["../../src/cnl.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAElE,kEAAkE;AAClE,eAAO,MAAM,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAUrD,CAAC;AAEF,mDAAmD;AACnD,eAAO,MAAM,SAAS;;;;CAOZ,CAAC;AAEX,oEAAoE;AACpE,eAAO,MAAM,yBAAyB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAO5D,CAAC;AA0BF;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,kBAAkB,GAAG,MAAM,CAwDzE"}
|
package/dist/src/cnl.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// src/cnl.ts
|
|
2
|
+
var COMPARATOR_PHRASES = {
|
|
3
|
+
"greater-than": "is greater than",
|
|
4
|
+
"greater-than-or-equal": "is greater than or equal to",
|
|
5
|
+
"less-than": "is less than",
|
|
6
|
+
"less-than-or-equal": "is less than or equal to",
|
|
7
|
+
"equal-to": "is exactly equal to",
|
|
8
|
+
"between-inclusive": "is greater than or equal to the lower bound and less than or equal to the upper bound of",
|
|
9
|
+
occurs: "occurs",
|
|
10
|
+
"does-not-occur": "does not occur"
|
|
11
|
+
};
|
|
12
|
+
var TEMPLATES = {
|
|
13
|
+
binaryThreshold: "This contract resolves YES if {METRIC}, as published by {PUBLISHER} ({SOURCE}), measured over {WINDOW}, {COMPARATOR_PHRASE} {THRESHOLD} {UNIT}, applying the {REVISION_POLICY} as of the resolution deadline; otherwise it resolves NO.",
|
|
14
|
+
binaryOccurrence: "This contract resolves YES if the following event {COMPARATOR_PHRASE} within {WINDOW}: {EVENT_CLAUSE} Otherwise it resolves NO.",
|
|
15
|
+
bucketMembership: "This contract resolves to the single bucket outcome listed in section payout whose range contains the value of {METRIC}, as published by {PUBLISHER} ({SOURCE}), measured over {WINDOW}, applying the {REVISION_POLICY}; bucket lower bounds are inclusive and upper bounds are exclusive."
|
|
16
|
+
};
|
|
17
|
+
var UNMET_DISPOSITION_PHRASES = {
|
|
18
|
+
"void-and-refund": "is voided and all positions are refunded at acquisition price",
|
|
19
|
+
"resolve-no": "resolves NO",
|
|
20
|
+
"resolve-to-floor": "settles at the floor value stated in section payout",
|
|
21
|
+
"exchange-determination-per-rulebook": "is resolved by exchange determination under the rulebook"
|
|
22
|
+
};
|
|
23
|
+
var REVISION_POLICY_PHRASES = {
|
|
24
|
+
"first-published-value": "first published value",
|
|
25
|
+
"value-as-of-observation-time": "value displayed at the stated observation time",
|
|
26
|
+
"final-revised-value": "final revised value"
|
|
27
|
+
};
|
|
28
|
+
function phraseFor(phrases, key, label) {
|
|
29
|
+
const phrase = phrases[key];
|
|
30
|
+
if (phrase === undefined) {
|
|
31
|
+
throw new Error(`Unsupported ${label}: ${key}`);
|
|
32
|
+
}
|
|
33
|
+
return phrase;
|
|
34
|
+
}
|
|
35
|
+
function windowPhrase(spec) {
|
|
36
|
+
const w = spec.resolution.observationWindow;
|
|
37
|
+
return `the period from ${w.start} to ${w.end} (${w.timezone})`;
|
|
38
|
+
}
|
|
39
|
+
function renderCanonicalStatement(spec) {
|
|
40
|
+
const c = spec.resolution.criterion;
|
|
41
|
+
const primary = spec.resolution.sources.find((s) => s.id === spec.resolution.primarySourceId);
|
|
42
|
+
const W = windowPhrase(spec);
|
|
43
|
+
switch (c.kind) {
|
|
44
|
+
case "threshold": {
|
|
45
|
+
const threshold = c.comparator === "between-inclusive" ? `the range [${c.threshold}, ${c.thresholdUpper}]` : String(c.threshold);
|
|
46
|
+
return TEMPLATES.binaryThreshold.replace("{METRIC}", c.metric.name).replace("{PUBLISHER}", primary.publisher).replace("{SOURCE}", primary.name).replace("{WINDOW}", W).replace("{COMPARATOR_PHRASE}", phraseFor(COMPARATOR_PHRASES, c.comparator, "comparator")).replace("{THRESHOLD}", threshold).replace("{UNIT}", c.metric.unit).replace("{REVISION_POLICY}", phraseFor(REVISION_POLICY_PHRASES, c.metric.revisionPolicy, "revision policy"));
|
|
47
|
+
}
|
|
48
|
+
case "occurrence":
|
|
49
|
+
return TEMPLATES.binaryOccurrence.replace("{COMPARATOR_PHRASE}", phraseFor(COMPARATOR_PHRASES, c.comparator, "comparator")).replace("{WINDOW}", W).replace("{EVENT_CLAUSE}", c.eventClause);
|
|
50
|
+
case "bucket-membership":
|
|
51
|
+
return TEMPLATES.bucketMembership.replace("{METRIC}", c.metric.name).replace("{PUBLISHER}", primary.publisher).replace("{SOURCE}", primary.name).replace("{WINDOW}", W).replace("{REVISION_POLICY}", phraseFor(REVISION_POLICY_PHRASES, c.metric.revisionPolicy, "revision policy"));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export {
|
|
55
|
+
renderCanonicalStatement,
|
|
56
|
+
UNMET_DISPOSITION_PHRASES,
|
|
57
|
+
TEMPLATES,
|
|
58
|
+
COMPARATOR_PHRASES
|
|
59
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,yBAAyB,CAAC;AACxC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC"}
|