Skip to content

Commit

Permalink
fix(sdk): split EventAction input params into two objects
Browse files Browse the repository at this point in the history
This allows for cleaner separations of concerns
  • Loading branch information
topocount committed Sep 23, 2024
1 parent 8b6cf3d commit 44504ed
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 54 deletions.
4 changes: 3 additions & 1 deletion packages/sdk/src/Actions/EventAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ describe('EventAction', () => {
transactionIndex: 0,
} as Log;
const action = await loadFixture(cloneEventAction(fixtures, erc721));
expect(await action.validateActionSteps({ logs: [log] })).toBe(true);
expect(
await action.validateActionSteps({ logParams: { logs: [log] } }),
).toBe(true);
});
});
90 changes: 37 additions & 53 deletions packages/sdk/src/Actions/EventAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
type AbiFunction,
type Address,
type ContractEventName,
type ContractFunctionName,
type Hex,
type Log,
type PublicClient,
Expand Down Expand Up @@ -65,6 +66,23 @@ export type {
PrimitiveType,
};

type ReadEventActionParams<
fnName extends ContractFunctionName<typeof eventActionAbi, 'pure' | 'view'>,
> = ReadParams<typeof eventActionAbi, fnName>;

type LogParams = GetLogsParams<Abi, ContractEventName<Abi>> & {
knownEvents?: Record<Hex, AbiEvent>;
logs?: Log[];
txHash?: Hex;
};

type validateInputParams<
fnName extends ContractFunctionName<typeof eventActionAbi, 'pure' | 'view'>,
> = {
getterParams?: ReadEventActionParams<fnName>;
logParams?: LogParams;
};

