@hyperscale0/hsx 2.4.0 → 3.0.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.
Files changed (249) hide show
  1. package/CHANGELOG.md +2 -400
  2. package/README.md +3 -92
  3. package/dist/src/ast.d.ts +99 -310
  4. package/dist/src/ast.d.ts.map +1 -1
  5. package/dist/src/ast.js +16 -69
  6. package/dist/src/ast.js.map +1 -1
  7. package/dist/src/cli.d.ts +6 -19
  8. package/dist/src/cli.d.ts.map +1 -1
  9. package/dist/src/cli.js +76 -315
  10. package/dist/src/cli.js.map +1 -1
  11. package/dist/src/compile.d.ts +23 -73
  12. package/dist/src/compile.d.ts.map +1 -1
  13. package/dist/src/compile.js +1113 -148
  14. package/dist/src/compile.js.map +1 -1
  15. package/dist/src/cost.d.ts +8 -77
  16. package/dist/src/cost.d.ts.map +1 -1
  17. package/dist/src/cost.js +13 -422
  18. package/dist/src/cost.js.map +1 -1
  19. package/dist/src/format.d.ts +8 -7
  20. package/dist/src/format.d.ts.map +1 -1
  21. package/dist/src/format.js +9 -175
  22. package/dist/src/format.js.map +1 -1
  23. package/dist/src/headers.d.ts +35 -0
  24. package/dist/src/headers.d.ts.map +1 -0
  25. package/dist/src/headers.js +108 -0
  26. package/dist/src/headers.js.map +1 -0
  27. package/dist/src/index.d.ts +6 -12
  28. package/dist/src/index.d.ts.map +1 -1
  29. package/dist/src/index.js +4 -9
  30. package/dist/src/index.js.map +1 -1
  31. package/dist/src/lex.d.ts +8 -28
  32. package/dist/src/lex.d.ts.map +1 -1
  33. package/dist/src/lex.js +88 -146
  34. package/dist/src/lex.js.map +1 -1
  35. package/dist/src/parse.d.ts +4 -17
  36. package/dist/src/parse.d.ts.map +1 -1
  37. package/dist/src/parse.js +402 -1040
  38. package/dist/src/parse.js.map +1 -1
  39. package/dist/src/std-bundle.d.ts.map +1 -1
  40. package/dist/src/std-bundle.js +12 -20
  41. package/dist/src/std-bundle.js.map +1 -1
  42. package/dist/src/std-library.d.ts +1 -1
  43. package/dist/src/std-library.d.ts.map +1 -1
  44. package/dist/src/std-library.js +1 -5
  45. package/dist/src/std-library.js.map +1 -1
  46. package/dist/src/tunables.d.ts +7 -0
  47. package/dist/src/tunables.d.ts.map +1 -0
  48. package/dist/src/tunables.js +26 -0
  49. package/dist/src/tunables.js.map +1 -0
  50. package/dist/src/version.d.ts +2 -4
  51. package/dist/src/version.d.ts.map +1 -1
  52. package/dist/src/version.js +2 -4
  53. package/dist/src/version.js.map +1 -1
  54. package/docs/README.md +179 -41
  55. package/docs/headers.md +44 -0
  56. package/examples/cost-table.json +99 -751
  57. package/examples/library.hsx +59 -0
  58. package/package.json +9 -17
  59. package/src/ast.ts +82 -441
  60. package/src/cli.ts +77 -362
  61. package/src/compile.ts +1535 -247
  62. package/src/cost.ts +28 -671
  63. package/src/format.ts +12 -256
  64. package/src/headers.ts +126 -0
  65. package/src/index.ts +7 -39
  66. package/src/lex.ts +99 -195
  67. package/src/parse.ts +403 -1209
  68. package/src/std-bundle.ts +12 -20
  69. package/src/std-library.ts +2 -7
  70. package/src/tunables.ts +31 -0
  71. package/src/version.ts +2 -5
  72. package/std/approvals.hsx +17 -0
  73. package/std/cards.hsx +63 -0
  74. package/std/collections.hsx +31 -0
  75. package/std/escrow.hsx +43 -0
  76. package/std/financing.hsx +372 -0
  77. package/std/insurance.hsx +59 -0
  78. package/std/lending.hsx +115 -0
  79. package/std/marketplace.hsx +24 -0
  80. package/std/money.hsx +195 -0
  81. package/std/savings.hsx +44 -0
  82. package/std/travel.hsx +98 -0
  83. package/std/wallet.hsx +41 -0
  84. package/dist/src/diagnostics.d.ts +0 -13
  85. package/dist/src/diagnostics.d.ts.map +0 -1
  86. package/dist/src/diagnostics.js +0 -587
  87. package/dist/src/diagnostics.js.map +0 -1
  88. package/dist/src/emit.d.ts +0 -51
  89. package/dist/src/emit.d.ts.map +0 -1
  90. package/dist/src/emit.js +0 -192
  91. package/dist/src/emit.js.map +0 -1
  92. package/dist/src/entry-overrides.d.ts +0 -58
  93. package/dist/src/entry-overrides.d.ts.map +0 -1
  94. package/dist/src/entry-overrides.js +0 -284
  95. package/dist/src/entry-overrides.js.map +0 -1
  96. package/dist/src/ir.d.ts +0 -73
  97. package/dist/src/ir.d.ts.map +0 -1
  98. package/dist/src/ir.js +0 -15
  99. package/dist/src/ir.js.map +0 -1
  100. package/dist/src/limits.d.ts +0 -23
  101. package/dist/src/limits.d.ts.map +0 -1
  102. package/dist/src/limits.js +0 -23
  103. package/dist/src/limits.js.map +0 -1
  104. package/dist/src/lsp/server.d.ts +0 -32
  105. package/dist/src/lsp/server.d.ts.map +0 -1
  106. package/dist/src/lsp/server.js +0 -391
  107. package/dist/src/lsp/server.js.map +0 -1
  108. package/dist/src/modules.d.ts +0 -38
  109. package/dist/src/modules.d.ts.map +0 -1
  110. package/dist/src/modules.js +0 -372
  111. package/dist/src/modules.js.map +0 -1
  112. package/dist/src/typecheck.d.ts +0 -8
  113. package/dist/src/typecheck.d.ts.map +0 -1
  114. package/dist/src/typecheck.js +0 -4131
  115. package/dist/src/typecheck.js.map +0 -1
  116. package/docs/guide/01-first-program.md +0 -24
  117. package/docs/guide/02-money.md +0 -25
  118. package/docs/guide/03-instruments.md +0 -93
  119. package/docs/guide/04-lifecycles.md +0 -40
  120. package/docs/guide/05-fees-and-splits.md +0 -45
  121. package/docs/guide/06-schedules.md +0 -26
  122. package/docs/guide/07-composition.md +0 -52
  123. package/docs/guide/08-writing-a-module.md +0 -85
  124. package/docs/guide/09-cost.md +0 -24
  125. package/docs/guide/10-diagnostics.md +0 -27
  126. package/docs/llms-full.txt +0 -3684
  127. package/docs/llms.txt +0 -44
  128. package/docs/piece-plans.md +0 -120
  129. package/docs/playground.md +0 -55
  130. package/docs/reference/cli.md +0 -36
  131. package/docs/reference/diagnostics.md +0 -901
  132. package/docs/reference/grammar.md +0 -38
  133. package/docs/reference/std/advance.md +0 -112
  134. package/docs/reference/std/cancellable_booking.md +0 -159
  135. package/docs/reference/std/captured_payment.md +0 -115
  136. package/docs/reference/std/conditional_disbursement.md +0 -101
  137. package/docs/reference/std/credit_facility.md +0 -108
  138. package/docs/reference/std/held_payment.md +0 -173
  139. package/docs/reference/std/instant_transfer.md +0 -97
  140. package/docs/reference/std/metered.md +0 -89
  141. package/docs/reference/std/pooled_split.md +0 -95
  142. package/docs/reference/std/premium_forward.md +0 -119
  143. package/docs/reference/std/reconciled_payout.md +0 -103
  144. package/docs/reference/std/rotating_pool.md +0 -130
  145. package/docs/reference/std/scheduled.md +0 -136
  146. package/docs/reference/std/security_deposit.md +0 -138
  147. package/docs/reference/std/settlement_batch.md +0 -123
  148. package/docs/reference/std/swap.md +0 -133
  149. package/docs/reference/std/threshold_pool.md +0 -138
  150. package/docs/reference/std/weighted_distribution.md +0 -135
  151. package/docs/reference/types.md +0 -61
  152. package/docs/reference/udl-output.md +0 -13
  153. package/examples/01-first-program/README.md +0 -45
  154. package/examples/01-first-program/tip-jar.hsx +0 -17
  155. package/examples/02-imports-and-modules/README.md +0 -96
  156. package/examples/02-imports-and-modules/photo-booth.hsx +0 -37
  157. package/examples/03-diagnostics/README.md +0 -68
  158. package/examples/03-diagnostics/corner-shop-fixed.hsx +0 -22
  159. package/examples/03-diagnostics/corner-shop.hsx +0 -19
  160. package/examples/04-complete-product/README.md +0 -84
  161. package/examples/04-complete-product/study-hall.hsx +0 -67
  162. package/examples/05-authored-instrument/README.md +0 -5
  163. package/examples/05-authored-instrument/payment.hsx +0 -37
  164. package/examples/05-watch-club/README.md +0 -6
  165. package/examples/05-watch-club/watch-club.hsx +0 -123
  166. package/examples/README.md +0 -48
  167. package/examples/advance/README.md +0 -3
  168. package/examples/advance/advance.hsx +0 -13
  169. package/examples/advance/advance.udl +0 -367
  170. package/examples/cancellable_booking/README.md +0 -3
  171. package/examples/cancellable_booking/cancellable_booking.hsx +0 -14
  172. package/examples/cancellable_booking/cancellable_booking.udl +0 -413
  173. package/examples/captured_payment/README.md +0 -3
  174. package/examples/captured_payment/captured_payment.hsx +0 -17
  175. package/examples/captured_payment/captured_payment.udl +0 -686
  176. package/examples/conditional_disbursement/README.md +0 -3
  177. package/examples/conditional_disbursement/conditional_disbursement.hsx +0 -15
  178. package/examples/conditional_disbursement/conditional_disbursement.udl +0 -418
  179. package/examples/credit_facility/README.md +0 -3
  180. package/examples/credit_facility/credit_facility.hsx +0 -24
  181. package/examples/credit_facility/credit_facility.udl +0 -1396
  182. package/examples/held_payment/README.md +0 -3
  183. package/examples/held_payment/held_payment.hsx +0 -13
  184. package/examples/held_payment/held_payment.udl +0 -569
  185. package/examples/instant_transfer/README.md +0 -3
  186. package/examples/instant_transfer/instant_transfer.hsx +0 -16
  187. package/examples/instant_transfer/instant_transfer.udl +0 -376
  188. package/examples/metered/README.md +0 -3
  189. package/examples/metered/metered.hsx +0 -13
  190. package/examples/metered/metered.udl +0 -253
  191. package/examples/pooled_split/README.md +0 -3
  192. package/examples/pooled_split/pooled_split.hsx +0 -15
  193. package/examples/pooled_split/pooled_split.udl +0 -432
  194. package/examples/premium_forward/README.md +0 -3
  195. package/examples/premium_forward/premium_forward.hsx +0 -19
  196. package/examples/premium_forward/premium_forward.udl +0 -604
  197. package/examples/reconciled_payout/README.md +0 -3
  198. package/examples/reconciled_payout/reconciled_payout.hsx +0 -13
  199. package/examples/reconciled_payout/reconciled_payout.udl +0 -312
  200. package/examples/rotating_pool/README.md +0 -3
  201. package/examples/rotating_pool/rotating_pool.hsx +0 -18
  202. package/examples/rotating_pool/rotating_pool.udl +0 -4370
  203. package/examples/scheduled/README.md +0 -3
  204. package/examples/scheduled/scheduled.hsx +0 -12
  205. package/examples/scheduled/scheduled.udl +0 -355
  206. package/examples/security_deposit/README.md +0 -3
  207. package/examples/security_deposit/security_deposit.hsx +0 -21
  208. package/examples/security_deposit/security_deposit.udl +0 -320
  209. package/examples/settlement_batch/README.md +0 -3
  210. package/examples/settlement_batch/settlement_batch.hsx +0 -19
  211. package/examples/settlement_batch/settlement_batch.udl +0 -757
  212. package/examples/swap/README.md +0 -3
  213. package/examples/swap/swap.hsx +0 -19
  214. package/examples/swap/swap.udl +0 -892
  215. package/examples/threshold_pool/README.md +0 -3
  216. package/examples/threshold_pool/threshold_pool.hsx +0 -16
  217. package/examples/threshold_pool/threshold_pool.udl +0 -680
  218. package/examples/weighted_distribution/README.md +0 -3
  219. package/examples/weighted_distribution/weighted_distribution.hsx +0 -20
  220. package/examples/weighted_distribution/weighted_distribution.udl +0 -348
  221. package/skills/hsx/SKILL.md +0 -449
  222. package/src/diagnostics.ts +0 -620
  223. package/src/emit.ts +0 -282
  224. package/src/entry-overrides.ts +0 -428
  225. package/src/ir.ts +0 -113
  226. package/src/limits.ts +0 -22
  227. package/src/lsp/server.ts +0 -460
  228. package/src/modules.ts +0 -452
  229. package/src/typecheck.ts +0 -5381
  230. package/std/SEMANTICS.md +0 -44
  231. package/std/money_flows/advance.hsx +0 -235
  232. package/std/money_flows/cancellable_booking.hsx +0 -402
  233. package/std/money_flows/captured_payment.hsx +0 -220
  234. package/std/money_flows/conditional_disbursement.hsx +0 -226
  235. package/std/money_flows/credit_facility.hsx +0 -214
  236. package/std/money_flows/held_payment.hsx +0 -812
  237. package/std/money_flows/index.hsx +0 -4
  238. package/std/money_flows/instant_transfer.hsx +0 -231
  239. package/std/money_flows/metered.hsx +0 -104
  240. package/std/money_flows/pooled_split.hsx +0 -109
  241. package/std/money_flows/premium_forward.hsx +0 -307
  242. package/std/money_flows/reconciled_payout.hsx +0 -119
  243. package/std/money_flows/rotating_pool.hsx +0 -305
  244. package/std/money_flows/scheduled.hsx +0 -599
  245. package/std/money_flows/security_deposit.hsx +0 -248
  246. package/std/money_flows/settlement_batch.hsx +0 -173
  247. package/std/money_flows/swap.hsx +0 -1192
  248. package/std/money_flows/threshold_pool.hsx +0 -307
  249. package/std/money_flows/weighted_distribution.hsx +0 -237
