@orbs-network/spot 2.0.0 → 2.1.1
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/README.md +27 -25
- package/config.json +19 -29
- package/index.css +92 -0
- package/index.html +50 -0
- package/out/02_DeployCosigner.s.sol/DeployCosigner.json +1 -1
- package/out/03_DeployReactor.s.sol/DeployReactor.json +1 -1
- package/out/04_DeployExecutor.s.sol/DeployExecutor.json +1 -1
- package/out/06_DeployDefaultAdapter.s.sol/DeployDefaultAdapter.json +1 -1
- package/out/06_DeployDefaultExchange.s.sol/DeployDefaultExchange.abi.json +28 -0
- package/out/06_DeployDefaultExchange.s.sol/DeployDefaultExchange.json +1 -0
- package/out/06_DeployDelegatingAdapter.s.sol/DeployDelegatingAdapter.json +1 -1
- package/out/06_DeployP2Adapter.s.sol/DeployP2Adapter.json +1 -1
- package/out/06_DeployP2Exchange.s.sol/DeployP2Exchange.abi.json +28 -0
- package/out/06_DeployP2Exchange.s.sol/DeployP2Exchange.json +1 -0
- package/out/06_DeployParaswapAdapter.s.sol/DeployParaswapAdapter.json +1 -1
- package/out/06_DeployParaswapExchange.s.sol/DeployParaswapExchange.abi.json +28 -0
- package/out/06_DeployParaswapExchange.s.sol/DeployParaswapExchange.json +1 -0
- package/out/06_DeployUniversalAdapter.s.sol/DeployUniversalAdapter.json +1 -1
- package/out/07_CheckOrder.s.sol/AlwaysValid1271.abi.json +26 -0
- package/out/07_CheckOrder.s.sol/AlwaysValid1271.json +1 -0
- package/out/07_CheckOrder.s.sol/CheckOrder.abi.json +78 -0
- package/out/07_CheckOrder.s.sol/CheckOrder.json +1 -0
- package/out/BaseTest.sol/BaseTest.json +1 -1
- package/out/CosignatureLib.sol/CosignatureLib.json +1 -1
- package/out/CosignatureLib.t.sol/CosignatureLibTest.json +1 -1
- package/out/Cosigner.sol/Cosigner.json +1 -1
- package/out/Cosigner.t.sol/CosignerTest.json +1 -1
- package/out/Cosigner.t.sol/CosignerWrapper.json +1 -1
- package/out/CosignerIntegration.t.sol/CosignerIntegrationTest.json +1 -1
- package/out/DefaultDexAdapter.sol/DefaultDexAdapter.json +1 -1
- package/out/DefaultDexAdapter.t.sol/DefaultDexAdapterTest.json +1 -1
- package/out/DelegatingAdapter.sol/DelegatingAdapter.json +1 -1
- package/out/DelegatingAdapter.t.sol/DelegatingAdapterTest.json +1 -1
- package/out/DelegatingAdapter.t.sol/MockDelegateTarget.json +1 -1
- package/out/ExclusivityOverrideLib.t.sol/ExclusivityCaller.json +1 -1
- package/out/ExclusivityOverrideLib.t.sol/ExclusivityLibTest.json +1 -1
- package/out/Executor.sol/Executor.json +1 -1
- package/out/Executor.t.sol/ExecutorTest.json +1 -1
- package/out/ICommitteeSync.sol/ICommitteeSync.json +1 -1
- package/out/IExchangeAdapter.sol/IExchangeAdapter.json +1 -1
- package/out/IReactor.sol/IReactor.json +1 -1
- package/out/IReactorCallback.sol/IReactorCallback.json +1 -1
- package/out/IWrappedNative.sol/IWrappedNative.json +1 -1
- package/out/MockCommitteeSync.sol/MockCommitteeSync.json +1 -1
- package/out/MockReactor.sol/MockReactor.json +1 -1
- package/out/OrderLib.sol/OrderLib.json +1 -1
- package/out/OrderReactor.E2E.t.sol/OrderReactorE2ETest.json +1 -1
- package/out/OrderReactor.sol/OrderReactor.json +1 -1
- package/out/OrderReactorPause.t.sol/OrderReactorPauseTest.json +1 -1
- package/out/OrderReactorPause.t.sol/SimpleCallback.json +1 -1
- package/out/OrderValidationLib.Fuzz.t.sol/OrderValidationLibFuzzTest.json +1 -1
- package/out/OrderValidationLib.sol/OrderValidationLib.json +1 -1
- package/out/OrderValidationLib.t.sol/OrderValidationLibTest.json +1 -1
- package/out/P2DexAdapter.sol/P2DexAdapter.json +1 -1
- package/out/P2DexAdapter.t.sol/P2DexAdapterTest.json +1 -1
- package/out/ParaswapDexAdapter.sol/ParaswapDexAdapter.json +1 -1
- package/out/ParaswapDexAdapter.t.sol/ParaswapDexAdapterTest.json +1 -1
- package/out/RePermit.t.sol/RePermitTest.json +1 -1
- package/out/Refinery.t.sol/PayableTarget.json +1 -1
- package/out/Refinery.t.sol/RefineryTest.json +1 -1
- package/out/ResolutionLib.Fuzz.t.sol/ResolutionLibFuzzTest.json +1 -1
- package/out/ResolutionLib.sol/ResolutionLib.json +1 -1
- package/out/ResolutionLib.t.sol/ResolutionLibTest.json +1 -1
- package/out/SettlementLib.sol/SettlementLib.json +1 -1
- package/out/SettlementLib.t.sol/SettlementLibTest.json +1 -1
- package/out/SettlementLib.t.sol/SettlementWrapper.json +1 -1
- package/out/Settler.sol/Settler.json +1 -1
- package/out/Settler.t.sol/SettlerTest.json +1 -1
- package/out/SwapAdapter.sol/SwapAdapterMock.json +1 -1
- package/out/UniversalAdapter.sol/UniversalAdapter.json +1 -1
- package/out/UniversalAdapter.t.sol/UniversalAdapterTest.json +1 -1
- package/out/WM.t.sol/WMTest.json +1 -1
- package/out/WrappedNativeMock.sol/WrappedNativeMock.json +1 -1
- package/out/build-info/3df4e20ba14c59dd.json +1 -0
- package/out/build-info/465c047ca4b4665a.json +1 -0
- package/out/build-info/53b32d3edef4f2f5.json +1 -0
- package/out/build-info/60dfb67f4a3af77c.json +1 -0
- package/out/build-info/9750d071e9bed8fe.json +1 -0
- package/package.json +4 -1
- package/skills/advanced-swap-orders/SKILL.md +54 -0
- package/skills/advanced-swap-orders/assets/repermit.skeleton.json +195 -0
- package/skills/advanced-swap-orders/assets/token-addressbook.md +97 -0
- package/skills/advanced-swap-orders/assets/web3-sign-and-submit.example.js +91 -0
- package/skills/advanced-swap-orders/manifest.json +70 -0
- package/skills/advanced-swap-orders/references/01-quickstart.md +13 -0
- package/skills/advanced-swap-orders/references/02-params.md +39 -0
- package/skills/advanced-swap-orders/references/03-sign.md +7 -0
- package/skills/advanced-swap-orders/references/04-patterns.md +12 -0
- package/skills/advanced-swap-orders/scripts/order.js +940 -0
|
@@ -0,0 +1,940 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const path = require('node:path');
|
|
7
|
+
|
|
8
|
+
const MAX_SLIPPAGE = 5000n;
|
|
9
|
+
const DEF_SLIPPAGE = 500n;
|
|
10
|
+
const EXCLUSIVITY = 0;
|
|
11
|
+
const REF_SHARE = 0;
|
|
12
|
+
const FRESHNESS = 30;
|
|
13
|
+
const TTL = 300n;
|
|
14
|
+
const U32_MAX = 4294967295n;
|
|
15
|
+
const NOTE_ORACLE = 'Oracle protection applies to all order types and every chunk.';
|
|
16
|
+
const NOTE_EPOCH = 'epoch is the delay between chunks, but it is not exact: one chunk can fill once anywhere inside each epoch window.';
|
|
17
|
+
const NOTE_SIGN = 'Sign typedData with any EIP-712 flow. eth_signTypedData_v4 is only an example.';
|
|
18
|
+
const WARN_LOW_SLIPPAGE = 'slippage below 5% can reduce fill probability. 5% is the default compromise; higher slippage still uses oracle pricing and offchain executors.';
|
|
19
|
+
const WARN_RECIPIENT = 'recipient differs from swapper and is dangerous to change';
|
|
20
|
+
|
|
21
|
+
const SCRIPT_DIR = __dirname;
|
|
22
|
+
const ROOT = path.resolve(SCRIPT_DIR, '..');
|
|
23
|
+
const SKELETON = path.join(ROOT, 'assets', 'repermit.skeleton.json');
|
|
24
|
+
const MANIFEST_JSON = path.join(ROOT, 'manifest.json');
|
|
25
|
+
|
|
26
|
+
let runtimeConfig = null;
|
|
27
|
+
let skeletonCache = null;
|
|
28
|
+
let ZERO = '';
|
|
29
|
+
let SINK = '';
|
|
30
|
+
let CREATE_URL = '';
|
|
31
|
+
let QUERY_URL = '';
|
|
32
|
+
let REPERMIT = '';
|
|
33
|
+
let REACTOR = '';
|
|
34
|
+
let EXECUTOR = '';
|
|
35
|
+
let SUPPORTED_CHAIN_IDS = '';
|
|
36
|
+
let warnings = [];
|
|
37
|
+
|
|
38
|
+
class CliError extends Error {}
|
|
39
|
+
|
|
40
|
+
function die(message) {
|
|
41
|
+
throw new CliError(message);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function lower(value) {
|
|
45
|
+
return String(value).toLowerCase();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function trim(value) {
|
|
49
|
+
return String(value ?? '').trim();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function warn(message) {
|
|
53
|
+
warnings.push(message);
|
|
54
|
+
process.stderr.write(`warning: ${message}\n`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function firstDefined(...values) {
|
|
58
|
+
for (const value of values) {
|
|
59
|
+
if (value !== undefined && value !== null) {
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function isPlainObject(value) {
|
|
67
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function objectOrEmpty(value) {
|
|
71
|
+
return isPlainObject(value) ? value : {};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function decimalString(value, name = 'value') {
|
|
75
|
+
const text = trim(value);
|
|
76
|
+
if (!text) {
|
|
77
|
+
die(`${name} is required`);
|
|
78
|
+
}
|
|
79
|
+
if (/^\d+$/.test(text)) {
|
|
80
|
+
return BigInt(text).toString(10);
|
|
81
|
+
}
|
|
82
|
+
if (/^0[xX][0-9a-fA-F]+$/.test(text)) {
|
|
83
|
+
return BigInt(text).toString(10);
|
|
84
|
+
}
|
|
85
|
+
die(`${name} must be decimal or 0x integer`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function decimalOnly(value, name = 'value') {
|
|
89
|
+
const text = trim(value);
|
|
90
|
+
if (!/^\d+$/.test(text)) {
|
|
91
|
+
die(`${name} must be decimal`);
|
|
92
|
+
}
|
|
93
|
+
return BigInt(text);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function compare(a, b) {
|
|
97
|
+
const left = decimalOnly(a);
|
|
98
|
+
const right = decimalOnly(b);
|
|
99
|
+
if (left < right) {
|
|
100
|
+
return -1;
|
|
101
|
+
}
|
|
102
|
+
if (left > right) {
|
|
103
|
+
return 1;
|
|
104
|
+
}
|
|
105
|
+
return 0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function eq(a, b) {
|
|
109
|
+
return compare(a, b) === 0;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function gt(a, b) {
|
|
113
|
+
return compare(a, b) === 1;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function add(a, b) {
|
|
117
|
+
return (decimalOnly(a) + decimalOnly(b)).toString(10);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function subtract(a, b) {
|
|
121
|
+
const left = decimalOnly(a);
|
|
122
|
+
const right = decimalOnly(b);
|
|
123
|
+
if (left < right) {
|
|
124
|
+
die('internal subtraction underflow');
|
|
125
|
+
}
|
|
126
|
+
return (left - right).toString(10);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function multiply(a, b) {
|
|
130
|
+
return (decimalOnly(a) * decimalOnly(b)).toString(10);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function divideAndRemainder(a, b) {
|
|
134
|
+
const left = decimalOnly(a);
|
|
135
|
+
const right = decimalOnly(b);
|
|
136
|
+
if (right === 0n) {
|
|
137
|
+
die('division by zero');
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
quotient: (left / right).toString(10),
|
|
141
|
+
remainder: (left % right).toString(10),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function ensureU32(value, name) {
|
|
146
|
+
if (decimalOnly(value, name) > U32_MAX) {
|
|
147
|
+
die(`${name} must fit in uint32`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function hexBody(value, name, { allowBare = false } = {}) {
|
|
152
|
+
const text = trim(value);
|
|
153
|
+
if (/^0x[0-9a-fA-F]*$/.test(text)) {
|
|
154
|
+
return text.slice(2);
|
|
155
|
+
}
|
|
156
|
+
if (allowBare && /^[0-9a-fA-F]+$/.test(text)) {
|
|
157
|
+
return text;
|
|
158
|
+
}
|
|
159
|
+
die(`${name} must be hex`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function parseAddress(value, name, allowZero = false) {
|
|
163
|
+
const text = `0x${hexBody(value, name)}`;
|
|
164
|
+
if (text.length !== 42) {
|
|
165
|
+
die(`${name} must be a 20-byte 0x address`);
|
|
166
|
+
}
|
|
167
|
+
if (!allowZero && ZERO && lower(text) === lower(ZERO)) {
|
|
168
|
+
die(`${name} cannot be zero`);
|
|
169
|
+
}
|
|
170
|
+
return text;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function requireHex(value, name) {
|
|
174
|
+
const raw = hexBody(value, name);
|
|
175
|
+
if (raw.length % 2 !== 0) {
|
|
176
|
+
die(`${name} must be hex`);
|
|
177
|
+
}
|
|
178
|
+
return `0x${raw}`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function normalizeSizedHex(value, name, size) {
|
|
182
|
+
const raw = hexBody(value, name, { allowBare: true });
|
|
183
|
+
if (raw.length !== size) {
|
|
184
|
+
die(`${name} must be ${size} hex chars`);
|
|
185
|
+
}
|
|
186
|
+
return `0x${raw}`;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function padHex64(value, name = 'value') {
|
|
190
|
+
const raw = lower(hexBody(value, name, { allowBare: true }));
|
|
191
|
+
if (raw.length > 64) {
|
|
192
|
+
die(`${name} must fit in uint256`);
|
|
193
|
+
}
|
|
194
|
+
return `0x${raw.padStart(64, '0')}`;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function parseOptions(args, spec, command) {
|
|
198
|
+
const values = Object.fromEntries(Object.values(spec).map((key) => [key, '']));
|
|
199
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
200
|
+
const arg = args[index];
|
|
201
|
+
const key = spec[arg];
|
|
202
|
+
if (!key) {
|
|
203
|
+
die(`unknown ${command} arg: ${arg}`);
|
|
204
|
+
}
|
|
205
|
+
const value = args[index + 1];
|
|
206
|
+
if (value === undefined) {
|
|
207
|
+
die(`${command} arg requires a value: ${arg}`);
|
|
208
|
+
}
|
|
209
|
+
values[key] = value;
|
|
210
|
+
index += 1;
|
|
211
|
+
}
|
|
212
|
+
return values;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function countPresent(...values) {
|
|
216
|
+
return values.reduce((count, value) => count + (value ? 1 : 0), 0);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function approveCalldata(spender, amount) {
|
|
220
|
+
const spenderHex = padHex64(parseAddress(spender, 'approve.spender'), 'approve.spender');
|
|
221
|
+
const amountHex = padHex64(decimalOnly(amount, 'approve.amount').toString(16), 'approve.amount');
|
|
222
|
+
return `0x095ea7b3${spenderHex.slice(2)}${amountHex.slice(2)}`;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function normalizeSigV(value, name = 'signature.v') {
|
|
226
|
+
const raw = trim(value);
|
|
227
|
+
if (!raw) {
|
|
228
|
+
die(`${name} is required`);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
let decimal;
|
|
232
|
+
if (/^0[xX][0-9a-fA-F]+$/.test(raw)) {
|
|
233
|
+
decimal = BigInt(raw);
|
|
234
|
+
} else if (/^\d+$/.test(raw)) {
|
|
235
|
+
decimal = BigInt(raw);
|
|
236
|
+
} else if (/^[0-9a-fA-F]+$/.test(raw)) {
|
|
237
|
+
decimal = BigInt(`0x${raw}`);
|
|
238
|
+
} else {
|
|
239
|
+
die(`${name} must be 0, 1, 27, 28, or equivalent hex`);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
switch (decimal.toString(10)) {
|
|
243
|
+
case '0':
|
|
244
|
+
case '27':
|
|
245
|
+
return '0x1b';
|
|
246
|
+
case '1':
|
|
247
|
+
case '28':
|
|
248
|
+
return '0x1c';
|
|
249
|
+
default:
|
|
250
|
+
die(`${name} must be 0, 1, 27, 28, or equivalent hex`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function now() {
|
|
255
|
+
return Math.floor(Date.now() / 1000).toString(10);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function iso() {
|
|
259
|
+
return new Date().toISOString().replace(/\.\d{3}Z$/, 'Z');
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function readSource(src, name) {
|
|
263
|
+
const source = trim(src);
|
|
264
|
+
if (!source) {
|
|
265
|
+
die(`${name} is required`);
|
|
266
|
+
}
|
|
267
|
+
if (source === '-') {
|
|
268
|
+
return fs.readFileSync(0, 'utf8');
|
|
269
|
+
}
|
|
270
|
+
try {
|
|
271
|
+
return fs.readFileSync(source, 'utf8');
|
|
272
|
+
} catch (error) {
|
|
273
|
+
if (error && error.code === 'ENOENT') {
|
|
274
|
+
die(`${name} not found: ${source}`);
|
|
275
|
+
}
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function parseJson(text, name) {
|
|
281
|
+
try {
|
|
282
|
+
return JSON.parse(text);
|
|
283
|
+
} catch {
|
|
284
|
+
die(`${name} must be valid JSON`);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function readJsonSource(src, name) {
|
|
289
|
+
return parseJson(readSource(src, name), name);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function jsonOrText(text) {
|
|
293
|
+
if (text === '') {
|
|
294
|
+
return '';
|
|
295
|
+
}
|
|
296
|
+
try {
|
|
297
|
+
return JSON.parse(text);
|
|
298
|
+
} catch {
|
|
299
|
+
return text;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function writeOutput(value, outFile) {
|
|
304
|
+
const text = typeof value === 'string' ? value : JSON.stringify(value, null, 2);
|
|
305
|
+
if (outFile) {
|
|
306
|
+
fs.writeFileSync(outFile, `${text}\n`);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
process.stdout.write(`${text}\n`);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function loadRuntimeConfig() {
|
|
313
|
+
if (runtimeConfig) {
|
|
314
|
+
return runtimeConfig;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
let manifest;
|
|
318
|
+
try {
|
|
319
|
+
manifest = parseJson(fs.readFileSync(MANIFEST_JSON, 'utf8'), 'manifest');
|
|
320
|
+
} catch (error) {
|
|
321
|
+
if (error instanceof CliError) {
|
|
322
|
+
throw error;
|
|
323
|
+
}
|
|
324
|
+
if (error && error.code === 'ENOENT') {
|
|
325
|
+
die(`skill manifest not found: ${MANIFEST_JSON}`);
|
|
326
|
+
}
|
|
327
|
+
throw error;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const runtime = manifest && typeof manifest === 'object' ? manifest.runtime : null;
|
|
331
|
+
const contracts = runtime && typeof runtime === 'object' ? runtime.contracts : null;
|
|
332
|
+
const chains = runtime && typeof runtime === 'object' ? runtime.chains : null;
|
|
333
|
+
const url = runtime && typeof runtime.url === 'string' ? runtime.url : '';
|
|
334
|
+
const zero = contracts && typeof contracts.zero === 'string' ? contracts.zero : '';
|
|
335
|
+
const repermit = contracts && typeof contracts.repermit === 'string' ? contracts.repermit : '';
|
|
336
|
+
const reactor = contracts && typeof contracts.reactor === 'string' ? contracts.reactor : '';
|
|
337
|
+
const executor = contracts && typeof contracts.executor === 'string' ? contracts.executor : '';
|
|
338
|
+
|
|
339
|
+
const invalidRuntime =
|
|
340
|
+
!url ||
|
|
341
|
+
!zero ||
|
|
342
|
+
!repermit ||
|
|
343
|
+
!reactor ||
|
|
344
|
+
!executor ||
|
|
345
|
+
!chains ||
|
|
346
|
+
typeof chains !== 'object' ||
|
|
347
|
+
Array.isArray(chains) ||
|
|
348
|
+
Object.keys(chains).length === 0;
|
|
349
|
+
|
|
350
|
+
if (invalidRuntime) {
|
|
351
|
+
die(`invalid skill manifest runtime config: ${MANIFEST_JSON}`);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
ZERO = parseAddress(zero, 'runtime.contracts.zero', true);
|
|
355
|
+
REPERMIT = parseAddress(repermit, 'runtime.contracts.repermit');
|
|
356
|
+
REACTOR = parseAddress(reactor, 'runtime.contracts.reactor');
|
|
357
|
+
EXECUTOR = parseAddress(executor, 'runtime.contracts.executor');
|
|
358
|
+
SINK = url;
|
|
359
|
+
|
|
360
|
+
if (!/^https?:\/\//.test(SINK)) {
|
|
361
|
+
die('runtime.url must be http(s)');
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const supported = Object.keys(chains)
|
|
365
|
+
.map((chainId) => {
|
|
366
|
+
const parsed = Number(chainId);
|
|
367
|
+
if (!Number.isInteger(parsed)) {
|
|
368
|
+
die(`invalid skill manifest runtime config: ${MANIFEST_JSON}`);
|
|
369
|
+
}
|
|
370
|
+
return parsed;
|
|
371
|
+
})
|
|
372
|
+
.sort((left, right) => left - right)
|
|
373
|
+
.map((value) => String(value));
|
|
374
|
+
|
|
375
|
+
SUPPORTED_CHAIN_IDS = supported.join(', ');
|
|
376
|
+
if (!SUPPORTED_CHAIN_IDS) {
|
|
377
|
+
die(`skill manifest runtime has no supported chains: ${MANIFEST_JSON}`);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
CREATE_URL = `${SINK}/orders/new`;
|
|
381
|
+
QUERY_URL = `${SINK}/orders`;
|
|
382
|
+
runtimeConfig = { chains };
|
|
383
|
+
return runtimeConfig;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function hasSupportedChain(chainId) {
|
|
387
|
+
const config = loadRuntimeConfig();
|
|
388
|
+
const chain = config.chains[String(chainId)];
|
|
389
|
+
return !!(chain && typeof chain.adapter === 'string' && chain.adapter.length > 0);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function unsupportedChain(chainId) {
|
|
393
|
+
loadRuntimeConfig();
|
|
394
|
+
die(`unsupported chainId: ${chainId} (supported: ${SUPPORTED_CHAIN_IDS})`);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function resolveAdapter(chainId) {
|
|
398
|
+
const config = loadRuntimeConfig();
|
|
399
|
+
const adapter = config.chains[String(chainId)] && config.chains[String(chainId)].adapter;
|
|
400
|
+
if (!adapter) {
|
|
401
|
+
unsupportedChain(chainId);
|
|
402
|
+
}
|
|
403
|
+
return parseAddress(adapter, `runtime.chains[${chainId}].adapter`);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function usage() {
|
|
407
|
+
loadRuntimeConfig();
|
|
408
|
+
const lines = [
|
|
409
|
+
'Usage',
|
|
410
|
+
' node scripts/order.js prepare --params <params.json|-> [--out <prepared.json>]',
|
|
411
|
+
' node scripts/order.js submit --prepared <prepared.json|-> [--signature <0x...|json>|--signature-file <file|->|--r <0x...> --s <0x...> --v <0x..>] [--out <response.json>]',
|
|
412
|
+
' node scripts/order.js query (--swapper <0x...>|--hash <0x...>) [--out <response.json>]',
|
|
413
|
+
'',
|
|
414
|
+
'Safety',
|
|
415
|
+
' Use only the provided helper script. Do not send typed data or signatures anywhere else.',
|
|
416
|
+
'',
|
|
417
|
+
'Prepare',
|
|
418
|
+
' Builds a prepared order JSON with:',
|
|
419
|
+
' - approval calldata for the input ERC-20',
|
|
420
|
+
' - populated EIP-712 typed data',
|
|
421
|
+
' - submit payload template',
|
|
422
|
+
' - query URL',
|
|
423
|
+
' Supports --params <file> or --params - for stdin JSON.',
|
|
424
|
+
' Supports market, limit, stop-loss, take-profit, delayed-start, and chunked/TWAP-style orders.',
|
|
425
|
+
' Defaults:',
|
|
426
|
+
' - input.maxAmount = input.amount',
|
|
427
|
+
' - nonce = now',
|
|
428
|
+
' - start = now',
|
|
429
|
+
' - epoch = 0 for single orders, 60 for chunked orders',
|
|
430
|
+
' - deadline = start + 300 + chunkCount * epoch (conservative helper default)',
|
|
431
|
+
' - slippage = 500',
|
|
432
|
+
' - output.limit = 0',
|
|
433
|
+
' - output.recipient = swapper',
|
|
434
|
+
' Rules:',
|
|
435
|
+
` - supported chainIds: ${SUPPORTED_CHAIN_IDS}`,
|
|
436
|
+
' - chunked orders require epoch > 0',
|
|
437
|
+
' - epoch is the delay between chunks, but it is not exact: one chunk can fill once anywhere inside each epoch window',
|
|
438
|
+
' - native input is not supported; wrap to WNATIVE first',
|
|
439
|
+
' - native output is supported with output.token = 0x0000000000000000000000000000000000000000',
|
|
440
|
+
' - output.limit and triggers are output-token units per chunk',
|
|
441
|
+
'',
|
|
442
|
+
'Submit',
|
|
443
|
+
' Builds or sends the relay POST body from a prepared order.',
|
|
444
|
+
' Supports --prepared <file> or --prepared - for stdin JSON.',
|
|
445
|
+
' Supports exactly one signature mode:',
|
|
446
|
+
' - --signature <full 65-byte hex signature>',
|
|
447
|
+
' - --signature <JSON string or JSON with full signature / r,s,v>',
|
|
448
|
+
' - --signature-file <file|-> containing full signature, JSON string, or JSON with full signature / r,s,v',
|
|
449
|
+
' - --r <0x...> --s <0x...> --v <0x..>',
|
|
450
|
+
" All signature inputs are normalized to the relay's r/s/v object format.",
|
|
451
|
+
'',
|
|
452
|
+
'Query',
|
|
453
|
+
' Builds or sends the relay GET request.',
|
|
454
|
+
' Supports only:',
|
|
455
|
+
' - --swapper <0x...>',
|
|
456
|
+
' - --hash <0x...>',
|
|
457
|
+
];
|
|
458
|
+
process.stdout.write(`${lines.join('\n')}\n`);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function loadSkeleton() {
|
|
462
|
+
if (!skeletonCache) {
|
|
463
|
+
skeletonCache = parseJson(fs.readFileSync(SKELETON, 'utf8'), 'typed data skeleton');
|
|
464
|
+
}
|
|
465
|
+
return JSON.parse(JSON.stringify(skeletonCache));
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function buildTypedData(
|
|
469
|
+
chainId,
|
|
470
|
+
swapper,
|
|
471
|
+
nonce,
|
|
472
|
+
start,
|
|
473
|
+
deadline,
|
|
474
|
+
epoch,
|
|
475
|
+
slippage,
|
|
476
|
+
inputToken,
|
|
477
|
+
inputAmount,
|
|
478
|
+
inputMaxAmount,
|
|
479
|
+
outputToken,
|
|
480
|
+
outputLimit,
|
|
481
|
+
outputTriggerLower,
|
|
482
|
+
outputTriggerUpper,
|
|
483
|
+
outputRecipient,
|
|
484
|
+
) {
|
|
485
|
+
const typedData = loadSkeleton();
|
|
486
|
+
typedData.domain.chainId = Number(chainId);
|
|
487
|
+
typedData.domain.verifyingContract = REPERMIT;
|
|
488
|
+
typedData.message.permitted.token = inputToken;
|
|
489
|
+
typedData.message.permitted.amount = inputMaxAmount;
|
|
490
|
+
typedData.message.spender = REACTOR;
|
|
491
|
+
typedData.message.nonce = nonce;
|
|
492
|
+
typedData.message.deadline = deadline;
|
|
493
|
+
typedData.message.witness.reactor = REACTOR;
|
|
494
|
+
typedData.message.witness.executor = EXECUTOR;
|
|
495
|
+
typedData.message.witness.exchange.adapter = resolveAdapter(chainId);
|
|
496
|
+
typedData.message.witness.exchange.ref = ZERO;
|
|
497
|
+
typedData.message.witness.exchange.share = REF_SHARE;
|
|
498
|
+
typedData.message.witness.exchange.data = '0x';
|
|
499
|
+
typedData.message.witness.swapper = swapper;
|
|
500
|
+
typedData.message.witness.nonce = nonce;
|
|
501
|
+
typedData.message.witness.start = start;
|
|
502
|
+
typedData.message.witness.deadline = deadline;
|
|
503
|
+
typedData.message.witness.chainid = Number(chainId);
|
|
504
|
+
typedData.message.witness.exclusivity = EXCLUSIVITY;
|
|
505
|
+
typedData.message.witness.epoch = Number(epoch);
|
|
506
|
+
typedData.message.witness.slippage = Number(slippage);
|
|
507
|
+
typedData.message.witness.freshness = FRESHNESS;
|
|
508
|
+
typedData.message.witness.input.token = inputToken;
|
|
509
|
+
typedData.message.witness.input.amount = inputAmount;
|
|
510
|
+
typedData.message.witness.input.maxAmount = inputMaxAmount;
|
|
511
|
+
typedData.message.witness.output.token = outputToken;
|
|
512
|
+
typedData.message.witness.output.limit = outputLimit;
|
|
513
|
+
typedData.message.witness.output.triggerLower = outputTriggerLower;
|
|
514
|
+
typedData.message.witness.output.triggerUpper = outputTriggerUpper;
|
|
515
|
+
typedData.message.witness.output.recipient = outputRecipient;
|
|
516
|
+
return typedData;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function signatureFieldsFromObject(parsed) {
|
|
520
|
+
if (typeof parsed.signature === 'string') {
|
|
521
|
+
return { payload: parsed.signature };
|
|
522
|
+
}
|
|
523
|
+
if (typeof parsed.full === 'string') {
|
|
524
|
+
return { payload: parsed.full };
|
|
525
|
+
}
|
|
526
|
+
const source = isPlainObject(parsed.signature) ? parsed.signature : parsed;
|
|
527
|
+
const r = source.r ?? '';
|
|
528
|
+
const s = source.s ?? '';
|
|
529
|
+
const v = source.v ?? '';
|
|
530
|
+
if (!r || !s || !v) {
|
|
531
|
+
die('signature JSON must contain a full signature string or r, s, v');
|
|
532
|
+
}
|
|
533
|
+
return { r, s, v, kind: 'rsv' };
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
function normalizeSignature(payloadInput) {
|
|
537
|
+
let payload = trim(payloadInput);
|
|
538
|
+
let r = '';
|
|
539
|
+
let s = '';
|
|
540
|
+
let v = '';
|
|
541
|
+
let full = '';
|
|
542
|
+
let kind = '';
|
|
543
|
+
|
|
544
|
+
if (!payload) {
|
|
545
|
+
die('signature input is empty');
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
try {
|
|
549
|
+
const parsed = JSON.parse(payload);
|
|
550
|
+
if (typeof parsed === 'string') {
|
|
551
|
+
payload = parsed;
|
|
552
|
+
} else if (isPlainObject(parsed)) {
|
|
553
|
+
({ payload = payload, r = '', s = '', v = '', kind = '' } = signatureFieldsFromObject(parsed));
|
|
554
|
+
} else {
|
|
555
|
+
die('signature JSON must contain a full signature string or r, s, v');
|
|
556
|
+
}
|
|
557
|
+
} catch (error) {
|
|
558
|
+
if (error instanceof CliError) {
|
|
559
|
+
throw error;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
if (r || s || v) {
|
|
564
|
+
r = normalizeSizedHex(r, 'signature.r', 64);
|
|
565
|
+
s = normalizeSizedHex(s, 'signature.s', 64);
|
|
566
|
+
v = normalizeSigV(v, 'signature.v');
|
|
567
|
+
full = `${r}${s.slice(2)}${v.slice(2)}`;
|
|
568
|
+
} else {
|
|
569
|
+
let signature = trim(payload);
|
|
570
|
+
if (!/^(?:0x)?[0-9a-fA-F]{130}$/.test(signature)) {
|
|
571
|
+
die('signature must be full hex, a JSON string, or r/s/v JSON');
|
|
572
|
+
}
|
|
573
|
+
if (!signature.startsWith('0x')) {
|
|
574
|
+
signature = `0x${signature}`;
|
|
575
|
+
}
|
|
576
|
+
full = signature;
|
|
577
|
+
r = `0x${signature.slice(2, 66)}`;
|
|
578
|
+
s = `0x${signature.slice(66, 130)}`;
|
|
579
|
+
v = normalizeSigV(`0x${signature.slice(130, 132)}`, 'signature.v');
|
|
580
|
+
if (!kind) {
|
|
581
|
+
kind = 'full';
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
return {
|
|
586
|
+
kind,
|
|
587
|
+
full,
|
|
588
|
+
signature: { r, s, v },
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function prepare(args) {
|
|
593
|
+
loadRuntimeConfig();
|
|
594
|
+
|
|
595
|
+
warnings = [];
|
|
596
|
+
|
|
597
|
+
const { paramsSource, outFile } = parseOptions(
|
|
598
|
+
args,
|
|
599
|
+
{ '--params': 'paramsSource', '--out': 'outFile' },
|
|
600
|
+
'prepare',
|
|
601
|
+
);
|
|
602
|
+
|
|
603
|
+
const params = readJsonSource(paramsSource, 'params');
|
|
604
|
+
const input = objectOrEmpty(params.input);
|
|
605
|
+
const output = objectOrEmpty(params.output);
|
|
606
|
+
const nowTs = now();
|
|
607
|
+
const chainId = decimalString(firstDefined(params.chainId, params.chainID), 'chainId');
|
|
608
|
+
if (!hasSupportedChain(chainId)) {
|
|
609
|
+
unsupportedChain(chainId);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
const swapper = parseAddress(firstDefined(params.swapper, params.account, params.signer), 'swapper');
|
|
613
|
+
const nonce = decimalString(firstDefined(params.nonce, nowTs), 'nonce');
|
|
614
|
+
const start = decimalString(firstDefined(params.start, nowTs), 'start');
|
|
615
|
+
const slippage = decimalString(firstDefined(params.slippage, DEF_SLIPPAGE.toString(10)), 'slippage');
|
|
616
|
+
const inputToken = parseAddress(firstDefined(input.token, params.inputToken), 'input.token');
|
|
617
|
+
const inputAmount = decimalString(firstDefined(input.amount, params.inputAmount), 'input.amount');
|
|
618
|
+
let inputMaxAmount = decimalString(
|
|
619
|
+
firstDefined(input.maxAmount, params.inputMaxAmount, inputAmount),
|
|
620
|
+
'input.maxAmount',
|
|
621
|
+
);
|
|
622
|
+
const outputToken = parseAddress(firstDefined(output.token, params.outputToken), 'output.token', true);
|
|
623
|
+
const outputLimit = decimalString(firstDefined(output.limit, params.outputLimit, '0'), 'output.limit');
|
|
624
|
+
const outputTriggerLower = decimalString(
|
|
625
|
+
firstDefined(output.triggerLower, params.outputTriggerLower, '0'),
|
|
626
|
+
'output.triggerLower',
|
|
627
|
+
);
|
|
628
|
+
const outputTriggerUpper = decimalString(
|
|
629
|
+
firstDefined(output.triggerUpper, params.outputTriggerUpper, '0'),
|
|
630
|
+
'output.triggerUpper',
|
|
631
|
+
);
|
|
632
|
+
const recipient = parseAddress(firstDefined(output.recipient, params.recipient, swapper), 'output.recipient');
|
|
633
|
+
|
|
634
|
+
let epoch;
|
|
635
|
+
if (params.epoch !== undefined && params.epoch !== null) {
|
|
636
|
+
epoch = decimalString(params.epoch, 'epoch');
|
|
637
|
+
} else {
|
|
638
|
+
epoch = eq(inputAmount, inputMaxAmount) ? '0' : '60';
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
ensureU32(epoch, 'epoch');
|
|
642
|
+
ensureU32(slippage, 'slippage');
|
|
643
|
+
|
|
644
|
+
if (eq(start, '0')) {
|
|
645
|
+
die('start must be non-zero');
|
|
646
|
+
}
|
|
647
|
+
if (eq(inputAmount, '0')) {
|
|
648
|
+
die('input.amount must be non-zero');
|
|
649
|
+
}
|
|
650
|
+
if (gt(inputAmount, inputMaxAmount)) {
|
|
651
|
+
die('input.amount cannot exceed input.maxAmount');
|
|
652
|
+
}
|
|
653
|
+
if (lower(inputToken) === lower(outputToken)) {
|
|
654
|
+
die('input.token and output.token must differ');
|
|
655
|
+
}
|
|
656
|
+
if (!eq(outputTriggerUpper, '0') && gt(outputTriggerLower, outputTriggerUpper)) {
|
|
657
|
+
die('output.triggerLower cannot exceed output.triggerUpper');
|
|
658
|
+
}
|
|
659
|
+
if (gt(slippage, MAX_SLIPPAGE.toString(10))) {
|
|
660
|
+
die(`slippage cannot exceed ${MAX_SLIPPAGE.toString(10)}`);
|
|
661
|
+
}
|
|
662
|
+
if (!eq(epoch, '0') && compare(String(FRESHNESS), epoch) !== -1) {
|
|
663
|
+
die('freshness must be < epoch when epoch != 0');
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
const requestedInputMaxAmount = inputMaxAmount;
|
|
667
|
+
const chunking = divideAndRemainder(inputMaxAmount, inputAmount);
|
|
668
|
+
const chunkCount = chunking.quotient;
|
|
669
|
+
const remainder = chunking.remainder;
|
|
670
|
+
|
|
671
|
+
if (!eq(remainder, '0')) {
|
|
672
|
+
inputMaxAmount = subtract(inputMaxAmount, remainder);
|
|
673
|
+
warn(
|
|
674
|
+
`input.maxAmount is not divisible by input.amount; rounding down from ${requestedInputMaxAmount} to ${inputMaxAmount} to keep fixed chunk sizes`,
|
|
675
|
+
);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
if (!eq(inputAmount, inputMaxAmount) && eq(epoch, '0')) {
|
|
679
|
+
die('chunked orders require epoch > 0');
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
const kind = eq(inputAmount, inputMaxAmount) ? 'single' : 'chunked';
|
|
683
|
+
let deadline;
|
|
684
|
+
if (params.deadline !== undefined && params.deadline !== null) {
|
|
685
|
+
deadline = decimalString(params.deadline, 'deadline');
|
|
686
|
+
} else {
|
|
687
|
+
deadline = add(start, TTL.toString(10));
|
|
688
|
+
if (gt(epoch, '0')) {
|
|
689
|
+
deadline = add(deadline, multiply(chunkCount, epoch));
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
if (gt(start, nowTs)) {
|
|
694
|
+
if (!gt(deadline, start)) {
|
|
695
|
+
die('deadline must be after start');
|
|
696
|
+
}
|
|
697
|
+
} else if (!gt(deadline, nowTs)) {
|
|
698
|
+
die('deadline must be after current time');
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
if (compare(slippage, DEF_SLIPPAGE.toString(10)) === -1) {
|
|
702
|
+
warn(WARN_LOW_SLIPPAGE);
|
|
703
|
+
}
|
|
704
|
+
if (lower(recipient) !== lower(swapper)) {
|
|
705
|
+
warn(WARN_RECIPIENT);
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
const approvalData = requireHex(approveCalldata(REPERMIT, inputMaxAmount), 'approval.tx.data');
|
|
709
|
+
const typedData = buildTypedData(
|
|
710
|
+
chainId,
|
|
711
|
+
swapper,
|
|
712
|
+
nonce,
|
|
713
|
+
start,
|
|
714
|
+
deadline,
|
|
715
|
+
epoch,
|
|
716
|
+
slippage,
|
|
717
|
+
inputToken,
|
|
718
|
+
inputAmount,
|
|
719
|
+
inputMaxAmount,
|
|
720
|
+
outputToken,
|
|
721
|
+
outputLimit,
|
|
722
|
+
outputTriggerLower,
|
|
723
|
+
outputTriggerUpper,
|
|
724
|
+
recipient,
|
|
725
|
+
);
|
|
726
|
+
|
|
727
|
+
const prepared = {
|
|
728
|
+
meta: {
|
|
729
|
+
preparedAt: iso(),
|
|
730
|
+
kind,
|
|
731
|
+
chunkCount,
|
|
732
|
+
chunkInputAmount: inputAmount,
|
|
733
|
+
start,
|
|
734
|
+
deadline,
|
|
735
|
+
epoch,
|
|
736
|
+
epochScheduling: NOTE_EPOCH,
|
|
737
|
+
limit: outputLimit,
|
|
738
|
+
oracleProtection: NOTE_ORACLE,
|
|
739
|
+
},
|
|
740
|
+
warnings,
|
|
741
|
+
approval: {
|
|
742
|
+
token: inputToken,
|
|
743
|
+
spender: REPERMIT,
|
|
744
|
+
amount: inputMaxAmount,
|
|
745
|
+
tx: {
|
|
746
|
+
to: inputToken,
|
|
747
|
+
data: approvalData,
|
|
748
|
+
value: '0x0',
|
|
749
|
+
},
|
|
750
|
+
},
|
|
751
|
+
typedData,
|
|
752
|
+
signing: {
|
|
753
|
+
signer: swapper,
|
|
754
|
+
note: NOTE_SIGN,
|
|
755
|
+
},
|
|
756
|
+
submit: {
|
|
757
|
+
url: CREATE_URL,
|
|
758
|
+
body: {
|
|
759
|
+
order: typedData.message,
|
|
760
|
+
signature: {
|
|
761
|
+
r: null,
|
|
762
|
+
s: null,
|
|
763
|
+
v: null,
|
|
764
|
+
},
|
|
765
|
+
status: 'pending',
|
|
766
|
+
},
|
|
767
|
+
},
|
|
768
|
+
query: {
|
|
769
|
+
url: QUERY_URL,
|
|
770
|
+
},
|
|
771
|
+
};
|
|
772
|
+
|
|
773
|
+
writeOutput(prepared, outFile);
|
|
774
|
+
return 0;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
function selectOrderPayload(prepared) {
|
|
778
|
+
if (prepared && prepared.submit && prepared.submit.body && prepared.submit.body.order) {
|
|
779
|
+
return prepared.submit.body.order;
|
|
780
|
+
}
|
|
781
|
+
if (prepared && prepared.typedData && prepared.typedData.message) {
|
|
782
|
+
return prepared.typedData.message;
|
|
783
|
+
}
|
|
784
|
+
if (prepared && prepared.domain && prepared.types && prepared.message) {
|
|
785
|
+
return prepared.message;
|
|
786
|
+
}
|
|
787
|
+
die('missing order payload');
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
async function requestJson(url, options) {
|
|
791
|
+
const response = await fetch(url, options);
|
|
792
|
+
const text = await response.text();
|
|
793
|
+
return {
|
|
794
|
+
ok: response.ok,
|
|
795
|
+
status: response.status,
|
|
796
|
+
response: jsonOrText(text),
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
async function submit(args) {
|
|
801
|
+
loadRuntimeConfig();
|
|
802
|
+
|
|
803
|
+
const { preparedSource, signatureInput, signatureFile, r, s, v, outFile } = parseOptions(
|
|
804
|
+
args,
|
|
805
|
+
{
|
|
806
|
+
'--prepared': 'preparedSource',
|
|
807
|
+
'--signature': 'signatureInput',
|
|
808
|
+
'--signature-file': 'signatureFile',
|
|
809
|
+
'--r': 'r',
|
|
810
|
+
'--s': 's',
|
|
811
|
+
'--v': 'v',
|
|
812
|
+
'--out': 'outFile',
|
|
813
|
+
},
|
|
814
|
+
'submit',
|
|
815
|
+
);
|
|
816
|
+
|
|
817
|
+
if (preparedSource === '-' && signatureFile === '-') {
|
|
818
|
+
die('submit supports only one stdin source');
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
const prepared = readJsonSource(preparedSource, 'prepared');
|
|
822
|
+
if (countPresent(signatureInput, signatureFile, r || s || v) !== 1) {
|
|
823
|
+
die('submit needs exactly one of --signature, --signature-file, or --r/--s/--v');
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
let normalizedSignature;
|
|
827
|
+
if (signatureFile) {
|
|
828
|
+
normalizedSignature = normalizeSignature(readSource(signatureFile, 'signature-file'));
|
|
829
|
+
} else if (signatureInput) {
|
|
830
|
+
normalizedSignature = normalizeSignature(signatureInput);
|
|
831
|
+
} else {
|
|
832
|
+
if (!r || !s || !v) {
|
|
833
|
+
die('--r --s --v must be used together');
|
|
834
|
+
}
|
|
835
|
+
normalizedSignature = normalizeSignature(JSON.stringify({ r, s, v }));
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
const request = {
|
|
839
|
+
url: (prepared.submit && prepared.submit.url) || CREATE_URL,
|
|
840
|
+
body: {
|
|
841
|
+
order: selectOrderPayload(prepared),
|
|
842
|
+
signature: normalizedSignature.signature,
|
|
843
|
+
status: (prepared.submit && prepared.submit.body && prepared.submit.body.status) || 'pending',
|
|
844
|
+
},
|
|
845
|
+
signatureInput: normalizedSignature.kind,
|
|
846
|
+
};
|
|
847
|
+
|
|
848
|
+
const result = await requestJson(request.url, {
|
|
849
|
+
method: 'POST',
|
|
850
|
+
headers: { 'content-type': 'application/json' },
|
|
851
|
+
body: JSON.stringify(request.body),
|
|
852
|
+
});
|
|
853
|
+
|
|
854
|
+
writeOutput(
|
|
855
|
+
{
|
|
856
|
+
ok: result.ok,
|
|
857
|
+
status: result.status,
|
|
858
|
+
url: request.url,
|
|
859
|
+
request,
|
|
860
|
+
response: result.response,
|
|
861
|
+
},
|
|
862
|
+
outFile,
|
|
863
|
+
);
|
|
864
|
+
|
|
865
|
+
return result.ok ? 0 : 1;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
async function query(args) {
|
|
869
|
+
loadRuntimeConfig();
|
|
870
|
+
|
|
871
|
+
let { swapper, hash, outFile } = parseOptions(
|
|
872
|
+
args,
|
|
873
|
+
{ '--swapper': 'swapper', '--hash': 'hash', '--out': 'outFile' },
|
|
874
|
+
'query',
|
|
875
|
+
);
|
|
876
|
+
|
|
877
|
+
if (!swapper && !hash) {
|
|
878
|
+
die('query needs --swapper or --hash');
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
let url = QUERY_URL;
|
|
882
|
+
if (swapper) {
|
|
883
|
+
swapper = parseAddress(swapper, 'swapper');
|
|
884
|
+
url = `${url}?swapper=${encodeURIComponent(swapper)}`;
|
|
885
|
+
}
|
|
886
|
+
if (hash) {
|
|
887
|
+
if (!/^0x[0-9a-fA-F]{64}$/.test(hash)) {
|
|
888
|
+
die('hash must be 32-byte 0x hex');
|
|
889
|
+
}
|
|
890
|
+
url = url.includes('?') ? `${url}&hash=${encodeURIComponent(hash)}` : `${url}?hash=${encodeURIComponent(hash)}`;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
const result = await requestJson(url, { method: 'GET' });
|
|
894
|
+
writeOutput(
|
|
895
|
+
{
|
|
896
|
+
ok: result.ok,
|
|
897
|
+
status: result.status,
|
|
898
|
+
url,
|
|
899
|
+
response: result.response,
|
|
900
|
+
},
|
|
901
|
+
outFile,
|
|
902
|
+
);
|
|
903
|
+
|
|
904
|
+
return result.ok ? 0 : 1;
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
async function run() {
|
|
908
|
+
const args = process.argv.slice(2);
|
|
909
|
+
const command = args[0] || '';
|
|
910
|
+
|
|
911
|
+
if (!command || command === 'help' || command === '--help' || command === '-h') {
|
|
912
|
+
usage();
|
|
913
|
+
return command ? 0 : 1;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
switch (command) {
|
|
917
|
+
case 'prepare':
|
|
918
|
+
return prepare(args.slice(1));
|
|
919
|
+
case 'submit':
|
|
920
|
+
return submit(args.slice(1));
|
|
921
|
+
case 'query':
|
|
922
|
+
return query(args.slice(1));
|
|
923
|
+
default:
|
|
924
|
+
usage();
|
|
925
|
+
return 1;
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
run()
|
|
930
|
+
.then((code) => {
|
|
931
|
+
process.exitCode = code;
|
|
932
|
+
})
|
|
933
|
+
.catch((error) => {
|
|
934
|
+
if (error instanceof CliError) {
|
|
935
|
+
process.stderr.write(`error: ${error.message}\n`);
|
|
936
|
+
process.exitCode = 1;
|
|
937
|
+
return;
|
|
938
|
+
}
|
|
939
|
+
throw error;
|
|
940
|
+
});
|