/
search.ts
134 lines (123 loc) · 2.93 KB
/
search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import {
ABI,
ArbitraryState,
isArgType,
JSONDetailsSearch,
StringSearch,
} from '@daohaus/utils';
import { Keychain, PinataApiKeys, ValidNetwork } from '@daohaus/keychain-utils';
import { processArg } from './args';
export const checkArgType = (arg: unknown) => {
if (isArgType(arg)) {
return arg;
}
throw new Error(`Invalid arg type ${arg}`);
};
export const deepSearch = (
appState: ArbitraryState,
pathString: StringSearch
): unknown => {
const path = pathString.trim().split('.').filter(Boolean);
let state = { ...appState };
for (let i = 0, len = path.length; i < len; i++) {
state = state?.[path?.[i]];
}
return state;
};
export const searchApp = (
appState: ArbitraryState,
pathString: StringSearch,
shouldThrow = false
) => {
const result = deepSearch(appState, pathString);
if (result == null) {
if (shouldThrow) {
console.log('**Application State**', appState);
console.log('result', result);
throw new Error(`Could not find ${pathString}`);
} else {
return false;
}
}
return result;
};
export const checkHasCondition = (pathString: StringSearch) =>
pathString.includes('||');
export const handleConditionalPath = (pathString: StringSearch) => {
const paths = pathString
.trim()
.split('||')
.map((str) => str.trim())
.filter(Boolean);
return paths;
};
export const searchArg = ({
appState,
searchString,
shouldThrow = false,
}: {
appState: ArbitraryState;
searchString: StringSearch;
shouldThrow: boolean;
}) => {
const hasCondition = checkHasCondition(searchString);
if (hasCondition) {
const paths = handleConditionalPath(searchString);
for (const path of paths) {
const result = searchApp(appState, path as StringSearch);
if (result) {
return checkArgType(result);
}
}
throw new Error(
`No paths in conditional path string: ${searchString} returns a value`
);
}
return checkArgType(searchApp(appState, searchString, shouldThrow));
};
export const handleDetailsJSON = async ({
arg,
appState,
localABIs,
chainId,
safeId,
rpcs,
pinataApiKeys,
explorerKeys,
}: {
arg: JSONDetailsSearch;
appState: ArbitraryState;
localABIs: Record<string, ABI>;
chainId: ValidNetwork;
safeId?: string;
rpcs: Keychain;
pinataApiKeys: PinataApiKeys;
explorerKeys: Keychain;
}) => {
const detailsList = await Promise.all(
Object.entries(arg.jsonSchema).map(async ([key, arg]) => {
return {
id: key,
value: await processArg({
arg,
chainId,
safeId,
localABIs,
appState,
rpcs,
pinataApiKeys,
explorerKeys,
}),
};
})
);
if (!detailsList) {
console.log('arg', arg);
throw new Error(`Error Compiling JSON Details`);
}
return JSON.stringify(
detailsList.reduce((acc, arg) => {
return { ...acc, [arg.id]: arg.value };
}, {})
);
};