Skip to content

Commit

Permalink
Require version
Browse files Browse the repository at this point in the history
Add unit test.
Update types.
  • Loading branch information
matthewwalsh0 committed Feb 11, 2025
1 parent b8f410d commit 64c48e2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 33 deletions.
15 changes: 15 additions & 0 deletions src/methods/wallet-get-calls-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('wallet_getCallsStatus', () => {

await callMethod();
expect(response.result?.status).toBe('PENDING');
expect(response.result?.receipts).toBeNull();
});

it('returns receipts', async () => {
Expand Down Expand Up @@ -127,4 +128,18 @@ describe('wallet_getCallsStatus', () => {
0 - Expected a nonempty string but received an empty one]
`);
});

it('removes excess properties from receipts', async () => {
getTransactionReceiptsByBatchIdMock.mockResolvedValue([
{
...RECEIPT_MOCK,
extra: 'value1',
logs: [{ ...RECEIPT_MOCK.logs[0], extra2: 'value2' }],
} as never,
]);

await callMethod();

expect(response.result?.receipts).toStrictEqual([RECEIPT_MOCK]);
});
});
28 changes: 14 additions & 14 deletions src/methods/wallet-get-calls-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ import { validateParams } from '../utils/validation';
const GetCallsStatusStruct = tuple([nonempty(string())]);

const GetCallsStatusReceiptStruct = object({
logs: array(
object({
address: HexChecksumAddressStruct,
data: StrictHexStruct,
topics: array(StrictHexStruct),
}),
logs: optional(
array(
object({
address: optional(HexChecksumAddressStruct),
data: optional(StrictHexStruct),
topics: optional(array(StrictHexStruct)),
}),
),
),
status: StrictHexStruct,
status: optional(StrictHexStruct),
chainId: optional(StrictHexStruct),
blockHash: StrictHexStruct,
blockNumber: StrictHexStruct,
gasUsed: StrictHexStruct,
transactionHash: StrictHexStruct,
blockHash: optional(StrictHexStruct),
blockNumber: optional(StrictHexStruct),
gasUsed: optional(StrictHexStruct),
transactionHash: optional(StrictHexStruct),
});

export type GetCallsStatusParams = Infer<typeof GetCallsStatusStruct>;
Expand Down Expand Up @@ -62,9 +64,7 @@ export async function walletGetCallsStatus(
throw rpcErrors.methodNotSupported();
}

if (!validateParams(req.params, GetCallsStatusStruct)) {
return;
}
validateParams(req.params, GetCallsStatusStruct);

const batchId = req.params[0];
const rawReceipts = await getTransactionReceiptsByBatchId(batchId, req);
Expand Down
4 changes: 1 addition & 3 deletions src/methods/wallet-get-capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export async function walletGetCapabilities(
throw rpcErrors.methodNotSupported();
}

if (!validateParams(req.params, GetCapabilitiesStruct)) {
return;
}
validateParams(req.params, GetCapabilitiesStruct);

const address = req.params[0];
const capabilities = await getCapabilities(address, req);
Expand Down
7 changes: 1 addition & 6 deletions src/methods/wallet-send-calls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const ID_MOCK = '1234-5678';
const REQUEST_MOCK = {
params: [
{
version: '1.0',
from: ADDRESS_MOCK,
chainId: HEX_MOCK,
calls: [
Expand Down Expand Up @@ -65,12 +66,6 @@ describe('wallet_sendCalls', () => {
expect(response.result).toStrictEqual(ID_MOCK);
});

it('supports version', async () => {
params[0].version = '1.0.0';
await callMethod();
expect(processSendCallsMock).toHaveBeenCalledWith(params[0], request);
});

it('supports capabilities', async () => {
params[0].capabilities = { test: 'value' };
await callMethod();
Expand Down
7 changes: 2 additions & 5 deletions src/methods/wallet-send-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {

const SendCallsStruct = tuple([
object({
version: optional(nonempty(string())),
version: nonempty(string()),
from: HexChecksumAddressStruct,
chainId: optional(StrictHexStruct),
calls: array(
Expand Down Expand Up @@ -60,10 +60,7 @@ export async function walletSendCalls(
throw rpcErrors.methodNotSupported();
}

if (!validateParams(req.params, SendCallsStruct)) {
// Not possible as throws.
return;
}
validateParams(req.params, SendCallsStruct);

const params = req.params[0];

Expand Down
4 changes: 2 additions & 2 deletions src/utils/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ describe('Validation Utils', () => {
});

describe('validateParams', () => {
it('returns true if superstruct returns no error', () => {
it('does now throw if superstruct returns no error', () => {
validateMock.mockReturnValue([undefined, undefined]);
expect(validateParams({}, any())).toBe(true);
expect(() => validateParams({}, any())).not.toThrow();
});

it('throws if superstruct returns error', () => {
Expand Down
4 changes: 1 addition & 3 deletions src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@ export async function validateAndNormalizeKeyholder(
export function validateParams<ParamsType>(
value: unknown | ParamsType,
struct: Struct<ParamsType>,
): value is ParamsType {
): asserts value is ParamsType {
const [error] = validate(value, struct);

if (error) {
throw rpcErrors.invalidInput(
formatValidationError(error, `Invalid params`),
);
}

return true;
}

export function resemblesAddress(str: string): boolean {
Expand Down

0 comments on commit 64c48e2

Please sign in to comment.