• 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

48.23
/packages/ui/src/GenericInputForm/utils.ts
1
import { AbiParameter, getAddress, parseAbi, parseAbiParameters } from "viem";
1✔
2
import {
3
    AbiInputParam,
4
    AbiValueParameter,
5
    FormSpecification,
6
    FinalValues,
7
} from "./types";
8
import { prepareSignatures } from "web/src/components/specification/utils";
1✔
9
import { isArray, isBlank, isObject } from "ramda-adjunct";
1✔
10

11
export const encodeFunctionParam = (param: AbiValueParameter) => {
1✔
NEW
12
    switch (param.type) {
×
NEW
13
        case "bool":
×
NEW
14
            return param.value === "true";
×
NEW
15
        case "address":
×
NEW
16
            return getAddress(param.value);
×
NEW
17
        case "uint":
×
NEW
18
        case "uint8":
×
NEW
19
        case "uint16":
×
NEW
20
        case "uint32":
×
NEW
21
        case "uint64":
×
NEW
22
        case "uint128":
×
NEW
23
        case "uint256":
×
NEW
24
            return BigInt(param.value);
×
NEW
25
        default:
×
NEW
26
            return param.value;
×
NEW
27
    }
×
NEW
28
};
×
29

30
export const generateHumanAbiFormSpecification = (humanAbi: string) => {
1✔
31
    if (isBlank(humanAbi)) {
83✔
32
        return undefined;
24✔
33
    }
24✔
34

35
    const readableList = prepareSignatures(humanAbi);
59✔
36
    let generatedAbi;
59✔
37
    try {
59✔
38
        generatedAbi = parseAbi(readableList);
59✔
39
    } catch (err) {}
83✔
40

41
    return isObject(generatedAbi)
59✔
42
        ? ({
55✔
43
              id: new Date().getTime().toString(),
55✔
44
              name: "Generated specification",
55✔
45
              abi: generatedAbi,
55✔
46
          } as FormSpecification)
55✔
47
        : undefined;
4✔
48
};
83✔
49

50
export const generateAbiParamFormSpecification = (abiParam: string) => {
1✔
51
    let abiParameters;
87✔
52

53
    try {
87✔
54
        abiParameters = parseAbiParameters(abiParam);
87✔
55
    } catch (err) {}
87✔
56

57
    return isArray(abiParameters)
87✔
58
        ? ({
52✔
59
              id: new Date().getTime().toString(),
52✔
60
              name: "Generated specification",
52✔
61
              abi: [
52✔
62
                  {
52✔
63
                      inputs: abiParameters,
52✔
64
                      name: "",
52✔
65
                      outputs: [],
52✔
66
                      stateMutability: "view",
52✔
67
                      type: "function",
52✔
68
                  },
52✔
69
              ],
52✔
70
          } as FormSpecification)
52✔
71
        : undefined;
35✔
72
};
87✔
73

74
export const generateInitialValues = (
1✔
75
    parentInput: AbiInputParam,
12✔
76
    flatInputs: AbiInputParam[],
12✔
77
) => {
12✔
78
    if (parentInput.components) {
12!
NEW
79
        parentInput.components.forEach((input: AbiInputParam) => {
×
NEW
80
            if (input.type === "tuple") {
×
NEW
81
                generateInitialValues(input, flatInputs);
×
NEW
82
            } else {
×
NEW
83
                const flatInput: AbiInputParam = {
×
NEW
84
                    ...input,
×
NEW
85
                    value: "",
×
NEW
86
                };
×
87

NEW
88
                if (parentInput.type === "tuple") {
×
NEW
89
                    flatInput.tupleName = parentInput.name;
×
NEW
90
                }
×
91

NEW
92
                flatInputs.push(flatInput);
×
NEW
93
            }
×
NEW
94
        });
×
95
    } else {
12✔
96
        flatInputs.push({
12✔
97
            ...parentInput,
12✔
98
            value: "",
12✔
99
        });
12✔
100
    }
12✔
101
};
12✔
102

103
export const generateFinalValues = (
1✔
NEW
104
    inputs: AbiParameter[],
×
NEW
105
    params: AbiValueParameter[],
×
NEW
106
) => {
×
NEW
107
    const finalArr: FinalValues = [];
×
108

NEW
109
    inputs.forEach((input) => {
×
NEW
110
        if (input.type === "tuple") {
×
NEW
111
            const currArr: FinalValues = [];
×
NEW
112
            finalArr.push(currArr);
×
113

NEW
114
            getTupleValues(input as AbiInputParam, params, finalArr, currArr);
×
NEW
115
        } else {
×
NEW
116
            const param = params.find((p) => {
×
NEW
117
                return p.name === input.name && p.type === input.type;
×
NEW
118
            }) as AbiValueParameter;
×
NEW
119
            const value = encodeFunctionParam(param);
×
NEW
120
            finalArr.push(value);
×
NEW
121
        }
×
NEW
122
    });
×
123

NEW
124
    return finalArr;
×
NEW
125
};
×
126

127
const getTupleValues = (
1✔
NEW
128
    tupleInput: AbiInputParam,
×
NEW
129
    params: AbiValueParameter[],
×
NEW
130
    finalArr: FinalValues = [],
×
NEW
131
    currentArr: FinalValues = [],
×
NEW
132
) => {
×
NEW
133
    tupleInput.components.forEach((input) => {
×
NEW
134
        if (input.type === "tuple") {
×
NEW
135
            const nextCurrentArr: FinalValues = [];
×
NEW
136
            currentArr.push(nextCurrentArr);
×
NEW
137
            getTupleValues(input, params, finalArr, nextCurrentArr);
×
NEW
138
        } else {
×
NEW
139
            const param = params.find((p) => {
×
NEW
140
                return (
×
NEW
141
                    p.tupleName === tupleInput.name &&
×
NEW
142
                    p.name === input.name &&
×
NEW
143
                    p.type === input.type
×
144
                );
NEW
145
            }) as AbiValueParameter;
×
NEW
146
            const value = encodeFunctionParam(param);
×
NEW
147
            currentArr.push(value);
×
NEW
148
        }
×
NEW
149
    });
×
NEW
150
};
×
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