Skip to content

Commit c5106c6

Browse files
committed
fix(rpc): use more consistent schema definitions
1 parent 694f7dc commit c5106c6

File tree

5 files changed

+101
-45
lines changed

5 files changed

+101
-45
lines changed

packages/rpc/src/methods/open-swap.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1-
import { DefineRpcMethod, RpcParameterByName, RpcRequest, RpcResponse } from '../rpc/schemas';
1+
import { z } from 'zod';
22

3-
interface OpenSwapResponseBody {
4-
message: string;
5-
}
3+
import {
4+
DefineRpcMethod,
5+
createRpcRequestSchema,
6+
createRpcResponseSchema,
7+
defaultErrorSchema,
8+
} from '../rpc/schemas';
69

7-
interface OpenSwapRequestParams extends RpcParameterByName {
8-
base: string;
9-
quote: string;
10-
}
10+
const methodName = 'openSwap';
1111

12-
export type OpenSwapRequest = RpcRequest<'openSwap', OpenSwapRequestParams>;
12+
// Request
13+
export const openSwapRequestParamsSchema = z.object({
14+
base: z.string(),
15+
quote: z.string(),
16+
});
17+
export type OpenSwapRequestParams = z.infer<typeof openSwapRequestParamsSchema>;
1318

14-
export type OpenSwapResponse = RpcResponse<OpenSwapResponseBody>;
19+
export const openSwapRequestSchema = createRpcRequestSchema(
20+
methodName,
21+
openSwapRequestParamsSchema
22+
);
23+
export type OpenSwapRequest = z.infer<typeof openSwapRequestSchema>;
24+
25+
// Response
26+
export const openSwapResponseBodySchema = z.object({
27+
message: z.string(),
28+
});
29+
export type OpenSwapResponseBody = z.infer<typeof openSwapRequestParamsSchema>;
30+
31+
export const openSwapResponseSchema = createRpcResponseSchema(
32+
openSwapResponseBodySchema,
33+
defaultErrorSchema
34+
);
35+
export type OpenSwapResponse = z.infer<typeof openSwapResponseSchema>;
1536

1637
export type DefineOpenSwapMethod = DefineRpcMethod<OpenSwapRequest, OpenSwapResponse>;

packages/rpc/src/methods/stacks/stx-sign-transaction.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { z } from 'zod';
22

3-
import { DefineRpcMethod, RpcRequest, RpcResponse } from '../../rpc/schemas';
3+
import {
4+
DefineRpcMethod,
5+
createRpcRequestSchema,
6+
createRpcResponseSchema,
7+
defaultErrorSchema,
8+
} from '../../rpc/schemas';
49

510
export const stxSignTransactionMethodName = 'stx_signTransaction';
611

@@ -24,23 +29,26 @@ export const stxSignTransactionRequestParamsSchema = z.union([
2429
stxSignTransactionRequestLeatherRpcParamsSchema,
2530
stxSignTransactionRequestSip30ParamsSchema,
2631
]);
27-
2832
export type StxSignTransactionRequestParams = z.infer<typeof stxSignTransactionRequestParamsSchema>;
2933