@@ -1,4 +0,0 @@
1
- module std.money_flows
2
-
3
- // Import named exports from "std/money_flows".
4
- // SEMANTICS.md groups modules by lifecycle and explains their money effects.
@@ -1,231 +0,0 @@
1
- module std.money_flows.instant_transfer
2
-
3
- // Direct, single-step push transfer from payer to payee with optional tiered or percentage fees.
4
- //
5
- // ### Purpose
6
- // `instant_transfer` debits a payer and credits a payee immediately in a single execution step.
7
- // It fits peer-to-peer transfers, direct disbursements, instant payouts, wallet reloads, and simple point-of-sale payments.
8
- //
9
- // ### Selection guidance
10
- // - vs `captured_payment`: `instant_transfer` moves money in one atomic step without prior authorization holds,
11
- // incremental captures, voids, or post-settlement correction ports. Use `captured_payment` when authorization must precede capture.
12
- // - vs `conditional_disbursement`: `instant_transfer` executes immediately without external decision evidence
13
- // or cumulative cap tracking. Use `conditional_disbursement` when payments require evidence submission or aggregate milestone caps.
14
- // - vs `held_payment`: `instant_transfer` never holds funds in an intermediary escrow account.
15
- //
16
- // ### Parameters
17
- // - `payer`: Funding party whose account is debited.
18
- // - `payee`: Recipient party whose account is credited.
19
- // - `amount`: Transfer amount in minor units of currency `C`.
20
- // - `fees`: Optional fee configuration block specifying flat fees, percentage fees, or bracketed fee tiers for payer and/or payee.
21
- // - `derived_amount`: Optional block declaring derived platform fees calculated from the transfer amount.
22
- //
23
- // ### Decision ports
24
- // None. Transfers execute immediately upon invocation.
25
- //
26
- // ### Example
27
- // ```hsx
28
- // program instant_transfer_example "Instant transfer example"
29
- // import { instant_transfer } from "std/money_flows"
30
- // party customer: person
31
- // party merchant: business
32
- // settlement transfer = instant_transfer {
33
- // payer: customer
34
- // payee: merchant
35
- // amount: transferAmount: money(SAR)
36
- // fees {
37
- // customer: checkoutFee: money(SAR)
38
- // merchant {
39
- // tier { from: 0, to: 10000, fee: 1% }
40
- // tier { from: 10000, fee: highValueFee: money(SAR) }
41
- // }
42
- // }
43
- // }
44
- // ```
45
- export instrument instant_transfer<C>(payer: party, payee: party, amount: money<C>, fees: optional<block>, derived_amount: optional<block>) {
46
- let(payer_fee): get(fees, payer);
47
- let(payee_fee): get(fees, payee);
48
- let(payer_fee_kind): kind(payer_fee);
49
- let(payee_fee_kind): kind(payee_fee);
50
- let(payer_account_field): camel(concat(payer, "_account_id"));
51
- let(payee_account_field): camel(concat(payee, "_account_id"));
52
- let(net_bps): sub(10000, payee_fee);
53
- let(net_percent): percent_text(net_bps);
54
- let(payee_bps): basis_points(payee_fee);
55
- let(payee_tiers): values(payee_fee);
56
- let(first_tier): at(payee_tiers, 1);
57
- let(second_tier): at(payee_tiers, 2);
58
- let(second_tier_fee): get(second_tier, "fee");
59
- when(derived_amount) {
60
- let(derived_field): get(derived_amount, "field");
61
- let(derived_source): get(derived_amount, "source");
62
- let(derived_rule): get(derived_amount, "rule");
63
- let(derived_bearer): get(derived_amount, "bearer");
64
- }
65
- agent_description: concat("Reach for instant transfer when the ", words(payer), " pays the ", words(payee), " straight through with no custody step. The money leaves and lands in one call. Pick deposit when the money must be held first, and captured payment when the payee takes it in slices.");
66
- summary: concat("Instant payment from ", words(payer), " to ", words(payee));
67
- title: sentence(instrument);
68
- description: concat("Instant transfer: the ", words(payer), " pays ", amount, " straight through to the ", words(payee), ", no custody");
69
- fields {
70
- when_not(fees) {
71
- when(derived_amount) {
72
- let(fee_collection_key): "x-hyperscale-fee-collection-port";
73
- let(reference_filter_key): "x-hyperscale-reference-filter";
74
- platformAccountId {
75
- type: account<C>;
76
- description: "The product fee collection account";
77
- [fee_collection_key]: true;
78
- [reference_filter_key]: { column: role; values: [customer_balance, product_revenue]; };
79
- }
80
- }
81
- }
82
- when(fees) {
83
- let(fee_collection_key): "x-hyperscale-fee-collection-port";
84
- let(reference_filter_key): "x-hyperscale-reference-filter";
85
- platformAccountId {
86
- type: account<C>;
87
- description: "The product fee collection account";
88
- [fee_collection_key]: true;
89
- [reference_filter_key]: { column: role; values: [customer_balance, product_revenue]; };
90
- }
91
- }
92
- when_not(fees) {
93
- amount { type: money<C>; description: "The amount in {C} minor units, paid through whole"; }
94
- }
95
- when(fees) {
96
- amount { type: money<C>; description: "The gross amount in {C} minor units; the piece fields below partition it exactly"; }
97
- when_eq(payee_fee_kind, percent) {
98
- piece1Amount { type: money<C>; description: concat(net_percent, " of ", amount, " (carries the integer-division remainder): released to the ", payee, ". Computed as floor(", amount, " * ", net_bps, " / 10000) in ", C, " minor units"); pattern: "^[1-9][0-9]{0,17}$"; }
99
- piece2Amount { type: money<C>; description: concat(payee_fee, " of ", amount, ": released to the platform. Computed as floor(", amount, " * ", payee_bps, " / 10000) in ", C, " minor units"); pattern: "^[1-9][0-9]{0,17}$"; optional: true; }
100
- }
101
- when_eq(payee_fee_kind, block) {
102
- netOfCarvedFeeAmount { type: money<C>; description: "The remainder of {amount} after the carved platform fee"; pattern: "^[1-9][0-9]{0,17}$"; }
103
- carvedFeeAmount { type: money<C>; description: "The carved platform fee selected from {amount}"; pattern: "^[1-9][0-9]{0,17}$"; optional: true; }
104
- }
105
- when_eq(payer_fee_kind, percent) {
106
- serviceFeeAmount { type: money<C>; description: "Machine-owned {payer_fee} service fee computed from {amount}; charged on top"; pattern: "^[1-9][0-9]{0,17}$"; optional: true; }
107
- }
108
- when_eq(payer_fee_kind, binding) {
109
- [payer_fee] { type: payer_fee; description: "Immutable exact {payer} fee in {C} minor units"; pattern: "^[1-9][0-9]{0,17}$"; }
110
- }
111
- when_eq(payee_fee_kind, block) {
112
- [second_tier_fee] { type: second_tier_fee; description: "Immutable exact {payee} fee in {C} minor units"; pattern: "^[1-9][0-9]{0,17}$"; }
113
- }
114
- }
115
- when(derived_amount) {
116
- [derived_field] { type: money<C>; description: "Machine-computed {derived_rule} of {derived_source}; callers never supply it"; pattern: "^[1-9][0-9]{0,17}$"; optional: true; }
117
- }
118
- }
119
- lifecycle {
120
- when_not(fees) { states created paid; }
121
- when(fees) { states created paying_1 paying_2 paid; }
122
- initial created;
123
- when_not(fees) { on pay_piece_1: created -> paid; }
124
- when(fees) {
125
- on pay_piece_1: created -> paying_1;
126
- on pay_piece_2: paying_1 -> paying_2;
127
- on collect_service_fee: paying_2 -> paid;
128
- }
129
- }
130
- parties {
131
- payer: payer;
132
- beneficiary: payee;
133
- }
134
- when(derived_amount) {
135
- computes derived: { field: derived_field; source_field: derived_source; rounding: floor; rule: { kind: percentage_of; bps: derived_rule; }; };
136
- }
137
- when(fees) {
138
- when_eq(payer_fee_kind, percent) {
139
- computes fees: { amount_field: serviceFeeAmount; base_field: amount; bearer_field: payer_account_field; position: on_top; rule: { kind: bps; bps: payer_fee; }; };
140
- }
141
- when_eq(payer_fee_kind, binding) {
142
- computes fees: { amount_field: camel(payer_fee); base_field: amount; bearer_field: payer_account_field; position: on_top; rule: { kind: exact; field: camel(payer_fee); currency_field: currency; }; };
143
- }
144
- when_eq(payee_fee_kind, percent) {
145
- computes fees: { amount_field: piece2Amount; base_field: amount; bearer_field: payee_account_field; position: carved; rule: { kind: bps; bps: payee_fee; }; };
146
- partitions: { total_field: amount; piece_fields: [piece1Amount, piece2Amount]; };
147
- }
148
- when_eq(payee_fee_kind, block) {
149
- computes fees: { amount_field: carvedFeeAmount; base_field: amount; bearer_field: payee_account_field; position: carved; rule: { kind: tiered; tiers: [{ from_inclusive: concat("", get(first_tier, "from")); to_exclusive: concat("", get(first_tier, "to")); rule: { kind: bps; bps: get(first_tier, "fee"); }; }, { from_inclusive: concat("", get(second_tier, "from")); rule: { kind: exact; field: camel(second_tier_fee); currency_field: currency; }; }]; }; };
150
- partitions: { total_field: amount; piece_fields: [netOfCarvedFeeAmount, carvedFeeAmount]; };
151
- }
152
- }
153
- action create {
154
- let(metadata_instrument): "metadata.instrumentId";
155
- let(metadata_instance): "metadata.instrumentInstanceId";
156
- let(metadata_phase): "metadata.phase";
157
- agent_description: "Open the payment row before anything moves. The caller supplies the {amount}, the currency, and both party accounts. Fee and derived pieces are computed from the {amount}, callers never supply them. Call pay piece 1 to move the money.";
158
- summary: concat("Create a ", words(instrument), " payment");
159
- when(derived_amount) {
160
- moves: [{
161
- key: "derived_amount";
162
- operation: internal_transfer.create;
163
- bind: {
164
- amount: { from: instance; path: concat("fields.", derived_field); };
165
- currency: { from: instance; path: fields.currency; };
166
- destinationAccountId: { from: instance; path: fields.platformAccountId; };
167
- [metadata_instrument]: { from: const; value: instrument; };
168
- [metadata_instance]: { from: instance; path: instrumentInstanceId; };
169
- [metadata_phase]: { from: const; value: create; };
170
- productId: { from: instance; path: productId; };
171
- sourceAccountId: { from: instance; path: concat("fields.", camel(derived_bearer), "AccountId"); };
172
- };
173
- capture: { createDerivedAmountTransferId: transferId; };
174
- }];
175
- }
176
- steps: [];
177
- }
178
- action pay_piece_1 {
179
- let(metadata_instrument): "metadata.instrumentId";
180
- let(metadata_instance): "metadata.instrumentInstanceId";
181
- let(metadata_phase): "metadata.phase";
182
- agent_description: concat("Move the first piece from the ", words(payer), " to the ", words(payee), ". The payment must be created. This moves the whole amount unless the product carves a payee fee, in which case it moves the net share and pay piece 2 takes the fee.");
183
- summary: "Pay piece 1 of the amount through";
184
- when_not(fees) {
185
- moves: [{ key: transfer; amount: amount; from: payer; to: beneficiary; }];
186
- }
187
- when(fees) {
188
- when_eq(payee_fee_kind, none) {
189
- moves: [{ key: transfer; amount: amount; from: payer; to: beneficiary; }];
190
- }
191
- when_eq(payee_fee_kind, percent) {
192
- moves: [{ key: transfer; operation: internal_transfer.create; bind: { amount: { from: instance; path: fields.piece1Amount; }; currency: { from: instance; path: fields.currency; }; destinationAccountId: { from: instance; path: concat("fields.", payee_account_field); }; [metadata_instrument]: { from: const; value: instrument; }; [metadata_instance]: { from: instance; path: instrumentInstanceId; }; [metadata_phase]: { from: const; value: pay_piece_1; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: concat("fields.", payer_account_field); }; }; capture: { payPiece1TransferTransferId: transferId; }; }];
193
- }
194
- when_eq(payee_fee_kind, block) {
195
- moves: [{ key: transfer; operation: internal_transfer.create; bind: { amount: { from: instance; path: fields.netOfCarvedFeeAmount; }; currency: { from: instance; path: fields.currency; }; destinationAccountId: { from: instance; path: concat("fields.", payee_account_field); }; [metadata_instrument]: { from: const; value: instrument; }; [metadata_instance]: { from: instance; path: instrumentInstanceId; }; [metadata_phase]: { from: const; value: pay_piece_1; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: concat("fields.", payer_account_field); }; }; capture: { payPiece1TransferTransferId: transferId; }; }];
196
- }
197
- }
198
- sandbox_failure_point: funding;
199
- steps: [];
200
- }
201
- when(fees) {
202
- action pay_piece_2 {
203
- let(metadata_instrument): "metadata.instrumentId";
204
- let(metadata_instance): "metadata.instrumentInstanceId";
205
- let(metadata_phase): "metadata.phase";
206
- agent_description: concat("Move the carved platform fee out of the amount. Call this after pay piece 1. When the product carves no ", words(payee), " fee this only steps the payment forward and moves nothing.");
207
- summary: "Pay piece 2 of the amount through";
208
- when_eq(payee_fee_kind, percent) {
209
- moves: [{ key: transfer; operation: internal_transfer.create; bind: { amount: { from: instance; path: fields.piece2Amount; }; currency: { from: instance; path: fields.currency; }; destinationAccountId: { from: instance; path: fields.platformAccountId; }; [metadata_instrument]: { from: const; value: instrument; }; [metadata_instance]: { from: instance; path: instrumentInstanceId; }; [metadata_phase]: { from: const; value: pay_piece_2; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: concat("fields.", payer_account_field); }; }; capture: { payPiece2TransferTransferId: transferId; }; }];
210
- }
211
- when_eq(payee_fee_kind, block) {
212
- moves: [{ key: transfer; operation: internal_transfer.create; bind: { amount: { from: instance; path: fields.carvedFeeAmount; }; currency: { from: instance; path: fields.currency; }; destinationAccountId: { from: instance; path: fields.platformAccountId; }; [metadata_instrument]: { from: const; value: instrument; }; [metadata_instance]: { from: instance; path: instrumentInstanceId; }; [metadata_phase]: { from: const; value: pay_piece_2; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: concat("fields.", payer_account_field); }; }; capture: { payPiece2TransferTransferId: transferId; }; }];
213
- }
214
- steps: [];
215
- }
216
- action collect_service_fee {
217
- let(metadata_instrument): "metadata.instrumentId";
218
- let(metadata_instance): "metadata.instrumentInstanceId";
219
- let(metadata_phase): "metadata.phase";
220
- agent_description: concat("Finish the payment and collect the service fee the ", words(payer), " owes on top of the amount. Both pieces must already be paid. When the product charges no payer fee this only closes the payment out.");
221
- summary: "Collect the payer-side service fee";
222
- when_eq(payer_fee_kind, percent) {
223
- moves: [{ key: transfer; operation: internal_transfer.create; bind: { amount: { from: instance; path: fields.serviceFeeAmount; }; currency: { from: instance; path: fields.currency; }; destinationAccountId: { from: instance; path: fields.platformAccountId; }; [metadata_instrument]: { from: const; value: instrument; }; [metadata_instance]: { from: instance; path: instrumentInstanceId; }; [metadata_phase]: { from: const; value: collect_service_fee; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: concat("fields.", payer_account_field); }; }; capture: { collectServiceFeeTransferTransferId: transferId; }; }];
224
- }
225
- when_eq(payer_fee_kind, binding) {
226
- moves: [{ key: transfer; operation: internal_transfer.create; bind: { amount: { from: instance; path: concat("fields.", payer_fee); }; currency: { from: instance; path: fields.currency; }; destinationAccountId: { from: instance; path: fields.platformAccountId; }; [metadata_instrument]: { from: const; value: instrument; }; [metadata_instance]: { from: instance; path: instrumentInstanceId; }; [metadata_phase]: { from: const; value: collect_service_fee; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: concat("fields.", payer_account_field); }; }; capture: { collectServiceFeeTransferTransferId: transferId; }; }];
227
- }
228
- steps: [];
229
- }
230
- }
231
- }
@@ -1,104 +0,0 @@
1
- module std.money_flows.metered
2
-
3
- // Usage-based billing against a committed rate card over an open period until a stored close date.
4
- //
5
- // ### Purpose
6
- // `metered` bills customers for variable resource consumption such as API requests, compute hours,
7
- // storage gigabytes, or utility consumption. The rate card is fixed when the period opens, and individual
8
- // usage events are charged incrementally until the billing period closes on `close_by`.
9
- //
10
- // ### Selection guidance
11
- // - vs `scheduled`: `metered` charges variable amounts per event calculated from consumed units and committed
12
- // rate card prices. `scheduled` executes transfers on fixed calendar recurrence intervals (`every`, `first_due`)
13
- // with predetermined amounts or installments.
14
- //
15
- // ### Parameters
16
- // - `payer`: The customer being billed for usage.
17
- // - `payee`: The service provider receiving usage revenue.
18
- // - `close_by`: Date when the metering period closes, preventing further usage charges.
19
- // - `rates`: Block mapping rate metric names to their per-unit money prices committed at period open.
20
- //
21
- // ### Decision ports
22
- // None. Charges are driven by caller billing actions and period closure.
23
- //
24
- // ### Example
25
- // ```hsx
26
- // program metered_example "Metered example"
27
- // import { metered } from "std/money_flows"
28
- // party customer: business
29
- // party provider: business
30
- // settlement usage = metered {
31
- // payer: customer
32
- // payee: provider
33
- // close_by: periodEnd
34
- // rates {
35
- // api_call: callRate: money(SAR)
36
- // storage_gib: storageRate: money(SAR)
37
- // }
38
- // }
39
- // ```
40
- export instrument metered(
41
- payer: party,
42
- payee: party,
43
- close_by: date,
44
- rates: unknown,
45
- ) {
46
- let(instrument_words): words(instrument);
47
- title: sentence(instrument);
48
- agent_description: "Reach for metered when the {payer} is billed per unit at a rate card fixed when the period opens, and the period closes on {close_by}. Every charge is its own transfer. Pick scheduled for a flat repeating amount and instant transfer for a one-off payment.";
49
- summary: "Metered charges from {payer} on a committed rate card";
50
- description: "Metered usage: the {payer} is charged per unit at the committed rate card until the period closes on its stored end date";
51
-
52
- fields {
53
- close_by: {
54
- type: date;
55
- description: "End of this metering period; the close makes further charges unreachable";
56
- }
57
- for item in keys(rates) {
58
- let(amount_binding): get(rates, item);
59
- let(rate_words): words(item);
60
- [amount_binding]: {
61
- type: amount_binding;
62
- description: "Per-unit price of {rate_words} in SAR minor units, committed at period open";
63
- }
64
- }
65
- }
66
-
67
- parties {
68
- payer: payer;
69
- beneficiary: payee;
70
- }
71
-
72
- lifecycle {
73
- states open closed;
74
- initial open;
75
- for item in keys(rates) {
76
- on charge_[item]: open -> open;
77
- }
78
- on close_period: open -> closed;
79
- }
80
-
81
- action create {
82
- agent_description: "Open the metering period with its committed rate card. The caller supplies the {close_by} anchor and every per-unit price. The prices are fixed here and no later charge can change them. No money moves.";
83
- summary: "Open a {instrument_words} period with its committed rate card";
84
- steps: [];
85
- }
86
-
87
- for item in keys(rates) {
88
- action charge_[item] {
89
- let(amount_binding): get(rates, item);
90
- let(amount_field): concat(amount_binding);
91
- let(rate_words): words(item);
92
- agent_description: "Charge one metered {rate_words} at the committed price. The period must still be open, so charge before {close_by}. The transfer posts from the {payer} to the {payee} at once and there is no reversal action.";
93
- summary: "Charge one metered {rate_words}; the emission is the transfer itself";
94
- moves: [{ amount: amount_field; from: payer; to: beneficiary; key: transfer; }];
95
- steps: [];
96
- }
97
- }
98
-
99
- action close_period {
100
- summary: "Close the metering period; no further usage can be charged";
101
- due: { field: close_by; };
102
- steps: [];
103
- }
104
- }
@@ -1,109 +0,0 @@
1
- module std.money_flows.pooled_split
2
-
3
- // Fixed multi-recipient revenue or period pool funded share-by-share and released automatically on a due date.
4
- //
5
- // ### Purpose
6
- // `pooled_split` pools a period total from one payer and splits it across a fixed roster of recipients
7
- // according to predetermined percentage shares (with integer division remainder assigned to `remainder_to`).
8
- // Callers fund the pool share-by-share, and payouts disburse automatically to each recipient on `payout_due`.
9
- //
10
- // ### Selection guidance
11
- // - vs `weighted_distribution`: `pooled_split` hardcodes a fixed set of recipients and static percentage shares
12
- // directly in the contract. `weighted_distribution` handles dynamic recipient counts where arbitrary weights
13
- // are recorded as child records and frozen via a snapshot port before distribution.
14
- // - vs `settlement_batch`: `settlement_batch` aggregates multiple payments from many sources into one net payout.
15
- // `pooled_split` takes one period total from one payer and partitions it out to multiple recipients.
16
- //
17
- // ### Parameters
18
- // - `payer`: The funding party providing the pooled amount.
19
- // - `amount`: Total pooled amount in minor units of currency `C`.
20
- // - `payout_due`: Stored payout date when the pool distributes to all recipients.
21
- // - `split`: Block declaring each recipient's percentage share and `remainder_to` recipient for rounding remainders.
22
- //
23
- // ### Decision ports
24
- // None. Payout distribution triggers automatically from the stored `payout_due` date.
25
- //
26
- // ### Example
27
- // ```hsx
28
- // program pooled_split_example "Pooled split example"
29
- // import { pooled_split } from "std/money_flows"
30
- // party payer: business
31
- // party first_recipient: business
32
- // party second_recipient: business
33
- // settlement pool = pooled_split {
34
- // payer: payer
35
- // amount: poolAmount: money(SAR)
36
- // payout_due: payoutDueAt
37
- // split {
38
- // first_recipient: 60%
39
- // second_recipient: 40%
40
- // remainder_to: first_recipient
41
- // }
42
- // }
43
- // ```
44
- export instrument pooled_split<C>(payer: party, amount: money<C>, payout_due: date, split: block) {
45
- let(recipients): keys_except(split, "remainder_to");
46
- let(recipient_count): len(recipients);
47
- let(remainder_recipient): get(split, "remainder_to");
48
- fields {
49
- currency { type: text; description: "ISO 4217 currency code"; minLength: 3; maxLength: 3; pattern: "^[A-Z]{3}$"; }
50
- [payer]AccountId { type: account<C>; description: concat("The ", words(payer), " account"); "x-hyperscale-reference-filter": { column: role; values: [customer_balance]; }; }
51
- amount { type: money<C>; description: concat("The pooled period total in {C} minor units; the share fields below partition it exactly"); }
52
- payout_due { type: date; description: "The period's payout date; the pool distributes from it"; }
53
- for index in recipient_count {
54
- let(recipient): at(recipients, index);
55
- let(share_bps): get(split, recipient);
56
- [recipient]AccountId { type: account<C>; description: concat("The ", words(recipient), " account"); "x-hyperscale-reference-filter": { column: role; values: [customer_balance]; }; }
57
- [recipient]ShareAmount {
58
- type: money<C>;
59
- description: concat(div_floor(basis_points(share_bps), 100), "% of ", amount, if_eq(recipient, remainder_recipient, " (carries the integer-division remainder): ", ": "), "the ", words(recipient), "'s share. Computed as floor(", amount, " * ", basis_points(share_bps), " / 10000) in {C} minor units");
60
- }
61
- }
62
- }
63
- lifecycle {
64
- states created;
65
- initial created;
66
- for index in sub(recipient_count, 1) { states pooling_[index]; }
67
- states pooled;
68
- for index in sub(recipient_count, 1) { states distributing_[index]; }
69
- states distributed;
70
- for index in recipient_count {
71
- let(fund_from): if_eq(index, 1, "created", concat("pooling_", sub(index, 1)));
72
- let(fund_to): if_eq(index, recipient_count, "pooled", concat("pooling_", index));
73
- on fund_share_[index]: [fund_from] -> [fund_to];
74
- let(distribute_from): if_eq(index, 1, "pooled", concat("distributing_", sub(index, 1)));
75
- let(distribute_to): if_eq(index, recipient_count, "distributed", concat("distributing_", index));
76
- on distribute_share_[index]: [distribute_from] -> [distribute_to];
77
- }
78
- }
79
- parties { beneficiary: concat(camel(at(recipients, 1)), "AccountId"); payer: concat(camel(payer), "AccountId"); }
80
- required: concat_lists([concat(camel(payer), "AccountId")], suffix_each(recipients, "AccountId"), [concat("", amount), concat("", payout_due)], suffix_each(recipients, "ShareAmount"), ["currency"]);
81
- title: sentence(instrument);
82
- id_prefix: prefix(instrument);
83
- agent_description: concat("Reach for ", words(instrument), " when the ", words(payer), " funds one period total that is then split across ", recipient_count, " fixed recipients by stored share amounts. Callers fund the pool one share at a time. Each recipient's payout fires from the stored payout date, not from a call.");
84
- summary: concat("Period pool from ", words(payer), " split ", recipient_count, " ways");
85
- description: concat("Pooled split: the ", words(payer), " pools the period's ", amount, " share by share; the pool distributes to every recipient in full on the stored payout date");
86
- partitions { totalField: amount; pieceFields: suffix_each(recipients, "ShareAmount"); }
87
- action create {
88
- agent_description: concat("Open a ", words(instrument), " period and provision the escrow account the pool sits in. This is the first call on the period, and funding a share refuses until it lands. It moves no money.");
89
- summary: concat("Open a ", words(instrument), " period");
90
- moves: [];
91
- steps: [{ operation: "account.escrow.provision"; bind: { currency: { from: instance; path: "fields.currency"; }; "owner.id": { from: instance; path: productId; }; "owner.type": { from: const; value: product; }; productId: { from: instance; path: productId; }; role: { from: const; value: product_escrow; }; }; capture: { escrowAccountId: accountId; }; }];
92
- }
93
- for index in recipient_count {
94
- let(recipient): at(recipients, index);
95
- let(share_field): concat(camel(recipient), "ShareAmount");
96
- action fund_share_[index] {
97
- agent_description: concat("Move share ", index, " of the period total, the ", words(recipient), "'s cut, from the ", words(payer), " into the pool escrow. Shares fund in order, so every earlier share must be in already. It debits the ", words(payer), " for real. The pool pays out from the stored payout date with no release call.");
98
- summary: concat("Pool share ", index, " of the period total");
99
- moves: [{ amount: share_field; from: payer; to: refs.escrowAccountId; key: transfer; }];
100
- steps: [];
101
- }
102
- action distribute_share_[index] {
103
- summary: concat("Distribute the ", words(recipient), "'s share of the pool");
104
- due { field: payout_due; }
105
- moves: [{ amount: share_field; from: refs.escrowAccountId; to: recipient; key: transfer; }];
106
- steps: [];
107
- }
108
- }
109
- }