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 16, 2024
1 parent cfd6bc1 commit 832c1b2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
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: selectors['Transfer(address,address,uint256)'] as Hex,
fieldIndex: 2,
targetContract: erc721.assertValidAddress(),
chainid: defaultOptions.config.chains[0].id,
},
actionSteps: [
{
signature: selectors['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 @@ -33,18 +33,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 @@ -121,8 +123,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 @@ -151,12 +153,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: 'bytes4', 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
2 changes: 2 additions & 0 deletions packages/sdk/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ export function makeMockEventActionPayload(
signatureType: SignatureType.EVENT,
actionType: 0,
targetContract: erc20Address,
chainid: 31337,
actionParameter: {
filterType: FilterType.EQUAL,
fieldType: PrimitiveType.ADDRESS,
Expand All @@ -658,6 +659,7 @@ export function makeMockEventActionPayload(
signature: '0xddf252ad',
fieldIndex: 0,
targetContract: erc20Address,
chainid: 31337,
},
actionStepOne: step,
actionStepTwo: step,
Expand Down

0 comments on commit 832c1b2

Please sign in to comment.