34+
export const stxSignTransactionRequestSchema = createRpcRequestSchema(
35+
stxSignTransactionMethodName,
36+
stxSignTransactionRequestParamsSchema
37+
);
38+
export type StxSignTransactionRequest = z.infer<typeof stxSignTransactionRequestSchema>;
39+
3040
// For backwards compatibility, we return the same data under both properties
31-
export const stxSignTransactionResponseSchema = z.object({
41+
export const stxSignTransactionResponseBodySchema = z.object({
3242
transaction: z.string(),
3343
txHex: z.string(),
3444
});
45+
export type StxSignTransactionResponseBody = z.infer<typeof stxSignTransactionResponseBodySchema>;
3546

36-
export type StxSignTransactionResponseBody = z.infer<typeof stxSignTransactionResponseSchema>;
37-
38-
export type StxSignTransactionRequest = RpcRequest<
39-
typeof stxSignTransactionMethodName,
40-
StxSignTransactionRequestParams
41-
>;
42-
43-
export type StxSignTransactionResponse = RpcResponse<StxSignTransactionResponseBody>;
47+
export const stxSignTransactionResponseSchema = createRpcResponseSchema(
48+
stxSignTransactionResponseBodySchema,
49+
defaultErrorSchema
50+
);
51+
export type StxSignTransactionResponse = z.infer<typeof stxSignTransactionResponseSchema>;
4452

4553
export type DefineStxSignTransactionMethod = DefineRpcMethod<
4654
StxSignTransactionRequest,

packages/rpc/src/methods/stacks/stx-transfer-sip10-ft.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
import { z } from 'zod';
22

3-
import { DefineRpcMethod, RpcRequest, RpcResponse } from '../../rpc/schemas';
3+
import {
4+
DefineRpcMethod,
5+
createRpcRequestSchema,
6+
createRpcResponseSchema,
7+
defaultErrorSchema,
8+
} from '../../rpc/schemas';
49
import { stacksTransactionDetailsSchema } from './_stacks-helpers';
510

611
export const stxTransferSip10FtMethodName = 'stx_transferSip10Ft';
712

8-
type StxTransferSip10FtRequestMethodName = typeof stxTransferSip10FtMethodName;
13+
export type StxTransferSip10FtRequestMethodName = typeof stxTransferSip10FtMethodName;
914

1015
// Request
1116
export const stxTransferSip10FtRequestParamsSchema = z.object({
1217
recipient: z.string(),
1318
asset: z.string(),
1419
amount: z.coerce.number(),
1520
});
16-
1721
export type StxTransferSip10FtRequestParams = z.infer<typeof stxTransferSip10FtRequestParamsSchema>;
1822

19-
export type StxTransferSip10FtRequest = RpcRequest<
20-
StxTransferSip10FtRequestMethodName,
21-
StxTransferSip10FtRequestParams
22-
>;
23+
export const stxTransferSip10FtRequestSchema = createRpcRequestSchema(
24+
stxTransferSip10FtMethodName,
25+
stxTransferSip10FtRequestParamsSchema
26+
);
27+
export type StxTransferSip10FtRequest = z.infer<typeof stxTransferSip10FtRequestSchema>;
2328

2429
// Result
2530
export const stxTransferSip10FtResponseBodySchema = stacksTransactionDetailsSchema;
2631

27-
export type StxTransferSip10FtResponse = RpcResponse<typeof stxTransferSip10FtResponseBodySchema>;
32+
export const stxTransferSip10FtResponseSchema = createRpcResponseSchema(
33+
stxTransferSip10FtResponseBodySchema,
34+
defaultErrorSchema
35+
);
36+
export type StxTransferSip10FtResponse = z.infer<typeof stxTransferSip10FtResponseSchema>;
2837

2938
export type DefineStxTransferSip10FtMethod = DefineRpcMethod<
3039
StxTransferSip10FtRequest,

packages/rpc/src/methods/stacks/stx-transfer-stx.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import { z } from 'zod';
22

3-
import { DefineRpcMethod, RpcRequest, RpcResponse } from '../../rpc/schemas';
3+
import {
4+
DefineRpcMethod,
5+
createRpcRequestSchema,
6+
createRpcResponseSchema,
7+
defaultErrorSchema,
8+
} from '../../rpc/schemas';
49
import {
510
baseStacksTransactionConfigSchema,
611
stacksTransactionDetailsSchema,
712
} from './_stacks-helpers';
813

914
export const stxTransferStxMethodName = 'stx_transferStx';
10-
11-
type StxTransferStxRequestMethodName = typeof stxTransferStxMethodName;
15+
export type StxTransferStxRequestMethodName = typeof stxTransferStxMethodName;
1216

1317
// Request
1418
export const stxTransferStxRequestParamsSchema = z.intersection(
@@ -19,18 +23,22 @@ export const stxTransferStxRequestParamsSchema = z.intersection(
1923
}),
2024
baseStacksTransactionConfigSchema
2125
);
22-
2326
export type StxTransferStxRequestParams = z.infer<typeof stxTransferStxRequestParamsSchema>;
2427

25-
export type StxTransferStxRequest = RpcRequest<
26-
StxTransferStxRequestMethodName,
27-
StxTransferStxRequestParams
28-
>;
28+
export const stxTransferStxRequestSchema = createRpcRequestSchema(
29+
stxTransferStxMethodName,
30+
stxTransferStxRequestParamsSchema
31+
);
32+
export type StxTransferStxRequest = z.infer<typeof stxTransferStxRequestSchema>;
2933

3034
// Result
3135
export const stxTransferStxResponseBodySchema = stacksTransactionDetailsSchema;
3236

33-
export type StxTransferStxResponse = RpcResponse<typeof stxTransferStxResponseBodySchema>;
37+
export const stxTransferStxResponseSchema = createRpcResponseSchema(
38+
stxTransferStxResponseBodySchema,
39+
defaultErrorSchema
40+
);
41+
type StxTransferStxResponse = z.infer<typeof stxTransferStxResponseSchema>;
3442

3543
export type DefineStxTransferStxMethod = DefineRpcMethod<
3644
StxTransferStxRequest,

packages/rpc/src/methods/stacks/stx-update-profile.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { z } from 'zod';
22

3-
import { DefineRpcMethod, RpcRequest, RpcResponse } from '../../rpc/schemas';
3+
import {
4+
DefineRpcMethod,
5+
createRpcRequestSchema,
6+
createRpcResponseSchema,
7+
defaultErrorSchema,
8+
} from '../../rpc/schemas';
49
import { stacksTransactionDetailsSchema } from './_stacks-helpers';
510

611
export const stxUpdateProfileMethodName = 'stx_updateProfile';
712

8-
type StxUpdateProfileRequestMethodName = typeof stxUpdateProfileMethodName;
13+
export type StxUpdateProfileRequestMethodName = typeof stxUpdateProfileMethodName;
914

1015
// Request
1116
export const stxUpdateProfileRequestParamsSchema = z.object({
@@ -15,15 +20,20 @@ export const stxUpdateProfileRequestParamsSchema = z.object({
1520

1621
export type StxUpdateProfileRequestParams = z.infer<typeof stxUpdateProfileRequestParamsSchema>;
1722

18-
export type StxUpdateProfileRequest = RpcRequest<
19-
StxUpdateProfileRequestMethodName,
20-
StxUpdateProfileRequestParams
21-
>;
23+
export const stxUpdateProfileRequestSchema = createRpcRequestSchema(
24+
stxUpdateProfileMethodName,
25+
stxUpdateProfileRequestParamsSchema
26+
);
27+
export type StxUpdateProfileRequest = z.infer<typeof stxUpdateProfileRequestSchema>;
2228

2329
// Result
2430
export const stxUpdateProfileResponseBodySchema = stacksTransactionDetailsSchema;
2531

26-
export type StxUpdateProfileResponse = RpcResponse<typeof stxUpdateProfileResponseBodySchema>;
32+
export const stxUpdateProfileResponseSchema = createRpcResponseSchema(
33+
stxUpdateProfileResponseBodySchema,
34+
defaultErrorSchema
35+
);
36+
export type StxUpdateProfileResponse = z.infer<typeof stxUpdateProfileResponseSchema>;
2737

2838
export type DefineStxUpdateProfileMethod = DefineRpcMethod<
2939
StxUpdateProfileRequest,

0 commit comments

Comments
 (0)