Skip to content

Commit

Permalink
Fixup typescript in entitlement.ts (#777)
Browse files Browse the repository at this point in the history
1) logically group the type definitions
2) use typescript to infer the supported logical operations type so when
we implement another we only need to add it in one place
3) fix up the other cast to be the of the supported type
  • Loading branch information
texuf authored Aug 15, 2024
1 parent 36ab061 commit 0fb6320
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions packages/web3/src/entitlement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export enum LogicalOperationType {
AND,
OR,
}

export type ContractOperation = {
opType: OperationType
index: number
Expand Down Expand Up @@ -123,6 +122,8 @@ export const NoopRuleData = {
type EntitledWalletOrZeroAddress = string

export type LogicalOperation = OrOperation | AndOperation
export type SupportedLogicalOperationType = LogicalOperation['logicalType']

export type Operation = CheckOperation | OrOperation | AndOperation | NoOperation

function isCheckOperation(operation: Operation): operation is CheckOperation {
Expand All @@ -137,15 +138,15 @@ function isAndOperation(operation: LogicalOperation): operation is AndOperation
return operation.logicalType === LogicalOperationType.AND
}

function isOrOperation(operation: LogicalOperation): operation is OrOperation {
return operation.logicalType === LogicalOperationType.OR
}

const publicClient: PublicClient = createPublicClient({
chain: mainnet,
transport: http(),
})

function isOrOperation(operation: LogicalOperation): operation is OrOperation {
return operation.logicalType === LogicalOperationType.OR
}

export function postOrderArrayToTree(operations: Operation[]): Operation {
const stack: Operation[] = []

Expand Down Expand Up @@ -258,13 +259,10 @@ export function ruleDataToOperations(data: IRuleEntitlementBase.RuleDataStruct[]
const logicalOperation = firstData.logicalOperations[operation.index]
decodedOperations.push({
opType: OperationType.LOGICAL,
logicalType: logicalOperation.logOpType as
| LogicalOperationType.AND
| LogicalOperationType.OR,

logicalType: logicalOperation.logOpType,
leftOperation: decodedOperations[logicalOperation.leftOperationIndex],
rightOperation: decodedOperations[logicalOperation.rightOperationIndex],
})
} satisfies LogicalOperation)
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
} else if (operation.opType === OperationType.NONE) {
decodedOperations.push(NoopOperation)
Expand Down Expand Up @@ -608,15 +606,13 @@ export async function evaluateTree(
}
}

type AndOr = LogicalOperationType.AND | LogicalOperationType.OR

// These two methods are used to create a rule data struct for an external token or NFT
// checks for testing.
export function createExternalTokenStruct(
addresses: Address[],
options?: {
checkOptions?: Partial<Omit<ContractCheckOperation, 'address'>>
logicalOp?: AndOr
logicalOp?: SupportedLogicalOperationType
},
) {
if (addresses.length === 0) {
Expand All @@ -635,7 +631,7 @@ export function createExternalNFTStruct(
addresses: Address[],
options?: {
checkOptions?: Partial<Omit<ContractCheckOperation, 'address'>>
logicalOp?: AndOr
logicalOp?: SupportedLogicalOperationType
},
) {
if (addresses.length === 0) {
Expand All @@ -662,7 +658,7 @@ export function createOperationsTree(
checkOp: (Omit<ContractCheckOperation, 'threshold'> & {
threshold?: bigint
})[],
logicalOp: AndOr = LogicalOperationType.OR,
logicalOp: SupportedLogicalOperationType = LogicalOperationType.OR,
): IRuleEntitlementBase.RuleDataStruct {
if (checkOp.length === 0) {
return {
Expand Down

0 comments on commit 0fb6320

Please sign in to comment.