@xyo-network/payment-plugin 3.0.19 → 3.0.20

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.
@@ -0,0 +1,6 @@
1
+ import type { Hash } from '@xylabs/hex';
2
+ import type { Payload } from '@xyo-network/payload-model';
3
+ import type { Coupon } from '@xyo-network/payment-payload-plugins';
4
+ export declare const areConditionsFulfilled: (coupon: Coupon, payloads: Payload[]) => Promise<boolean>;
5
+ export declare const findUnfulfilledConditions: (coupon: Coupon, payloads: Payload[]) => Promise<Hash[]>;
6
+ //# sourceMappingURL=findUnfulfilledConditions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"findUnfulfilledConditions.d.ts","sourceRoot":"","sources":["../../../../src/Discount/lib/findUnfulfilledConditions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAEvC,OAAO,KAAK,EAAE,OAAO,EAAY,MAAM,4BAA4B,CAAA;AACnE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sCAAsC,CAAA;AAUlE,eAAO,MAAM,sBAAsB,WAAkB,MAAM,YAAY,OAAO,EAAE,KAAG,OAAO,CAAC,OAAO,CAChC,CAAA;AASlE,eAAO,MAAM,yBAAyB,WAAkB,MAAM,YAAY,OAAO,EAAE,KAAG,OAAO,CAAC,IAAI,EAAE,CAwCnG,CAAA"}
@@ -1,2 +1,3 @@
1
1
  export * from './applyCoupons.ts';
2
+ export * from './findUnfulfilledConditions.ts';
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/Discount/lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/Discount/lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAA;AACjC,cAAc,gCAAgC,CAAA"}
@@ -24,7 +24,7 @@ import {
24
24
  asDivinerInstance
25
25
  } from "@xyo-network/diviner-model";
26
26
  import { creatableModule } from "@xyo-network/module-model";
