Skip to content

Commit

Permalink
[BOOST-4707] support chainid in EventAction
Browse files Browse the repository at this point in the history
The chainid typing is a bit dicey. it's a uint256 but treated as a
number by viem, so I have upgrade and downgrade conversion functions
implemented to help with this
  • Loading branch information
topocount committed Sep 20, 2024
1 parent dc15116 commit 1c095e2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
2 changes: 2 additions & 0 deletions packages/sdk/src/Actions/Action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ export function basicErc721TransferAction(
): EventActionPayloadSimple {
return {
actionClaimant: {
chainid: 31337,
signatureType: SignatureType.EVENT,
signature: selectors['Transfer(address,address,uint256)'] as Hex,
fieldIndex: 2,
targetContract: erc721.assertValidAddress(),
},
actionSteps: [
{
chainid: 31337,
signature: selectors['Transfer(address,address,uint256)'] as Hex,
signatureType: SignatureType.EVENT,
actionType: 0,
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/src/Actions/EventAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ function basicErc721TransferAction(
signature: eventSelectors['Transfer(address,address,uint256)'] as Hex,
fieldIndex: 2,
targetContract: erc721.assertValidAddress(),
chainid: defaultOptions.config.chains[0].id,
},
actionSteps: [
{
signature: eventSelectors['Transfer(address,address,uint256)'] as Hex,
signatureType: SignatureType.EVENT,
actionType: 0,
targetContract: erc721.assertValidAddress(),
chainid: defaultOptions.config.chains[0].id,
actionParameter: {
filterType: FilterType.EQUAL,
fieldType: PrimitiveType.ADDRESS,
Expand Down
13 changes: 8 additions & 5 deletions packages/sdk/src/Actions/EventAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ import {
UnrecognizedFilterTypeError,
} from '../errors';
import {
type ActionClaimant,
type ActionStep,
type Criteria,
type EventActionPayload,
type EventActionPayloadRaw,
FilterType,
type GetLogsParams,
PrimitiveType,
type RawActionClaimant,
type RawActionStep,
type ReadParams,
RegistryType,
type WriteParams,
dedupeActionSteps,
fromRawActionStep,
isEventActionPayloadSimple,
prepareEventActionPayload,
} from '../utils';
Expand Down Expand Up @@ -127,8 +129,8 @@ export class EventAction extends DeployableTarget<
...this.optionallyAttachAccount(),
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
})) as ActionStep[];
return dedupeActionSteps(steps);
})) as RawActionStep[];
return dedupeActionSteps(steps.map(fromRawActionStep));
}

/**
Expand Down Expand Up @@ -157,12 +159,13 @@ export class EventAction extends DeployableTarget<
public async getActionClaimant(
params?: ReadParams<typeof eventActionAbi, 'getActionClaimant'>,
) {
return readEventActionGetActionClaimant(this._config, {
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),
}) as Promise<ActionClaimant>;
})) as RawActionClaimant;
return fromRawActionStep(result);
}

/**
Expand Down
50 changes: 45 additions & 5 deletions packages/sdk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ export interface ActionClaimant {
* @type {Address}
*/
targetContract: Address;
/**
* The chain id of the target contract.
* @type {number}
*/
chainid: number;
}

/**
Expand Down Expand Up @@ -396,13 +401,41 @@ export interface ActionStep {
* @type {Address}
*/
targetContract: Address;
/**
* The chain id of the target contract.
* @type {number}
*/
chainid: number;
/**
* The criteria used for this action step.
*
* @type {Criteria}
*/
actionParameter: Criteria;
}
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
export type RawActionStep = Overwrite<ActionStep, { chainid: bigint }>;
export type RawActionClaimant = Overwrite<ActionClaimant, { chainid: bigint }>;

export function toRawActionStep<T extends ActionStep | ActionClaimant>(obj: T) {
return {
...obj,
chainid: BigInt(obj.chainid),
};
}

