@witnet/sdk 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env_witnet +6 -6
- package/LICENSE +21 -21
- package/README.md +103 -103
- package/dist/bin/helpers.d.ts +88 -0
- package/dist/bin/helpers.d.ts.map +1 -0
- package/dist/bin/helpers.js +866 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +42 -0
- package/dist/lib/crypto/account.d.ts +18 -0
- package/dist/lib/crypto/account.d.ts.map +1 -0
- package/dist/lib/crypto/account.js +152 -0
- package/dist/lib/crypto/coinbase.d.ts +9 -0
- package/dist/lib/crypto/coinbase.d.ts.map +1 -0
- package/dist/lib/crypto/coinbase.js +39 -0
- package/dist/lib/crypto/index.d.ts +7 -0
- package/dist/lib/crypto/index.d.ts.map +1 -0
- package/dist/lib/crypto/index.js +28 -0
- package/dist/lib/crypto/interfaces.d.ts +80 -0
- package/dist/lib/crypto/interfaces.d.ts.map +1 -0
- package/dist/lib/crypto/interfaces.js +3 -0
- package/dist/lib/crypto/payloads/DataRequestPayload.d.ts +47 -0
- package/dist/lib/crypto/payloads/DataRequestPayload.d.ts.map +1 -0
- package/dist/lib/crypto/payloads/DataRequestPayload.js +384 -0
- package/dist/lib/crypto/payloads/StakePayload.d.ts +27 -0
- package/dist/lib/crypto/payloads/StakePayload.d.ts.map +1 -0
- package/dist/lib/crypto/payloads/StakePayload.js +184 -0
- package/dist/lib/crypto/payloads/UnstakePayload.d.ts +35 -0
- package/dist/lib/crypto/payloads/UnstakePayload.d.ts.map +1 -0
- package/dist/lib/crypto/payloads/UnstakePayload.js +244 -0
- package/dist/lib/crypto/payloads/ValueTransferPayload.d.ts +24 -0
- package/dist/lib/crypto/payloads/ValueTransferPayload.d.ts.map +1 -0
- package/dist/lib/crypto/payloads/ValueTransferPayload.js +182 -0
- package/dist/lib/crypto/payloads.d.ts +54 -0
- package/dist/lib/crypto/payloads.d.ts.map +1 -0
- package/dist/lib/crypto/payloads.js +224 -0
- package/dist/lib/crypto/signer.d.ts +26 -0
- package/dist/lib/crypto/signer.d.ts.map +1 -0
- package/dist/lib/crypto/signer.js +299 -0
- package/dist/lib/crypto/transmitters/DataRequests.d.ts +14 -0
- package/dist/lib/crypto/transmitters/DataRequests.d.ts.map +1 -0
- package/dist/lib/crypto/transmitters/DataRequests.js +62 -0
- package/dist/lib/crypto/transmitters/StakeDeposits.d.ts +11 -0
- package/dist/lib/crypto/transmitters/StakeDeposits.d.ts.map +1 -0
- package/dist/lib/crypto/transmitters/StakeDeposits.js +48 -0
- package/dist/lib/crypto/transmitters/StakeWithdrawals.d.ts +17 -0
- package/dist/lib/crypto/transmitters/StakeWithdrawals.d.ts.map +1 -0
- package/dist/lib/crypto/transmitters/StakeWithdrawals.js +115 -0
- package/dist/lib/crypto/transmitters/ValueTransfers.d.ts +10 -0
- package/dist/lib/crypto/transmitters/ValueTransfers.d.ts.map +1 -0
- package/dist/lib/crypto/transmitters/ValueTransfers.js +47 -0
- package/dist/lib/crypto/transmitters.d.ts +46 -0
- package/dist/lib/crypto/transmitters.d.ts.map +1 -0
- package/dist/lib/crypto/transmitters.js +506 -0
- package/dist/lib/crypto/types.d.ts +127 -0
- package/dist/lib/crypto/types.d.ts.map +1 -0
- package/dist/lib/crypto/types.js +261 -0
- package/dist/lib/crypto/utils.d.ts +10 -0
- package/dist/lib/crypto/utils.d.ts.map +1 -0
- package/dist/lib/crypto/utils.js +97 -0
- package/dist/lib/crypto/wallet.d.ts +26 -0
- package/dist/lib/crypto/wallet.d.ts.map +1 -0
- package/dist/lib/crypto/wallet.js +327 -0
- package/dist/lib/helpers.d.ts +90 -0
- package/dist/lib/helpers.d.ts.map +1 -0
- package/dist/lib/helpers.js +1031 -0
- package/dist/lib/index.d.ts +5 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/index.js +21 -0
- package/dist/lib/radon/artifacts.d.ts +55 -0
- package/dist/lib/radon/artifacts.d.ts.map +1 -0
- package/dist/lib/radon/artifacts.js +347 -0
- package/dist/lib/radon/ccdr/eth.d.ts +100 -0
- package/dist/lib/radon/ccdr/eth.d.ts.map +1 -0
- package/dist/lib/radon/ccdr/eth.js +237 -0
- package/dist/lib/radon/ccdr/index.d.ts +34 -0
- package/dist/lib/radon/ccdr/index.d.ts.map +1 -0
- package/dist/lib/radon/ccdr/index.js +63 -0
- package/dist/lib/radon/ccdr/wit.d.ts +29 -0
- package/dist/lib/radon/ccdr/wit.d.ts.map +1 -0
- package/dist/lib/radon/ccdr/wit.js +60 -0
- package/dist/lib/radon/filters.d.ts +14 -0
- package/dist/lib/radon/filters.d.ts.map +1 -0
- package/dist/lib/radon/filters.js +47 -0
- package/dist/lib/radon/index.d.ts +36 -0
- package/dist/lib/radon/index.d.ts.map +1 -0
- package/dist/lib/radon/index.js +154 -0
- package/dist/lib/radon/reducers.d.ts +29 -0
- package/dist/lib/radon/reducers.d.ts.map +1 -0
- package/dist/lib/radon/reducers.js +101 -0
- package/dist/lib/radon/retrievals.d.ts +120 -0
- package/dist/lib/radon/retrievals.d.ts.map +1 -0
- package/dist/lib/radon/retrievals.js +358 -0
- package/dist/lib/radon/sources.d.ts +102 -0
- package/dist/lib/radon/sources.d.ts.map +1 -0
- package/dist/lib/radon/sources.js +294 -0
- package/dist/lib/radon/types.d.ts +521 -0
- package/dist/lib/radon/types.d.ts.map +1 -0
- package/dist/lib/radon/types.js +1066 -0
- package/dist/lib/radon/utils.d.ts +55 -0
- package/dist/lib/radon/utils.d.ts.map +1 -0
- package/dist/lib/radon/utils.js +181 -0
- package/dist/lib/rpc/farm.d.ts +66 -0
- package/dist/lib/rpc/farm.d.ts.map +1 -0
- package/dist/lib/rpc/farm.js +808 -0
- package/dist/lib/rpc/index.d.ts +3 -0
- package/dist/lib/rpc/index.d.ts.map +1 -0
- package/dist/lib/rpc/index.js +19 -0
- package/dist/lib/rpc/node.d.ts +38 -0
- package/dist/lib/rpc/node.d.ts.map +1 -0
- package/dist/lib/rpc/node.js +335 -0
- package/dist/lib/rpc/nodes.d.ts +40 -0
- package/dist/lib/rpc/nodes.d.ts.map +1 -0
- package/dist/lib/rpc/nodes.js +531 -0
- package/dist/lib/rpc/provider.d.ts +72 -0
- package/dist/lib/rpc/provider.d.ts.map +1 -0
- package/dist/lib/rpc/provider.js +402 -0
- package/dist/lib/rpc/reporter.d.ts +18 -0
- package/dist/lib/rpc/reporter.d.ts.map +1 -0
- package/dist/lib/rpc/reporter.js +99 -0
- package/dist/lib/rpc/types.d.ts +396 -0
- package/dist/lib/rpc/types.d.ts.map +1 -0
- package/dist/lib/rpc/types.js +81 -0
- package/dist/lib/rpc/wallet.d.ts +72 -0
- package/dist/lib/rpc/wallet.d.ts.map +1 -0
- package/dist/lib/rpc/wallet.js +41 -0
- package/dist/lib/types.d.ts +19 -0
- package/dist/lib/types.d.ts.map +1 -0
- package/dist/lib/types.js +7 -0
- package/dist/lib/utils.d.ts +5 -0
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/lib/utils.js +51 -0
- package/dist/package.json +1 -1
- package/dist/src/bin/helpers.js +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/lib/crypto/account.js +1 -1
- package/dist/src/lib/crypto/coinbase.js +1 -1
- package/dist/src/lib/crypto/index.js +1 -1
- package/dist/src/lib/crypto/interfaces.js +1 -1
- package/dist/src/lib/crypto/payloads/DataRequestPayload.js +1 -1
- package/dist/src/lib/crypto/payloads/StakePayload.js +1 -1
- package/dist/src/lib/crypto/payloads/UnstakePayload.js +1 -1
- package/dist/src/lib/crypto/payloads/ValueTransferPayload.js +1 -1
- package/dist/src/lib/crypto/payloads.js +1 -1
- package/dist/src/lib/crypto/signer.js +1 -1
- package/dist/src/lib/crypto/transmitters/DataRequests.js +1 -1
- package/dist/src/lib/crypto/transmitters/StakeDeposits.js +1 -1
- package/dist/src/lib/crypto/transmitters/StakeWithdrawals.js +1 -1
- package/dist/src/lib/crypto/transmitters/ValueTransfers.js +1 -1
- package/dist/src/lib/crypto/transmitters.js +1 -1
- package/dist/src/lib/crypto/types.js +1 -1
- package/dist/src/lib/crypto/utils.js +1 -1
- package/dist/src/lib/crypto/wallet.js +1 -1
- package/dist/src/lib/index.js +1 -1
- package/dist/src/lib/radon/ccdr/eth.js +1 -1
- package/dist/src/lib/radon/ccdr/index.js +1 -1
- package/dist/src/lib/radon/ccdr/wit.js +1 -1
- package/dist/src/lib/radon/filters.js +1 -1
- package/dist/src/lib/radon/index.js +1 -1
- package/dist/src/lib/radon/reducers.js +1 -1
- package/dist/src/lib/radon/types.js +1 -1
- package/dist/src/lib/radon/utils.js +1 -1
- package/dist/src/lib/rpc/index.js +1 -1
- package/dist/src/lib/rpc/nodes.js +1 -1
- package/dist/src/lib/rpc/provider.js +1 -1
- package/dist/src/lib/rpc/reporter.js +1 -1
- package/dist/src/lib/rpc/types.js +1 -1
- package/dist/src/lib/types.d.ts +1 -1
- package/dist/src/lib/types.d.ts.map +1 -1
- package/dist/src/lib/types.js +1 -1
- package/dist/src/lib/utils.js +1 -1
- package/dist/witnet/assets/index.js +1 -1
- package/dist/witnet/assets/modals/index.js +1 -1
- package/dist/witnet/assets/modals/web3/eth.js +1 -1
- package/dist/witnet/assets/modals/web3/ipfs.js +1 -1
- package/dist/witnet/assets/modals/web3/wit.js +1 -1
- package/dist/witnet/assets/requests.js +1 -1
- package/package.json +1 -1
- package/src/bin/cli/history.js +31 -31
- package/src/bin/cli/inspect.js +405 -405
- package/src/bin/cli/network.js +594 -594
- package/src/bin/cli/nodes.js +364 -364
- package/src/bin/cli/radon.js +815 -815
- package/src/bin/cli/wallet.js +1117 -1117
- package/src/bin/helpers.js +840 -840
- package/src/bin/postinstall.js +9 -9
- package/src/bin/toolkit.js +295 -295
- package/witnet/assets/_index.js +8 -8
- package/witnet/assets/_requests.js +25 -25
- package/witnet/assets/_sources.js +36 -36
- package/witnet/assets/_templates.js +36 -36
- package/witnet/assets/index.js +4 -4
- package/witnet/assets/modals/index.js +9 -9
- package/witnet/assets/modals/web3/eth.js +29 -29
- package/witnet/assets/modals/web3/ipfs.js +19 -19
- package/witnet/assets/modals/web3/wit.js +21 -21
- package/witnet/assets/requests.js +95 -95
package/src/bin/cli/radon.js
CHANGED
|
@@ -1,815 +1,815 @@
|
|
|
1
|
-
const fs = require("fs")
|
|
2
|
-
const inquirer = require("inquirer")
|
|
3
|
-
const merge = require("lodash.merge")
|
|
4
|
-
const path = require("path")
|
|
5
|
-
|
|
6
|
-
const helpers = require("../helpers")
|
|
7
|
-
const { Witnet } = require("../../../dist/src")
|
|
8
|
-
|
|
9
|
-
const WITNET_ASSETS_PATH = process.env.WITNET_SDK_RADON_ASSETS_PATH || "../../../../../witnet/assets"
|
|
10
|
-
|
|
11
|
-
/// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
12
|
-
/// CLI SUBMODULE CONSTANTS ===========================================================================================
|
|
13
|
-
|
|
14
|
-
const isModuleInitialized = fs.existsSync("./witnet/assets/index.js")
|
|
15
|
-
|
|
16
|
-
module.exports = {
|
|
17
|
-
flags: {
|
|
18
|
-
module: {
|
|
19
|
-
hint: "Package where to fetch Radon assets from (supersedes --legacy).",
|
|
20
|
-
param: "NPM_PACKAGE",
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
router: isModuleInitialized
|
|
24
|
-
? {
|
|
25
|
-
assets: {
|
|
26
|
-
hint: "List available Witnet Radon assets.",
|
|
27
|
-
params: "[RADON_ASSETS ...]",
|
|
28
|
-
options: {
|
|
29
|
-
legacy: {
|
|
30
|
-
hint: "List only those declared in witnet/assets folder.",
|
|
31
|
-
},
|
|
32
|
-
filter: {
|
|
33
|
-
hint: "Restrict output to name-matching assets.",
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
},
|
|
37
|
-
check: {
|
|
38
|
-
hint: "Check correctness of Witnet Radon artifacts declared in witnet/assets folder.",
|
|
39
|
-
params: [],
|
|
40
|
-
options: {},
|
|
41
|
-
},
|
|
42
|
-
decode: {
|
|
43
|
-
hint: "Break down specs of one or more Radon assets.",
|
|
44
|
-
params: ["RAD_BYTECODE | RAD_HASH | RADON_ASSET"],
|
|
45
|
-
options: {
|
|
46
|
-
json: {
|
|
47
|
-
hint: "Outputs data in JSON format.",
|
|
48
|
-
},
|
|
49
|
-
headline: {
|
|
50
|
-
hint: "Settles output report headline.",
|
|
51
|
-
param: ":string",
|
|
52
|
-
},
|
|
53
|
-
indent: {
|
|
54
|
-
hint: "Prefixes given number of white spaces for every output line.",
|
|
55
|
-
param: ":number",
|
|
56
|
-
},
|
|
57
|
-
},
|
|
58
|
-
},
|
|
59
|
-
"dry-run": {
|
|
60
|
-
hint: "Simulate resolution of one or more Radon assets, as if solved by the Wit/Oracle.",
|
|
61
|
-
params: ["RAD_BYTECODE | RAD_HASH | RADON_ASSET"],
|
|
62
|
-
options: {
|
|
63
|
-
default: {
|
|
64
|
-
hint: "Use default sample parameters on parametrized Radon assets.",
|
|
65
|
-
},
|
|
66
|
-
json: {
|
|
67
|
-
hint: "Outputs data in JSON format.",
|
|
68
|
-
},
|
|
69
|
-
headline: {
|
|
70
|
-
hint: "Settles output report headline.",
|
|
71
|
-
param: ":string",
|
|
72
|
-
},
|
|
73
|
-
indent: {
|
|
74
|
-
hint: "Prefixes given number of white spaces for every output line.",
|
|
75
|
-
param: ":number",
|
|
76
|
-
},
|
|
77
|
-
verbose: {
|
|
78
|
-
hint: "Outputs detailed dry-run report.",
|
|
79
|
-
},
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
}
|
|
83
|
-
: {
|
|
84
|
-
assets: {
|
|
85
|
-
hint: "List available Witnet Radon assets.",
|
|
86
|
-
params: "[RADON_ASSETS ...]",
|
|
87
|
-
options: {
|
|
88
|
-
filter: {
|
|
89
|
-
hint: "Restrict output to name-matching assets.",
|
|
90
|
-
},
|
|
91
|
-
},
|
|
92
|
-
},
|
|
93
|
-
decode: {
|
|
94
|
-
hint: "Break down specs of one or more Radon assets.",
|
|
95
|
-
params: ["RAD_BYTECODE | RAD_HASH | RADON_ASSET"],
|
|
96
|
-
options: {
|
|
97
|
-
json: {
|
|
98
|
-
hint: "Outputs data in JSON format.",
|
|
99
|
-
},
|
|
100
|
-
headline: {
|
|
101
|
-
hint: "Settles output report headline.",
|
|
102
|
-
param: ":string",
|
|
103
|
-
},
|
|
104
|
-
indent: {
|
|
105
|
-
hint: "Prefixes given number of white spaces for every output line.",
|
|
106
|
-
param: ":number",
|
|
107
|
-
},
|
|
108
|
-
},
|
|
109
|
-
},
|
|
110
|
-
"dry-run": {
|
|
111
|
-
hint: "Simulate resolution of one or more Radon assets, as if solved by the Wit/Oracle.",
|
|
112
|
-
params: ["RAD_BYTECODE | RAD_HASH | RADON_ASSET"],
|
|
113
|
-
options: {
|
|
114
|
-
json: {
|
|
115
|
-
hint: "Outputs data in JSON format.",
|
|
116
|
-
},
|
|
117
|
-
headline: {
|
|
118
|
-
hint: "Settles output report headline.",
|
|
119
|
-
param: ":string",
|
|
120
|
-
},
|
|
121
|
-
indent: {
|
|
122
|
-
hint: "Prefixes given number of white spaces for every output line.",
|
|
123
|
-
param: ":number",
|
|
124
|
-
},
|
|
125
|
-
verbose: {
|
|
126
|
-
hint: "Outputs detailed dry-run report.",
|
|
127
|
-
},
|
|
128
|
-
},
|
|
129
|
-
},
|
|
130
|
-
init: {
|
|
131
|
-
hint: "Initialize a Witnet Radon workspace in this project.",
|
|
132
|
-
},
|
|
133
|
-
},
|
|
134
|
-
subcommands: {
|
|
135
|
-
assets, init, check, decode, "dry-run": dryrun,
|
|
136
|
-
},
|
|
137
|
-
loadAssets,
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
142
|
-
/// CLI SUBMODULE COMMANDS ============================================================================================
|
|
143
|
-
|
|
144
|
-
async function init () {
|
|
145
|
-
if (!fs.existsSync("./witnet/assets/")) {
|
|
146
|
-
fs.mkdirSync("./witnet/assets", { recursive: true })
|
|
147
|
-
}
|
|
148
|
-
if (!fs.existsSync(".env_witnet")) {
|
|
149
|
-
fs.cpSync("node_modules/@witnet/sdk/.env_witnet", ".env_witnet")
|
|
150
|
-
}
|
|
151
|
-
if (!fs.existsSync("./witnet/assets/index.js")) {
|
|
152
|
-
fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_index.js", "./witnet/assets/index.js")
|
|
153
|
-
}
|
|
154
|
-
if (!fs.existsSync("./witnet/assets/requests.js")) {
|
|
155
|
-
fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_requests.js", "./witnet/assets/requests.js")
|
|
156
|
-
}
|
|
157
|
-
if (!fs.existsSync("./witnet/assets/sources.js")) {
|
|
158
|
-
fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_sources.js", "./witnet/assets/sources.js")
|
|
159
|
-
}
|
|
160
|
-
if (!fs.existsSync("./witnet/assets/templates.js")) {
|
|
161
|
-
fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_templates.js", "./witnet/assets/templates.js")
|
|
162
|
-
}
|
|
163
|
-
console.info(`Initialized Witnet Radon workspace at folder ${process.cwd()}/witnet/assets`)
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
async function assets (options = {}, [...patterns]) {
|
|
167
|
-
helpers.traceHeader(
|
|
168
|
-
`${options?.module ? options.module.toUpperCase() : path.basename(process.cwd()).toUpperCase()} RADON ASSETS`,
|
|
169
|
-
helpers.colors.white
|
|
170
|
-
)
|
|
171
|
-
const assets = clearEmptyBranches(loadAssets(options), patterns, options?.filter)
|
|
172
|
-
if (assets && Object.keys(assets).length > 0) {
|
|
173
|
-
traceWitnetArtifacts(assets, patterns, " ", options?.filter)
|
|
174
|
-
} else {
|
|
175
|
-
console.info("> No custom Radon assets declared just yet.")
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
/// -------------------------------------------------------------------------------------------------------------------
|
|
179
|
-
|
|
180
|
-
async function check (options = {}) {
|
|
181
|
-
if (!isModuleInitialized) {
|
|
182
|
-
throw new Error(`No Witnet Radon workspace has been initialized. Please, run ${white("npx witnet radon init")} if willing to declare your own Radon assets.`)
|
|
183
|
-
}
|
|
184
|
-
try {
|
|
185
|
-
const assets = loadAssets({ ...options, legacy: true })
|
|
186
|
-
const [
|
|
187
|
-
modals,
|
|
188
|
-
requests,
|
|
189
|
-
sources,
|
|
190
|
-
templates,
|
|
191
|
-
] = [
|
|
192
|
-
helpers.countLeaves(Witnet.Radon.RadonModal, assets),
|
|
193
|
-
helpers.countLeaves(Witnet.Radon.RadonRequest, assets),
|
|
194
|
-
helpers.countLeaves(Witnet.Radon.RadonRetrieval, assets?.sources),
|
|
195
|
-
helpers.countLeaves(Witnet.Radon.RadonTemplate, assets),
|
|
196
|
-
]
|
|
197
|
-
if (modals > 0) console.info("> Radon modals: ", modals)
|
|
198
|
-
if (sources > 0) console.info("> Radon sources: ", sources)
|
|
199
|
-
if (requests > 0) console.info("> Radon requests: ", requests)
|
|
200
|
-
if (templates > 0) console.info("> Radon templates:", templates)
|
|
201
|
-
|
|
202
|
-
if (modals + sources + requests + templates === 0) {
|
|
203
|
-
console.info("-----------------------------")
|
|
204
|
-
console.info("No Radon assets declared yet!")
|
|
205
|
-
} else {
|
|
206
|
-
console.info("--------------------------------------")
|
|
207
|
-
console.info("All Radon assets checked successfully!")
|
|
208
|
-
}
|
|
209
|
-
} catch (e) {
|
|
210
|
-
console.error("Radon assets verification failed:")
|
|
211
|
-
console.info("----------------------------------")
|
|
212
|
-
console.info(e)
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
/// -------------------------------------------------------------------------------------------------------------------
|
|
216
|
-
|
|
217
|
-
async function decode (options = {}, args = []) {
|
|
218
|
-
if (args.length === 0) {
|
|
219
|
-
throw Error("No Radon asset was specified")
|
|
220
|
-
}
|
|
221
|
-
const asset = args[0]
|
|
222
|
-
if (helpers.isHexString(asset)) {
|
|
223
|
-
try {
|
|
224
|
-
const request = Witnet.Radon.RadonRequest.fromBytecode(asset)
|
|
225
|
-
traceWitnetRadonRequest(request, options)
|
|
226
|
-
} catch (err) {
|
|
227
|
-
console.error(err)
|
|
228
|
-
if ((asset.startsWith("0x") && asset.length === 66) || (!asset.startsWith("0x") && asset.length === 64)) {
|
|
229
|
-
// TODO: assume it's a RAD_HASH, and try to retrieve the BYTECODE from the Witnet network
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
} else {
|
|
233
|
-
args = args.slice(1)
|
|
234
|
-
|
|
235
|
-
const assets = loadAssets(options)
|
|
236
|
-
const headline = options?.headline
|
|
237
|
-
const crafts = flattenRadonArtifacts(assets).filter(craft => craft.key.toLowerCase().indexOf(asset.toLowerCase()) >= 0)
|
|
238
|
-
if (crafts.length === 0) {
|
|
239
|
-
throw Error(`No matched found for pattern "${asset}"`)
|
|
240
|
-
}
|
|
241
|
-
for (let { artifact, key } of crafts) {
|
|
242
|
-
let prefix = ""
|
|
243
|
-
if (artifact instanceof Witnet.Radon.RadonRequest) {
|
|
244
|
-
prefix = "RadonRequest::"
|
|
245
|
-
} else if (artifact instanceof Witnet.Radon.RadonModal) {
|
|
246
|
-
artifact.providers = ["https://dummy"]
|
|
247
|
-
const modalArgs = []
|
|
248
|
-
let argIndex = 0
|
|
249
|
-
while (modalArgs.length < artifact.argsCount) {
|
|
250
|
-
modalArgs.push(`{:${++argIndex}}`)
|
|
251
|
-
}
|
|
252
|
-
artifact = artifact.buildRadonRequest(modalArgs)
|
|
253
|
-
prefix = "RadonModal::"
|
|
254
|
-
} else if (artifact instanceof Witnet.Radon.RadonTemplate) {
|
|
255
|
-
const templateArgs = artifact.sources.map(({ argsCount }) =>
|
|
256
|
-
Array.from({ length: argsCount }, (_, i) => `{:${i}}`)
|
|
257
|
-
)
|
|
258
|
-
artifact = artifact.buildRadonRequest(templateArgs)
|
|
259
|
-
prefix = "RadonTemplate::"
|
|
260
|
-
} else if (artifact instanceof Witnet.Radon.RadonRetrieval) {
|
|
261
|
-
if (artifact.argsCount > 0) {
|
|
262
|
-
const retrievalArgs = Array.from(
|
|
263
|
-
{ length: artifact.argsCount },
|
|
264
|
-
(_, i) => `{:${i + 1}}`
|
|
265
|
-
)
|
|
266
|
-
artifact = artifact.foldArgs(retrievalArgs)
|
|
267
|
-
}
|
|
268
|
-
artifact = new Witnet.Radon.RadonRequest({ sources: artifact })
|
|
269
|
-
prefix = "RadonRetrieval::"
|
|
270
|
-
}
|
|
271
|
-
if (!headline) {
|
|
272
|
-
options.headline = `${prefix}${key}`
|
|
273
|
-
}
|
|
274
|
-
traceWitnetRadonRequest(artifact, options)
|
|
275
|
-
if (options?.verbose && key !== crafts[crafts.length - 1].key) {
|
|
276
|
-
console.info(`${options?.indent || ""}${"─".repeat(150)}`)
|
|
277
|
-
}
|
|
278
|
-
console.info()
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
/// -------------------------------------------------------------------------------------------------------------------
|
|
283
|
-
|
|
284
|
-
async function dryrun (options = {}, args = []) {
|
|
285
|
-
if (args.length === 0) {
|
|
286
|
-
throw Error("No Radon asset was specified")
|
|
287
|
-
}
|
|
288
|
-
const asset = args[0]
|
|
289
|
-
if (helpers.isHexString(asset)) {
|
|
290
|
-
try {
|
|
291
|
-
const request = Witnet.Radon.RadonRequest.fromBytecode(asset)
|
|
292
|
-
await traceWitnetRadonRequestDryRun(request, options)
|
|
293
|
-
} catch {
|
|
294
|
-
if ((asset.startsWith("0x") && asset.length === 66) || (!asset.startsWith("0x") && asset.length === 64)) {
|
|
295
|
-
// TODO: assume it's a RAD_HASH, and try to retrieve the BYTECODE from the Witnet network
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
} else {
|
|
299
|
-
args = args.slice(1)
|
|
300
|
-
const assets = loadAssets(options)
|
|
301
|
-
const headline = options?.headline
|
|
302
|
-
const crafts = flattenRadonArtifacts(assets).filter(craft => craft.key.toLowerCase().indexOf(asset.toLowerCase()) >= 0)
|
|
303
|
-
if (crafts.length === 0) {
|
|
304
|
-
throw Error(`No matched found for pattern "${asset}"`)
|
|
305
|
-
}
|
|
306
|
-
for (let { artifact, key } of crafts) {
|
|
307
|
-
let prefix = ""
|
|
308
|
-
if (artifact instanceof Witnet.Radon.RadonRequest) {
|
|
309
|
-
prefix = "RadonRequest::"
|
|
310
|
-
} else {
|
|
311
|
-
if (!artifact?.samples) {
|
|
312
|
-
console.error(`${artifact.constructor.name}::${key}: cannot dry-run if no sample parameters are declared.\n`)
|
|
313
|
-
continue
|
|
314
|
-
}
|
|
315
|
-
let artifactArgs = []
|
|
316
|
-
if (options?.default) {
|
|
317
|
-
artifactArgs = Object.values(artifact.samples)[0]
|
|
318
|
-
} else {
|
|
319
|
-
const prompt = inquirer.createPromptModule()
|
|
320
|
-
const sample = await prompt([{
|
|
321
|
-
choices: Object.keys(artifact.samples),
|
|
322
|
-
message: `${artifact.constructor.name}::${key} args:`,
|
|
323
|
-
name: "key",
|
|
324
|
-
type: "list",
|
|
325
|
-
}])
|
|
326
|
-
artifactArgs = artifact.samples[sample.key]
|
|
327
|
-
}
|
|
328
|
-
if (artifact instanceof Witnet.Radon.RadonModal) {
|
|
329
|
-
artifact.providers = artifactArgs.shift().split(";")
|
|
330
|
-
artifact = artifact.buildRadonRequest(artifactArgs)
|
|
331
|
-
prefix = "RadonModal::"
|
|
332
|
-
} else if (artifact instanceof Witnet.Radon.RadonTemplate) {
|
|
333
|
-
artifact = artifact.buildRadonRequest(artifactArgs)
|
|
334
|
-
prefix = "RadonTemplate::"
|
|
335
|
-
} else if (artifact instanceof Witnet.Radon.RadonRetrieval) {
|
|
336
|
-
artifact = new Witnet.Radon.RadonRequest({ sources: artifact.foldArgs(artifactArgs) })
|
|
337
|
-
prefix = "RadonRetrieval::"
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
if (!headline) {
|
|
341
|
-
options.headline = `${prefix}${key}`
|
|
342
|
-
}
|
|
343
|
-
await traceWitnetRadonRequestDryRun(artifact, options)
|
|
344
|
-
if (options?.verbose && key !== crafts[crafts.length - 1].key) {
|
|
345
|
-
console.info(`${options?.indent || ""}${"─".repeat(150)}`)
|
|
346
|
-
}
|
|
347
|
-
console.info()
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
/// -------------------------------------------------------------------------------------------------------------------
|
|
352
|
-
|
|
353
|
-
/// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
354
|
-
/// CLI SUBMODULE INTERNAL METHODS ------------------------------------------------------------------------------------
|
|
355
|
-
|
|
356
|
-
const extractTypeName = (str) => str ? str.split(/(?=[A-Z])/).slice(1).join("") : "Any"
|
|
357
|
-
|
|
358
|
-
const stringifyFilter = (x, c) => {
|
|
359
|
-
const color = c || helpers.colors.mcyan
|
|
360
|
-
return color(`${Witnet.Radon.filters.Opcodes[x.opcode]}(${x.args ? JSON.stringify(x.args) : ""})`)
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
const stringifyReducer = (x, c) => {
|
|
364
|
-
const color = c || helpers.colors.mcyan
|
|
365
|
-
return color(`${Witnet.Radon.reducers.Opcodes[x.opcode]}()`)
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
function loadAssets (options) {
|
|
369
|
-
const { assets } = options?.module ? require(options.module) : (options?.legacy ? {} : require("@witnet/sdk"))
|
|
370
|
-
return isModuleInitialized && fs.existsSync(`${WITNET_ASSETS_PATH}`) ? merge(assets, require(`${WITNET_ASSETS_PATH}`)) : assets
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
function flattenRadonArtifacts (tree, headers) {
|
|
374
|
-
if (!headers) headers = []
|
|
375
|
-
const matches = []
|
|
376
|
-
for (const key in tree) {
|
|
377
|
-
if (
|
|
378
|
-
tree[key] instanceof Witnet.Radon.RadonRetrieval ||
|
|
379
|
-
tree[key] instanceof Witnet.Radon.RadonModal ||
|
|
380
|
-
tree[key] instanceof Witnet.Radon.RadonRequest ||
|
|
381
|
-
tree[key] instanceof Witnet.Radon.RadonTemplate
|
|
382
|
-
) {
|
|
383
|
-
matches.push({
|
|
384
|
-
key,
|
|
385
|
-
artifact: tree[key],
|
|
386
|
-
})
|
|
387
|
-
} else if (typeof tree[key] === "object") {
|
|
388
|
-
matches.push(...flattenRadonArtifacts(
|
|
389
|
-
tree[key],
|
|
390
|
-
[...headers, key]
|
|
391
|
-
))
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
return matches
|
|
395
|
-
};
|
|
396
|
-
|
|
397
|
-
function countWitnetArtifacts (assets, args, filter = false) {
|
|
398
|
-
let counter = 0
|
|
399
|
-
Object.entries(assets).forEach(([key, value]) => {
|
|
400
|
-
if ((
|
|
401
|
-
value instanceof Witnet.Radon.RadonModal ||
|
|
402
|
-
value instanceof Witnet.Radon.RadonRequest ||
|
|
403
|
-
value instanceof Witnet.Radon.RadonTemplate ||
|
|
404
|
-
value instanceof Witnet.Radon.RadonRetrieval
|
|
405
|
-
) && (
|
|
406
|
-
!filter ||
|
|
407
|
-
!args ||
|
|
408
|
-
args.length === 0 ||
|
|
409
|
-
args.find(arg => key.toLowerCase().indexOf(arg.toLowerCase()) >= 0)
|
|
410
|
-
)) {
|
|
411
|
-
counter++
|
|
412
|
-
} else if (typeof value === "object") {
|
|
413
|
-
counter += countWitnetArtifacts(value, args)
|
|
414
|
-
}
|
|
415
|
-
})
|
|
416
|
-
return counter
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
function clearEmptyBranches (node, args, filter) {
|
|
420
|
-
if (node) {
|
|
421
|
-
const assets = Object.fromEntries(
|
|
422
|
-
Object.entries(node).map(([key, value]) => {
|
|
423
|
-
if (
|
|
424
|
-
(!filter || args.find(arg => key.toLowerCase().indexOf(arg.toLowerCase()) >= 0)) && (
|
|
425
|
-
value instanceof Witnet.Radon.RadonRetrieval ||
|
|
426
|
-
value instanceof Witnet.Radon.RadonModal ||
|
|
427
|
-
value instanceof Witnet.Radon.RadonRequest ||
|
|
428
|
-
value instanceof Witnet.Radon.RadonTemplate
|
|
429
|
-
)
|
|
430
|
-
) {
|
|
431
|
-
return [key, value]
|
|
432
|
-
} else if (typeof value === "object") {
|
|
433
|
-
if (countWitnetArtifacts(value, args, filter) > 0) {
|
|
434
|
-
return [key, clearEmptyBranches(value, args, filter)]
|
|
435
|
-
} else {
|
|
436
|
-
return [key, undefined]
|
|
437
|
-
}
|
|
438
|
-
} else {
|
|
439
|
-
return [key, undefined]
|
|
440
|
-
}
|
|
441
|
-
})
|
|
442
|
-
.filter(([, value]) => value !== undefined)
|
|
443
|
-
)
|
|
444
|
-
if (Object.keys(assets).length > 0) {
|
|
445
|
-
return assets
|
|
446
|
-
} else {
|
|
447
|
-
return undefined
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
function traceWitnetArtifacts (assets, args, indent = "", filter = false) {
|
|
453
|
-
const prefix = `${indent}`
|
|
454
|
-
Object.keys(assets).forEach((key, index) => {
|
|
455
|
-
const isLast = index === Object.keys(assets).length - 1
|
|
456
|
-
const found = args.find(arg => key.toLowerCase().indexOf(arg.toLowerCase()) >= 0)
|
|
457
|
-
const color = found ? helpers.colors.mcyan : helpers.colors.cyan
|
|
458
|
-
if (assets[key] instanceof Witnet.Radon.RadonRequest) {
|
|
459
|
-
if (!filter || found) {
|
|
460
|
-
console.info(`${prefix}${color(key)}`)
|
|
461
|
-
if (isLast) {
|
|
462
|
-
console.info(`${prefix}`)
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
} else if (
|
|
466
|
-
assets[key] instanceof Witnet.Radon.RadonTemplate ||
|
|
467
|
-
assets[key] instanceof Witnet.Radon.RadonModal
|
|
468
|
-
) {
|
|
469
|
-
const argsCount = assets[key].argsCount
|
|
470
|
-
if (!filter || found) {
|
|
471
|
-
console.info(`${prefix}${color(key)} ${argsCount > 0 ? helpers.colors.green(`(${argsCount} args)`) : ""}`)
|
|
472
|
-
if (isLast) {
|
|
473
|
-
console.info(`${prefix}`)
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
} else if (assets[key] instanceof Witnet.Radon.RadonRetrieval) {
|
|
477
|
-
const argsCount = assets[key].argsCount
|
|
478
|
-
if (!filter || found) {
|
|
479
|
-
console.info(`${prefix}${color(key)} ${argsCount > 0 ? helpers.colors.green(`(${argsCount} args)`) : ""}`)
|
|
480
|
-
if (isLast) {
|
|
481
|
-
console.info(`${prefix}`)
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
} else if (typeof assets[key] === "object" && countWitnetArtifacts(assets[key], args, filter) > 0) {
|
|
485
|
-
console.info(`${indent}${isLast ? "└─ " : "├─ "}${key}`)
|
|
486
|
-
traceWitnetArtifacts(assets[key], args, !isLast ? `${indent}│ ` : `${indent} `, filter)
|
|
487
|
-
}
|
|
488
|
-
})
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
function traceWitnetRadonReportHeadline (request, options) {
|
|
492
|
-
const trait = (str) => `${str}${" ".repeat(66 - str.length)}`
|
|
493
|
-
const indent = options?.indent ? " ".repeat(options.indent) : ""
|
|
494
|
-
const resultDataType = `Result<${extractTypeName(request.sources[0]?.script?.outputType.constructor.name)}, RadonError>`
|
|
495
|
-
console.info(`${indent}╔══════════════════════════════════════════════════════════════════════════════╗`)
|
|
496
|
-
console.info(`${indent}║ ${helpers.colors.white(options?.headline)}${" ".repeat(77 - options?.headline.length)}║`)
|
|
497
|
-
console.info(`${indent}╠══════════════════════════════════════════════════════════════════════════════╣`)
|
|
498
|
-
console.info(`${indent}║ ${helpers.colors.white("RAD hash")}: ${helpers.colors.lgreen(request.radHash)} ║`)
|
|
499
|
-
console.info(`${indent}║ RAD size: ${helpers.colors.green(trait(helpers.commas(request.weight()) + " bytes"))} ║`)
|
|
500
|
-
console.info(`${indent}║ RAD type: ${helpers.colors.yellow(trait(resultDataType))} ║`)
|
|
501
|
-
// if (!options.verbose) {
|
|
502
|
-
// console.info(`${indent}║ > Radon operators: ${white(trait(commas(request.opsCount())))} ║`)
|
|
503
|
-
// }
|
|
504
|
-
// console.info(`${indent}╠════════════════════════════════════════════════════════════════════════════╣`)
|
|
505
|
-
// console.info(`${indent}║ > Times solved: ${white(trait("{ values: 123, errors: 220 }"))} ║`)
|
|
506
|
-
// console.info(`${indent}║ > Times witnessed: ${white(trait("{ values: 2130, errors: 1326 }"))} ║`)
|
|
507
|
-
// console.info(`${indent}║ > Total fees: ${white(trait("15,234.123 Wits"))} ║`)
|
|
508
|
-
// console.info(`${indent}║ > Total slash: ${white(trait(" 56.123 Wits"))} ║`)
|
|
509
|
-
// console.info(`${indent}║ > Total burn: ${white(trait(" 0.789 Wits"))} ║`)
|
|
510
|
-
// if (verbose) {
|
|
511
|
-
// console.info(`${indent}╚══╤═════════════════════════════════════════════════════════════════════════╝`)
|
|
512
|
-
// } else {
|
|
513
|
-
// console.info(`${indent}╚════════════════════════════════════════════════════════════════════════════╝`)
|
|
514
|
-
// }
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
function traceWitnetRadonRequest (request, options) {
|
|
518
|
-
const indent = options?.indent ? " ".repeat(parseInt(options.indent)) : ""
|
|
519
|
-
if (options?.json) {
|
|
520
|
-
console.info(JSON.stringify(request.toProtobuf(), null, options?.indent || 0))
|
|
521
|
-
} else {
|
|
522
|
-
if (!options.headline) options.headline = "WITNET DATA REQUEST DISASSEMBLE"
|
|
523
|
-
traceWitnetRadonReportHeadline(request, options)
|
|
524
|
-
|
|
525
|
-
console.info(`${indent}╚══╤═══════════════════════════════════════════════════════════════════════════╝`)
|
|
526
|
-
console.info(`${indent}┌──┴─────────────────┐`)
|
|
527
|
-
console.info(`${indent}│ ${helpers.colors.white("RETRIEVE DATA")} │`) // ├ ┤
|
|
528
|
-
console.info(`${indent}└──┬─┬───────────────┘`)
|
|
529
|
-
request.sources.forEach((source, sourceIndex) => {
|
|
530
|
-
const authority = source.authority?.toUpperCase().split(".").slice(-2).join(".") || (
|
|
531
|
-
source.method === Witnet.Radon.retrievals.Methods.RNG ? "WIT/RNG" : ""
|
|
532
|
-
)
|
|
533
|
-
const corner = sourceIndex === request.sources.length - 1 ? "└" : "├"
|
|
534
|
-
const sep = sourceIndex === request.sources.length - 1 ? " " : "│"
|
|
535
|
-
console.info(
|
|
536
|
-
`${indent} │ ${corner}─ ${helpers.colors.white("[ ")}${helpers.colors.white(`Data source #${sourceIndex + 1}`)
|
|
537
|
-
} ${" ".repeat(3 - sourceIndex.toString().length)}${helpers.colors.white(authority)} ${helpers.colors.white("]")}`
|
|
538
|
-
)
|
|
539
|
-
if (source.method !== Witnet.Radon.retrievals.Methods.RNG) {
|
|
540
|
-
console.info(
|
|
541
|
-
`${indent} │ ${sep} > Request: ${
|
|
542
|
-
helpers.colors.mgreen(Witnet.Radon.retrievals.Methods[source.method].split(/(?=[A-Z])/).join("-").toUpperCase())
|
|
543
|
-
}`
|
|
544
|
-
)
|
|
545
|
-
console.info(`${indent} │ ${sep} > URL query: ${helpers.colors.green(source.url)}`)
|
|
546
|
-
if (source?.headers) {
|
|
547
|
-
console.info(`${indent} │ ${sep} > HTTP headers: ${helpers.colors.green(JSON.stringify(source.headers))}`)
|
|
548
|
-
}
|
|
549
|
-
if (source?.body) {
|
|
550
|
-
console.info(`${indent} │ ${sep} > HTTP body: ${helpers.colors.green(source.body)}`)
|
|
551
|
-
}
|
|
552
|
-
if (source?.script) {
|
|
553
|
-
const steps = source.script.disect()
|
|
554
|
-
console.info(
|
|
555
|
-
`${indent} │ ${sep} > Radon script: ${helpers.colors.lyellow("[ ")
|
|
556
|
-
}${helpers.colors.yellow(steps[0][1])}${" ".repeat(12 - steps[0][1].length)
|
|
557
|
-
}${helpers.colors.lyellow(" ]")
|
|
558
|
-
} ${helpers.colors.mcyan(steps[0][2])}`
|
|
559
|
-
)
|
|
560
|
-
steps.slice(1).forEach(step => {
|
|
561
|
-
console.info(
|
|
562
|
-
`${indent} │ ${sep} ${helpers.colors.lyellow("[ ")
|
|
563
|
-
}${helpers.colors.yellow(step[1])
|
|
564
|
-
}${" ".repeat(12 - step[1].length)
|
|
565
|
-
}${helpers.colors.lyellow(" ]")
|
|
566
|
-
} ${" ".repeat(2 * step[0])}${helpers.colors.mcyan(step[2])
|
|
567
|
-
}`
|
|
568
|
-
)
|
|
569
|
-
})
|
|
570
|
-
const outputType = source.script.outputType.constructor.name || "RadonAny"
|
|
571
|
-
console.info(
|
|
572
|
-
`${indent} │ ${sep} ${helpers.colors.lyellow("[ ")
|
|
573
|
-
}${helpers.colors.yellow(outputType)
|
|
574
|
-
}${" ".repeat(12 - outputType.length)
|
|
575
|
-
}${helpers.colors.lyellow(" ]")
|
|
576
|
-
}`
|
|
577
|
-
)
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
if (sourceIndex < request.sources.length - 1) {
|
|
581
|
-
console.info(`${indent} │ │`)
|
|
582
|
-
}
|
|
583
|
-
})
|
|
584
|
-
console.info(`${indent}┌──┴──────────────────┐`)
|
|
585
|
-
console.info(`${indent}│ ${helpers.colors.white("AGGREGATE SOURCES")} │`)
|
|
586
|
-
console.info(`${indent}└──┬──────────────────┘`) // ┬
|
|
587
|
-
request.sourcesReducer?.filters.forEach(filter => console.info(`${indent} │ > Radon filter: ${stringifyFilter(filter)}`))
|
|
588
|
-
console.info(`${indent} │ > Radon reducer: ${stringifyReducer(request.sourcesReducer)}`)
|
|
589
|
-
console.info(`${indent}┌──┴──────────────────┐`)
|
|
590
|
-
console.info(`${indent}│ ${helpers.colors.white("WITNESSING TALLY")} │`)
|
|
591
|
-
console.info(`${indent}└─────────────────────┘`) // ┬
|
|
592
|
-
request.witnessReducer?.filters.forEach(filter => console.info(`${indent} > Radon filter: ${stringifyFilter(filter)}`))
|
|
593
|
-
console.info(`${indent} > Radon reducer: ${stringifyReducer(request.witnessReducer)}`)
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
async function traceWitnetRadonRequestDryRun (request, options) {
|
|
598
|
-
const bytecode = request.toBytecode()
|
|
599
|
-
let report = await helpers
|
|
600
|
-
.toolkitRun(options, ["try-data-request", "--hex", bytecode.startsWith("0x") ? bytecode.slice(2) : bytecode])
|
|
601
|
-
.catch((err) => {
|
|
602
|
-
let errorMessage = err.message.split("\n").slice(1).join("\n").trim()
|
|
603
|
-
const errorRegex = /.*^error: (?<message>.*)$.*/gm
|
|
604
|
-
const matched = errorRegex.exec(err.message)
|
|
605
|
-
if (matched) {
|
|
606
|
-
errorMessage = matched.groups.message
|
|
607
|
-
}
|
|
608
|
-
throw errorMessage || err
|
|
609
|
-
})
|
|
610
|
-
if (!report) {
|
|
611
|
-
throw Error("No dry-report?")
|
|
612
|
-
} else {
|
|
613
|
-
report = JSON.parse(report)
|
|
614
|
-
}
|
|
615
|
-
const result = report?.aggregate.result
|
|
616
|
-
const resultType = Object.keys(result)[0]
|
|
617
|
-
const resultValue = Object.values(result)[0]
|
|
618
|
-
if (options?.json) {
|
|
619
|
-
if (options?.verbose) {
|
|
620
|
-
console.info(JSON.stringify(report, null, options?.indent ? " ".repeat(options.indent) : ""))
|
|
621
|
-
} else {
|
|
622
|
-
result[resultType] = resultValue
|
|
623
|
-
console.info(JSON.stringify(result, null, options?.indent ? " ".repeat(options.indent) : ""))
|
|
624
|
-
}
|
|
625
|
-
return
|
|
626
|
-
}
|
|
627
|
-
if (!options.headline) options.headline = "WITNET DATA REQUEST DRY-RUN REPORT"
|
|
628
|
-
traceWitnetRadonReportHeadline(request, options)
|
|
629
|
-
const indent = options?.indent ? " ".repeat(options.indent) : ""
|
|
630
|
-
console.info(`${indent}╚══╤═══════════════════════════════════════════════════════════════════════════╝`)
|
|
631
|
-
let execTimeMs = report.retrieve?.map(retrieval =>
|
|
632
|
-
(retrieval?.running_time.secs || 0) + (retrieval?.running_time.nanos || 0) / 1000
|
|
633
|
-
).reduce(
|
|
634
|
-
(sum, secs) => sum + secs
|
|
635
|
-
)
|
|
636
|
-
execTimeMs = Math.round(execTimeMs) + " ms"
|
|
637
|
-
let flexbar = "─".repeat(17)
|
|
638
|
-
let flexspc = " ".repeat(flexbar.length + 12)
|
|
639
|
-
console.info(`${indent}┌──┴─────────────────────────────${flexbar}──────┐`)
|
|
640
|
-
console.info(`${indent}│ ${helpers.colors.white("Data providers")} ${flexspc} │`) // ├ ┤
|
|
641
|
-
console.info(`${indent}├────────────────────────────────${flexbar}──────┤`)
|
|
642
|
-
console.info(`${indent}│ Execution time: ${helpers.colors.green(execTimeMs)} ${" ".repeat(flexbar.length + 19 - execTimeMs.length)} │`)
|
|
643
|
-
console.info(`${indent}└──┬─┬───────────────────────────${flexbar}──────┘`)
|
|
644
|
-
request.sources.forEach((source, sourceIndex) => {
|
|
645
|
-
const authority = source.authority?.toUpperCase().split(".").slice(-2).join(".") || (
|
|
646
|
-
source.method === Witnet.Radon.retrievals.Methods.RNG ? "WIT/RNG" : ""
|
|
647
|
-
)
|
|
648
|
-
const corner = sourceIndex === request.sources.length - 1 ? "└" : "├"
|
|
649
|
-
const sep = sourceIndex === request.sources.length - 1 ? " " : "│"
|
|
650
|
-
const color = report.retrieve[sourceIndex].result?.RadonError
|
|
651
|
-
? (options?.verbose ? helpers.colors.lgray : helpers.colors.gray)
|
|
652
|
-
: (options?.verbose ? helpers.colors.lgreen : helpers.colors.green)
|
|
653
|
-
if (options?.verbose) {
|
|
654
|
-
console.info(
|
|
655
|
-
`${indent} │ ${corner}─ ${
|
|
656
|
-
helpers.colors.white("[ ")
|
|
657
|
-
}${
|
|
658
|
-
helpers.colors.white(`Data Source #${sourceIndex + 1}`)
|
|
659
|
-
} ${
|
|
660
|
-
" ".repeat(3 - sourceIndex.toString().length)
|
|
661
|
-
}${
|
|
662
|
-
color(authority)
|
|
663
|
-
} ${
|
|
664
|
-
helpers.colors.white("]")
|
|
665
|
-
}`
|
|
666
|
-
)
|
|
667
|
-
} else {
|
|
668
|
-
console.info(`${indent} │ ${corner}─ [ ${color(authority)} ]`)
|
|
669
|
-
}
|
|
670
|
-
if (source.method !== Witnet.Radon.retrievals.Methods.RNG && options?.verbose) {
|
|
671
|
-
// const result = report.retrieve[sourceIndex].result
|
|
672
|
-
// const resultType = Object.keys(result)[0]
|
|
673
|
-
// const resultValue = JSON.stringify(Object.values(result)[0])
|
|
674
|
-
console.info(
|
|
675
|
-
`${indent} │ ${sep} > Request: ${
|
|
676
|
-
helpers.colors.mgreen(Witnet.Radon.retrievals.Methods[source.method].split(/(?=[A-Z])/).join("-").toUpperCase())
|
|
677
|
-
}`
|
|
678
|
-
)
|
|
679
|
-
console.info(`${indent} │ ${sep} > URL query: ${helpers.colors.green(source.url)}`)
|
|
680
|
-
if (source?.headers) {
|
|
681
|
-
console.info(`${indent} │ ${sep} > HTTP headers: ${helpers.colors.green(JSON.stringify(source.headers))}`)
|
|
682
|
-
}
|
|
683
|
-
if (source?.body) {
|
|
684
|
-
console.info(`${indent} │ ${sep} > HTTP body: ${helpers.colors.green(source.body)}`)
|
|
685
|
-
}
|
|
686
|
-
const printData = (headline, data, color) => {
|
|
687
|
-
const type = Object.keys(data)[0]
|
|
688
|
-
data = typeof data[type] === "object" || Array.isArray(data[type]) ? JSON.stringify(data[type]) : data[type]
|
|
689
|
-
const lines = data.match(/.{1,96}/g).slice(0, 256)
|
|
690
|
-
if (lines.length === 256) lines[255] += "..."
|
|
691
|
-
const typeColor = (type === "RadonError") ? helpers.colors.red : helpers.colors.yellow
|
|
692
|
-
const lineColor = (type === "RadonError") ? helpers.colors.gray : color
|
|
693
|
-
console.info(
|
|
694
|
-
`${indent} │ ${sep} > ${headline}${" ".repeat(15 - headline.length)} \x1b[1;m${
|
|
695
|
-
typeColor("[ ")
|
|
696
|
-
}\x1b[0m${typeColor(type)}${
|
|
697
|
-
" ".repeat(12 - type.length)
|
|
698
|
-
}\x1b[1;m${typeColor(" ]")}\x1b[0m ${
|
|
699
|
-
lineColor(lines[0])
|
|
700
|
-
}`)
|
|
701
|
-
lines.slice(1).forEach(line => {
|
|
702
|
-
console.info(`${indent} │ ${sep} ${lineColor(line)}`)
|
|
703
|
-
})
|
|
704
|
-
}
|
|
705
|
-
if (report?.retrieve[sourceIndex]?.partial_results) {
|
|
706
|
-
printData("HTTP response:", report?.retrieve[sourceIndex]?.partial_results[0], helpers.colors.cyan)
|
|
707
|
-
}
|
|
708
|
-
printData("Radon result:", report?.retrieve[sourceIndex]?.result, helpers.colors.mcyan)
|
|
709
|
-
}
|
|
710
|
-
if (options?.verbose && sourceIndex < request.sources.length - 1) {
|
|
711
|
-
console.info(`${indent} │ │`)
|
|
712
|
-
}
|
|
713
|
-
})
|
|
714
|
-
flexbar = "─".repeat(24)
|
|
715
|
-
flexspc = " ".repeat(36)
|
|
716
|
-
console.info(`${indent}┌──┴───────────────────────────${flexbar}─┐`)
|
|
717
|
-
console.info(`${indent}│ ${helpers.colors.white("Aggregated result")}${flexspc} │`) // ├ ┤
|
|
718
|
-
console.info(`${indent}├──────────────────────────────${flexbar}─┤`)
|
|
719
|
-
if (options?.verbose) {
|
|
720
|
-
let partial_index = 0
|
|
721
|
-
const partial_results = report.sourcesReducer?.partial_results
|
|
722
|
-
request.sourcesReducer?.filters.forEach(filter => {
|
|
723
|
-
const color = (partial_results && partial_results[partial_index]?.RadonArray) ? helpers.colors.mcyan : helpers.colors.gray
|
|
724
|
-
const items = (partial_results && partial_results[partial_index]?.RadonArray)
|
|
725
|
-
? ` over ${partial_results[partial_index]?.RadonArray.length} sources`
|
|
726
|
-
: ""
|
|
727
|
-
partial_index += 1
|
|
728
|
-
filter = stringifyFilter(filter, color)
|
|
729
|
-
console.info(
|
|
730
|
-
`${indent}│ Radon filter: ${filter}${
|
|
731
|
-
helpers.colors.cyan(items)
|
|
732
|
-
}${
|
|
733
|
-
" ".repeat(flexbar.length + 22 - filter.length - items.length)
|
|
734
|
-
} │`
|
|
735
|
-
)
|
|
736
|
-
})
|
|
737
|
-
const color = (partial_results && partial_results[partial_index]?.RadonArray) ? helpers.colors.mcyan : helpers.colors.gray
|
|
738
|
-
const items = (partial_results && partial_results[partial_index]?.RadonArray)
|
|
739
|
-
? ` over ${partial_results[partial_index]?.RadonArray.length} sources`
|
|
740
|
-
: ""
|
|
741
|
-
const reducer = stringifyReducer(request.sourcesReducer, color)
|
|
742
|
-
console.info(
|
|
743
|
-
`${indent}│ Radon reducer: ${reducer}${
|
|
744
|
-
helpers.colors.cyan(items)}${" ".repeat(flexbar.length + 22 - reducer.length - items.length)
|
|
745
|
-
} │`
|
|
746
|
-
)
|
|
747
|
-
}
|
|
748
|
-
console.info(`${indent}│ Result size: ${helpers.colors.cyan("xxx bytes")}${" ".repeat(flexbar.length + 13 - 9)} │`)
|
|
749
|
-
console.info(`${indent}└────┬─────────────────────────${flexbar}─┘`)
|
|
750
|
-
const printMapItem = (indent, width, key, value, indent2 = "") => {
|
|
751
|
-
if (key) key = `${indent2}"${key}": `
|
|
752
|
-
else key = `${indent2}`
|
|
753
|
-
let type = extractTypeName(Object.keys(value)[0])
|
|
754
|
-
value = Object.values(value)[0]
|
|
755
|
-
if (["Map", "Array"].includes(type)) {
|
|
756
|
-
if (key.length > width - 12) {
|
|
757
|
-
console.info(
|
|
758
|
-
`${indent} ${
|
|
759
|
-
helpers.colors.myellow(`[ ${type}${" ".repeat(7 - type.length)} ]`)
|
|
760
|
-
} ${
|
|
761
|
-
" ".repeat(width - 15)}${helpers.colors.green("...")
|
|
762
|
-
}`
|
|
763
|
-
)
|
|
764
|
-
} else {
|
|
765
|
-
console.info(
|
|
766
|
-
`${indent} ${
|
|
767
|
-
helpers.colors.myellow(`[ ${type}${" ".repeat(7 - type.length)} ]`)
|
|
768
|
-
} ${
|
|
769
|
-
helpers.colors.green(key)
|
|
770
|
-
}${
|
|
771
|
-
" ".repeat(width - 12 - key.length)
|
|
772
|
-
}`
|
|
773
|
-
)
|
|
774
|
-
}
|
|
775
|
-
Object.entries(value).forEach(([key, value]) => printMapItem(indent, width, type === "Map" ? key : null, value, indent2 + " "))
|
|
776
|
-
} else {
|
|
777
|
-
if (key.length > width - 12) {
|
|
778
|
-
console.info(`${indent} ${helpers.colors.yellow(type)} ${" ".repeat(width - 15)}${helpers.colors.green("...")}`)
|
|
779
|
-
} else {
|
|
780
|
-
if (["String", "Error"].includes(type)) {
|
|
781
|
-
value = JSON.stringify(value)
|
|
782
|
-
}
|
|
783
|
-
type = `[ ${type}${" ".repeat(7 - type.length)} ]`
|
|
784
|
-
const result = key + value
|
|
785
|
-
// let spaces = width - 12 - result.length
|
|
786
|
-
if (result.length > width - 15) {
|
|
787
|
-
value = value.slice(0, width - 15 - key.length) + "..."
|
|
788
|
-
// spaces = 0
|
|
789
|
-
}
|
|
790
|
-
console.info(`${indent} ${helpers.colors.yellow(type)} ${helpers.colors.green(key)}${helpers.colors.mcyan(value)}`)
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
}
|
|
794
|
-
const printResult = (indent, width, resultType, resultValue) => {
|
|
795
|
-
resultType = extractTypeName(resultType)
|
|
796
|
-
resultValue = typeof resultValue === "object" || Array.isArray(resultValue) ? JSON.stringify(resultValue) : resultValue
|
|
797
|
-
if (["Map", "Array"].includes(resultType)) {
|
|
798
|
-
console.info(`${indent} └─ ${helpers.colors.lyellow(`[ ${resultType}${" ".repeat(7 - resultType.length)} ]`)}`)
|
|
799
|
-
const obj = JSON.parse(resultValue)
|
|
800
|
-
Object.entries(obj).forEach(([key, value]) => printMapItem(indent, width, resultType === "Map" ? key : null, value))
|
|
801
|
-
} else {
|
|
802
|
-
if (resultType === "Bytes") {
|
|
803
|
-
resultValue = JSON.parse(resultValue).map(char => ("00" + char.toString(16)).slice(-2)).join("")
|
|
804
|
-
}
|
|
805
|
-
const color = resultType.indexOf("Error") > -1 ? helpers.colors.gray : helpers.colors.lcyan
|
|
806
|
-
const typeText = resultType.indexOf("Error") > -1 ? "\x1b[1;98;41m Error \x1b[0m" : helpers.colors.lyellow(`[ ${resultType} ]`)
|
|
807
|
-
const lines = resultValue.match(/.{1,96}/g).slice(0, 256)
|
|
808
|
-
console.info(`${indent} └─ ${typeText} ${color(lines[0])}`)
|
|
809
|
-
lines.slice(1).forEach(line => {
|
|
810
|
-
console.info(`${indent} ${" ".repeat(resultType.length)}${color(line)}`)
|
|
811
|
-
})
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
printResult(indent, 134, resultType, resultValue)
|
|
815
|
-
}
|
|
1
|
+
const fs = require("fs")
|
|
2
|
+
const inquirer = require("inquirer")
|
|
3
|
+
const merge = require("lodash.merge")
|
|
4
|
+
const path = require("path")
|
|
5
|
+
|
|
6
|
+
const helpers = require("../helpers")
|
|
7
|
+
const { Witnet } = require("../../../dist/src")
|
|
8
|
+
|
|
9
|
+
const WITNET_ASSETS_PATH = process.env.WITNET_SDK_RADON_ASSETS_PATH || "../../../../../witnet/assets"
|
|
10
|
+
|
|
11
|
+
/// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
12
|
+
/// CLI SUBMODULE CONSTANTS ===========================================================================================
|
|
13
|
+
|
|
14
|
+
const isModuleInitialized = fs.existsSync("./witnet/assets/index.js")
|
|
15
|
+
|
|
16
|
+
module.exports = {
|
|
17
|
+
flags: {
|
|
18
|
+
module: {
|
|
19
|
+
hint: "Package where to fetch Radon assets from (supersedes --legacy).",
|
|
20
|
+
param: "NPM_PACKAGE",
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
router: isModuleInitialized
|
|
24
|
+
? {
|
|
25
|
+
assets: {
|
|
26
|
+
hint: "List available Witnet Radon assets.",
|
|
27
|
+
params: "[RADON_ASSETS ...]",
|
|
28
|
+
options: {
|
|
29
|
+
legacy: {
|
|
30
|
+
hint: "List only those declared in witnet/assets folder.",
|
|
31
|
+
},
|
|
32
|
+
filter: {
|
|
33
|
+
hint: "Restrict output to name-matching assets.",
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
check: {
|
|
38
|
+
hint: "Check correctness of Witnet Radon artifacts declared in witnet/assets folder.",
|
|
39
|
+
params: [],
|
|
40
|
+
options: {},
|
|
41
|
+
},
|
|
42
|
+
decode: {
|
|
43
|
+
hint: "Break down specs of one or more Radon assets.",
|
|
44
|
+
params: ["RAD_BYTECODE | RAD_HASH | RADON_ASSET"],
|
|
45
|
+
options: {
|
|
46
|
+
json: {
|
|
47
|
+
hint: "Outputs data in JSON format.",
|
|
48
|
+
},
|
|
49
|
+
headline: {
|
|
50
|
+
hint: "Settles output report headline.",
|
|
51
|
+
param: ":string",
|
|
52
|
+
},
|
|
53
|
+
indent: {
|
|
54
|
+
hint: "Prefixes given number of white spaces for every output line.",
|
|
55
|
+
param: ":number",
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
"dry-run": {
|
|
60
|
+
hint: "Simulate resolution of one or more Radon assets, as if solved by the Wit/Oracle.",
|
|
61
|
+
params: ["RAD_BYTECODE | RAD_HASH | RADON_ASSET"],
|
|
62
|
+
options: {
|
|
63
|
+
default: {
|
|
64
|
+
hint: "Use default sample parameters on parametrized Radon assets.",
|
|
65
|
+
},
|
|
66
|
+
json: {
|
|
67
|
+
hint: "Outputs data in JSON format.",
|
|
68
|
+
},
|
|
69
|
+
headline: {
|
|
70
|
+
hint: "Settles output report headline.",
|
|
71
|
+
param: ":string",
|
|
72
|
+
},
|
|
73
|
+
indent: {
|
|
74
|
+
hint: "Prefixes given number of white spaces for every output line.",
|
|
75
|
+
param: ":number",
|
|
76
|
+
},
|
|
77
|
+
verbose: {
|
|
78
|
+
hint: "Outputs detailed dry-run report.",
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
}
|
|
83
|
+
: {
|
|
84
|
+
assets: {
|
|
85
|
+
hint: "List available Witnet Radon assets.",
|
|
86
|
+
params: "[RADON_ASSETS ...]",
|
|
87
|
+
options: {
|
|
88
|
+
filter: {
|
|
89
|
+
hint: "Restrict output to name-matching assets.",
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
decode: {
|
|
94
|
+
hint: "Break down specs of one or more Radon assets.",
|
|
95
|
+
params: ["RAD_BYTECODE | RAD_HASH | RADON_ASSET"],
|
|
96
|
+
options: {
|
|
97
|
+
json: {
|
|
98
|
+
hint: "Outputs data in JSON format.",
|
|
99
|
+
},
|
|
100
|
+
headline: {
|
|
101
|
+
hint: "Settles output report headline.",
|
|
102
|
+
param: ":string",
|
|
103
|
+
},
|
|
104
|
+
indent: {
|
|
105
|
+
hint: "Prefixes given number of white spaces for every output line.",
|
|
106
|
+
param: ":number",
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
"dry-run": {
|
|
111
|
+
hint: "Simulate resolution of one or more Radon assets, as if solved by the Wit/Oracle.",
|
|
112
|
+
params: ["RAD_BYTECODE | RAD_HASH | RADON_ASSET"],
|
|
113
|
+
options: {
|
|
114
|
+
json: {
|
|
115
|
+
hint: "Outputs data in JSON format.",
|
|
116
|
+
},
|
|
117
|
+
headline: {
|
|
118
|
+
hint: "Settles output report headline.",
|
|
119
|
+
param: ":string",
|
|
120
|
+
},
|
|
121
|
+
indent: {
|
|
122
|
+
hint: "Prefixes given number of white spaces for every output line.",
|
|
123
|
+
param: ":number",
|
|
124
|
+
},
|
|
125
|
+
verbose: {
|
|
126
|
+
hint: "Outputs detailed dry-run report.",
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
init: {
|
|
131
|
+
hint: "Initialize a Witnet Radon workspace in this project.",
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
subcommands: {
|
|
135
|
+
assets, init, check, decode, "dry-run": dryrun,
|
|
136
|
+
},
|
|
137
|
+
loadAssets,
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
142
|
+
/// CLI SUBMODULE COMMANDS ============================================================================================
|
|
143
|
+
|
|
144
|
+
async function init () {
|
|
145
|
+
if (!fs.existsSync("./witnet/assets/")) {
|
|
146
|
+
fs.mkdirSync("./witnet/assets", { recursive: true })
|
|
147
|
+
}
|
|
148
|
+
if (!fs.existsSync(".env_witnet")) {
|
|
149
|
+
fs.cpSync("node_modules/@witnet/sdk/.env_witnet", ".env_witnet")
|
|
150
|
+
}
|
|
151
|
+
if (!fs.existsSync("./witnet/assets/index.js")) {
|
|
152
|
+
fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_index.js", "./witnet/assets/index.js")
|
|
153
|
+
}
|
|
154
|
+
if (!fs.existsSync("./witnet/assets/requests.js")) {
|
|
155
|
+
fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_requests.js", "./witnet/assets/requests.js")
|
|
156
|
+
}
|
|
157
|
+
if (!fs.existsSync("./witnet/assets/sources.js")) {
|
|
158
|
+
fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_sources.js", "./witnet/assets/sources.js")
|
|
159
|
+
}
|
|
160
|
+
if (!fs.existsSync("./witnet/assets/templates.js")) {
|
|
161
|
+
fs.cpSync("node_modules/@witnet/sdk/witnet/assets/_templates.js", "./witnet/assets/templates.js")
|
|
162
|
+
}
|
|
163
|
+
console.info(`Initialized Witnet Radon workspace at folder ${process.cwd()}/witnet/assets`)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async function assets (options = {}, [...patterns]) {
|
|
167
|
+
helpers.traceHeader(
|
|
168
|
+
`${options?.module ? options.module.toUpperCase() : path.basename(process.cwd()).toUpperCase()} RADON ASSETS`,
|
|
169
|
+
helpers.colors.white
|
|
170
|
+
)
|
|
171
|
+
const assets = clearEmptyBranches(loadAssets(options), patterns, options?.filter)
|
|
172
|
+
if (assets && Object.keys(assets).length > 0) {
|
|
173
|
+
traceWitnetArtifacts(assets, patterns, " ", options?.filter)
|
|
174
|
+
} else {
|
|
175
|
+
console.info("> No custom Radon assets declared just yet.")
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/// -------------------------------------------------------------------------------------------------------------------
|
|
179
|
+
|
|
180
|
+
async function check (options = {}) {
|
|
181
|
+
if (!isModuleInitialized) {
|
|
182
|
+
throw new Error(`No Witnet Radon workspace has been initialized. Please, run ${white("npx witnet radon init")} if willing to declare your own Radon assets.`)
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
const assets = loadAssets({ ...options, legacy: true })
|
|
186
|
+
const [
|
|
187
|
+
modals,
|
|
188
|
+
requests,
|
|
189
|
+
sources,
|
|
190
|
+
templates,
|
|
191
|
+
] = [
|
|
192
|
+
helpers.countLeaves(Witnet.Radon.RadonModal, assets),
|
|
193
|
+
helpers.countLeaves(Witnet.Radon.RadonRequest, assets),
|
|
194
|
+
helpers.countLeaves(Witnet.Radon.RadonRetrieval, assets?.sources),
|
|
195
|
+
helpers.countLeaves(Witnet.Radon.RadonTemplate, assets),
|
|
196
|
+
]
|
|
197
|
+
if (modals > 0) console.info("> Radon modals: ", modals)
|
|
198
|
+
if (sources > 0) console.info("> Radon sources: ", sources)
|
|
199
|
+
if (requests > 0) console.info("> Radon requests: ", requests)
|
|
200
|
+
if (templates > 0) console.info("> Radon templates:", templates)
|
|
201
|
+
|
|
202
|
+
if (modals + sources + requests + templates === 0) {
|
|
203
|
+
console.info("-----------------------------")
|
|
204
|
+
console.info("No Radon assets declared yet!")
|
|
205
|
+
} else {
|
|
206
|
+
console.info("--------------------------------------")
|
|
207
|
+
console.info("All Radon assets checked successfully!")
|
|
208
|
+
}
|
|
209
|
+
} catch (e) {
|
|
210
|
+
console.error("Radon assets verification failed:")
|
|
211
|
+
console.info("----------------------------------")
|
|
212
|
+
console.info(e)
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/// -------------------------------------------------------------------------------------------------------------------
|
|
216
|
+
|
|
217
|
+
async function decode (options = {}, args = []) {
|
|
218
|
+
if (args.length === 0) {
|
|
219
|
+
throw Error("No Radon asset was specified")
|
|
220
|
+
}
|
|
221
|
+
const asset = args[0]
|
|
222
|
+
if (helpers.isHexString(asset)) {
|
|
223
|
+
try {
|
|
224
|
+
const request = Witnet.Radon.RadonRequest.fromBytecode(asset)
|
|
225
|
+
traceWitnetRadonRequest(request, options)
|
|
226
|
+
} catch (err) {
|
|
227
|
+
console.error(err)
|
|
228
|
+
if ((asset.startsWith("0x") && asset.length === 66) || (!asset.startsWith("0x") && asset.length === 64)) {
|
|
229
|
+
// TODO: assume it's a RAD_HASH, and try to retrieve the BYTECODE from the Witnet network
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
} else {
|
|
233
|
+
args = args.slice(1)
|
|
234
|
+
|
|
235
|
+
const assets = loadAssets(options)
|
|
236
|
+
const headline = options?.headline
|
|
237
|
+
const crafts = flattenRadonArtifacts(assets).filter(craft => craft.key.toLowerCase().indexOf(asset.toLowerCase()) >= 0)
|
|
238
|
+
if (crafts.length === 0) {
|
|
239
|
+
throw Error(`No matched found for pattern "${asset}"`)
|
|
240
|
+
}
|
|
241
|
+
for (let { artifact, key } of crafts) {
|
|
242
|
+
let prefix = ""
|
|
243
|
+
if (artifact instanceof Witnet.Radon.RadonRequest) {
|
|
244
|
+
prefix = "RadonRequest::"
|
|
245
|
+
} else if (artifact instanceof Witnet.Radon.RadonModal) {
|
|
246
|
+
artifact.providers = ["https://dummy"]
|
|
247
|
+
const modalArgs = []
|
|
248
|
+
let argIndex = 0
|
|
249
|
+
while (modalArgs.length < artifact.argsCount) {
|
|
250
|
+
modalArgs.push(`{:${++argIndex}}`)
|
|
251
|
+
}
|
|
252
|
+
artifact = artifact.buildRadonRequest(modalArgs)
|
|
253
|
+
prefix = "RadonModal::"
|
|
254
|
+
} else if (artifact instanceof Witnet.Radon.RadonTemplate) {
|
|
255
|
+
const templateArgs = artifact.sources.map(({ argsCount }) =>
|
|
256
|
+
Array.from({ length: argsCount }, (_, i) => `{:${i}}`)
|
|
257
|
+
)
|
|
258
|
+
artifact = artifact.buildRadonRequest(templateArgs)
|
|
259
|
+
prefix = "RadonTemplate::"
|
|
260
|
+
} else if (artifact instanceof Witnet.Radon.RadonRetrieval) {
|
|
261
|
+
if (artifact.argsCount > 0) {
|
|
262
|
+
const retrievalArgs = Array.from(
|
|
263
|
+
{ length: artifact.argsCount },
|
|
264
|
+
(_, i) => `{:${i + 1}}`
|
|
265
|
+
)
|
|
266
|
+
artifact = artifact.foldArgs(retrievalArgs)
|
|
267
|
+
}
|
|
268
|
+
artifact = new Witnet.Radon.RadonRequest({ sources: artifact })
|
|
269
|
+
prefix = "RadonRetrieval::"
|
|
270
|
+
}
|
|
271
|
+
if (!headline) {
|
|
272
|
+
options.headline = `${prefix}${key}`
|
|
273
|
+
}
|
|
274
|
+
traceWitnetRadonRequest(artifact, options)
|
|
275
|
+
if (options?.verbose && key !== crafts[crafts.length - 1].key) {
|
|
276
|
+
console.info(`${options?.indent || ""}${"─".repeat(150)}`)
|
|
277
|
+
}
|
|
278
|
+
console.info()
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/// -------------------------------------------------------------------------------------------------------------------
|
|
283
|
+
|
|
284
|
+
async function dryrun (options = {}, args = []) {
|
|
285
|
+
if (args.length === 0) {
|
|
286
|
+
throw Error("No Radon asset was specified")
|
|
287
|
+
}
|
|
288
|
+
const asset = args[0]
|
|
289
|
+
if (helpers.isHexString(asset)) {
|
|
290
|
+
try {
|
|
291
|
+
const request = Witnet.Radon.RadonRequest.fromBytecode(asset)
|
|
292
|
+
await traceWitnetRadonRequestDryRun(request, options)
|
|
293
|
+
} catch {
|
|
294
|
+
if ((asset.startsWith("0x") && asset.length === 66) || (!asset.startsWith("0x") && asset.length === 64)) {
|
|
295
|
+
// TODO: assume it's a RAD_HASH, and try to retrieve the BYTECODE from the Witnet network
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
} else {
|
|
299
|
+
args = args.slice(1)
|
|
300
|
+
const assets = loadAssets(options)
|
|
301
|
+
const headline = options?.headline
|
|
302
|
+
const crafts = flattenRadonArtifacts(assets).filter(craft => craft.key.toLowerCase().indexOf(asset.toLowerCase()) >= 0)
|
|
303
|
+
if (crafts.length === 0) {
|
|
304
|
+
throw Error(`No matched found for pattern "${asset}"`)
|
|
305
|
+
}
|
|
306
|
+
for (let { artifact, key } of crafts) {
|
|
307
|
+
let prefix = ""
|
|
308
|
+
if (artifact instanceof Witnet.Radon.RadonRequest) {
|
|
309
|
+
prefix = "RadonRequest::"
|
|
310
|
+
} else {
|
|
311
|
+
if (!artifact?.samples) {
|
|
312
|
+
console.error(`${artifact.constructor.name}::${key}: cannot dry-run if no sample parameters are declared.\n`)
|
|
313
|
+
continue
|
|
314
|
+
}
|
|
315
|
+
let artifactArgs = []
|
|
316
|
+
if (options?.default) {
|
|
317
|
+
artifactArgs = Object.values(artifact.samples)[0]
|
|
318
|
+
} else {
|
|
319
|
+
const prompt = inquirer.createPromptModule()
|
|
320
|
+
const sample = await prompt([{
|
|
321
|
+
choices: Object.keys(artifact.samples),
|
|
322
|
+
message: `${artifact.constructor.name}::${key} args:`,
|
|
323
|
+
name: "key",
|
|
324
|
+
type: "list",
|
|
325
|
+
}])
|
|
326
|
+
artifactArgs = artifact.samples[sample.key]
|
|
327
|
+
}
|
|
328
|
+
if (artifact instanceof Witnet.Radon.RadonModal) {
|
|
329
|
+
artifact.providers = artifactArgs.shift().split(";")
|
|
330
|
+
artifact = artifact.buildRadonRequest(artifactArgs)
|
|
331
|
+
prefix = "RadonModal::"
|
|
332
|
+
} else if (artifact instanceof Witnet.Radon.RadonTemplate) {
|
|
333
|
+
artifact = artifact.buildRadonRequest(artifactArgs)
|
|
334
|
+
prefix = "RadonTemplate::"
|
|
335
|
+
} else if (artifact instanceof Witnet.Radon.RadonRetrieval) {
|
|
336
|
+
artifact = new Witnet.Radon.RadonRequest({ sources: artifact.foldArgs(artifactArgs) })
|
|
337
|
+
prefix = "RadonRetrieval::"
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (!headline) {
|
|
341
|
+
options.headline = `${prefix}${key}`
|
|
342
|
+
}
|
|
343
|
+
await traceWitnetRadonRequestDryRun(artifact, options)
|
|
344
|
+
if (options?.verbose && key !== crafts[crafts.length - 1].key) {
|
|
345
|
+
console.info(`${options?.indent || ""}${"─".repeat(150)}`)
|
|
346
|
+
}
|
|
347
|
+
console.info()
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
/// -------------------------------------------------------------------------------------------------------------------
|
|
352
|
+
|
|
353
|
+
/// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
354
|
+
/// CLI SUBMODULE INTERNAL METHODS ------------------------------------------------------------------------------------
|
|
355
|
+
|
|
356
|
+
const extractTypeName = (str) => str ? str.split(/(?=[A-Z])/).slice(1).join("") : "Any"
|
|
357
|
+
|
|
358
|
+
const stringifyFilter = (x, c) => {
|
|
359
|
+
const color = c || helpers.colors.mcyan
|
|
360
|
+
return color(`${Witnet.Radon.filters.Opcodes[x.opcode]}(${x.args ? JSON.stringify(x.args) : ""})`)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const stringifyReducer = (x, c) => {
|
|
364
|
+
const color = c || helpers.colors.mcyan
|
|
365
|
+
return color(`${Witnet.Radon.reducers.Opcodes[x.opcode]}()`)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function loadAssets (options) {
|
|
369
|
+
const { assets } = options?.module ? require(options.module) : (options?.legacy ? {} : require("@witnet/sdk"))
|
|
370
|
+
return isModuleInitialized && fs.existsSync(`${WITNET_ASSETS_PATH}`) ? merge(assets, require(`${WITNET_ASSETS_PATH}`)) : assets
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function flattenRadonArtifacts (tree, headers) {
|
|
374
|
+
if (!headers) headers = []
|
|
375
|
+
const matches = []
|
|
376
|
+
for (const key in tree) {
|
|
377
|
+
if (
|
|
378
|
+
tree[key] instanceof Witnet.Radon.RadonRetrieval ||
|
|
379
|
+
tree[key] instanceof Witnet.Radon.RadonModal ||
|
|
380
|
+
tree[key] instanceof Witnet.Radon.RadonRequest ||
|
|
381
|
+
tree[key] instanceof Witnet.Radon.RadonTemplate
|
|
382
|
+
) {
|
|
383
|
+
matches.push({
|
|
384
|
+
key,
|
|
385
|
+
artifact: tree[key],
|
|
386
|
+
})
|
|
387
|
+
} else if (typeof tree[key] === "object") {
|
|
388
|
+
matches.push(...flattenRadonArtifacts(
|
|
389
|
+
tree[key],
|
|
390
|
+
[...headers, key]
|
|
391
|
+
))
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return matches
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
function countWitnetArtifacts (assets, args, filter = false) {
|
|
398
|
+
let counter = 0
|
|
399
|
+
Object.entries(assets).forEach(([key, value]) => {
|
|
400
|
+
if ((
|
|
401
|
+
value instanceof Witnet.Radon.RadonModal ||
|
|
402
|
+
value instanceof Witnet.Radon.RadonRequest ||
|
|
403
|
+
value instanceof Witnet.Radon.RadonTemplate ||
|
|
404
|
+
value instanceof Witnet.Radon.RadonRetrieval
|
|
405
|
+
) && (
|
|
406
|
+
!filter ||
|
|
407
|
+
!args ||
|
|
408
|
+
args.length === 0 ||
|
|
409
|
+
args.find(arg => key.toLowerCase().indexOf(arg.toLowerCase()) >= 0)
|
|
410
|
+
)) {
|
|
411
|
+
counter++
|
|
412
|
+
} else if (typeof value === "object") {
|
|
413
|
+
counter += countWitnetArtifacts(value, args)
|
|
414
|
+
}
|
|
415
|
+
})
|
|
416
|
+
return counter
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function clearEmptyBranches (node, args, filter) {
|
|
420
|
+
if (node) {
|
|
421
|
+
const assets = Object.fromEntries(
|
|
422
|
+
Object.entries(node).map(([key, value]) => {
|
|
423
|
+
if (
|
|
424
|
+
(!filter || args.find(arg => key.toLowerCase().indexOf(arg.toLowerCase()) >= 0)) && (
|
|
425
|
+
value instanceof Witnet.Radon.RadonRetrieval ||
|
|
426
|
+
value instanceof Witnet.Radon.RadonModal ||
|
|
427
|
+
value instanceof Witnet.Radon.RadonRequest ||
|
|
428
|
+
value instanceof Witnet.Radon.RadonTemplate
|
|
429
|
+
)
|
|
430
|
+
) {
|
|
431
|
+
return [key, value]
|
|
432
|
+
} else if (typeof value === "object") {
|
|
433
|
+
if (countWitnetArtifacts(value, args, filter) > 0) {
|
|
434
|
+
return [key, clearEmptyBranches(value, args, filter)]
|
|
435
|
+
} else {
|
|
436
|
+
return [key, undefined]
|
|
437
|
+
}
|
|
438
|
+
} else {
|
|
439
|
+
return [key, undefined]
|
|
440
|
+
}
|
|
441
|
+
})
|
|
442
|
+
.filter(([, value]) => value !== undefined)
|
|
443
|
+
)
|
|
444
|
+
if (Object.keys(assets).length > 0) {
|
|
445
|
+
return assets
|
|
446
|
+
} else {
|
|
447
|
+
return undefined
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function traceWitnetArtifacts (assets, args, indent = "", filter = false) {
|
|
453
|
+
const prefix = `${indent}`
|
|
454
|
+
Object.keys(assets).forEach((key, index) => {
|
|
455
|
+
const isLast = index === Object.keys(assets).length - 1
|
|
456
|
+
const found = args.find(arg => key.toLowerCase().indexOf(arg.toLowerCase()) >= 0)
|
|
457
|
+
const color = found ? helpers.colors.mcyan : helpers.colors.cyan
|
|
458
|
+
if (assets[key] instanceof Witnet.Radon.RadonRequest) {
|
|
459
|
+
if (!filter || found) {
|
|
460
|
+
console.info(`${prefix}${color(key)}`)
|
|
461
|
+
if (isLast) {
|
|
462
|
+
console.info(`${prefix}`)
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
} else if (
|
|
466
|
+
assets[key] instanceof Witnet.Radon.RadonTemplate ||
|
|
467
|
+
assets[key] instanceof Witnet.Radon.RadonModal
|
|
468
|
+
) {
|
|
469
|
+
const argsCount = assets[key].argsCount
|
|
470
|
+
if (!filter || found) {
|
|
471
|
+
console.info(`${prefix}${color(key)} ${argsCount > 0 ? helpers.colors.green(`(${argsCount} args)`) : ""}`)
|
|
472
|
+
if (isLast) {
|
|
473
|
+
console.info(`${prefix}`)
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
} else if (assets[key] instanceof Witnet.Radon.RadonRetrieval) {
|
|
477
|
+
const argsCount = assets[key].argsCount
|
|
478
|
+
if (!filter || found) {
|
|
479
|
+
console.info(`${prefix}${color(key)} ${argsCount > 0 ? helpers.colors.green(`(${argsCount} args)`) : ""}`)
|
|
480
|
+
if (isLast) {
|
|
481
|
+
console.info(`${prefix}`)
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
} else if (typeof assets[key] === "object" && countWitnetArtifacts(assets[key], args, filter) > 0) {
|
|
485
|
+
console.info(`${indent}${isLast ? "└─ " : "├─ "}${key}`)
|
|
486
|
+
traceWitnetArtifacts(assets[key], args, !isLast ? `${indent}│ ` : `${indent} `, filter)
|
|
487
|
+
}
|
|
488
|
+
})
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function traceWitnetRadonReportHeadline (request, options) {
|
|
492
|
+
const trait = (str) => `${str}${" ".repeat(66 - str.length)}`
|
|
493
|
+
const indent = options?.indent ? " ".repeat(options.indent) : ""
|
|
494
|
+
const resultDataType = `Result<${extractTypeName(request.sources[0]?.script?.outputType.constructor.name)}, RadonError>`
|
|
495
|
+
console.info(`${indent}╔══════════════════════════════════════════════════════════════════════════════╗`)
|
|
496
|
+
console.info(`${indent}║ ${helpers.colors.white(options?.headline)}${" ".repeat(77 - options?.headline.length)}║`)
|
|
497
|
+
console.info(`${indent}╠══════════════════════════════════════════════════════════════════════════════╣`)
|
|
498
|
+
console.info(`${indent}║ ${helpers.colors.white("RAD hash")}: ${helpers.colors.lgreen(request.radHash)} ║`)
|
|
499
|
+
console.info(`${indent}║ RAD size: ${helpers.colors.green(trait(helpers.commas(request.weight()) + " bytes"))} ║`)
|
|
500
|
+
console.info(`${indent}║ RAD type: ${helpers.colors.yellow(trait(resultDataType))} ║`)
|
|
501
|
+
// if (!options.verbose) {
|
|
502
|
+
// console.info(`${indent}║ > Radon operators: ${white(trait(commas(request.opsCount())))} ║`)
|
|
503
|
+
// }
|
|
504
|
+
// console.info(`${indent}╠════════════════════════════════════════════════════════════════════════════╣`)
|
|
505
|
+
// console.info(`${indent}║ > Times solved: ${white(trait("{ values: 123, errors: 220 }"))} ║`)
|
|
506
|
+
// console.info(`${indent}║ > Times witnessed: ${white(trait("{ values: 2130, errors: 1326 }"))} ║`)
|
|
507
|
+
// console.info(`${indent}║ > Total fees: ${white(trait("15,234.123 Wits"))} ║`)
|
|
508
|
+
// console.info(`${indent}║ > Total slash: ${white(trait(" 56.123 Wits"))} ║`)
|
|
509
|
+
// console.info(`${indent}║ > Total burn: ${white(trait(" 0.789 Wits"))} ║`)
|
|
510
|
+
// if (verbose) {
|
|
511
|
+
// console.info(`${indent}╚══╤═════════════════════════════════════════════════════════════════════════╝`)
|
|
512
|
+
// } else {
|
|
513
|
+
// console.info(`${indent}╚════════════════════════════════════════════════════════════════════════════╝`)
|
|
514
|
+
// }
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function traceWitnetRadonRequest (request, options) {
|
|
518
|
+
const indent = options?.indent ? " ".repeat(parseInt(options.indent)) : ""
|
|
519
|
+
if (options?.json) {
|
|
520
|
+
console.info(JSON.stringify(request.toProtobuf(), null, options?.indent || 0))
|
|
521
|
+
} else {
|
|
522
|
+
if (!options.headline) options.headline = "WITNET DATA REQUEST DISASSEMBLE"
|
|
523
|
+
traceWitnetRadonReportHeadline(request, options)
|
|
524
|
+
|
|
525
|
+
console.info(`${indent}╚══╤═══════════════════════════════════════════════════════════════════════════╝`)
|
|
526
|
+
console.info(`${indent}┌──┴─────────────────┐`)
|
|
527
|
+
console.info(`${indent}│ ${helpers.colors.white("RETRIEVE DATA")} │`) // ├ ┤
|
|
528
|
+
console.info(`${indent}└──┬─┬───────────────┘`)
|
|
529
|
+
request.sources.forEach((source, sourceIndex) => {
|
|
530
|
+
const authority = source.authority?.toUpperCase().split(".").slice(-2).join(".") || (
|
|
531
|
+
source.method === Witnet.Radon.retrievals.Methods.RNG ? "WIT/RNG" : ""
|
|
532
|
+
)
|
|
533
|
+
const corner = sourceIndex === request.sources.length - 1 ? "└" : "├"
|
|
534
|
+
const sep = sourceIndex === request.sources.length - 1 ? " " : "│"
|
|
535
|
+
console.info(
|
|
536
|
+
`${indent} │ ${corner}─ ${helpers.colors.white("[ ")}${helpers.colors.white(`Data source #${sourceIndex + 1}`)
|
|
537
|
+
} ${" ".repeat(3 - sourceIndex.toString().length)}${helpers.colors.white(authority)} ${helpers.colors.white("]")}`
|
|
538
|
+
)
|
|
539
|
+
if (source.method !== Witnet.Radon.retrievals.Methods.RNG) {
|
|
540
|
+
console.info(
|
|
541
|
+
`${indent} │ ${sep} > Request: ${
|
|
542
|
+
helpers.colors.mgreen(Witnet.Radon.retrievals.Methods[source.method].split(/(?=[A-Z])/).join("-").toUpperCase())
|
|
543
|
+
}`
|
|
544
|
+
)
|
|
545
|
+
console.info(`${indent} │ ${sep} > URL query: ${helpers.colors.green(source.url)}`)
|
|
546
|
+
if (source?.headers) {
|
|
547
|
+
console.info(`${indent} │ ${sep} > HTTP headers: ${helpers.colors.green(JSON.stringify(source.headers))}`)
|
|
548
|
+
}
|
|
549
|
+
if (source?.body) {
|
|
550
|
+
console.info(`${indent} │ ${sep} > HTTP body: ${helpers.colors.green(source.body)}`)
|
|
551
|
+
}
|
|
552
|
+
if (source?.script) {
|
|
553
|
+
const steps = source.script.disect()
|
|
554
|
+
console.info(
|
|
555
|
+
`${indent} │ ${sep} > Radon script: ${helpers.colors.lyellow("[ ")
|
|
556
|
+
}${helpers.colors.yellow(steps[0][1])}${" ".repeat(12 - steps[0][1].length)
|
|
557
|
+
}${helpers.colors.lyellow(" ]")
|
|
558
|
+
} ${helpers.colors.mcyan(steps[0][2])}`
|
|
559
|
+
)
|
|
560
|
+
steps.slice(1).forEach(step => {
|
|
561
|
+
console.info(
|
|
562
|
+
`${indent} │ ${sep} ${helpers.colors.lyellow("[ ")
|
|
563
|
+
}${helpers.colors.yellow(step[1])
|
|
564
|
+
}${" ".repeat(12 - step[1].length)
|
|
565
|
+
}${helpers.colors.lyellow(" ]")
|
|
566
|
+
} ${" ".repeat(2 * step[0])}${helpers.colors.mcyan(step[2])
|
|
567
|
+
}`
|
|
568
|
+
)
|
|
569
|
+
})
|
|
570
|
+
const outputType = source.script.outputType.constructor.name || "RadonAny"
|
|
571
|
+
console.info(
|
|
572
|
+
`${indent} │ ${sep} ${helpers.colors.lyellow("[ ")
|
|
573
|
+
}${helpers.colors.yellow(outputType)
|
|
574
|
+
}${" ".repeat(12 - outputType.length)
|
|
575
|
+
}${helpers.colors.lyellow(" ]")
|
|
576
|
+
}`
|
|
577
|
+
)
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
if (sourceIndex < request.sources.length - 1) {
|
|
581
|
+
console.info(`${indent} │ │`)
|
|
582
|
+
}
|
|
583
|
+
})
|
|
584
|
+
console.info(`${indent}┌──┴──────────────────┐`)
|
|
585
|
+
console.info(`${indent}│ ${helpers.colors.white("AGGREGATE SOURCES")} │`)
|
|
586
|
+
console.info(`${indent}└──┬──────────────────┘`) // ┬
|
|
587
|
+
request.sourcesReducer?.filters.forEach(filter => console.info(`${indent} │ > Radon filter: ${stringifyFilter(filter)}`))
|
|
588
|
+
console.info(`${indent} │ > Radon reducer: ${stringifyReducer(request.sourcesReducer)}`)
|
|
589
|
+
console.info(`${indent}┌──┴──────────────────┐`)
|
|
590
|
+
console.info(`${indent}│ ${helpers.colors.white("WITNESSING TALLY")} │`)
|
|
591
|
+
console.info(`${indent}└─────────────────────┘`) // ┬
|
|
592
|
+
request.witnessReducer?.filters.forEach(filter => console.info(`${indent} > Radon filter: ${stringifyFilter(filter)}`))
|
|
593
|
+
console.info(`${indent} > Radon reducer: ${stringifyReducer(request.witnessReducer)}`)
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
async function traceWitnetRadonRequestDryRun (request, options) {
|
|
598
|
+
const bytecode = request.toBytecode()
|
|
599
|
+
let report = await helpers
|
|
600
|
+
.toolkitRun(options, ["try-data-request", "--hex", bytecode.startsWith("0x") ? bytecode.slice(2) : bytecode])
|
|
601
|
+
.catch((err) => {
|
|
602
|
+
let errorMessage = err.message.split("\n").slice(1).join("\n").trim()
|
|
603
|
+
const errorRegex = /.*^error: (?<message>.*)$.*/gm
|
|
604
|
+
const matched = errorRegex.exec(err.message)
|
|
605
|
+
if (matched) {
|
|
606
|
+
errorMessage = matched.groups.message
|
|
607
|
+
}
|
|
608
|
+
throw errorMessage || err
|
|
609
|
+
})
|
|
610
|
+
if (!report) {
|
|
611
|
+
throw Error("No dry-report?")
|
|
612
|
+
} else {
|
|
613
|
+
report = JSON.parse(report)
|
|
614
|
+
}
|
|
615
|
+
const result = report?.aggregate.result
|
|
616
|
+
const resultType = Object.keys(result)[0]
|
|
617
|
+
const resultValue = Object.values(result)[0]
|
|
618
|
+
if (options?.json) {
|
|
619
|
+
if (options?.verbose) {
|
|
620
|
+
console.info(JSON.stringify(report, null, options?.indent ? " ".repeat(options.indent) : ""))
|
|
621
|
+
} else {
|
|
622
|
+
result[resultType] = resultValue
|
|
623
|
+
console.info(JSON.stringify(result, null, options?.indent ? " ".repeat(options.indent) : ""))
|
|
624
|
+
}
|
|
625
|
+
return
|
|
626
|
+
}
|
|
627
|
+
if (!options.headline) options.headline = "WITNET DATA REQUEST DRY-RUN REPORT"
|
|
628
|
+
traceWitnetRadonReportHeadline(request, options)
|
|
629
|
+
const indent = options?.indent ? " ".repeat(options.indent) : ""
|
|
630
|
+
console.info(`${indent}╚══╤═══════════════════════════════════════════════════════════════════════════╝`)
|
|
631
|
+
let execTimeMs = report.retrieve?.map(retrieval =>
|
|
632
|
+
(retrieval?.running_time.secs || 0) + (retrieval?.running_time.nanos || 0) / 1000
|
|
633
|
+
).reduce(
|
|
634
|
+
(sum, secs) => sum + secs
|
|
635
|
+
)
|
|
636
|
+
execTimeMs = Math.round(execTimeMs) + " ms"
|
|
637
|
+
let flexbar = "─".repeat(17)
|
|
638
|
+
let flexspc = " ".repeat(flexbar.length + 12)
|
|
639
|
+
console.info(`${indent}┌──┴─────────────────────────────${flexbar}──────┐`)
|
|
640
|
+
console.info(`${indent}│ ${helpers.colors.white("Data providers")} ${flexspc} │`) // ├ ┤
|
|
641
|
+
console.info(`${indent}├────────────────────────────────${flexbar}──────┤`)
|
|
642
|
+
console.info(`${indent}│ Execution time: ${helpers.colors.green(execTimeMs)} ${" ".repeat(flexbar.length + 19 - execTimeMs.length)} │`)
|
|
643
|
+
console.info(`${indent}└──┬─┬───────────────────────────${flexbar}──────┘`)
|
|
644
|
+
request.sources.forEach((source, sourceIndex) => {
|
|
645
|
+
const authority = source.authority?.toUpperCase().split(".").slice(-2).join(".") || (
|
|
646
|
+
source.method === Witnet.Radon.retrievals.Methods.RNG ? "WIT/RNG" : ""
|
|
647
|
+
)
|
|
648
|
+
const corner = sourceIndex === request.sources.length - 1 ? "└" : "├"
|
|
649
|
+
const sep = sourceIndex === request.sources.length - 1 ? " " : "│"
|
|
650
|
+
const color = report.retrieve[sourceIndex].result?.RadonError
|
|
651
|
+
? (options?.verbose ? helpers.colors.lgray : helpers.colors.gray)
|
|
652
|
+
: (options?.verbose ? helpers.colors.lgreen : helpers.colors.green)
|
|
653
|
+
if (options?.verbose) {
|
|
654
|
+
console.info(
|
|
655
|
+
`${indent} │ ${corner}─ ${
|
|
656
|
+
helpers.colors.white("[ ")
|
|
657
|
+
}${
|
|
658
|
+
helpers.colors.white(`Data Source #${sourceIndex + 1}`)
|
|
659
|
+
} ${
|
|
660
|
+
" ".repeat(3 - sourceIndex.toString().length)
|
|
661
|
+
}${
|
|
662
|
+
color(authority)
|
|
663
|
+
} ${
|
|
664
|
+
helpers.colors.white("]")
|
|
665
|
+
}`
|
|
666
|
+
)
|
|
667
|
+
} else {
|
|
668
|
+
console.info(`${indent} │ ${corner}─ [ ${color(authority)} ]`)
|
|
669
|
+
}
|
|
670
|
+
if (source.method !== Witnet.Radon.retrievals.Methods.RNG && options?.verbose) {
|
|
671
|
+
// const result = report.retrieve[sourceIndex].result
|
|
672
|
+
// const resultType = Object.keys(result)[0]
|
|
673
|
+
// const resultValue = JSON.stringify(Object.values(result)[0])
|
|
674
|
+
console.info(
|
|
675
|
+
`${indent} │ ${sep} > Request: ${
|
|
676
|
+
helpers.colors.mgreen(Witnet.Radon.retrievals.Methods[source.method].split(/(?=[A-Z])/).join("-").toUpperCase())
|
|
677
|
+
}`
|
|
678
|
+
)
|
|
679
|
+
console.info(`${indent} │ ${sep} > URL query: ${helpers.colors.green(source.url)}`)
|
|
680
|
+
if (source?.headers) {
|
|
681
|
+
console.info(`${indent} │ ${sep} > HTTP headers: ${helpers.colors.green(JSON.stringify(source.headers))}`)
|
|
682
|
+
}
|
|
683
|
+
if (source?.body) {
|
|
684
|
+
console.info(`${indent} │ ${sep} > HTTP body: ${helpers.colors.green(source.body)}`)
|
|
685
|
+
}
|
|
686
|
+
const printData = (headline, data, color) => {
|
|
687
|
+
const type = Object.keys(data)[0]
|
|
688
|
+
data = typeof data[type] === "object" || Array.isArray(data[type]) ? JSON.stringify(data[type]) : data[type]
|
|
689
|
+
const lines = data.match(/.{1,96}/g).slice(0, 256)
|
|
690
|
+
if (lines.length === 256) lines[255] += "..."
|
|
691
|
+
const typeColor = (type === "RadonError") ? helpers.colors.red : helpers.colors.yellow
|
|
692
|
+
const lineColor = (type === "RadonError") ? helpers.colors.gray : color
|
|
693
|
+
console.info(
|
|
694
|
+
`${indent} │ ${sep} > ${headline}${" ".repeat(15 - headline.length)} \x1b[1;m${
|
|
695
|
+
typeColor("[ ")
|
|
696
|
+
}\x1b[0m${typeColor(type)}${
|
|
697
|
+
" ".repeat(12 - type.length)
|
|
698
|
+
}\x1b[1;m${typeColor(" ]")}\x1b[0m ${
|
|
699
|
+
lineColor(lines[0])
|
|
700
|
+
}`)
|
|
701
|
+
lines.slice(1).forEach(line => {
|
|
702
|
+
console.info(`${indent} │ ${sep} ${lineColor(line)}`)
|
|
703
|
+
})
|
|
704
|
+
}
|
|
705
|
+
if (report?.retrieve[sourceIndex]?.partial_results) {
|
|
706
|
+
printData("HTTP response:", report?.retrieve[sourceIndex]?.partial_results[0], helpers.colors.cyan)
|
|
707
|
+
}
|
|
708
|
+
printData("Radon result:", report?.retrieve[sourceIndex]?.result, helpers.colors.mcyan)
|
|
709
|
+
}
|
|
710
|
+
if (options?.verbose && sourceIndex < request.sources.length - 1) {
|
|
711
|
+
console.info(`${indent} │ │`)
|
|
712
|
+
}
|
|
713
|
+
})
|
|
714
|
+
flexbar = "─".repeat(24)
|
|
715
|
+
flexspc = " ".repeat(36)
|
|
716
|
+
console.info(`${indent}┌──┴───────────────────────────${flexbar}─┐`)
|
|
717
|
+
console.info(`${indent}│ ${helpers.colors.white("Aggregated result")}${flexspc} │`) // ├ ┤
|
|
718
|
+
console.info(`${indent}├──────────────────────────────${flexbar}─┤`)
|
|
719
|
+
if (options?.verbose) {
|
|
720
|
+
let partial_index = 0
|
|
721
|
+
const partial_results = report.sourcesReducer?.partial_results
|
|
722
|
+
request.sourcesReducer?.filters.forEach(filter => {
|
|
723
|
+
const color = (partial_results && partial_results[partial_index]?.RadonArray) ? helpers.colors.mcyan : helpers.colors.gray
|
|
724
|
+
const items = (partial_results && partial_results[partial_index]?.RadonArray)
|
|
725
|
+
? ` over ${partial_results[partial_index]?.RadonArray.length} sources`
|
|
726
|
+
: ""
|
|
727
|
+
partial_index += 1
|
|
728
|
+
filter = stringifyFilter(filter, color)
|
|
729
|
+
console.info(
|
|
730
|
+
`${indent}│ Radon filter: ${filter}${
|
|
731
|
+
helpers.colors.cyan(items)
|
|
732
|
+
}${
|
|
733
|
+
" ".repeat(flexbar.length + 22 - filter.length - items.length)
|
|
734
|
+
} │`
|
|
735
|
+
)
|
|
736
|
+
})
|
|
737
|
+
const color = (partial_results && partial_results[partial_index]?.RadonArray) ? helpers.colors.mcyan : helpers.colors.gray
|
|
738
|
+
const items = (partial_results && partial_results[partial_index]?.RadonArray)
|
|
739
|
+
? ` over ${partial_results[partial_index]?.RadonArray.length} sources`
|
|
740
|
+
: ""
|
|
741
|
+
const reducer = stringifyReducer(request.sourcesReducer, color)
|
|
742
|
+
console.info(
|
|
743
|
+
`${indent}│ Radon reducer: ${reducer}${
|
|
744
|
+
helpers.colors.cyan(items)}${" ".repeat(flexbar.length + 22 - reducer.length - items.length)
|
|
745
|
+
} │`
|
|
746
|
+
)
|
|
747
|
+
}
|
|
748
|
+
console.info(`${indent}│ Result size: ${helpers.colors.cyan("xxx bytes")}${" ".repeat(flexbar.length + 13 - 9)} │`)
|
|
749
|
+
console.info(`${indent}└────┬─────────────────────────${flexbar}─┘`)
|
|
750
|
+
const printMapItem = (indent, width, key, value, indent2 = "") => {
|
|
751
|
+
if (key) key = `${indent2}"${key}": `
|
|
752
|
+
else key = `${indent2}`
|
|
753
|
+
let type = extractTypeName(Object.keys(value)[0])
|
|
754
|
+
value = Object.values(value)[0]
|
|
755
|
+
if (["Map", "Array"].includes(type)) {
|
|
756
|
+
if (key.length > width - 12) {
|
|
757
|
+
console.info(
|
|
758
|
+
`${indent} ${
|
|
759
|
+
helpers.colors.myellow(`[ ${type}${" ".repeat(7 - type.length)} ]`)
|
|
760
|
+
} ${
|
|
761
|
+
" ".repeat(width - 15)}${helpers.colors.green("...")
|
|
762
|
+
}`
|
|
763
|
+
)
|
|
764
|
+
} else {
|
|
765
|
+
console.info(
|
|
766
|
+
`${indent} ${
|
|
767
|
+
helpers.colors.myellow(`[ ${type}${" ".repeat(7 - type.length)} ]`)
|
|
768
|
+
} ${
|
|
769
|
+
helpers.colors.green(key)
|
|
770
|
+
}${
|
|
771
|
+
" ".repeat(width - 12 - key.length)
|
|
772
|
+
}`
|
|
773
|
+
)
|
|
774
|
+
}
|
|
775
|
+
Object.entries(value).forEach(([key, value]) => printMapItem(indent, width, type === "Map" ? key : null, value, indent2 + " "))
|
|
776
|
+
} else {
|
|
777
|
+
if (key.length > width - 12) {
|
|
778
|
+
console.info(`${indent} ${helpers.colors.yellow(type)} ${" ".repeat(width - 15)}${helpers.colors.green("...")}`)
|
|
779
|
+
} else {
|
|
780
|
+
if (["String", "Error"].includes(type)) {
|
|
781
|
+
value = JSON.stringify(value)
|
|
782
|
+
}
|
|
783
|
+
type = `[ ${type}${" ".repeat(7 - type.length)} ]`
|
|
784
|
+
const result = key + value
|
|
785
|
+
// let spaces = width - 12 - result.length
|
|
786
|
+
if (result.length > width - 15) {
|
|
787
|
+
value = value.slice(0, width - 15 - key.length) + "..."
|
|
788
|
+
// spaces = 0
|
|
789
|
+
}
|
|
790
|
+
console.info(`${indent} ${helpers.colors.yellow(type)} ${helpers.colors.green(key)}${helpers.colors.mcyan(value)}`)
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
const printResult = (indent, width, resultType, resultValue) => {
|
|
795
|
+
resultType = extractTypeName(resultType)
|
|
796
|
+
resultValue = typeof resultValue === "object" || Array.isArray(resultValue) ? JSON.stringify(resultValue) : resultValue
|
|
797
|
+
if (["Map", "Array"].includes(resultType)) {
|
|
798
|
+
console.info(`${indent} └─ ${helpers.colors.lyellow(`[ ${resultType}${" ".repeat(7 - resultType.length)} ]`)}`)
|
|
799
|
+
const obj = JSON.parse(resultValue)
|
|
800
|
+
Object.entries(obj).forEach(([key, value]) => printMapItem(indent, width, resultType === "Map" ? key : null, value))
|
|
801
|
+
} else {
|
|
802
|
+
if (resultType === "Bytes") {
|
|
803
|
+
resultValue = JSON.parse(resultValue).map(char => ("00" + char.toString(16)).slice(-2)).join("")
|
|
804
|
+
}
|
|
805
|
+
const color = resultType.indexOf("Error") > -1 ? helpers.colors.gray : helpers.colors.lcyan
|
|
806
|
+
const typeText = resultType.indexOf("Error") > -1 ? "\x1b[1;98;41m Error \x1b[0m" : helpers.colors.lyellow(`[ ${resultType} ]`)
|
|
807
|
+
const lines = resultValue.match(/.{1,96}/g).slice(0, 256)
|
|
808
|
+
console.info(`${indent} └─ ${typeText} ${color(lines[0])}`)
|
|
809
|
+
lines.slice(1).forEach(line => {
|
|
810
|
+
console.info(`${indent} ${" ".repeat(resultType.length)}${color(line)}`)
|
|
811
|
+
})
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
printResult(indent, 134, resultType, resultValue)
|
|
815
|
+
}
|