Skip to content

Commit 6d8f3e4

Browse files
committed
refactor: custom zod schema for NanoContractActionSchema
1 parent aeda3c6 commit 6d8f3e4

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

packages/hathor-rpc-handler/src/rpcMethods/sendNanoContractTx.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,30 @@ import {
2222
SendNanoContractTxLoadingFinishedTrigger,
2323
} from '../types';
2424
import { PromptRejectedError, SendNanoContractTxError, InvalidParamsError } from '../errors';
25-
import { NanoContractAction } from '@hathor/wallet-lib/lib/nano_contracts/types';
25+
import { NanoContractAction, NanoContractActionType } from '@hathor/wallet-lib/lib/nano_contracts/types';
2626

2727
export type NanoContractActionWithStringAmount = Omit<NanoContractAction, 'amount'> & {
2828
amount: string,
2929
}
3030

31+
const NanoContractActionSchema = z.object({
32+
type: z.nativeEnum(NanoContractActionType),
33+
token: z.string(),
34+
amount: z.string().regex(/^\d+$/)
35+
.pipe(z.coerce.bigint().positive()),
36+
address: z.string().nullish(),
37+
changeAddress: z.string().nullish(),
38+
}).transform((data): NanoContractAction => ({
39+
...data,
40+
address: data.address ?? null,
41+
changeAddress: data.changeAddress ?? null,
42+
}));
43+
3144
const sendNanoContractSchema = z.object({
3245
method: z.string().min(1),
3346
blueprint_id: z.string().nullish(),
3447
nc_id: z.string().nullish(),
35-
actions: z.array(
36-
z.custom<NanoContractActionWithStringAmount>().transform(action => ({
37-
...action,
38-
amount: z.string().regex(/^\d+$/)
39-
.pipe(z.coerce.bigint().nonnegative())
40-
.parse(action.amount)
41-
}))
42-
),
48+
actions: z.array(NanoContractActionSchema),
4349
args: z.array(z.unknown()).default([]),
4450
push_tx: z.boolean().default(true),
4551
}).transform(data => ({

0 commit comments

Comments
 (0)