• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cartesi / rollups-explorer / 11726651411

07 Nov 2024 03:57PM CUT coverage: 86.104% (-0.5%) from 86.605%
11726651411

Pull #256

github

nevendyulgerov
chore(packages/ui): Cleanup
Pull Request #256: #251 Add ABI encoding for raw input form

1366 of 1627 branches covered (83.96%)

Branch coverage included in aggregate %.

756 of 920 new or added lines in 13 files covered. (82.17%)

9428 of 10909 relevant lines covered (86.42%)

47.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.75
/packages/ui/src/GenericInputForm/validations.ts
1
import { isAddress, isHex, parseAbi, parseAbiParameters } from "viem";
1✔
2
import { FormValues } from "./types";
3
import { isBlank } from "ramda-adjunct";
1✔
4
import { prepareSignatures } from "web/src/components/specification/utils";
1✔
5

6
export const validateApplication = (value: string) =>
1✔
7
    value !== "" && isAddress(value) ? null : "Invalid application";
307✔
8

9
export const validateHexInput = (value: string) =>
1✔
10
    isHex(value) ? null : "Invalid hex value";
307✔
11

12
export const validateAbiMethod = (value: string, values: FormValues) =>
1✔
13
    values.mode !== "abi" || value === "new" || value === "existing"
307✔
14
        ? null
307!
NEW
15
        : "Invalid ABI method";
×
16

17
export const validateSpecificationId = (value: string, values: FormValues) =>
1✔
18
    values.mode !== "abi" ||
307✔
19
    (values.abiMethod === "new" && values.specificationMode === "json_abi") ||
192✔
20
    value !== ""
126✔
21
        ? null
255✔
22
        : "Invalid specification";
52✔
23

24
export const validateAbiFunctionName = (value: string, values: FormValues) =>
1✔
25
    values.mode !== "abi" ||
307✔
26
    values.specificationMode !== "json_abi" ||
192✔
27
    value !== ""
136✔
28
        ? null
235✔
29
        : "Invalid ABI function";
72✔
30

31
export const validateAbiFunctionParamValue = (
1✔
32
    value: string,
192✔
33
    values: FormValues,
192✔
34
    key: string,
192✔
35
) => {
192✔
36
    if (values.mode !== "abi") {
192!
NEW
37
        return null;
×
NEW
38
    }
×
39

40
    const [paramIndex] = key
192✔
41
        .split(".")
192✔
42
        .map((part) => parseInt(part))
192✔
43
        .filter((n) => !isNaN(n));
192✔
44
    const param = values.abiFunctionParams[paramIndex];
192✔
45

46
    if (!param) {
192!
NEW
47
        return null;
×
NEW
48
    }
×
49

50
    const message = `Invalid ${param.type} value`;
192✔
51
    let error: string | null = null;
192✔
52

53
    // Validate the field for non-empty content
54
    if (value === "") {
192✔
55
        return message;
75✔
56
    }
75✔
57

58
    // Otherwise, if some content exists, validate it based on the type
59
    switch (param.type) {
117✔
60
        case "uint":
192!
61
        case "uint8":
192!
62
        case "uint16":
192!
63
        case "uint32":
192!
64
        case "uint64":
192!
65
        case "uint128":
192!
66
        case "uint256":
192✔
67
            try {
78✔
68
                BigInt(value);
78✔
69
            } catch (e) {
78✔
70
                error = message;
21✔
71
            }
21✔
72
            break;
78✔
73
        case "bool":
192✔
74
            error = value === "true" || value === "false" ? null : message;
39✔
75
            break;
39✔
76
        case "bytes":
192!
NEW
77
            error = isHex(value) ? null : message;
×
NEW
78
            break;
×
79
        case "address":
192!
NEW
80
            error = isAddress(value) ? null : message;
×
NEW
81
            break;
×
82
        // All other types like 'string' are handled in the non-empty content check above
83
        default:
192!
NEW
84
            break;
×
85
    }
192✔
86

87
    return error;
117✔
88
};
117✔
89

90
export const validateHumanAbi = (value: string, values: FormValues) => {
1✔
91
    if (
307✔
92
        values.abiMethod === "existing" ||
307✔
93
        values.specificationMode === "abi_params"
122✔
94
    ) {
307✔
95
        return null;
241✔
96
    }
241✔
97

98
    if (isBlank(value)) {
307✔
99
        return "The ABI signature definition is required";
18✔
100
    }
18✔
101

102
    const items = prepareSignatures(value);
48✔
103
    try {
48✔
104
        parseAbi(items);
48✔
105
    } catch (error: any) {
307✔
106
        return error.message;
4✔
107
    }
4✔
108

109
    return null;
44✔
110
};
44✔
111

112
export const validateAbiParam = (value: string, values: FormValues) => {
1✔
113
    if (
307✔
114
        values.abiMethod === "existing" ||
307✔
115
        values.specificationMode === "json_abi"
122✔
116
    ) {
307✔
117
        return null;
251✔
118
    }
251✔
119

120
    if (isBlank(value)) {
307✔
121
        return "ABI parameter is required.";
12✔
122
    }
12✔
123

124
    try {
44✔
125
        parseAbiParameters(value);
44✔
126
        return null;
44✔
127
    } catch (error: any) {
307✔
128
        return error.message as string;
4✔
129
    }
4✔
130
};
307✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc