Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wallet call canister arg and argType params #140

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 125 additions & 18 deletions src/types/wallet-request.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,135 @@
import {WalletRequestOptionsSchema} from './wallet-request';
import {IDL} from '@dfinity/candid';
import {mockPrincipalText} from '../constants/icrc-accounts.mocks';
import {
WalletRequestOptionsSchema,
extendIcrcCallCanisterRequestParamsSchema,
type WalletCallParams
} from './wallet-request';

describe('WalletRequest', () => {
it('should validate with a specified timeoutInMilliseconds', () => {
const validData = {
timeoutInMilliseconds: 3000
};
describe('Options', () => {
it('should validate with a specified timeoutInMilliseconds', () => {
const validData = {
timeoutInMilliseconds: 3000
};

const result = WalletRequestOptionsSchema.safeParse(validData);
expect(result.success).toBe(true);
});
const result = WalletRequestOptionsSchema.safeParse(validData);
expect(result.success).toBe(true);
});

it('should validate without specifying timeoutInMilliseconds', () => {
const validData = {};

const result = WalletRequestOptionsSchema.safeParse(validData);
expect(result.success).toBe(true);
});

it('should validate without specifying timeoutInMilliseconds', () => {
const validData = {};
it('should fail validation with a non-numeric timeoutInMilliseconds', () => {
const invalidData = {
timeoutInMilliseconds: 'three thousand'
};

const result = WalletRequestOptionsSchema.safeParse(validData);
expect(result.success).toBe(true);
const result = WalletRequestOptionsSchema.safeParse(invalidData);
expect(result.success).toBe(false);
});
});

it('should fail validation with a non-numeric timeoutInMilliseconds', () => {
const invalidData = {
timeoutInMilliseconds: 'three thousand'
};
describe('Call', () => {
interface MyTest {
hello: string;
}

const argType = IDL.Record({
hello: IDL.Text
});

const schema = extendIcrcCallCanisterRequestParamsSchema<MyTest>();

it('should validate correct parameters', () => {
const validParams: WalletCallParams<{hello: string}> = {
canisterId: mockPrincipalText,
sender: mockPrincipalText,
method: 'some_method',
arg: {hello: 'world'},
argType
};

const result = schema.safeParse(validParams);
expect(result.success).toBe(true);
});

// TODO: not sure how to solve this with zod that's why this test succeed.
it('should validate incorrect "arg" type', () => {
const invalidParams = {
canisterId: mockPrincipalText,
sender: mockPrincipalText,
method: 'some_method',
arg: 'invalid_arg',
argType
};

const result = schema.safeParse(invalidParams);
expect(result.success).toBe(true);
});

it('should fail validation with missing "argType"', () => {
const invalidParams = {
canisterId: mockPrincipalText,
sender: mockPrincipalText,
method: 'some_method',
arg: {hello: 'world'}
};

const result = schema.safeParse(invalidParams);
expect(result.success).toBe(false);
});

it('should fail validation with missing "arg"', () => {
const invalidParams = {
canisterId: mockPrincipalText,
sender: mockPrincipalText,
method: 'some_method',
argType
};

const result = schema.safeParse(invalidParams);
expect(result.success).toBe(false);
});

it('should fail validation with missing "canisterId"', () => {
const invalidParams = {
sender: mockPrincipalText,
method: 'some_method',
arg: {hello: 'world'},
argType
};

const result = schema.safeParse(invalidParams);
expect(result.success).toBe(false);
});

it('should fail validation with missing "sender"', () => {
const invalidParams = {
canisterId: mockPrincipalText,
method: 'some_method',
arg: {hello: 'world'},
argType
};

const result = schema.safeParse(invalidParams);
expect(result.success).toBe(false);
});

it('should fail validation with missing "method"', () => {
const invalidParams = {
canisterId: mockPrincipalText,
sender: mockPrincipalText,
arg: {hello: 'world'},
argType
};

const result = WalletRequestOptionsSchema.safeParse(invalidData);
expect(result.success).toBe(false);
const result = schema.safeParse(invalidParams);
expect(result.success).toBe(false);
});
});
});
36 changes: 36 additions & 0 deletions src/types/wallet-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {Type} from '@dfinity/candid/lib/cjs/idl';
import {nonNullish} from '@dfinity/utils';
import {z} from 'zod';
import {IcrcCallCanisterRequestParamsSchema} from './icrc-requests';
import {RpcIdSchema} from './rpc';

export const WalletRequestOptionsTimeoutSchema = z.object({
Expand Down Expand Up @@ -29,3 +32,36 @@ export const WalletRequestOptionsWithTimeoutSchema = WalletRequestOptionsSchema.
}).merge(WalletRequestOptionsTimeoutSchema);

export type WalletRequestOptionsWithTimeout = z.infer<typeof WalletRequestOptionsWithTimeoutSchema>;

/**
* Creates an extended schema for ICRC call canister request parameters.
*
* This function generates a schema that extends the base `IcrcCallCanisterRequestParamsSchema`
* by replacing the `arg` field with a custom type `T` and adding an `argType` field that must be
* an instance of the `Type` class from `@dfinity/candid`.
*
* @template T - The type of the `arg` field.
* @returns {z.ZodObject} - A Zod schema object that includes the base fields and the custom `arg` and `argType`.
*/
/* eslint-disable @typescript-eslint/explicit-function-return-type */
export const extendIcrcCallCanisterRequestParamsSchema = <T>() => {
return IcrcCallCanisterRequestParamsSchema.omit({arg: true}).extend({
arg: z.custom<T>().refine(nonNullish, {
message: 'arg is required'
}),
argType: z.instanceof(Type) as z.ZodType<Type>
});
};

/**
* Represents the type of parameters used in a wallet call, based on the
* extended ICRC call canister request schema.
*
* This type is inferred from the return type of `extendIcrcCallCanisterRequestParamsSchema<T>`,
* meaning it includes all fields of `IcrcCallCanisterRequestParamsSchema` with a generic `arg` type.
*
* @template T - The type of the `arg` field in the schema.
*/
export type WalletCallParams<T> = z.infer<
ReturnType<typeof extendIcrcCallCanisterRequestParamsSchema<T>>
>;