27
- import { PayloadBuilder } from "@xyo-network/payload-builder";
27
+ import { PayloadBuilder as PayloadBuilder2 } from "@xyo-network/payload-builder";
28
28
  import {
29
29
  isCoupon,
30
30
  isCouponWithMeta,
@@ -83,6 +83,43 @@ var calculateSingularFixedPriceDiscount = (total, appraisals, coupons, stackable
83
83
  return discount > 0 ? discount : 0;
84
84
  };
85
85
 
86
+ // src/Discount/lib/findUnfulfilledConditions.ts
87
+ import { PayloadBuilder } from "@xyo-network/payload-builder";
88
+ import { isSchemaPayloadWithMeta } from "@xyo-network/schema-payload-plugin";
89
+ import { Ajv } from "ajv";
90
+ var ajv = new Ajv({ strict: false });
91
+ var schemaCache = /* @__PURE__ */ new Map();
92
+ var areConditionsFulfilled = async (coupon, payloads) => (await findUnfulfilledConditions(coupon, payloads)).length === 0;
93
+ var findUnfulfilledConditions = async (coupon, payloads) => {
94
+ const unfulfilledConditions = [];
95
+ if (!coupon.conditions || coupon.conditions.length === 0) return unfulfilledConditions;
96
+ const hashMap = await PayloadBuilder.toAllHashMap(payloads);
97
+ const conditions = coupon.conditions.map((hash) => hashMap[hash]).filter(isSchemaPayloadWithMeta);
98
+ if (conditions.length !== coupon.conditions.length) {
99
+ const missing = coupon.conditions.filter((hash) => !hashMap[hash]);
100
+ unfulfilledConditions.push(...missing);
101
+ return unfulfilledConditions;
102
+ }
103
+ for (const hash of coupon.conditions) {
104
+ let validator;
105
+ if (schemaCache.has(hash)) {
106
+ validator = schemaCache.get(hash);
107
+ } else {
108
+ const payload = hashMap[hash];
109
+ const definition = isSchemaPayloadWithMeta(payload) ? payload.definition : void 0;
110
+ if (definition) {
111
+ validator = ajv.compile(definition);
112
+ schemaCache.set(hash, validator);
113
+ } else {
114
+ unfulfilledConditions.push(hash);
115
+ continue;
116
+ }
117
+ }
118
+ if (!validator(payloads)) unfulfilledConditions.push(hash);
119
+ }
120
+ return unfulfilledConditions;
121
+ };
122
+
86
123
  // src/Discount/Diviner.ts
87
124
  var DEFAULT_BOUND_WITNESS_DIVINER_QUERY_PROPS = {
88
125
  limit: 1,
@@ -97,15 +134,15 @@ var PaymentDiscountDiviner = class extends AbstractDiviner {
97
134
  const sources = [];
98
135
  const terms = payloads.find(isEscrowTerms);
99
136
  if (!terms) return [{ ...NO_DISCOUNT, sources }];
100
- sources.push(await PayloadBuilder.hash(terms));
137
+ sources.push(await PayloadBuilder2.hash(terms));
101
138
  const termsAppraisals = terms?.appraisals;
102
139
  if (!termsAppraisals || termsAppraisals.length === 0) return [{ ...NO_DISCOUNT, sources }];
103
- const hashMap = await PayloadBuilder.toAllHashMap(payloads);
140
+ const hashMap = await PayloadBuilder2.toAllHashMap(payloads);
104
141
  const appraisals = this.getEscrowAppraisals(terms, hashMap);
105
142
  sources.push(...termsAppraisals);
106
143
  if (appraisals.length !== termsAppraisals.length) return [{ ...NO_DISCOUNT, sources }];
107
144
  const coupons = await this.getEscrowDiscounts(terms, hashMap);
108
- const couponHashes = await PayloadBuilder.hashes(coupons);
145
+ const couponHashes = await PayloadBuilder2.hashes(coupons);
109
146
  sources.push(...couponHashes);
110
147
  const validCoupons = await this.filterToSigned(coupons.filter(this.isCouponCurrent));
111
148
  if (validCoupons.length === 0) return [{ ...NO_DISCOUNT, sources }];
@@ -120,7 +157,7 @@ var PaymentDiscountDiviner = class extends AbstractDiviner {
120
157
  */
121
158
  async filterToSigned(coupons) {
122
159
  const signed = [];
123
- const dataHashMap = await PayloadBuilder.toDataHashMap(coupons);
160
+ const dataHashMap = await PayloadBuilder2.toDataHashMap(coupons);
124
161
  const boundWitnessDiviner = await this.getDiscountsBoundWitnessDiviner();
125
162
  const hashes = Object.keys(dataHashMap);
126
163
  const addresses = this.couponAuthorities;
@@ -176,8 +213,8 @@ var PaymentDiscountDiviner = class extends AbstractDiviner {
176
213
  discounts.push(...payloads.filter(isCouponWithMeta));
177
214
  }
178
215
  if (discounts.length !== hashes.length) {
179
- const termsHash = await PayloadBuilder.hash(terms);
180
- const foundHashes = await PayloadBuilder.hashes(discounts);
216
+ const termsHash = await PayloadBuilder2.hash(terms);
217
+ const foundHashes = await PayloadBuilder2.hashes(discounts);
181
218
  for (const hash of hashes) {
182
219
  if (!foundHashes.includes(hash)) console.warn(`Discount ${hash} not found for terms ${termsHash}`);
183
220
  }
@@ -196,7 +233,7 @@ PaymentDiscountDiviner = __decorateClass([
196
233
  ], PaymentDiscountDiviner);
197
234
 
198
235
  // src/Invoice/getInvoiceForEscrow.ts
199
- import { PayloadBuilder as PayloadBuilder2 } from "@xyo-network/payload-builder";
236
+ import { PayloadBuilder as PayloadBuilder3 } from "@xyo-network/payload-builder";
200
237
  import {
201
238
  isDiscount,
202
239
  isSubtotal,
@@ -223,14 +260,14 @@ var getInvoiceForEscrow = async (terms, dataHashMap, paymentTotalDiviner) => {
223
260
  };
224
261
  var getSources = async (terms, subtotal, total, discount) => {
225
262
  const sources = discount ? [terms, subtotal, total, discount] : [terms, subtotal, total];
226
- return await Promise.all(sources.map((p) => PayloadBuilder2.dataHash(p)));
263
+ return await Promise.all(sources.map((p) => PayloadBuilder3.dataHash(p)));
227
264
  };
228
265
 
229
266
  // src/Subtotal/Diviner.ts
230
267
  import { AbstractDiviner as AbstractDiviner2 } from "@xyo-network/diviner-abstract";
231
268
  import { isHashLeaseEstimate as isHashLeaseEstimate2 } from "@xyo-network/diviner-hash-lease";
232
269
  import { creatableModule as creatableModule2 } from "@xyo-network/module-model";
233
- import { PayloadBuilder as PayloadBuilder3 } from "@xyo-network/payload-builder";
270
+ import { PayloadBuilder as PayloadBuilder4 } from "@xyo-network/payload-builder";
234
271
  import {
235
272
  isEscrowTerms as isEscrowTerms2,
236
273
  PaymentSubtotalDivinerConfigSchema,
@@ -295,12 +332,12 @@ var PaymentSubtotalDiviner = class extends AbstractDiviner2 {
295
332
  if (!terms) return [];
296
333
  if (!termsValidators.every((validator) => validator(terms))) return [];
297
334
  const validTerms = terms;
298
- const hashMap = await PayloadBuilder3.toAllHashMap(payloads);
335
+ const hashMap = await PayloadBuilder4.toAllHashMap(payloads);
299
336
  const appraisals = validTerms.appraisals.map((appraisal) => hashMap[appraisal]).filter(isHashLeaseEstimate2);
300
337
  if (appraisals.length !== validTerms.appraisals.length) return [];
301
338
  if (!appraisalValidators.every((validator) => validator(appraisals))) return [];
302
339
  const amount = calculateSubtotal(appraisals);
303
- const sources = [await PayloadBuilder3.dataHash(validTerms), ...validTerms.appraisals];
340
+ const sources = [await PayloadBuilder4.dataHash(validTerms), ...validTerms.appraisals];
304
341
  return [{
305
342
  amount,
306
343
  currency,
@@ -376,6 +413,8 @@ export {
376
413
  PaymentSubtotalDiviner,
377
414
  PaymentTotalDiviner,
378
415
  applyCoupons,
416
+ areConditionsFulfilled,
417
+ findUnfulfilledConditions,
379
418
  getInvoiceForEscrow
380
419
  };
381
420
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/Discount/Diviner.ts","../../src/Discount/lib/applyCoupons.ts","../../src/Invoice/getInvoiceForEscrow.ts","../../src/Subtotal/Diviner.ts","../../src/Subtotal/lib/appraisalValidators.ts","../../src/Subtotal/lib/durationValidators.ts","../../src/Subtotal/lib/termsValidators.ts","../../src/Total/Diviner.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { Address, Hash } from '@xylabs/hex'\nimport { ArchivistInstance, asArchivistInstance } from '@xyo-network/archivist-model'\nimport { AbstractDiviner } from '@xyo-network/diviner-abstract'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport {\n HashLeaseEstimate,\n isHashLeaseEstimate,\n} from '@xyo-network/diviner-hash-lease'\nimport {\n asDivinerInstance, DivinerInstance, DivinerModuleEventData,\n} from '@xyo-network/diviner-model'\nimport { creatableModule } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload } from '@xyo-network/payload-model'\nimport {\n Coupon,\n Discount,\n EscrowTerms, isCoupon,\n isCouponWithMeta,\n isEscrowTerms, NO_DISCOUNT, PaymentDiscountDivinerConfigSchema, PaymentDiscountDivinerParams,\n} from '@xyo-network/payment-payload-plugins'\n\nimport { applyCoupons } from './lib/index.ts'\n\nconst DEFAULT_BOUND_WITNESS_DIVINER_QUERY_PROPS: Readonly<BoundWitnessDivinerQueryPayload> = {\n limit: 1,\n order: 'desc',\n schema: BoundWitnessDivinerQuerySchema,\n}\n\nexport type PaymentDiscountDivinerInputType = EscrowTerms | Coupon | HashLeaseEstimate | Payload\n\n@creatableModule()\nexport class PaymentDiscountDiviner<\n TParams extends PaymentDiscountDivinerParams = PaymentDiscountDivinerParams,\n TIn extends PaymentDiscountDivinerInputType = PaymentDiscountDivinerInputType,\n TOut extends Discount = Discount,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override configSchemas = [PaymentDiscountDivinerConfigSchema]\n static override defaultConfigSchema = PaymentDiscountDivinerConfigSchema\n\n protected get couponAuthorities(): Address[] {\n return [...(this.config.couponAuthorities ?? []), ...(this.params.couponAuthorities ?? [])]\n }\n\n protected async divineHandler(payloads: TIn[] = []): Promise<TOut[]> {\n const sources: Hash[] = []\n\n // Parse terms\n const terms = payloads.find(isEscrowTerms) as EscrowTerms | undefined\n if (!terms) return [{ ...NO_DISCOUNT, sources }] as TOut[]\n sources.push(await PayloadBuilder.hash(terms))\n\n // Parse appraisals\n const termsAppraisals = terms?.appraisals\n // If the escrow terms do not have appraisals, return no discount\n if (!termsAppraisals || termsAppraisals.length === 0) return [{ ...NO_DISCOUNT, sources }] as TOut[]\n const hashMap = await PayloadBuilder.toAllHashMap(payloads) as Record<Hash, Payload>\n const appraisals = this.getEscrowAppraisals(terms, hashMap)\n // Add the appraisals that were found to the sources\n sources.push(...termsAppraisals)\n // If not all appraisals are found, return no discount\n if (appraisals.length !== termsAppraisals.length) return [{ ...NO_DISCOUNT, sources }] as TOut[]\n\n // Parse coupons\n const coupons = await this.getEscrowDiscounts(terms, hashMap)\n // Add the coupons that were found to the sources\n // NOTE: Should we throw if not all coupons are found?\n const couponHashes = await PayloadBuilder.hashes(coupons)\n sources.push(...couponHashes)\n\n const validCoupons = await this.filterToSigned(coupons.filter(this.isCouponCurrent))\n // NOTE: Should we throw if not all coupons are valid?\n if (validCoupons.length === 0) return [{ ...NO_DISCOUNT, sources }] as TOut[]\n\n // TODO: Call paymentSubtotalDiviner to get the subtotal to centralize the logic\n const discount = applyCoupons(appraisals, validCoupons)\n return [{ ...discount, sources }] as TOut[]\n }\n\n /**\n * Filters the supplied list of coupons to only those that are signed by\n * addresses specified in the couponAuthorities\n * @param coupons The list of coupons to filter\n * @returns The filtered list of coupons that are signed by the couponAuthorities\n */\n protected async filterToSigned(coupons: Coupon[]): Promise<Coupon[]> {\n const signed: Coupon[] = []\n const dataHashMap = await PayloadBuilder.toDataHashMap(coupons)\n const boundWitnessDiviner = await this.getDiscountsBoundWitnessDiviner()\n const hashes = Object.keys(dataHashMap)\n const addresses = this.couponAuthorities\n // TODO: Keep an in memory cache of the hashes queried and their results\n // to avoid querying the same hash multiple times\n await Promise.all(hashes.map((h) => {\n const hash = h as Hash\n return Promise.all(addresses.map(async (address) => {\n const query: BoundWitnessDivinerQueryPayload = {\n ...DEFAULT_BOUND_WITNESS_DIVINER_QUERY_PROPS, addresses: [address], payload_hashes: [hash],\n }\n const result = await boundWitnessDiviner.divine([query])\n if (result.length > 0) signed.push(dataHashMap[hash])\n }))\n }))\n return signed\n }\n\n protected async getDiscountsArchivist(): Promise<ArchivistInstance> {\n const name = assertEx(this.config.archivist, () => 'Missing archivist in config')\n const mod = assertEx(await this.resolve(name), () => `Error resolving archivist: ${name}`)\n return assertEx(asArchivistInstance(mod), () => `Resolved module ${mod.address} not a valid Archivist`)\n }\n\n protected async getDiscountsBoundWitnessDiviner(): Promise<DivinerInstance> {\n const name = assertEx(this.config.boundWitnessDiviner, () => 'Missing boundWitnessDiviner in config')\n const mod = assertEx(await this.resolve(name), () => `Error resolving boundWitnessDiviner: ${name}`)\n return assertEx(asDivinerInstance(mod), () => `Resolved module ${mod.address} not a valid Diviner`)\n }\n\n /**\n * Finds the appraisals specified by the escrow terms from the supplied payloads\n * @param terms The escrow terms\n * @param payloads The payloads to search for the appraisals\n * @returns The appraisals found in the payloads\n */\n protected getEscrowAppraisals(terms: EscrowTerms, hashMap: Record<Hash, Payload>): HashLeaseEstimate[] {\n const hashes = terms?.appraisals ?? []\n if (hashes.length === 0) return []\n return hashes.map(hash => hashMap[hash]).filter(exists).filter(isHashLeaseEstimate)\n }\n\n /**\n * Finds the discounts specified by the escrow terms from the supplied payloads\n * @param terms The escrow terms\n * @param hashMap The payloads to search for the discounts\n * @returns The discounts found in the payloads\n */\n protected async getEscrowDiscounts(terms: EscrowTerms, hashMap: Record<Hash, Payload>): Promise<Coupon[]> {\n // Parse discounts\n const hashes = terms.discounts ?? []\n if (hashes.length === 0) return []\n\n // Use the supplied payloads to find the discounts\n const discounts: Coupon[] = hashes.map(hash => hashMap[hash]).filter(exists).filter(isCoupon)\n const missing = hashes.filter(hash => !hashMap[hash])\n // If not all discounts are found\n if (missing.length > 0) {\n // Find any remaining from discounts archivist\n const discountsArchivist = await this.getDiscountsArchivist()\n const payloads = await discountsArchivist.get(missing)\n discounts.push(...payloads.filter(isCouponWithMeta))\n }\n // If not all discounts are found\n if (discounts.length !== hashes.length) {\n const termsHash = await PayloadBuilder.hash(terms)\n const foundHashes = await PayloadBuilder.hashes(discounts)\n // Log individual discounts that were not found\n for (const hash of hashes) {\n if (!foundHashes.includes(hash)) console.warn(`Discount ${hash} not found for terms ${termsHash}`)\n }\n }\n return discounts\n }\n\n protected isCouponCurrent(coupon: Coupon): boolean {\n const now = Date.now()\n return coupon.exp > now && coupon.nbf < now\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport type { HashLeaseEstimate } from '@xyo-network/diviner-hash-lease'\nimport type {\n AmountFields,\n Coupon, Discount, FixedAmountCoupon,\n FixedPercentageCoupon,\n FixedPriceCoupon,\n} from '@xyo-network/payment-payload-plugins'\nimport {\n DiscountSchema, isFixedAmountCoupon, isFixedPercentageCoupon,\n isFixedPriceCoupon,\n isStackable,\n} from '@xyo-network/payment-payload-plugins'\n\nexport const applyCoupons = (appraisals: HashLeaseEstimate[], coupons: Coupon[]): Discount => {\n // Ensure all appraisals and coupons are in USD\n const allAppraisalsAreUSD = appraisals.every(appraisal => appraisal.currency === 'USD')\n assertEx(allAppraisalsAreUSD, 'All appraisals must be in USD')\n const allCouponsAreUSD = coupons.map(coupon => (coupon as Partial<AmountFields>)?.currency).filter(exists).every(currency => currency === 'USD')\n assertEx(allCouponsAreUSD, 'All coupons must be in USD')\n const total = appraisals.reduce((acc, appraisal) => acc + appraisal.price, 0)\n\n // Calculated non-stackable discount coupons\n const singularFixedDiscount = Math.max(...coupons\n .filter(coupon => isFixedAmountCoupon(coupon) && !isStackable(coupon))\n .map(coupon => (coupon as FixedAmountCoupon).amount), 0)\n const singularPercentageDiscount = (Math.max(...coupons\n .filter(coupon => isFixedPercentageCoupon(coupon) && !isStackable(coupon))\n .map(coupon => (coupon as FixedPercentageCoupon).percentage), 0)) * total\n const singularFixedPriceDiscount = calculateSingularFixedPriceDiscount(total, appraisals, coupons, false)\n\n // Calculate stackable discount coupons\n // First calculate the total discount from fixed amount coupons\n const stackedFixedDiscount = coupons\n .filter(coupon => isFixedAmountCoupon(coupon) && isStackable(coupon))\n .reduce((acc, coupon) => acc + (coupon as FixedAmountCoupon).amount, 0)\n // Then calculate the total discount from percentage coupons and apply\n // the percentage discount to the remaining total after fixed discounts\n const stackedPercentageDiscount = coupons\n .filter(coupon => isFixedPercentageCoupon(coupon) && isStackable(coupon))\n .reduce((acc, coupon) => acc + (coupon as FixedPercentageCoupon).percentage, 0) * (total - stackedFixedDiscount)\n // Then calculate the total discount from fixed price coupons\n const stackedFixedPriceDiscount = calculateSingularFixedPriceDiscount(total, appraisals, coupons, true)\n\n // Sum all stackable discounts\n const stackedDiscount = stackedFixedDiscount + stackedPercentageDiscount + stackedFixedPriceDiscount\n\n // Find the best coupon(s) to apply\n const maxDiscount = Math.max(\n singularFixedDiscount,\n singularFixedPriceDiscount,\n singularPercentageDiscount,\n stackedDiscount,\n 0,\n )\n\n // Ensure discount is not more than the total\n const amount = Math.min(maxDiscount, total)\n\n // Return single discount payload\n return {\n amount, schema: DiscountSchema, currency: 'USD',\n }\n}\n\nconst calculateSingularFixedPriceDiscount = (total: number, appraisals: HashLeaseEstimate[], coupons: Coupon[], stackable = false): number => {\n // Find all singular fixed price discounts\n const singularFixedPriceDiscounts = coupons\n .filter(isFixedPriceCoupon)\n .filter(coupon => stackable ? isStackable(coupon) : !isStackable(coupon))\n .map(coupon => (coupon as FixedPriceCoupon).amount)\n // Ensure all fixed price discounts are positive\n .filter(amount => amount > 0)\n\n // If there are no singular fixed price discounts, return no discount\n if (singularFixedPriceDiscounts.length === 0) return 0\n\n // Find the maximum discount (the lowest fixed price)\n const lowestFixedPrice = Math.min(...singularFixedPriceDiscounts)\n\n // Apply the fixed price to all appraisals to get the reduced prices\n const reducedPrices = appraisals.map(appraisal =>\n // If the appraisal price is less than the fixed price\n appraisal.price < lowestFixedPrice\n // Use the appraisal price as the coupon will result in a higher price\n ? appraisal.price\n // Otherwise, apply the fixed price discount\n : lowestFixedPrice)\n\n // Calculate the reduced total using the reduced prices\n const reducedTotal = reducedPrices.reduce((acc, price) => acc + price, 0)\n\n // Calculate the discount\n const discount = total - reducedTotal\n\n // Return the discount or 0 if the discount would have resulted in a negative value\n return discount > 0 ? discount : 0\n}\n","import type { Hash } from '@xylabs/hex'\nimport type { DivinerInstance } from '@xyo-network/diviner-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type { Payload } from '@xyo-network/payload-model'\nimport type {\n Discount, EscrowTerms, Invoice, Payment, Subtotal, Total,\n} from '@xyo-network/payment-payload-plugins'\nimport {\n isDiscount, isSubtotal, isTotal, PaymentSchema,\n} from '@xyo-network/payment-payload-plugins'\n\n/**\n * Validates the escrow terms to ensure they are valid for a purchase\n * @returns A payment if the terms are valid for a purchase, undefined otherwise\n */\nexport const getInvoiceForEscrow = async (\n terms: EscrowTerms,\n dataHashMap: Record<Hash, Payload>,\n paymentTotalDiviner: DivinerInstance,\n): Promise<Invoice | undefined> => {\n const payloads = Object.values(dataHashMap)\n const results = await paymentTotalDiviner.divine([terms, ...payloads])\n const subtotal = results.find(isSubtotal) as Subtotal | undefined\n const discount = results.find(isDiscount) as Discount | undefined\n const total = results.find(isTotal) as Total | undefined\n if (!subtotal || !total) return undefined\n const { amount, currency } = total\n if (currency !== 'USD') return undefined\n const sources = await getSources(terms, subtotal, total, discount)\n const payment: Payment = {\n amount, currency, schema: PaymentSchema, sources,\n }\n return discount ? [subtotal, total, payment, discount] : [subtotal, total, payment]\n}\n\nconst getSources = async (terms: EscrowTerms, subtotal: Subtotal, total: Total, discount?: Discount): Promise<Hash[]> => {\n const sources = discount ? [terms, subtotal, total, discount] : [terms, subtotal, total]\n return await Promise.all(sources.map(p => PayloadBuilder.dataHash(p)))\n}\n","import { AbstractDiviner } from '@xyo-network/diviner-abstract'\nimport { HashLeaseEstimate, isHashLeaseEstimate } from '@xyo-network/diviner-hash-lease'\nimport { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { creatableModule } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload } from '@xyo-network/payload-model'\nimport {\n EscrowTerms, isEscrowTerms, PaymentSubtotalDivinerConfigSchema, PaymentSubtotalDivinerParams, Subtotal, SubtotalSchema,\n} from '@xyo-network/payment-payload-plugins'\n\nimport {\n appraisalValidators, termsValidators, ValidEscrowTerms,\n} from './lib/index.ts'\n\nconst currency = 'USD'\n\n/**\n * Escrow terms that contain all the valid fields for calculating a subtotal\n */\nexport type PaymentSubtotalDivinerInputType = EscrowTerms | HashLeaseEstimate | Payload\n\n@creatableModule()\nexport class PaymentSubtotalDiviner<\n TParams extends PaymentSubtotalDivinerParams = PaymentSubtotalDivinerParams,\n TIn extends PaymentSubtotalDivinerInputType = PaymentSubtotalDivinerInputType,\n TOut extends Subtotal = Subtotal,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override configSchemas = [PaymentSubtotalDivinerConfigSchema]\n static override defaultConfigSchema = PaymentSubtotalDivinerConfigSchema\n\n protected async divineHandler(payloads: TIn[] = []): Promise<TOut[]> {\n // Find the escrow terms\n const terms = payloads.find(isEscrowTerms) as EscrowTerms | undefined\n if (!terms) return []\n\n // Run all terms validations\n if (!termsValidators.every(validator => validator(terms))) return []\n const validTerms = terms as ValidEscrowTerms\n\n // Retrieve all appraisals from terms\n const hashMap = await PayloadBuilder.toAllHashMap(payloads)\n const appraisals = validTerms.appraisals.map(appraisal => hashMap[appraisal]).filter(isHashLeaseEstimate) as unknown as HashLeaseEstimate[]\n\n // Ensure all appraisals are present\n if (appraisals.length !== validTerms.appraisals.length) return []\n\n // Run all appraisal validations\n if (!appraisalValidators.every(validator => validator(appraisals))) return []\n const amount = calculateSubtotal(appraisals)\n const sources = [await PayloadBuilder.dataHash(validTerms), ...validTerms.appraisals]\n return [{\n amount, currency, schema: SubtotalSchema, sources,\n }] as TOut[]\n }\n}\n\n// TODO: Add support for other currencies\nconst calculateSubtotal = (appraisals: HashLeaseEstimate[]): number => {\n return appraisals.reduce((sum, appraisal) => sum + appraisal.price, 0)\n}\n","import type { HashLeaseEstimate } from '@xyo-network/diviner-hash-lease'\nimport { isIso4217CurrencyCode } from '@xyo-network/payment-payload-plugins'\n\nimport { validateDuration } from './durationValidators.ts'\n\nconst validateAppraisalAmount = (appraisals: HashLeaseEstimate[]): boolean => {\n // Ensure all appraisals are numeric\n if (appraisals.some(appraisal => typeof appraisal.price !== 'number')) return false\n // Ensure all appraisals are positive numbers\n if (appraisals.some(appraisal => appraisal.price < 0)) return false\n return true\n}\n\nconst validateAppraisalCurrency = (appraisals: HashLeaseEstimate[]): boolean => {\n // NOTE: Only supporting USD for now, the remaining checks are for future-proofing.\n if (!appraisals.every(appraisal => appraisal.currency == 'USD')) return false\n\n // Check every object in the array to ensure they all are in a supported currency.\n if (!appraisals.every(appraisal => isIso4217CurrencyCode(appraisal.currency))) return false\n\n return true\n}\n\nconst validateAppraisalConsistentCurrency = (appraisals: HashLeaseEstimate[]): boolean => {\n // Check if the array is empty or contains only one element, no need to compare.\n if (appraisals.length <= 1) return true\n\n // Get the currency of the first element to compare with others.\n const { currency } = appraisals[0]\n if (!currency) return false\n\n // Check every object in the array to ensure they all have the same currency.\n if (!appraisals.every(item => item.currency === currency)) return false\n\n return true\n}\n\nconst validateAppraisalWindow = (appraisals: HashLeaseEstimate[]): boolean => appraisals.every(validateDuration)\n\nexport const appraisalValidators = [\n validateAppraisalAmount,\n validateAppraisalCurrency,\n validateAppraisalConsistentCurrency,\n validateAppraisalWindow,\n]\n","import type { DurationFields } from '@xyo-network/xns-record-payload-plugins'\n\nconst FIVE_MINUTES = 1000 * 60 * 5\n\n/**\n * Validates that the current time is within the duration window, within a configurable a buffer\n * @param value The duration value\n * @param windowMs The window in milliseconds to allow for a buffer\n * @returns True if the duration is valid, false otherwise\n */\nexport const validateDuration = (value: Partial<DurationFields>, windowMs = FIVE_MINUTES): boolean => {\n const now = Date.now()\n if (!value.nbf || value.nbf > now) return false\n // If already expired (include for a 5 minute buffer to allow for a reasonable\n // minimum amount of time for the transaction to be processed)\n if (!value.exp || value.exp - now < windowMs) return false\n return true\n}\n","import type { Hash } from '@xylabs/hex'\nimport type { EscrowTerms } from '@xyo-network/payment-payload-plugins'\n\nimport { validateDuration } from './durationValidators.ts'\n\nexport type ValidEscrowTerms = Required<EscrowTerms>\n\nconst validateTermsAppraisals = (terms: EscrowTerms): terms is Required<EscrowTerms & { appraisals: Hash[] }> => {\n if (!terms.appraisals) return false\n if (terms.appraisals.length === 0) return false\n return true\n}\nconst validateTermsWindow = (terms: EscrowTerms): boolean => validateDuration(terms)\n\nexport const termsValidators = [\n validateTermsAppraisals,\n validateTermsWindow,\n]\n","import { assertEx } from '@xylabs/assert'\nimport { Hash } from '@xylabs/hex'\nimport { AbstractDiviner } from '@xyo-network/diviner-abstract'\nimport {\n asDivinerInstance, DivinerInstance, DivinerModuleEventData,\n} from '@xyo-network/diviner-model'\nimport { creatableModule } from '@xyo-network/module-model'\nimport {\n Discount,\n isDiscountWithMeta,\n isSubtotalWithMeta,\n PaymentTotalDivinerConfigSchema, PaymentTotalDivinerParams, Subtotal, Total, TotalSchema,\n} from '@xyo-network/payment-payload-plugins'\n\nimport { PaymentDiscountDiviner, PaymentDiscountDivinerInputType } from '../Discount/index.ts'\nimport { PaymentSubtotalDiviner, PaymentSubtotalDivinerInputType } from '../Subtotal/index.ts'\n\ntype InputType = PaymentDiscountDivinerInputType | PaymentSubtotalDivinerInputType\ntype OutputType = Subtotal | Discount | Total\n\n@creatableModule()\nexport class PaymentTotalDiviner<\n TParams extends PaymentTotalDivinerParams = PaymentTotalDivinerParams,\n TIn extends InputType = InputType,\n TOut extends OutputType = OutputType,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override configSchemas = [PaymentTotalDivinerConfigSchema]\n static override defaultConfigSchema: PaymentTotalDivinerConfigSchema = PaymentTotalDivinerConfigSchema\n\n protected async divineHandler(payloads: TIn[] = []): Promise<TOut[]> {\n const subtotalDiviner = await this.getPaymentSubtotalDiviner()\n const subtotalResult = await subtotalDiviner.divine(payloads)\n const subtotal = subtotalResult.find(isSubtotalWithMeta)\n if (!subtotal) return []\n const discountDiviner = await this.getPaymentDiscountsDiviner()\n const discountResult = await discountDiviner.divine(payloads)\n const discount = discountResult.find(isDiscountWithMeta)\n if (!discount) return []\n const { currency: subtotalCurrency } = subtotal\n const { currency: discountCurrency } = discount\n assertEx(subtotalCurrency === discountCurrency, () => `Subtotal currency ${subtotalCurrency} does not match discount currency ${discountCurrency}`)\n const amount = Math.max(0, subtotal.amount - discount.amount)\n const currency = subtotalCurrency\n const sources = [subtotal.$hash, discount.$hash] as Hash[]\n const total: Total = {\n amount, currency, sources, schema: TotalSchema,\n }\n return [subtotal, discount, total] as TOut[]\n }\n\n protected async getPaymentDiscountsDiviner(): Promise<PaymentDiscountDiviner> {\n const name = assertEx(this.config.paymentDiscountDiviner, () => 'Missing paymentDiscountDiviner in config')\n const mod = assertEx(await this.resolve(name), () => `Error resolving paymentDiscountDiviner: ${name}`)\n return assertEx(asDivinerInstance(mod), () => `Resolved module ${mod.address} not a valid Diviner`) as PaymentDiscountDiviner\n }\n\n protected async getPaymentSubtotalDiviner(): Promise<PaymentSubtotalDiviner> {\n const name = assertEx(this.config.paymentSubtotalDiviner, () => 'Missing paymentSubtotalDiviner in config')\n const mod = assertEx(await this.resolve(name), () => `Error resolving paymentSubtotalDiviner: ${name}`)\n return assertEx(asDivinerInstance(mod), () => `Resolved module ${mod.address} not a valid Diviner`) as PaymentSubtotalDiviner\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAAA,SAAS,YAAAA,iBAAgB;AACzB,SAAS,UAAAC,eAAc;AAEvB,SAA4B,2BAA2B;AACvD,SAAS,uBAAuB;AAChC,SAA0C,sCAAsC;AAChF;AAAA,EAEE;AAAA,OACK;AACP;AAAA,EACE;AAAA,OACK;AACP,SAAS,uBAAuB;AAChC,SAAS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AAM,UAAU,CAAC,SAAS,OAAO,SAAS,KAAK;AAC/C,UAAM,QAAe;AAAA,MACnB;AAAA,MAAQ,UAAAA;AAAA,MAAU;AAAA,MAAS,QAAQ;AAAA,IACrC;AACA,WAAO,CAAC,UAAU,UAAU,KAAK;AAAA,EACnC;AAAA,EAEA,MAAgB,6BAA8D;AAC5E,UAAM,OAAOD,UAAS,KAAK,OAAO,wBAAwB,MAAM,0CAA0C;AAC1G,UAAM,MAAMA,UAAS,MAAM,KAAK,QAAQ,IAAI,GAAG,MAAM,2CAA2C,IAAI,EAAE;AACtG,WAAOA,UAASE,mBAAkB,GAAG,GAAG,MAAM,mBAAmB,IAAI,OAAO,sBAAsB;AAAA,EACpG;AAAA,EAEA,MAAgB,4BAA6D;AAC3E,UAAM,OAAOF,UAAS,KAAK,OAAO,wBAAwB,MAAM,0CAA0C;AAC1G,UAAM,MAAMA,UAAS,MAAM,KAAK,QAAQ,IAAI,GAAG,MAAM,2CAA2C,IAAI,EAAE;AACtG,WAAOA,UAASE,mBAAkB,GAAG,GAAG,MAAM,mBAAmB,IAAI,OAAO,sBAAsB;AAAA,EACpG;AACF;AAnCE,cAVW,qBAUK,iBAAgB,CAAC,+BAA+B;AAChE,cAXW,qBAWK,uBAAuD;AAX5D,sBAAN;AAAA,EADNC,iBAAgB;AAAA,GACJ;","names":["assertEx","exists","currency","assertEx","exists","PayloadBuilder","currency","AbstractDiviner","isHashLeaseEstimate","creatableModule","PayloadBuilder","isEscrowTerms","currency","AbstractDiviner","isEscrowTerms","PayloadBuilder","isHashLeaseEstimate","creatableModule","assertEx","AbstractDiviner","asDivinerInstance","creatableModule","AbstractDiviner","assertEx","currency","asDivinerInstance","creatableModule"]}
1
+ {"version":3,"sources":["../../src/Discount/Diviner.ts","../../src/Discount/lib/applyCoupons.ts","../../src/Discount/lib/findUnfulfilledConditions.ts","../../src/Invoice/getInvoiceForEscrow.ts","../../src/Subtotal/Diviner.ts","../../src/Subtotal/lib/appraisalValidators.ts","../../src/Subtotal/lib/durationValidators.ts","../../src/Subtotal/lib/termsValidators.ts","../../src/Total/Diviner.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { Address, Hash } from '@xylabs/hex'\nimport { ArchivistInstance, asArchivistInstance } from '@xyo-network/archivist-model'\nimport { AbstractDiviner } from '@xyo-network/diviner-abstract'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport {\n HashLeaseEstimate,\n isHashLeaseEstimate,\n} from '@xyo-network/diviner-hash-lease'\nimport {\n asDivinerInstance, DivinerInstance, DivinerModuleEventData,\n} from '@xyo-network/diviner-model'\nimport { creatableModule } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload } from '@xyo-network/payload-model'\nimport {\n Coupon,\n Discount,\n EscrowTerms, isCoupon,\n isCouponWithMeta,\n isEscrowTerms, NO_DISCOUNT, PaymentDiscountDivinerConfigSchema, PaymentDiscountDivinerParams,\n} from '@xyo-network/payment-payload-plugins'\n\nimport { applyCoupons } from './lib/index.ts'\n\nconst DEFAULT_BOUND_WITNESS_DIVINER_QUERY_PROPS: Readonly<BoundWitnessDivinerQueryPayload> = {\n limit: 1,\n order: 'desc',\n schema: BoundWitnessDivinerQuerySchema,\n}\n\nexport type PaymentDiscountDivinerInputType = EscrowTerms | Coupon | HashLeaseEstimate | Payload\n\n@creatableModule()\nexport class PaymentDiscountDiviner<\n TParams extends PaymentDiscountDivinerParams = PaymentDiscountDivinerParams,\n TIn extends PaymentDiscountDivinerInputType = PaymentDiscountDivinerInputType,\n TOut extends Discount = Discount,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override configSchemas = [PaymentDiscountDivinerConfigSchema]\n static override defaultConfigSchema = PaymentDiscountDivinerConfigSchema\n\n protected get couponAuthorities(): Address[] {\n return [...(this.config.couponAuthorities ?? []), ...(this.params.couponAuthorities ?? [])]\n }\n\n protected async divineHandler(payloads: TIn[] = []): Promise<TOut[]> {\n const sources: Hash[] = []\n\n // Parse terms\n const terms = payloads.find(isEscrowTerms) as EscrowTerms | undefined\n if (!terms) return [{ ...NO_DISCOUNT, sources }] as TOut[]\n sources.push(await PayloadBuilder.hash(terms))\n\n // Parse appraisals\n const termsAppraisals = terms?.appraisals\n // If the escrow terms do not have appraisals, return no discount\n if (!termsAppraisals || termsAppraisals.length === 0) return [{ ...NO_DISCOUNT, sources }] as TOut[]\n const hashMap = await PayloadBuilder.toAllHashMap(payloads) as Record<Hash, Payload>\n const appraisals = this.getEscrowAppraisals(terms, hashMap)\n // Add the appraisals that were found to the sources\n sources.push(...termsAppraisals)\n // If not all appraisals are found, return no discount\n if (appraisals.length !== termsAppraisals.length) return [{ ...NO_DISCOUNT, sources }] as TOut[]\n\n // Parse coupons\n const coupons = await this.getEscrowDiscounts(terms, hashMap)\n // Add the coupons that were found to the sources\n // NOTE: Should we throw if not all coupons are found?\n const couponHashes = await PayloadBuilder.hashes(coupons)\n sources.push(...couponHashes)\n\n const validCoupons = await this.filterToSigned(coupons.filter(this.isCouponCurrent))\n // NOTE: Should we throw if not all coupons are valid?\n if (validCoupons.length === 0) return [{ ...NO_DISCOUNT, sources }] as TOut[]\n\n // TODO: Call paymentSubtotalDiviner to get the subtotal to centralize the logic\n const discount = applyCoupons(appraisals, validCoupons)\n return [{ ...discount, sources }] as TOut[]\n }\n\n /**\n * Filters the supplied list of coupons to only those that are signed by\n * addresses specified in the couponAuthorities\n * @param coupons The list of coupons to filter\n * @returns The filtered list of coupons that are signed by the couponAuthorities\n */\n protected async filterToSigned(coupons: Coupon[]): Promise<Coupon[]> {\n const signed: Coupon[] = []\n const dataHashMap = await PayloadBuilder.toDataHashMap(coupons)\n const boundWitnessDiviner = await this.getDiscountsBoundWitnessDiviner()\n const hashes = Object.keys(dataHashMap)\n const addresses = this.couponAuthorities\n // TODO: Keep an in memory cache of the hashes queried and their results\n // to avoid querying the same hash multiple times\n await Promise.all(hashes.map((h) => {\n const hash = h as Hash\n return Promise.all(addresses.map(async (address) => {\n const query: BoundWitnessDivinerQueryPayload = {\n ...DEFAULT_BOUND_WITNESS_DIVINER_QUERY_PROPS, addresses: [address], payload_hashes: [hash],\n }\n const result = await boundWitnessDiviner.divine([query])\n if (result.length > 0) signed.push(dataHashMap[hash])\n }))\n }))\n return signed\n }\n\n protected async getDiscountsArchivist(): Promise<ArchivistInstance> {\n const name = assertEx(this.config.archivist, () => 'Missing archivist in config')\n const mod = assertEx(await this.resolve(name), () => `Error resolving archivist: ${name}`)\n return assertEx(asArchivistInstance(mod), () => `Resolved module ${mod.address} not a valid Archivist`)\n }\n\n protected async getDiscountsBoundWitnessDiviner(): Promise<DivinerInstance> {\n const name = assertEx(this.config.boundWitnessDiviner, () => 'Missing boundWitnessDiviner in config')\n const mod = assertEx(await this.resolve(name), () => `Error resolving boundWitnessDiviner: ${name}`)\n return assertEx(asDivinerInstance(mod), () => `Resolved module ${mod.address} not a valid Diviner`)\n }\n\n /**\n * Finds the appraisals specified by the escrow terms from the supplied payloads\n * @param terms The escrow terms\n * @param payloads The payloads to search for the appraisals\n * @returns The appraisals found in the payloads\n */\n protected getEscrowAppraisals(terms: EscrowTerms, hashMap: Record<Hash, Payload>): HashLeaseEstimate[] {\n const hashes = terms?.appraisals ?? []\n if (hashes.length === 0) return []\n return hashes.map(hash => hashMap[hash]).filter(exists).filter(isHashLeaseEstimate)\n }\n\n /**\n * Finds the discounts specified by the escrow terms from the supplied payloads\n * @param terms The escrow terms\n * @param hashMap The payloads to search for the discounts\n * @returns The discounts found in the payloads\n */\n protected async getEscrowDiscounts(terms: EscrowTerms, hashMap: Record<Hash, Payload>): Promise<Coupon[]> {\n // Parse discounts\n const hashes = terms.discounts ?? []\n if (hashes.length === 0) return []\n\n // Use the supplied payloads to find the discounts\n const discounts: Coupon[] = hashes.map(hash => hashMap[hash]).filter(exists).filter(isCoupon)\n const missing = hashes.filter(hash => !hashMap[hash])\n // If not all discounts are found\n if (missing.length > 0) {\n // Find any remaining from discounts archivist\n const discountsArchivist = await this.getDiscountsArchivist()\n const payloads = await discountsArchivist.get(missing)\n discounts.push(...payloads.filter(isCouponWithMeta))\n }\n // If not all discounts are found\n if (discounts.length !== hashes.length) {\n const termsHash = await PayloadBuilder.hash(terms)\n const foundHashes = await PayloadBuilder.hashes(discounts)\n // Log individual discounts that were not found\n for (const hash of hashes) {\n if (!foundHashes.includes(hash)) console.warn(`Discount ${hash} not found for terms ${termsHash}`)\n }\n }\n return discounts\n }\n\n protected isCouponCurrent(coupon: Coupon): boolean {\n const now = Date.now()\n return coupon.exp > now && coupon.nbf < now\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport type { HashLeaseEstimate } from '@xyo-network/diviner-hash-lease'\nimport type {\n AmountFields,\n Coupon, Discount, FixedAmountCoupon,\n FixedPercentageCoupon,\n FixedPriceCoupon,\n} from '@xyo-network/payment-payload-plugins'\nimport {\n DiscountSchema, isFixedAmountCoupon, isFixedPercentageCoupon,\n isFixedPriceCoupon,\n isStackable,\n} from '@xyo-network/payment-payload-plugins'\n\nexport const applyCoupons = (appraisals: HashLeaseEstimate[], coupons: Coupon[]): Discount => {\n // Ensure all appraisals and coupons are in USD\n const allAppraisalsAreUSD = appraisals.every(appraisal => appraisal.currency === 'USD')\n assertEx(allAppraisalsAreUSD, 'All appraisals must be in USD')\n const allCouponsAreUSD = coupons.map(coupon => (coupon as Partial<AmountFields>)?.currency).filter(exists).every(currency => currency === 'USD')\n assertEx(allCouponsAreUSD, 'All coupons must be in USD')\n const total = appraisals.reduce((acc, appraisal) => acc + appraisal.price, 0)\n\n // Calculated non-stackable discount coupons\n const singularFixedDiscount = Math.max(...coupons\n .filter(coupon => isFixedAmountCoupon(coupon) && !isStackable(coupon))\n .map(coupon => (coupon as FixedAmountCoupon).amount), 0)\n const singularPercentageDiscount = (Math.max(...coupons\n .filter(coupon => isFixedPercentageCoupon(coupon) && !isStackable(coupon))\n .map(coupon => (coupon as FixedPercentageCoupon).percentage), 0)) * total\n const singularFixedPriceDiscount = calculateSingularFixedPriceDiscount(total, appraisals, coupons, false)\n\n // Calculate stackable discount coupons\n // First calculate the total discount from fixed amount coupons\n const stackedFixedDiscount = coupons\n .filter(coupon => isFixedAmountCoupon(coupon) && isStackable(coupon))\n .reduce((acc, coupon) => acc + (coupon as FixedAmountCoupon).amount, 0)\n // Then calculate the total discount from percentage coupons and apply\n // the percentage discount to the remaining total after fixed discounts\n const stackedPercentageDiscount = coupons\n .filter(coupon => isFixedPercentageCoupon(coupon) && isStackable(coupon))\n .reduce((acc, coupon) => acc + (coupon as FixedPercentageCoupon).percentage, 0) * (total - stackedFixedDiscount)\n // Then calculate the total discount from fixed price coupons\n const stackedFixedPriceDiscount = calculateSingularFixedPriceDiscount(total, appraisals, coupons, true)\n\n // Sum all stackable discounts\n const stackedDiscount = stackedFixedDiscount + stackedPercentageDiscount + stackedFixedPriceDiscount\n\n // Find the best coupon(s) to apply\n const maxDiscount = Math.max(\n singularFixedDiscount,\n singularFixedPriceDiscount,\n singularPercentageDiscount,\n stackedDiscount,\n 0,\n )\n\n // Ensure discount is not more than the total\n const amount = Math.min(maxDiscount, total)\n\n // Return single discount payload\n return {\n amount, schema: DiscountSchema, currency: 'USD',\n }\n}\n\nconst calculateSingularFixedPriceDiscount = (total: number, appraisals: HashLeaseEstimate[], coupons: Coupon[], stackable = false): number => {\n // Find all singular fixed price discounts\n const singularFixedPriceDiscounts = coupons\n .filter(isFixedPriceCoupon)\n .filter(coupon => stackable ? isStackable(coupon) : !isStackable(coupon))\n .map(coupon => (coupon as FixedPriceCoupon).amount)\n // Ensure all fixed price discounts are positive\n .filter(amount => amount > 0)\n\n // If there are no singular fixed price discounts, return no discount\n if (singularFixedPriceDiscounts.length === 0) return 0\n\n // Find the maximum discount (the lowest fixed price)\n const lowestFixedPrice = Math.min(...singularFixedPriceDiscounts)\n\n // Apply the fixed price to all appraisals to get the reduced prices\n const reducedPrices = appraisals.map(appraisal =>\n // If the appraisal price is less than the fixed price\n appraisal.price < lowestFixedPrice\n // Use the appraisal price as the coupon will result in a higher price\n ? appraisal.price\n // Otherwise, apply the fixed price discount\n : lowestFixedPrice)\n\n // Calculate the reduced total using the reduced prices\n const reducedTotal = reducedPrices.reduce((acc, price) => acc + price, 0)\n\n // Calculate the discount\n const discount = total - reducedTotal\n\n // Return the discount or 0 if the discount would have resulted in a negative value\n return discount > 0 ? discount : 0\n}\n","import type { Hash } from '@xylabs/hex'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type { Payload, WithMeta } from '@xyo-network/payload-model'\nimport type { Coupon } from '@xyo-network/payment-payload-plugins'\nimport type { SchemaPayload } from '@xyo-network/schema-payload-plugin'\nimport { isSchemaPayloadWithMeta } from '@xyo-network/schema-payload-plugin'\nimport type { ValidateFunction } from 'ajv'\nimport { Ajv } from 'ajv'\n\n// TODO: Use our schema cache\nconst ajv = new Ajv({ strict: false }) // Create the Ajv instance once\nconst schemaCache = new Map() // Cache to store compiled validators\n\nexport const areConditionsFulfilled = async (coupon: Coupon, payloads: Payload[]): Promise<boolean> =>\n (await findUnfulfilledConditions(coupon, payloads)).length === 0\n\n// TODO: Should we separate conditions and payloads to prevent conflating data and \"operands\" (schemas to validate against data)?\n/**\n * Validates the conditions of a coupon against the provided payloads\n * @param coupon The coupon to check\n * @param payloads The associated payloads (containing the conditions and data to validate the conditions against)\n * @returns The unfulfilled condition hashes\n */\nexport const findUnfulfilledConditions = async (coupon: Coupon, payloads: Payload[]): Promise<Hash[]> => {\n const unfulfilledConditions: Hash[] = []\n // If there are no conditions, then they are fulfilled\n if (!coupon.conditions || coupon.conditions.length === 0) return unfulfilledConditions\n const hashMap = await PayloadBuilder.toAllHashMap(payloads)\n // Find all the conditions\n const conditions = coupon.conditions.map(hash => hashMap[hash]).filter(isSchemaPayloadWithMeta) as WithMeta<SchemaPayload>[]\n // Not all conditions were found\n if (conditions.length !== coupon.conditions.length) {\n const missing = coupon.conditions.filter(hash => !hashMap[hash])\n unfulfilledConditions.push(...missing)\n return unfulfilledConditions\n }\n\n // Test each condition\n for (const hash of coupon.conditions) {\n let validator: ValidateFunction\n\n // Check if the schema is already cached\n if (schemaCache.has(hash)) {\n validator = schemaCache.get(hash)\n } else {\n const payload = hashMap[hash]\n const definition = isSchemaPayloadWithMeta(payload) ? payload.definition : undefined\n if (definition) {\n // Compile and cache the validator\n validator = ajv.compile(definition)\n schemaCache.set(hash, validator)\n\n // Validate the payload\n } else {\n unfulfilledConditions.push(hash)\n continue\n }\n }\n if (!validator(payloads)) unfulfilledConditions.push(hash)\n }\n\n // All conditions passed\n return unfulfilledConditions\n}\n","import type { Hash } from '@xylabs/hex'\nimport type { DivinerInstance } from '@xyo-network/diviner-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type { Payload } from '@xyo-network/payload-model'\nimport type {\n Discount, EscrowTerms, Invoice, Payment, Subtotal, Total,\n} from '@xyo-network/payment-payload-plugins'\nimport {\n isDiscount, isSubtotal, isTotal, PaymentSchema,\n} from '@xyo-network/payment-payload-plugins'\n\n/**\n * Validates the escrow terms to ensure they are valid for a purchase\n * @returns A payment if the terms are valid for a purchase, undefined otherwise\n */\nexport const getInvoiceForEscrow = async (\n terms: EscrowTerms,\n dataHashMap: Record<Hash, Payload>,\n paymentTotalDiviner: DivinerInstance,\n): Promise<Invoice | undefined> => {\n const payloads = Object.values(dataHashMap)\n const results = await paymentTotalDiviner.divine([terms, ...payloads])\n const subtotal = results.find(isSubtotal) as Subtotal | undefined\n const discount = results.find(isDiscount) as Discount | undefined\n const total = results.find(isTotal) as Total | undefined\n if (!subtotal || !total) return undefined\n const { amount, currency } = total\n if (currency !== 'USD') return undefined\n const sources = await getSources(terms, subtotal, total, discount)\n const payment: Payment = {\n amount, currency, schema: PaymentSchema, sources,\n }\n return discount ? [subtotal, total, payment, discount] : [subtotal, total, payment]\n}\n\nconst getSources = async (terms: EscrowTerms, subtotal: Subtotal, total: Total, discount?: Discount): Promise<Hash[]> => {\n const sources = discount ? [terms, subtotal, total, discount] : [terms, subtotal, total]\n return await Promise.all(sources.map(p => PayloadBuilder.dataHash(p)))\n}\n","import { AbstractDiviner } from '@xyo-network/diviner-abstract'\nimport { HashLeaseEstimate, isHashLeaseEstimate } from '@xyo-network/diviner-hash-lease'\nimport { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { creatableModule } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload } from '@xyo-network/payload-model'\nimport {\n EscrowTerms, isEscrowTerms, PaymentSubtotalDivinerConfigSchema, PaymentSubtotalDivinerParams, Subtotal, SubtotalSchema,\n} from '@xyo-network/payment-payload-plugins'\n\nimport {\n appraisalValidators, termsValidators, ValidEscrowTerms,\n} from './lib/index.ts'\n\nconst currency = 'USD'\n\n/**\n * Escrow terms that contain all the valid fields for calculating a subtotal\n */\nexport type PaymentSubtotalDivinerInputType = EscrowTerms | HashLeaseEstimate | Payload\n\n@creatableModule()\nexport class PaymentSubtotalDiviner<\n TParams extends PaymentSubtotalDivinerParams = PaymentSubtotalDivinerParams,\n TIn extends PaymentSubtotalDivinerInputType = PaymentSubtotalDivinerInputType,\n TOut extends Subtotal = Subtotal,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override configSchemas = [PaymentSubtotalDivinerConfigSchema]\n static override defaultConfigSchema = PaymentSubtotalDivinerConfigSchema\n\n protected async divineHandler(payloads: TIn[] = []): Promise<TOut[]> {\n // Find the escrow terms\n const terms = payloads.find(isEscrowTerms) as EscrowTerms | undefined\n if (!terms) return []\n\n // Run all terms validations\n if (!termsValidators.every(validator => validator(terms))) return []\n const validTerms = terms as ValidEscrowTerms\n\n // Retrieve all appraisals from terms\n const hashMap = await PayloadBuilder.toAllHashMap(payloads)\n const appraisals = validTerms.appraisals.map(appraisal => hashMap[appraisal]).filter(isHashLeaseEstimate) as unknown as HashLeaseEstimate[]\n\n // Ensure all appraisals are present\n if (appraisals.length !== validTerms.appraisals.length) return []\n\n // Run all appraisal validations\n if (!appraisalValidators.every(validator => validator(appraisals))) return []\n const amount = calculateSubtotal(appraisals)\n const sources = [await PayloadBuilder.dataHash(validTerms), ...validTerms.appraisals]\n return [{\n amount, currency, schema: SubtotalSchema, sources,\n }] as TOut[]\n }\n}\n\n// TODO: Add support for other currencies\nconst calculateSubtotal = (appraisals: HashLeaseEstimate[]): number => {\n return appraisals.reduce((sum, appraisal) => sum + appraisal.price, 0)\n}\n","import type { HashLeaseEstimate } from '@xyo-network/diviner-hash-lease'\nimport { isIso4217CurrencyCode } from '@xyo-network/payment-payload-plugins'\n\nimport { validateDuration } from './durationValidators.ts'\n\nconst validateAppraisalAmount = (appraisals: HashLeaseEstimate[]): boolean => {\n // Ensure all appraisals are numeric\n if (appraisals.some(appraisal => typeof appraisal.price !== 'number')) return false\n // Ensure all appraisals are positive numbers\n if (appraisals.some(appraisal => appraisal.price < 0)) return false\n return true\n}\n\nconst validateAppraisalCurrency = (appraisals: HashLeaseEstimate[]): boolean => {\n // NOTE: Only supporting USD for now, the remaining checks are for future-proofing.\n if (!appraisals.every(appraisal => appraisal.currency == 'USD')) return false\n\n // Check every object in the array to ensure they all are in a supported currency.\n if (!appraisals.every(appraisal => isIso4217CurrencyCode(appraisal.currency))) return false\n\n return true\n}\n\nconst validateAppraisalConsistentCurrency = (appraisals: HashLeaseEstimate[]): boolean => {\n // Check if the array is empty or contains only one element, no need to compare.\n if (appraisals.length <= 1) return true\n\n // Get the currency of the first element to compare with others.\n const { currency } = appraisals[0]\n if (!currency) return false\n\n // Check every object in the array to ensure they all have the same currency.\n if (!appraisals.every(item => item.currency === currency)) return false\n\n return true\n}\n\nconst validateAppraisalWindow = (appraisals: HashLeaseEstimate[]): boolean => appraisals.every(validateDuration)\n\nexport const appraisalValidators = [\n validateAppraisalAmount,\n validateAppraisalCurrency,\n validateAppraisalConsistentCurrency,\n validateAppraisalWindow,\n]\n","import type { DurationFields } from '@xyo-network/xns-record-payload-plugins'\n\nconst FIVE_MINUTES = 1000 * 60 * 5\n\n/**\n * Validates that the current time is within the duration window, within a configurable a buffer\n * @param value The duration value\n * @param windowMs The window in milliseconds to allow for a buffer\n * @returns True if the duration is valid, false otherwise\n */\nexport const validateDuration = (value: Partial<DurationFields>, windowMs = FIVE_MINUTES): boolean => {\n const now = Date.now()\n if (!value.nbf || value.nbf > now) return false\n // If already expired (include for a 5 minute buffer to allow for a reasonable\n // minimum amount of time for the transaction to be processed)\n if (!value.exp || value.exp - now < windowMs) return false\n return true\n}\n","import type { Hash } from '@xylabs/hex'\nimport type { EscrowTerms } from '@xyo-network/payment-payload-plugins'\n\nimport { validateDuration } from './durationValidators.ts'\n\nexport type ValidEscrowTerms = Required<EscrowTerms>\n\nconst validateTermsAppraisals = (terms: EscrowTerms): terms is Required<EscrowTerms & { appraisals: Hash[] }> => {\n if (!terms.appraisals) return false\n if (terms.appraisals.length === 0) return false\n return true\n}\nconst validateTermsWindow = (terms: EscrowTerms): boolean => validateDuration(terms)\n\nexport const termsValidators = [\n validateTermsAppraisals,\n validateTermsWindow,\n]\n","import { assertEx } from '@xylabs/assert'\nimport { Hash } from '@xylabs/hex'\nimport { AbstractDiviner } from '@xyo-network/diviner-abstract'\nimport {\n asDivinerInstance, DivinerInstance, DivinerModuleEventData,\n} from '@xyo-network/diviner-model'\nimport { creatableModule } from '@xyo-network/module-model'\nimport {\n Discount,\n isDiscountWithMeta,\n isSubtotalWithMeta,\n PaymentTotalDivinerConfigSchema, PaymentTotalDivinerParams, Subtotal, Total, TotalSchema,\n} from '@xyo-network/payment-payload-plugins'\n\nimport { PaymentDiscountDiviner, PaymentDiscountDivinerInputType } from '../Discount/index.ts'\nimport { PaymentSubtotalDiviner, PaymentSubtotalDivinerInputType } from '../Subtotal/index.ts'\n\ntype InputType = PaymentDiscountDivinerInputType | PaymentSubtotalDivinerInputType\ntype OutputType = Subtotal | Discount | Total\n\n@creatableModule()\nexport class PaymentTotalDiviner<\n TParams extends PaymentTotalDivinerParams = PaymentTotalDivinerParams,\n TIn extends InputType = InputType,\n TOut extends OutputType = OutputType,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override configSchemas = [PaymentTotalDivinerConfigSchema]\n static override defaultConfigSchema: PaymentTotalDivinerConfigSchema = PaymentTotalDivinerConfigSchema\n\n protected async divineHandler(payloads: TIn[] = []): Promise<TOut[]> {\n const subtotalDiviner = await this.getPaymentSubtotalDiviner()\n const subtotalResult = await subtotalDiviner.divine(payloads)\n const subtotal = subtotalResult.find(isSubtotalWithMeta)\n if (!subtotal) return []\n const discountDiviner = await this.getPaymentDiscountsDiviner()\n const discountResult = await discountDiviner.divine(payloads)\n const discount = discountResult.find(isDiscountWithMeta)\n if (!discount) return []\n const { currency: subtotalCurrency } = subtotal\n const { currency: discountCurrency } = discount\n assertEx(subtotalCurrency === discountCurrency, () => `Subtotal currency ${subtotalCurrency} does not match discount currency ${discountCurrency}`)\n const amount = Math.max(0, subtotal.amount - discount.amount)\n const currency = subtotalCurrency\n const sources = [subtotal.$hash, discount.$hash] as Hash[]\n const total: Total = {\n amount, currency, sources, schema: TotalSchema,\n }\n return [subtotal, discount, total] as TOut[]\n }\n\n protected async getPaymentDiscountsDiviner(): Promise<PaymentDiscountDiviner> {\n const name = assertEx(this.config.paymentDiscountDiviner, () => 'Missing paymentDiscountDiviner in config')\n const mod = assertEx(await this.resolve(name), () => `Error resolving paymentDiscountDiviner: ${name}`)\n return assertEx(asDivinerInstance(mod), () => `Resolved module ${mod.address} not a valid Diviner`) as PaymentDiscountDiviner\n }\n\n protected async getPaymentSubtotalDiviner(): Promise<PaymentSubtotalDiviner> {\n const name = assertEx(this.config.paymentSubtotalDiviner, () => 'Missing paymentSubtotalDiviner in config')\n const mod = assertEx(await this.resolve(name), () => `Error resolving paymentSubtotalDiviner: ${name}`)\n return assertEx(asDivinerInstance(mod), () => `Resolved module ${mod.address} not a valid Diviner`) as PaymentSubtotalDiviner\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAAA,SAAS,YAAAA,iBAAgB;AACzB,SAAS,UAAAC,eAAc;AAEvB,SAA4B,2BAA2B;AACvD,SAAS,uBAAuB;AAChC,SAA0C,sCAAsC;AAChF;AAAA,EAEE;AAAA,OACK;AACP;AAAA,EACE;AAAA,OACK;AACP,SAAS,uBAAuB;AAChC,SAAS,kBAAAC,uBAAsB;AAE/B;AAAA,EAGe;AAAA,EACb;AAAA,EACA;AAAA,EAAe;AAAA,EAAa;AAAA,OACvB;;;ACtBP,SAAS,gBAAgB;AACzB,SAAS,cAAc;AAQvB;AAAA,EACE;AAAA,EAAgB;AAAA,EAAqB;AAAA,EACrC;AAAA,EACA;AAAA,OACK;AAEA,IAAM,eAAe,CAAC,YAAiC,YAAgC;AAE5F,QAAM,sBAAsB,WAAW,MAAM,eAAa,UAAU,aAAa,KAAK;AACtF,WAAS,qBAAqB,+BAA+B;AAC7D,QAAM,mBAAmB,QAAQ,IAAI,YAAW,QAAkC,QAAQ,EAAE,OAAO,MAAM,EAAE,MAAM,CAAAC,cAAYA,cAAa,KAAK;AAC/I,WAAS,kBAAkB,4BAA4B;AACvD,QAAM,QAAQ,WAAW,OAAO,CAAC,KAAK,cAAc,MAAM,UAAU,OAAO,CAAC;AAG5E,QAAM,wBAAwB,KAAK,IAAI,GAAG,QACvC,OAAO,YAAU,oBAAoB,MAAM,KAAK,CA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BAAgB,CAAC,+BAA+B;AAChE,cAXW,qBAWK,uBAAuD;AAX5D,sBAAN;AAAA,EADNC,iBAAgB;AAAA,GACJ;","names":["assertEx","exists","PayloadBuilder","currency","PayloadBuilder","assertEx","exists","PayloadBuilder","currency","AbstractDiviner","isHashLeaseEstimate","creatableModule","PayloadBuilder","isEscrowTerms","currency","AbstractDiviner","isEscrowTerms","PayloadBuilder","isHashLeaseEstimate","creatableModule","assertEx","AbstractDiviner","asDivinerInstance","creatableModule","AbstractDiviner","assertEx","currency","asDivinerInstance","creatableModule"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xyo-network/payment-plugin",
3
- "version": "3.0.19",
3
+ "version": "3.0.20",
4
4
  "description": "Typescript/Javascript Plugins for XYO Platform",
5
5
  "homepage": "https://xyo.network",
6
6
  "bugs": {
@@ -29,28 +29,31 @@
29
29
  "module": "dist/neutral/index.mjs",
30
30
  "types": "dist/neutral/index.d.ts",
31
31
  "dependencies": {
32
- "@xylabs/assert": "^4.0.9",
32
+ "@xylabs/assert": "^4.0.10",
33
33
  "@xylabs/exists": "^4.0.10",
34
34
  "@xylabs/hex": "^4.0.10",
35
- "@xyo-network/archivist-model": "^3.1.9",
36
- "@xyo-network/diviner-abstract": "^3.1.9",
37
- "@xyo-network/diviner-boundwitness-model": "^3.1.9",
38
- "@xyo-network/diviner-hash-lease": "^3.1.9",
39
- "@xyo-network/diviner-model": "^3.1.9",
40
- "@xyo-network/module-model": "^3.1.9",
41
- "@xyo-network/payload-builder": "^3.1.9",
42
- "@xyo-network/payload-model": "^3.1.9",
43
- "@xyo-network/payment-payload-plugins": "^3.0.19",
44
- "@xyo-network/xns-record-payload-plugins": "^3.0.19"
35
+ "@xyo-network/archivist-model": "^3.1.10",
36
+ "@xyo-network/diviner-abstract": "^3.1.10",
37
+ "@xyo-network/diviner-boundwitness-model": "^3.1.10",
38
+ "@xyo-network/diviner-hash-lease": "^3.1.10",
39
+ "@xyo-network/diviner-model": "^3.1.10",
40
+ "@xyo-network/module-model": "^3.1.10",
41
+ "@xyo-network/payload-builder": "^3.1.10",
42
+ "@xyo-network/payload-model": "^3.1.10",
43
+ "@xyo-network/payment-payload-plugins": "^3.0.20",
44
+ "@xyo-network/schema-payload-plugin": "^3.1.10",
45
+ "@xyo-network/xns-record-payload-plugins": "^3.0.20",
46
+ "ajv": "^8.17.1"
45
47
  },
46
48
  "devDependencies": {
47
49
  "@xylabs/ts-scripts-yarn3": "^4.0.7",
48
50
  "@xylabs/tsconfig": "^4.0.7",
49
- "@xyo-network/account": "^3.1.9",
50
- "@xyo-network/archivist-memory": "^3.1.9",
51
- "@xyo-network/boundwitness-builder": "^3.1.9",
52
- "@xyo-network/diviner-boundwitness-memory": "^3.1.9",
53
- "@xyo-network/node-memory": "^3.1.9",
51
+ "@xyo-network/account": "^3.1.10",
52
+ "@xyo-network/archivist-memory": "^3.1.10",
53
+ "@xyo-network/boundwitness-builder": "^3.1.10",
54
+ "@xyo-network/diviner-boundwitness-memory": "^3.1.10",
55
+ "@xyo-network/id-payload-plugin": "^3.1.10",
56
+ "@xyo-network/node-memory": "^3.1.10",
54
57
  "jest": "^29.7.0",
55
58
  "typescript": "^5.5.4",
56
59
  "vitest": "^2.0.5"
@@ -0,0 +1,64 @@
1
+ import type { Hash } from '@xylabs/hex'
2
+ import { PayloadBuilder } from '@xyo-network/payload-builder'
3
+ import type { Payload, WithMeta } from '@xyo-network/payload-model'
4
+ import type { Coupon } from '@xyo-network/payment-payload-plugins'
5
+ import type { SchemaPayload } from '@xyo-network/schema-payload-plugin'
6
+ import { isSchemaPayloadWithMeta } from '@xyo-network/schema-payload-plugin'
7
+ import type { ValidateFunction } from 'ajv'
8
+ import { Ajv } from 'ajv'
9
+
10
+ // TODO: Use our schema cache
11
+ const ajv = new Ajv({ strict: false }) // Create the Ajv instance once
12
+ const schemaCache = new Map() // Cache to store compiled validators
13
+
14
+ export const areConditionsFulfilled = async (coupon: Coupon, payloads: Payload[]): Promise<boolean> =>
15
+ (await findUnfulfilledConditions(coupon, payloads)).length === 0
16
+
17
+ // TODO: Should we separate conditions and payloads to prevent conflating data and "operands" (schemas to validate against data)?
18
+ /**
19
+ * Validates the conditions of a coupon against the provided payloads
20
+ * @param coupon The coupon to check
21
+ * @param payloads The associated payloads (containing the conditions and data to validate the conditions against)
22
+ * @returns The unfulfilled condition hashes
23
+ */
24
+ export const findUnfulfilledConditions = async (coupon: Coupon, payloads: Payload[]): Promise<Hash[]> => {
25
+ const unfulfilledConditions: Hash[] = []
26
+ // If there are no conditions, then they are fulfilled
27
+ if (!coupon.conditions || coupon.conditions.length === 0) return unfulfilledConditions
28
+ const hashMap = await PayloadBuilder.toAllHashMap(payloads)
29
+ // Find all the conditions
30
+ const conditions = coupon.conditions.map(hash => hashMap[hash]).filter(isSchemaPayloadWithMeta) as WithMeta<SchemaPayload>[]
31
+ // Not all conditions were found
32
+ if (conditions.length !== coupon.conditions.length) {
33
+ const missing = coupon.conditions.filter(hash => !hashMap[hash])
34
+ unfulfilledConditions.push(...missing)
35
+ return unfulfilledConditions
36
+ }
37
+
38
+ // Test each condition
39
+ for (const hash of coupon.conditions) {
40
+ let validator: ValidateFunction
41
+
42
+ // Check if the schema is already cached
43
+ if (schemaCache.has(hash)) {
44
+ validator = schemaCache.get(hash)
45
+ } else {
46
+ const payload = hashMap[hash]
47
+ const definition = isSchemaPayloadWithMeta(payload) ? payload.definition : undefined
48
+ if (definition) {
49
+ // Compile and cache the validator
50
+ validator = ajv.compile(definition)
51
+ schemaCache.set(hash, validator)
52
+
53
+ // Validate the payload
54
+ } else {
55
+ unfulfilledConditions.push(hash)
56
+ continue
57
+ }
58
+ }
59
+ if (!validator(payloads)) unfulfilledConditions.push(hash)
60
+ }
61
+
62
+ // All conditions passed
63
+ return unfulfilledConditions
64
+ }
@@ -1 +1,2 @@
1
1
  export * from './applyCoupons.ts'
2
+ export * from './findUnfulfilledConditions.ts'