export function fromRawActionStep<T extends RawActionStep | RawActionClaimant>(
obj: T,
) {
if (obj.chainid > BigInt(Number.MAX_SAFE_INTEGER)) {
throw new Error('Chain ID exceeds max safe integer');
}

return {
...obj,
chainid: Number(obj.chainid),
};
}

/**
* You can either supply a simplified version of the payload, or one that explicitly declares action steps.
Expand Down Expand Up @@ -501,6 +534,8 @@ export const prepareEventActionPayload = ({
actionStepThree,
actionStepFour,
}: EventActionPayloadRaw) => {
// note chainIds are technically uint256 but viem treats them (safely) as numbers,
// so we encode them as uint32 here to avoid downcast issues
return encodeAbiParameters(
[
{
Expand All @@ -515,6 +550,7 @@ export const prepareEventActionPayload = ({
{ type: 'bytes32', name: 'signature' },
{ type: 'uint8', name: 'fieldIndex' },
{ type: 'address', name: 'targetContract' },
{ type: 'uint256', name: 'chainid' },
],
},
{
Expand All @@ -525,6 +561,7 @@ export const prepareEventActionPayload = ({
{ type: 'uint8', name: 'signatureType' },
{ type: 'uint8', name: 'actionType' },
{ type: 'address', name: 'targetContract' },
{ type: 'uint256', name: 'chainid' },
{
type: 'tuple',
name: 'actionParameter',
Expand All @@ -545,6 +582,7 @@ export const prepareEventActionPayload = ({
{ type: 'uint8', name: 'signatureType' },
{ type: 'uint8', name: 'actionType' },
{ type: 'address', name: 'targetContract' },
{ type: 'uint256', name: 'chainid' },
{
type: 'tuple',
name: 'actionParameter',
Expand All @@ -565,6 +603,7 @@ export const prepareEventActionPayload = ({
{ type: 'uint8', name: 'signatureType' },
{ type: 'uint8', name: 'actionType' },
{ type: 'address', name: 'targetContract' },
{ type: 'uint256', name: 'chainid' },
{
type: 'tuple',
name: 'actionParameter',
Expand All @@ -585,6 +624,7 @@ export const prepareEventActionPayload = ({
{ type: 'uint8', name: 'signatureType' },
{ type: 'uint8', name: 'actionType' },
{ type: 'address', name: 'targetContract' },
{ type: 'uint256', name: 'chainid' },
{
type: 'tuple',
name: 'actionParameter',
Expand All @@ -602,11 +642,11 @@ export const prepareEventActionPayload = ({
],
[
{
actionClaimant,
actionStepOne,
actionStepTwo,
actionStepThree,
actionStepFour,
actionClaimant: toRawActionStep(actionClaimant),
actionStepOne: toRawActionStep(actionStepOne),
actionStepTwo: toRawActionStep(actionStepTwo),
actionStepThree: toRawActionStep(actionStepThree),
actionStepFour: toRawActionStep(actionStepFour),
},
],
);
Expand Down
4 changes: 4 additions & 0 deletions packages/sdk/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ export function makeMockEventActionPayload(
signatureType: SignatureType.EVENT,
actionType: 0,
targetContract: erc20Address,
chainid: 31337,
actionParameter: {
filterType: FilterType.EQUAL,
fieldType: PrimitiveType.ADDRESS,
Expand All @@ -655,6 +656,7 @@ export function makeMockEventActionPayload(

return {
actionClaimant: {
chainid: 31337,
signatureType: SignatureType.EVENT,
signature:
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
Expand All @@ -674,6 +676,7 @@ export function makeMockFunctionActionPayload(
) {
const step: ActionStep = {
signature: '0x40c10f19',
chainid: 31337,
signatureType: SignatureType.FUNC,
actionType: 0,
targetContract: erc20Address,
Expand All @@ -691,6 +694,7 @@ export function makeMockFunctionActionPayload(
signature: '0x40c10f19',
fieldIndex: 0,
targetContract: erc20Address,
chainid: 31337,
},
actionStepOne: step,
actionStepTwo: step,
Expand Down

0 comments on commit 1c095e2

Please sign in to comment.