Skip to content

Commit caa38d1

Browse files
feat: pretty print getter result (#90)
1 parent a585ce9 commit caa38d1

File tree

5 files changed

+62
-35
lines changed

5 files changed

+62
-35
lines changed

src/components/shared/LogView/LogView.module.scss

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
display: block;
1515
}
1616
}
17+
div[class*='xterm-rows'] {
18+
font-size: 17px;
19+
}
1720

1821
.tab {
1922
display: inline-flex;

src/components/shared/LogView/LogView.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ const LogView: FC<Props> = ({ filter }) => {
108108
import('@xterm/addon-search'),
109109
]);
110110
_terminal = new Terminal({
111-
fontSize: 17,
111+
fontSize: 16.5,
112112
cursorBlink: false,
113113
cursorStyle: 'bar',
114114
disableStdin: true,
115+
convertEol: true,
115116
});
116117

117118
terminal.current = _terminal;

src/components/workspace/ABIUi/TactABIUi.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ const TactABIUi: FC<TactABI> = ({
375375
);
376376

377377
if (Array.isArray(response)) {
378-
createLog(JSON.stringify(response));
378+
createLog(JSON.stringify(response, null, 2));
379379
} else if (response?.logs) {
380380
for (const log of response.logs) {
381381
createLog(
@@ -384,7 +384,7 @@ const TactABIUi: FC<TactABI> = ({
384384
);
385385
}
386386
} else {
387-
createLog(JSON.stringify(response));
387+
createLog(JSON.stringify(response, null, 2));
388388
}
389389
updateABIInputValues(
390390
{ key: abiType.name, value: formValues, type: type },

src/hooks/contract.hooks.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import {
88
} from '@/interfaces/workspace.interface';
99
import EventEmitter from '@/utility/eventEmitter';
1010
import {
11-
convertToText,
11+
GetterJSONReponse,
1212
tonHttpEndpoint as getHttpEndpoint,
13+
serializeToJSONFormat,
1314
} from '@/utility/utils';
1415
import { Network } from '@orbs-network/ton-access';
1516
import {
@@ -291,8 +292,8 @@ export function useContractAction() {
291292
}
292293

293294
type RESPONSE_VALUES =
294-
| { method: string; value: string }
295-
| { type: string; value: string };
295+
| { method: string; value: string | GetterJSONReponse }
296+
| { type: string; value: string | GetterJSONReponse };
296297

297298
async function callGetter(
298299
contractAddress: string,
@@ -340,7 +341,7 @@ export function useContractAction() {
340341
printDebugLog();
341342
responseValues.push({
342343
method: methodName,
343-
value: convertToText(response),
344+
value: serializeToJSONFormat(response),
344345
});
345346
} else {
346347
const call = await contract.getData(

src/utility/utils.ts

+50-28
Original file line numberDiff line numberDiff line change
@@ -131,55 +131,77 @@ export const getFileExtension = (fileName: string) => {
131131
};
132132

133133
// eslint-disable-next-line complexity, @typescript-eslint/no-explicit-any
134-
export const convertToText = (obj: any): string => {
135-
//create an array that will later be joined into a string.
136-
const string = [];
134+
export type GetterJSONReponse =
135+
| string
136+
| number
137+
| boolean
138+
| null
139+
| GetterJSONReponse[]
140+
| { [key: string]: GetterJSONReponse };
137141

142+
/**
143+
* Converts various types of objects, including custom types, arrays, dictionaries, and primitives, into a JSON-compatible format.
144+
*
145+
* This function is particularly useful when dealing with complex data structures that need to be serialized into a JSON format.
146+
* It handles different types, including custom objects like `Address`, `Slice`, `Cell`, and `Dictionary`, converting them into strings.
147+
* Arrays and objects are recursively converted, ensuring that all nested structures are correctly transformed.
148+
*
149+
* @param obj - The object or value to be converted to a JSON-compatible format. It can be of any type.
150+
* @returns A JSON-compatible value (`string`, `number`, `boolean`, `null`, array, or object), or `null` if the input is `undefined` or `null`.
151+
*/
152+
export const serializeToJSONFormat = (
153+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
154+
obj: any,
155+
): GetterJSONReponse | GetterJSONReponse[] | null => {
138156
//is object
139157
// Both arrays and objects seem to return "object"
140158
// when typeof(obj) is applied to them. So instead
141159
// I am checking to see if they have the property
142160
// join, which normal objects don't have but
143161
// arrays do.
144-
if (obj == undefined) {
145-
return String(obj);
146-
} else if (typeof obj == 'object' && obj.join == undefined) {
162+
if (obj === undefined || obj === null) {
163+
return null;
164+
}
165+
166+
if (typeof obj === 'object' && obj.join === undefined) {
147167
if (obj instanceof Address) return obj.toString();
148168
if (obj instanceof Slice) return obj.toString();
149169
if (obj instanceof Cell) return obj.toString();
150170
if (obj instanceof Dictionary) {
151-
const items = [];
152-
for (const key of obj.keys())
153-
items.push(`${convertToText(key)}: ${convertToText(obj.get(key))}`);
154-
const itemsStr = items.join(', ');
155-
return itemsStr ? `{ ${itemsStr} }` : `{}`;
171+
const resultDict: Record<string, GetterJSONReponse> = {};
172+
for (const key of obj.keys()) {
173+
const jsonKey = serializeToJSONFormat(key);
174+
if (typeof jsonKey === 'string') {
175+
resultDict[jsonKey] = serializeToJSONFormat(obj.get(key));
176+
}
177+
}
178+
return resultDict;
156179
}
157180

158-
for (const prop in obj) {
159-
if (Object.prototype.hasOwnProperty.call(obj, prop))
160-
string.push(prop + ': ' + convertToText(obj[prop]));
181+
if (Array.isArray(obj)) {
182+
return obj.map((item) => serializeToJSONFormat(item));
161183
}
162-
return '{' + string.join(', ') + '}';
163184

164-
//is array
165-
} else if (typeof obj == 'object' && !(obj.join == undefined)) {
185+
const resultObj: Record<string, GetterJSONReponse> = {};
166186
for (const prop in obj) {
167-
string.push(convertToText(obj[prop]));
187+
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
188+
resultObj[prop] = serializeToJSONFormat(
189+
(obj as Record<string, unknown>)[prop],
190+
);
191+
}
168192
}
169-
return '[' + string.join(',') + ']';
193+
return resultObj;
194+
}
170195

171-
//is function
172-
} else if (typeof obj == 'function') {
173-
string.push(obj.toString());
196+
if (typeof obj === 'function') {
197+
return obj.toString();
198+
}
174199

175-
//all other values can be done with JSON.stringify
176-
} else {
177-
if (typeof obj == 'string') string.push(JSON.stringify(obj));
178-
else if (typeof obj == 'bigint') string.push(obj.toString());
179-
else string.push(obj.toString());
200+
if (typeof obj === 'string' || typeof obj === 'bigint') {
201+
return obj.toString();
180202
}
181203

182-
return string.join(',');
204+
return obj as GetterJSONReponse;
183205
};
184206

185207
export const tonHttpEndpoint = ({ network }: Config) => {

0 commit comments

Comments
 (0)