Skip to content

Commit

Permalink
Merge pull request #46 from XYOracleNetwork/feature/subtotal-diviner-…
Browse files Browse the repository at this point in the history
…in-discounts

Coupon Condition Helpers
  • Loading branch information
JoelBCarter authored Sep 19, 2024
2 parents b7ef864 + 1fdfb25 commit 53e167c
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-disable unicorn/no-thenable */
import { SchemaSchema } from '@xyo-network/schema-payload-plugin'

import type { Condition } from '../types/index.ts'

/**
* Creates a coupon condition that requires an appraisal amount below a certain value
* @param maximumAppraisalAmount The maximum appraisal amount
* @returns A condition that requires an appraisal amount below a certain value
*/
export const createConditionForMaximumAppraisalAmount = (maximumAppraisalAmount: number): Condition => {
return {
schema: SchemaSchema,
definition: {
allOf: [
{
type: 'array',
contains: {
type: 'object',
properties: {
schema: { type: 'string', const: 'network.xyo.escrow.terms' },
appraisals: { type: 'array', minItems: 1 },
},
required: ['schema', 'appraisals'],
},
},
{
type: 'array',
contains: {
type: 'object',
properties: { schema: { type: 'string', const: 'network.xyo.hash.lease.estimate' } },
required: ['schema'],
},
items: {
type: 'object',
if: { properties: { schema: { type: 'string', const: 'network.xyo.hash.lease.estimate' } } },
then: { properties: { price: { type: 'number', maximum: maximumAppraisalAmount } }, required: ['price'] },
},
},
],
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { SchemaSchema } from '@xyo-network/schema-payload-plugin'

import type { Condition } from '../types/index.ts'

/**
* Returns a coupon condition that requires a minimum quantity of assets
* @param minimumAssetQuantity The minimum quantity of assets required
* @returns A condition that requires a minimum quantity of assets
*/
export const createConditionForMinimumAssetQuantity = (minimumAssetQuantity: number): Condition => {
return {
schema: SchemaSchema,
definition: {
type: 'array',
contains: {
type: 'object',
properties: {
schema: { type: 'string', const: 'network.xyo.escrow.terms' },
assets: { type: 'array', minItems: minimumAssetQuantity },
},
required: ['schema', 'assets'],
},
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { Address } from '@xylabs/hex'
import { SchemaSchema } from '@xyo-network/schema-payload-plugin'

import type { Condition } from '../types/index.ts'

export type BuyerCondition = Condition & {
definition: {
contains: {
properties: {
buyer: {
items: {
const: Address
type: 'string'
}
minItems: 1
type: 'array'
}
schema: { const: 'network.xyo.escrow.terms'; type: 'string' }
}
required: ['schema', 'buyer']
type: 'object'
}
type: 'array'
}
schema: SchemaSchema
}

/**
* Creates a coupon condition that requires a specific buyer
* @param buyer The buyer's address
* @returns A coupon condition that requires a specific buyer
*/
export const createConditionForRequiredBuyer = (buyer: Address): BuyerCondition => {
return {
schema: SchemaSchema,
definition: {
type: 'array',
contains: {
type: 'object',
properties: {
schema: { type: 'string', const: 'network.xyo.escrow.terms' },
buyer: {
type: 'array', items: { type: 'string', const: buyer }, minItems: 1,
},
},
required: ['schema', 'buyer'],
},
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './AppraisalAmountCondition.ts'
export * from './AssetQuantityCondition.ts'
export * from './BuyerCondition.ts'
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './Conditions/index.ts'
export * from './Coupons/index.ts'
export * from './Payload.ts'
export * from './Schema.ts'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
/* eslint-disable unicorn/no-thenable */
import type { Address } from '@xylabs/hex'
import type { WalletInstance } from '@xyo-network/account'
import { HDWallet } from '@xyo-network/account'
import { type HashLeaseEstimate, HashLeaseEstimateSchema } from '@xyo-network/diviner-hash-lease'
import type { IdPayload } from '@xyo-network/id-payload-plugin'
import { IdSchema } from '@xyo-network/id-payload-plugin'
import { PayloadBuilder } from '@xyo-network/payload-builder'
import type { Coupon, EscrowTerms } from '@xyo-network/payment-payload-plugins'
import { EscrowTermsSchema, FixedAmountCouponSchema } from '@xyo-network/payment-payload-plugins'
import type {
BuyerCondition,
Condition, Coupon, EscrowTerms,
} from '@xyo-network/payment-payload-plugins'
import {
createConditionForMaximumAppraisalAmount,
createConditionForMinimumAssetQuantity, createConditionForRequiredBuyer, EscrowTermsSchema, FixedAmountCouponSchema,
} from '@xyo-network/payment-payload-plugins'
import type { SchemaPayload } from '@xyo-network/schema-payload-plugin'
import { SchemaSchema } from '@xyo-network/schema-payload-plugin'
import {
beforeEach, describe, it,
} from 'vitest'
Expand All @@ -24,68 +30,9 @@ describe('findUnfulfilledConditions', () => {
}

// Conditions
const CONDITION_REQUIRES_BUYING_TWO = {
schema: SchemaSchema,
definition: {
type: 'array',
contains: {
type: 'object',
properties: {
schema: { type: 'string', const: 'network.xyo.escrow.terms' },
assets: { type: 'array', minItems: 2 },
},
required: ['schema', 'assets'],
},
},
}
const CONDITION_REQUIRES_APPRAISAL_DOES_NOT_EXCEED_AMOUNT = {
schema: SchemaSchema,
definition: {
allOf: [
{
type: 'array',
contains: {
type: 'object',
properties: {
schema: { type: 'string', const: 'network.xyo.escrow.terms' },
appraisals: { type: 'array', minItems: 1 },
},
required: ['schema', 'appraisals'],
},
},
{
type: 'array',
contains: {
type: 'object',
properties: { schema: { type: 'string', const: 'network.xyo.hash.lease.estimate' } },
required: ['schema'],
},
items: {
type: 'object',
if: { properties: { schema: { type: 'string', const: 'network.xyo.hash.lease.estimate' } } },
then: { properties: { price: { type: 'number', maximum: 20 } }, required: ['price'] },
},
},
],
},
}

const CONDITION_FOR_SPECIFIC_BUYER = {
schema: SchemaSchema,
definition: {
type: 'array',
contains: {
type: 'object',
properties: {
schema: { type: 'string', const: 'network.xyo.escrow.terms' },
buyer: {
type: 'array', items: { type: 'string', const: '' }, minItems: 1,
},
},
required: ['schema', 'buyer'],
},
},
}
const CONDITION_REQUIRES_BUYING_TWO: Condition = createConditionForMinimumAssetQuantity(2)
const CONDITION_REQUIRES_APPRAISAL_DOES_NOT_EXCEED_AMOUNT: Condition = createConditionForMaximumAppraisalAmount(20)
const CONDITION_FOR_SPECIFIC_BUYER: BuyerCondition = createConditionForRequiredBuyer('TODO: Replace in beforeAll' as Address)

const allConditions: SchemaPayload[] = [
CONDITION_REQUIRES_BUYING_TWO,
Expand Down

0 comments on commit 53e167c

Please sign in to comment.