Skip to content

Commit

Permalink
refactor(sdk): refactor entitlement data ABI encoding and decoding (#675
Browse files Browse the repository at this point in the history
)
  • Loading branch information
shuhuiluo authored Aug 12, 2024
1 parent 4bdacfa commit f522a88
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions packages/web3/src/entitlement.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { AbiParameter, AbiFunction } from 'abitype'
import type { ExtractAbiFunction } from 'abitype'
import { IRuleEntitlementBase, IRuleEntitlementAbi } from './v3/IRuleEntitlementShim'

import {
createPublicClient,
http,
decodeAbiParameters,
encodeAbiParameters,
getAbiItem,
Hex,
PublicClient,
} from 'viem'

Expand Down Expand Up @@ -197,36 +199,36 @@ export const getOperationTree = async (address: Address, roleId: bigint): Promis
return postOrderArrayToTree(operations)
}

const encodeRuleDataInputs: readonly AbiParameter[] | undefined = (
Object.values(IRuleEntitlementAbi).find((abi) => abi.name === 'encodeRuleData') as
| AbiFunction
| undefined
)?.inputs

export function encodeEntitlementData(ruleData: IRuleEntitlementBase.RuleDataStruct): Address {
if (!encodeRuleDataInputs) {
throw new Error('setRuleDataInputs not found')
const encodeRuleDataAbi: ExtractAbiFunction<typeof IRuleEntitlementAbi, 'encodeRuleData'> =
getAbiItem({
abi: IRuleEntitlementAbi,
name: 'encodeRuleData',
})

if (!encodeRuleDataAbi) {
throw new Error('encodeRuleData ABI not found')
}
return encodeAbiParameters(encodeRuleDataInputs, [ruleData])
// @ts-ignore
return encodeAbiParameters(encodeRuleDataAbi.inputs, [ruleData])
}

const getRuleDataOutputs: readonly AbiParameter[] | undefined = (
Object.values(IRuleEntitlementAbi).find((abi) => abi.name === 'getRuleData') as
| AbiFunction
| undefined
)?.outputs
export function decodeEntitlementData(entitlementData: Hex): IRuleEntitlementBase.RuleDataStruct[] {
const getRuleDataAbi: ExtractAbiFunction<typeof IRuleEntitlementAbi, 'getRuleData'> =
getAbiItem({
abi: IRuleEntitlementAbi,
name: 'getRuleData',
})

export function decodeEntitlementData(
entitlementData: Address,
): IRuleEntitlementBase.RuleDataStruct[] {
if (!getRuleDataOutputs) {
throw new Error('getRuleDataOutputs not found')
if (!getRuleDataAbi) {
throw new Error('getRuleData ABI not found')
}
return decodeAbiParameters(
getRuleDataOutputs,
getRuleDataAbi.outputs,
entitlementData,
) as IRuleEntitlementBase.RuleDataStruct[]
) as unknown as IRuleEntitlementBase.RuleDataStruct[]
}

export function ruleDataToOperations(data: IRuleEntitlementBase.RuleDataStruct[]): Operation[] {
if (data.length === 0) {
return []
Expand Down

0 comments on commit f522a88

Please sign in to comment.