/**
* A generic event action
*
Expand Down Expand Up @@ -108,12 +126,12 @@ export class EventAction extends DeployableTarget<
* @public
* @async
* @param {number} index The index of the action event to retrieve
* @param {?ReadParams<typeof eventActionAbi, 'getActionStep'>} [params]
* @param {?ReadEventActionParams<'getActionStep'>} [params]
* @returns {Promise<ActionStep>}
*/
public async getActionStep(
index: number,
params?: ReadParams<typeof eventActionAbi, 'getActionStep'>,
params?: ReadEventActionParams<'getActionStep'>,
) {
const steps = await this.getActionSteps(params);
return steps.at(index);
Expand All @@ -124,11 +142,11 @@ export class EventAction extends DeployableTarget<
*
* @public
* @async
* @param {?ReadParams<typeof eventActionAbi, 'getActionSteps'>} [params]
* @param {?ReadEventActionParams<'getActionSteps'>} [params]
* @returns {Promise<ActionStep[]>}
*/
public async getActionSteps(
params?: ReadParams<typeof eventActionAbi, 'getActionSteps'>,
params?: ReadEventActionParams<'getActionSteps'>,
) {
const steps = (await readEventActionGetActionSteps(this._config, {
address: this.assertValidAddress(),
Expand All @@ -144,11 +162,11 @@ export class EventAction extends DeployableTarget<
*
* @public
* @async
* @param {?ReadParams<typeof eventActionAbi, 'getActionStepsCount'>} [params]
* @param {?ReadEventActionParams<'getActionStepsCount'>} [params]
* @returns {Promise<bigint>}
*/
public async getActionStepsCount(
params?: ReadParams<typeof eventActionAbi, 'getActionStepsCount'>,
params?: ReadEventActionParams<'getActionStepsCount'>,
) {
const steps = await this.getActionSteps(params);
return steps.length;
Expand All @@ -159,17 +177,16 @@ export class EventAction extends DeployableTarget<
*
* @public
* @async
* @param {?ReadParams<typeof eventActionAbi, 'getActionClaimant'>} [params]
* @param {?ReadEventActionParams<'getActionClaimant'>} [params]
* @returns {Promise<ActionClaimant>}
*/
public async getActionClaimant(
params?: ReadParams<typeof eventActionAbi, 'getActionClaimant'>,
params?: ReadEventActionParams<'getActionClaimant'>,
) {
const result = (await readEventActionGetActionClaimant(this._config, {
address: this.assertValidAddress(),
...this.optionallyAttachAccount(),
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
...params,
})) as RawActionClaimant;
return fromRawActionStep(result);
}
Expand Down Expand Up @@ -220,23 +237,15 @@ export class EventAction extends DeployableTarget<
*
* @public
* @async
* @param {?ReadParams<typeof eventActionAbi, 'getActionSteps'> &
* GetLogsParams<Abi, ContractEventName<Abi>> & {
* knownEvents?: Record<Hex, AbiEvent>;
* logs?: Log[];
* }} [params]
* @param {?validateInputParams<'getActionSteps'>} [params]
* @returns {Promise<boolean>}
*/
public async validateActionSteps(
params?: ReadParams<typeof eventActionAbi, 'getActionSteps'> &
GetLogsParams<Abi, ContractEventName<Abi>> & {
knownEvents?: Record<Hex, AbiEvent>;
logs?: Log[];
},
params?: validateInputParams<'getActionSteps'>,
) {
const actionSteps = await this.getActionSteps(params);
const actionSteps = await this.getActionSteps(params?.getterParams);
for (const actionStep of actionSteps) {
if (!(await this.isActionStepValid(actionStep, params))) {
if (!(await this.isActionStepValid(actionStep, params?.logParams))) {
return false;
}
}
Expand All @@ -250,20 +259,10 @@ export class EventAction extends DeployableTarget<
* @public
* @async
* @param {ActionStep} actionStep
* @param {?GetLogsParams<Abi, ContractEventName<Abi>> & {
* knownEvents?: Record<Hex, AbiEvent>;
* logs?: Log[];
* }} [params]
* @param {?LogParams} [params]
* @returns {Promise<boolean>}
*/
public async isActionStepValid(
actionStep: ActionStep,
params?: GetLogsParams<Abi, ContractEventName<Abi>> & {
knownEvents?: Record<Hex, AbiEvent>;
logs?: Log[];
txHash?: Hex;
},
) {
public async isActionStepValid(actionStep: ActionStep, params?: LogParams) {
if (actionStep.signatureType === SignatureType.EVENT) {
return await this.isActionEventValid(actionStep, params);
}
Expand All @@ -280,19 +279,10 @@ export class EventAction extends DeployableTarget<
* @public
* @async
* @param {ActionStep} actionStep
* @param {?GetLogsParams<Abi, ContractEventName<Abi>> & {
* knownEvents?: Record<Hex, AbiEvent>;
* logs?: Log[];
* }} [params]
* @param {?LogParams} [params]
* @returns {Promise<boolean>}
*/
public async isActionEventValid(
actionStep: ActionStep,
params?: GetLogsParams<Abi, ContractEventName<Abi>> & {
knownEvents?: Record<Hex, AbiEvent>;
logs?: Log[];
},
) {
public async isActionEventValid(actionStep: ActionStep, params?: LogParams) {
const criteria = actionStep.actionParameter;
const signature = actionStep.signature;
let event: AbiEvent;
Expand Down Expand Up @@ -330,18 +320,12 @@ export class EventAction extends DeployableTarget<
* @public
* @async
* @param {ActionStep} actionStep
* @param {?GetLogsParams<Abi, ContractEventName<Abi>> & {
* knownEvents?: Record<Hex, AbiEvent>;
* txHash?: Hex;
* }} [params]
* @param {?LogParams} [params]
* @returns {Promise<boolean>}
*/
public async isActionFunctionValid(
actionStep: ActionStep,
params?: GetLogsParams<Abi, ContractEventName<Abi>> & {
knownEvents?: Record<Hex, AbiEvent>;
txHash?: Hex;
},
params?: LogParams,
) {
const criteria = actionStep.actionParameter;
const signature = actionStep.signature;
Expand Down

0 comments on commit 44504ed

Please sign in to comment.