@vordium/sdk 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +109 -2
- package/dist/cjs/account.js +126 -0
- package/dist/cjs/agents.js +237 -0
- package/dist/cjs/client.js +526 -0
- package/dist/cjs/domains.js +37 -0
- package/dist/cjs/errors.js +39 -0
- package/dist/cjs/feeds.js +278 -0
- package/dist/cjs/http.js +97 -0
- package/dist/cjs/index.js +37 -0
- package/dist/cjs/network.js +114 -0
- package/dist/cjs/nonce.js +36 -0
- package/dist/cjs/orders.js +260 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/reads.js +121 -0
- package/dist/cjs/rounding.js +252 -0
- package/dist/cjs/sign.js +49 -0
- package/dist/cjs/types.js +57 -0
- package/dist/cjs/units.js +78 -0
- package/dist/esm/account.d.ts +61 -0
- package/dist/esm/account.js +114 -0
- package/dist/esm/agents.d.ts +71 -0
- package/dist/esm/agents.js +225 -0
- package/dist/esm/client.d.ts +239 -0
- package/dist/esm/client.js +520 -0
- package/dist/esm/domains.d.ts +21 -0
- package/dist/esm/domains.js +30 -0
- package/dist/esm/errors.d.ts +18 -0
- package/dist/esm/errors.js +32 -0
- package/dist/esm/feeds.d.ts +97 -0
- package/dist/esm/feeds.js +267 -0
- package/dist/esm/http.d.ts +24 -0
- package/dist/esm/http.js +93 -0
- package/dist/esm/index.d.ts +15 -0
- package/dist/esm/index.js +15 -0
- package/dist/esm/network.d.ts +44 -0
- package/dist/esm/network.js +107 -0
- package/dist/esm/nonce.d.ts +15 -0
- package/dist/esm/nonce.js +31 -0
- package/dist/esm/orders.d.ts +141 -0
- package/dist/esm/orders.js +238 -0
- package/dist/esm/reads.d.ts +59 -0
- package/dist/esm/reads.js +113 -0
- package/dist/esm/rounding.d.ts +70 -0
- package/dist/esm/rounding.js +229 -0
- package/dist/esm/sign.d.ts +27 -0
- package/dist/esm/sign.js +43 -0
- package/dist/esm/types.d.ts +41 -0
- package/dist/esm/types.js +51 -0
- package/dist/esm/units.d.ts +16 -0
- package/dist/esm/units.js +70 -0
- package/package.json +53 -4
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Account = exports.Vordium = void 0;
|
|
4
|
+
exports.twapOnSteps = twapOnSteps;
|
|
5
|
+
const ethers_1 = require("ethers");
|
|
6
|
+
const account_js_1 = require("./account.js");
|
|
7
|
+
const agents_js_1 = require("./agents.js");
|
|
8
|
+
const domains_js_1 = require("./domains.js");
|
|
9
|
+
const errors_js_1 = require("./errors.js");
|
|
10
|
+
const http_js_1 = require("./http.js");
|
|
11
|
+
const network_js_1 = require("./network.js");
|
|
12
|
+
const nonce_js_1 = require("./nonce.js");
|
|
13
|
+
const orders_js_1 = require("./orders.js");
|
|
14
|
+
const reads_js_1 = require("./reads.js");
|
|
15
|
+
const sign_js_1 = require("./sign.js");
|
|
16
|
+
const units_js_1 = require("./units.js");
|
|
17
|
+
const rounding_js_1 = require("./rounding.js");
|
|
18
|
+
const types_js_1 = require("./types.js");
|
|
19
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
20
|
+
/** Read the chain until `fn` is true or `ms` runs out. A failed read counts as "not yet". */
|
|
21
|
+
async function waitFor(fn, ms, stepMs = 1000) {
|
|
22
|
+
const end = Date.now() + ms;
|
|
23
|
+
for (;;) {
|
|
24
|
+
try {
|
|
25
|
+
if (await fn())
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
catch { /* not yet */ }
|
|
29
|
+
if (Date.now() >= end)
|
|
30
|
+
return false;
|
|
31
|
+
await sleep(stepMs);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** A connection to one Vordium network: reads, and the signing domains of its node. */
|
|
35
|
+
class Vordium {
|
|
36
|
+
network;
|
|
37
|
+
transport;
|
|
38
|
+
timeoutMs;
|
|
39
|
+
ttlMs;
|
|
40
|
+
checkedAt = Date.now();
|
|
41
|
+
constructor(network, transport, timeoutMs, ttlMs) {
|
|
42
|
+
this.network = network;
|
|
43
|
+
this.transport = transport;
|
|
44
|
+
this.timeoutMs = timeoutMs;
|
|
45
|
+
this.ttlMs = ttlMs;
|
|
46
|
+
}
|
|
47
|
+
/** Read and check the network (spec, node and JSON-RPC must agree) and return a client for it. Testnet by default. */
|
|
48
|
+
static async connect(opts = {}) {
|
|
49
|
+
const transport = opts.transport ?? http_js_1.fetchTransport;
|
|
50
|
+
const timeoutMs = opts.timeoutMs ?? 15000;
|
|
51
|
+
const net = await (0, network_js_1.connectNetwork)(opts.network ?? 'testnet', { transport, timeoutMs });
|
|
52
|
+
return new Vordium(net, transport, timeoutMs, opts.identityTtlMs ?? 60000);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The signing domain of the node this client posts to. The node's identity is read again after the TTL; a node that
|
|
56
|
+
* now serves another chain is refused, never followed.
|
|
57
|
+
*/
|
|
58
|
+
async domain(cls) {
|
|
59
|
+
if (Date.now() - this.checkedAt >= this.ttlMs) {
|
|
60
|
+
const id = await (0, network_js_1.nodeIdentity)(this.network.api, this.transport, this.timeoutMs);
|
|
61
|
+
if (id.chainId !== this.network.chainId || id.genesis !== this.network.genesis) {
|
|
62
|
+
throw new errors_js_1.NetworkMismatch(`The node now serves chain ${id.chainId} genesis ${id.genesis.slice(0, 8)}…, not the chain this client signs for; refusing to sign.`);
|
|
63
|
+
}
|
|
64
|
+
this.checkedAt = Date.now();
|
|
65
|
+
}
|
|
66
|
+
return (0, domains_js_1.domain)(cls, this.network.chainId, this.network.genesis);
|
|
67
|
+
}
|
|
68
|
+
// ── reads ─────────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
69
|
+
/** GET a path of the REST API; null on 404, raises on another failure. */
|
|
70
|
+
async get(path) {
|
|
71
|
+
const r = await this.transport.get(this.network.api + path, this.timeoutMs);
|
|
72
|
+
if (r.status === 404)
|
|
73
|
+
return null;
|
|
74
|
+
if (r.status !== 200)
|
|
75
|
+
throw new errors_js_1.VordiumError('read_failed', `The read ${path} answered HTTP ${r.status}.`);
|
|
76
|
+
return r.body;
|
|
77
|
+
}
|
|
78
|
+
async post(path, body) {
|
|
79
|
+
return this.transport.post(this.network.api + path, body, this.timeoutMs);
|
|
80
|
+
}
|
|
81
|
+
status() { return this.get('/status'); }
|
|
82
|
+
/** Perpetual markets with their lifecycle and limits. */
|
|
83
|
+
markets() { return this.get('/api/pairs/perps'); }
|
|
84
|
+
/** Per-market rules from committed state: tick, lot, margins, fees, limits. */
|
|
85
|
+
marketSpecs() { return this.get('/markets/specs'); }
|
|
86
|
+
/** The market rules read now (never cached): every market's tick, lot and minimums. */
|
|
87
|
+
async marketRules() {
|
|
88
|
+
let j;
|
|
89
|
+
try {
|
|
90
|
+
j = await this.marketSpecs();
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
throw new rounding_js_1.SpecsUnavailable(`The market rules could not be read (${e.message}); nothing was signed.`);
|
|
94
|
+
}
|
|
95
|
+
if (!j || typeof j !== 'object' || !j.specs)
|
|
96
|
+
throw new rounding_js_1.SpecsUnavailable('The market rules read has no specs; nothing was signed.');
|
|
97
|
+
return (0, rounding_js_1.marketRules)(j);
|
|
98
|
+
}
|
|
99
|
+
orderbook(pairId) { return this.get(`/orderbook/${pairId}`); }
|
|
100
|
+
oracle(pairId) { return this.get(`/oracle/${pairId}`); }
|
|
101
|
+
funding(pairId) { return this.get(`/funding/${pairId}`); }
|
|
102
|
+
/** The truth endpoint: facts the network vouches for. */
|
|
103
|
+
truth() { return this.get('/truth/v1'); }
|
|
104
|
+
async equity(address) { return (0, reads_js_1.readEquity)(await this.get(`/account/equity/${address}`)); }
|
|
105
|
+
async positions(address) { return (0, reads_js_1.readPositions)((await this.get(`/positions/${address}`)) ?? []); }
|
|
106
|
+
async openOrders(address) { return (0, reads_js_1.readOpenOrders)((await this.get(`/orders/${address}`)) ?? []); }
|
|
107
|
+
/** The account's fills (newest first). */
|
|
108
|
+
orderHistory(address, limit = 50) { return this.get(`/orders/history/${address}?limit=${limit}`); }
|
|
109
|
+
/** The account's order records (order_placed, fill, order_closed with its status, …), newest first. */
|
|
110
|
+
async orderRecords(address, limit = 50) {
|
|
111
|
+
const j = await this.get(`/events/account/${address.toLowerCase()}?limit=${limit}`);
|
|
112
|
+
const ev = j && typeof j === 'object' ? j.events : null;
|
|
113
|
+
return Array.isArray(ev) ? ev : [];
|
|
114
|
+
}
|
|
115
|
+
/** The session the chain holds for `owner`: {session_key, expires_at, confirmed}, or null. */
|
|
116
|
+
async session(owner) {
|
|
117
|
+
const j = await this.get(`/session/get?owner=${owner}`);
|
|
118
|
+
return j && typeof j === 'object' && typeof j.session_key === 'string' ? j : null;
|
|
119
|
+
}
|
|
120
|
+
agents(owner) { return this.get(`/agents/${owner}`); }
|
|
121
|
+
/** One agent's committed record (name, status, policy_v1, risk_v1, market limits), or null. */
|
|
122
|
+
async agent(owner, agent) {
|
|
123
|
+
const j = await this.get(`/agents/${owner}/${agent}`);
|
|
124
|
+
return j && typeof j === 'object' && j.found !== false ? j : null;
|
|
125
|
+
}
|
|
126
|
+
/** The actions the chain refused for an agent, with the reason it wrote (a refused agent order is answered 202). */
|
|
127
|
+
async agentRefusals(agent, limit = 50) {
|
|
128
|
+
return (0, reads_js_1.readAgentRefusals)(await this.get(`/events/agent/${agent.toLowerCase()}?limit=${limit}`), this.refusalMeanings());
|
|
129
|
+
}
|
|
130
|
+
/** Agent refusal reasons and their meanings as the network publishes them (empty when it does not). */
|
|
131
|
+
refusalMeanings() { return (0, reads_js_1.refusalMeanings)(this.network.specRead); }
|
|
132
|
+
/** The rename wait (ms) the network publishes for agent names, or null when it publishes none. */
|
|
133
|
+
renameWaitMs() {
|
|
134
|
+
const v = this.network.specRead.agents?.nameRule?.renameCooldownMs;
|
|
135
|
+
const n = v && typeof v === 'object' ? v.value : v;
|
|
136
|
+
return typeof n === 'number' && Number.isSafeInteger(n) && n >= 0 ? n : null;
|
|
137
|
+
}
|
|
138
|
+
async twaps(owner) {
|
|
139
|
+
const j = await this.get(`/twaps/${owner.toLowerCase()}`);
|
|
140
|
+
const rows = j && typeof j === 'object' && !Array.isArray(j) ? j.twaps : j;
|
|
141
|
+
return Array.isArray(rows) ? rows : [];
|
|
142
|
+
}
|
|
143
|
+
/** The cross-margin read: account value, maintenance, withdrawable, each market's mode. */
|
|
144
|
+
crossMargin(owner) { return this.get(`/account/cross/${owner}`); }
|
|
145
|
+
/** The dead-man switch's committed state and limits. */
|
|
146
|
+
scheduleCancelState(owner) { return this.get(`/schedule-cancel/${owner}`); }
|
|
147
|
+
/** The TWAP route and limits from the network spec (refused when not published). */
|
|
148
|
+
twapRules() { return (0, account_js_1.readTwapRules)(this.network.specRead); }
|
|
149
|
+
/** A trading account: `owner` signs account operations; `sessionSigner` (a session or agent key) signs orders. */
|
|
150
|
+
async account(owner, opts = {}) {
|
|
151
|
+
const address = (0, ethers_1.getAddress)(await owner.getAddress());
|
|
152
|
+
return new Account(this, address, owner, opts.sessionSigner ?? null, opts.nonce ?? (0, nonce_js_1.nonceClock)(address), opts.specs === undefined ? null : (0, rounding_js_1.marketRules)(opts.specs));
|
|
153
|
+
}
|
|
154
|
+
/** An agent's own handle: places orders with the agent key for its owner, and may tighten its own policy. */
|
|
155
|
+
async agentKey(owner, agentSigner, opts = {}) {
|
|
156
|
+
const o = (0, ethers_1.getAddress)(owner);
|
|
157
|
+
return new Account(this, o, null, agentSigner, (0, nonce_js_1.nonceClock)(o), opts.specs === undefined ? null : (0, rounding_js_1.marketRules)(opts.specs));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
exports.Vordium = Vordium;
|
|
161
|
+
/** One account's writes. Every write is signed under the node's own genesis-bound domain. */
|
|
162
|
+
class Account {
|
|
163
|
+
client;
|
|
164
|
+
owner;
|
|
165
|
+
ownerSigner;
|
|
166
|
+
sessionSigner;
|
|
167
|
+
nonce;
|
|
168
|
+
specs;
|
|
169
|
+
constructor(client, owner, ownerSigner, sessionSigner, nonce,
|
|
170
|
+
/** Market rules this handle holds; null = read them from the node for each order. */
|
|
171
|
+
specs = null) {
|
|
172
|
+
this.client = client;
|
|
173
|
+
this.owner = owner;
|
|
174
|
+
this.ownerSigner = ownerSigner;
|
|
175
|
+
this.sessionSigner = sessionSigner;
|
|
176
|
+
this.nonce = nonce;
|
|
177
|
+
this.specs = specs;
|
|
178
|
+
}
|
|
179
|
+
/** The rules an order uses: the ones this handle holds, else read from the node now. */
|
|
180
|
+
async rules() { return this.specs ?? (await this.client.marketRules()); }
|
|
181
|
+
/** One market's tick, lot and minimums. */
|
|
182
|
+
async pairSpec(pairId) { return (0, rounding_js_1.selectSpec)(await this.rules(), pairId); }
|
|
183
|
+
needOwner(what) {
|
|
184
|
+
if (!this.ownerSigner)
|
|
185
|
+
throw new errors_js_1.InvalidRequest(`${what} is signed by the account's own key; this handle holds only an agent or session key.`);
|
|
186
|
+
return this.ownerSigner;
|
|
187
|
+
}
|
|
188
|
+
/** Orders are signed by the session (or agent) key, else by the owner key. */
|
|
189
|
+
orderSigner() {
|
|
190
|
+
const s = this.sessionSigner ?? this.ownerSigner;
|
|
191
|
+
if (!s)
|
|
192
|
+
throw new errors_js_1.InvalidRequest('No key to sign orders with.');
|
|
193
|
+
return s;
|
|
194
|
+
}
|
|
195
|
+
async sign(signer, primary, value) {
|
|
196
|
+
return (0, sign_js_1.signOp)(signer, await this.client.domain((0, types_js_1.domainOf)(primary)), primary, value);
|
|
197
|
+
}
|
|
198
|
+
async send(path, body) {
|
|
199
|
+
const r = await this.client.post(path, typeof body === 'string' ? body : JSON.stringify(body));
|
|
200
|
+
return (0, orders_js_1.readAnswer)(r.status, r.body, r.retryAfter, this.client.refusalMeanings());
|
|
201
|
+
}
|
|
202
|
+
// ── orders ────────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
203
|
+
/**
|
|
204
|
+
* Sign and submit one order (Market or Limit). The answer is a submission; the order is on the book when
|
|
205
|
+
* {@link Vordium.openOrders} lists its `clientNonce`, or filled when the account's fills show it. Use
|
|
206
|
+
* {@link waitForOrder} to read it back.
|
|
207
|
+
*/
|
|
208
|
+
async placeOrder(o) {
|
|
209
|
+
const signer = this.orderSigner();
|
|
210
|
+
const clientNonce = o.clientNonce ?? this.nonce.next();
|
|
211
|
+
(0, orders_js_1.placeOrderValue)({ ...o, owner: this.owner, clientNonce }); // refusals that need no market rules come first
|
|
212
|
+
const v = (0, orders_js_1.placeOrderValue)({ ...o, owner: this.owner, clientNonce }, await this.pairSpec(o.pairId));
|
|
213
|
+
const sig = await this.sign(signer, 'PlaceOrder', v);
|
|
214
|
+
const body = (0, orders_js_1.placeOrderBody)(v, (0, ethers_1.getAddress)(await signer.getAddress()), (0, sign_js_1.rs64)(sig));
|
|
215
|
+
const r = await this.client.post('/order/place', JSON.stringify(body));
|
|
216
|
+
const s = (0, orders_js_1.readAnswer)(r.status === 200 && r.body?.status === 'submitted' ? 202 : r.status, r.body, r.retryAfter, this.client.refusalMeanings());
|
|
217
|
+
return { ...s, status: r.status, clientNonce };
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Where the order with `clientNonce` stands, from the chain: `resting` while the book holds it, else what the account's
|
|
221
|
+
* order records say (`filled`, `cancelled`, `expired`), or null when the chain has no record of it (a refused order
|
|
222
|
+
* has none). Reads until it is known or `ms` runs out. With `final: true` (a market order, or an order you expect to
|
|
223
|
+
* end) it waits for the order's closing record: the book can list an order for a moment before it fills.
|
|
224
|
+
*/
|
|
225
|
+
async waitForOrder(clientNonce, ms = 20000, opts = {}) {
|
|
226
|
+
let state = null;
|
|
227
|
+
await waitFor(async () => {
|
|
228
|
+
if (!opts.final && (await this.client.openOrders(this.owner)).some((x) => x.clientNonce === clientNonce)) {
|
|
229
|
+
state = 'resting';
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
const closed = (await this.client.orderRecords(this.owner)).find((e) => e.kind === 'order_closed' && String(e.client_nonce) === String(clientNonce));
|
|
233
|
+
const st = closed ? String(closed.status) : '';
|
|
234
|
+
if (st === 'filled' || st === 'cancelled' || st === 'expired') {
|
|
235
|
+
state = st;
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
return false;
|
|
239
|
+
}, ms);
|
|
240
|
+
return state;
|
|
241
|
+
}
|
|
242
|
+
async cancelOrder(orderId) {
|
|
243
|
+
const signer = this.orderSigner();
|
|
244
|
+
const id = (0, units_js_1.uint)(orderId, units_js_1.U64, 'orderId');
|
|
245
|
+
const sig = await this.sign(signer, 'CancelOrder', { owner: this.owner, orderId: id });
|
|
246
|
+
const r = await this.client.post('/order/cancel', JSON.stringify((0, orders_js_1.cancelOrderBody)(this.owner, (0, ethers_1.getAddress)(await signer.getAddress()), id, (0, sign_js_1.rs64)(sig))));
|
|
247
|
+
return (0, orders_js_1.readAnswer)(r.status === 200 && r.body?.status === 'submitted' ? 202 : r.status, r.body, r.retryAfter, this.client.refusalMeanings());
|
|
248
|
+
}
|
|
249
|
+
/** Take-profit / stop-loss on an open position (8-decimal prices; 0 leaves a leg unset). */
|
|
250
|
+
async setTpSl(pairId, positionId, takeProfit8, stopLoss8) {
|
|
251
|
+
const signer = this.orderSigner();
|
|
252
|
+
const p = (0, units_js_1.safeNumber)((0, units_js_1.uint)(pairId, units_js_1.U32, 'pairId'), 'pairId');
|
|
253
|
+
const spec = await this.pairSpec(p);
|
|
254
|
+
const value = { owner: this.owner, pairId: p, positionId: (0, units_js_1.uint)(positionId, units_js_1.U64, 'positionId'),
|
|
255
|
+
takeProfit8dec: (0, rounding_js_1.snapTrigger8)((0, units_js_1.uint)(takeProfit8, units_js_1.U64, 'takeProfit'), spec), stopLoss8dec: (0, rounding_js_1.snapTrigger8)((0, units_js_1.uint)(stopLoss8, units_js_1.U64, 'stopLoss'), spec) };
|
|
256
|
+
const sig = await this.sign(signer, 'SetTpSl', value);
|
|
257
|
+
return this.send('/tpsl/set', (0, orders_js_1.setTpSlBody)(this.owner, (0, ethers_1.getAddress)(await signer.getAddress()), value.pairId, value.positionId, value.takeProfit8dec, value.stopLoss8dec, sig));
|
|
258
|
+
}
|
|
259
|
+
/** Up to the published maximum of place / cancel actions in one signed batch. Cancels apply first. */
|
|
260
|
+
async batch(actions, opts = {}) {
|
|
261
|
+
const max = opts.maxActions ?? orders_js_1.BATCH_MAX;
|
|
262
|
+
if (actions.length < 1 || actions.length > max)
|
|
263
|
+
throw new errors_js_1.InvalidRequest(`A batch holds 1 to ${max} actions, not ${actions.length}.`);
|
|
264
|
+
actions = await this.onSteps(actions);
|
|
265
|
+
const nonce = opts.nonce ?? this.nonce.next();
|
|
266
|
+
const sig = await this.sign(this.orderSigner(), 'BatchOrders', { owner: this.owner, nonce, actions });
|
|
267
|
+
return { ...(await this.send('/order/batch-signed', (0, orders_js_1.batchBody)(this.owner, nonce, actions, sig))), nonce };
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* The batch as it will be signed: every place action's price and size on its market's tick and lot (one rules read
|
|
271
|
+
* per batch). Refuses, before signing, a size that rounds to 0 or an order below the network's minimum.
|
|
272
|
+
*/
|
|
273
|
+
async onSteps(actions) {
|
|
274
|
+
if (!actions.some((a) => a.kind === 0))
|
|
275
|
+
return actions;
|
|
276
|
+
const rules = await this.rules();
|
|
277
|
+
return actions.map((a) => {
|
|
278
|
+
if (a.kind !== 0)
|
|
279
|
+
return a;
|
|
280
|
+
const spec = (0, rounding_js_1.selectSpec)(rules, a.pairId);
|
|
281
|
+
const price = (0, rounding_js_1.snapPrice8)(a.price, spec, a.side === 0 ? 'buy' : 'sell'); // a market order keeps price 0
|
|
282
|
+
const size = (0, rounding_js_1.snapSize18)(a.size, spec);
|
|
283
|
+
(0, rounding_js_1.checkNotional)(size, price, spec);
|
|
284
|
+
return { ...a, price, size };
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
/** Cancel every open order on `pairId` (0 = every market). */
|
|
288
|
+
async cancelAll(pairId = 0) {
|
|
289
|
+
const nonce = this.nonce.next();
|
|
290
|
+
const p = (0, units_js_1.safeNumber)((0, units_js_1.uint)(pairId, units_js_1.U32, 'pairId'), 'pairId');
|
|
291
|
+
const sig = await this.sign(this.orderSigner(), 'CancelAllOrders', { owner: this.owner, pairId: p, nonce });
|
|
292
|
+
return this.send('/order/cancel-all', (0, orders_js_1.cancelAllBody)(this.owner, p, nonce, sig));
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Cancel-and-replace at a new price and size (a new order id, no time priority). The new price and size go onto the
|
|
296
|
+
* order's market tick and lot (a buy's price down, a sell's up, the size down); the market and side come from `opts`,
|
|
297
|
+
* else from the account's open orders.
|
|
298
|
+
*/
|
|
299
|
+
async modify(orderId, newPrice8, newSize18, opts = {}) {
|
|
300
|
+
const id = (0, units_js_1.uint)(orderId, units_js_1.U64, 'orderId');
|
|
301
|
+
let p = (0, units_js_1.uint)(newPrice8, units_js_1.U64, 'newPrice8'), s = (0, units_js_1.uint)(newSize18, units_js_1.U128, 'newSize18');
|
|
302
|
+
if (p === 0n || s === 0n)
|
|
303
|
+
throw new errors_js_1.InvalidRequest('A modified order needs a price and a size.');
|
|
304
|
+
let { pairId, side } = opts;
|
|
305
|
+
if (pairId === undefined || side === undefined) {
|
|
306
|
+
const mine = (await this.client.openOrders(this.owner)).find((o) => BigInt(o.orderId) === id);
|
|
307
|
+
if (!mine)
|
|
308
|
+
throw new rounding_js_1.OrderValueError(`Order ${id} is not open on this account; pass pairId and side.`);
|
|
309
|
+
pairId = mine.pairId;
|
|
310
|
+
side = mine.side;
|
|
311
|
+
}
|
|
312
|
+
const spec = await this.pairSpec(pairId);
|
|
313
|
+
p = (0, rounding_js_1.snapPrice8)(p, spec, side);
|
|
314
|
+
s = (0, rounding_js_1.snapSize18)(s, spec);
|
|
315
|
+
(0, rounding_js_1.checkNotional)(s, p, spec);
|
|
316
|
+
const clientNonce = this.nonce.next();
|
|
317
|
+
const sig = await this.sign(this.orderSigner(), 'ModifyOrderV2', { owner: this.owner, orderId: id, newPrice: p, newSize: s, clientNonce });
|
|
318
|
+
return { ...(await this.send('/order/modify', (0, orders_js_1.modifyBody)(this.owner, id, p, s, clientNonce, sig))), clientNonce };
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* The dead-man switch: at the first block at or past `cancelAtMs` the chain cancels every open order and TWAP of the
|
|
322
|
+
* account. Call again with a later time to keep orders alive; 0 clears it.
|
|
323
|
+
*/
|
|
324
|
+
async scheduleCancel(cancelAtMs) {
|
|
325
|
+
const nonce = this.nonce.next();
|
|
326
|
+
const at = (0, units_js_1.safeNumber)((0, units_js_1.uint)(cancelAtMs, units_js_1.U64, 'cancelAtMs'), 'cancelAtMs');
|
|
327
|
+
const sig = await this.sign(this.orderSigner(), 'ScheduleCancel', { owner: this.owner, cancelAtMs: at, nonce });
|
|
328
|
+
return { ...(await this.send('/schedule-cancel', (0, orders_js_1.scheduleCancelBody)(this.owner, at, nonce, sig))), nonce };
|
|
329
|
+
}
|
|
330
|
+
// ── session key ───────────────────────────────────────────────────────────────────────────────────────────────────
|
|
331
|
+
async revokeSession(waitMs = 30000) {
|
|
332
|
+
const signer = this.needOwner('Ending a session');
|
|
333
|
+
const held = await this.client.session(this.owner);
|
|
334
|
+
if (!held)
|
|
335
|
+
return { applied: true, status: 0, answer: null, detail: 'no session to revoke' };
|
|
336
|
+
const n = (0, account_js_1.nextNonce)();
|
|
337
|
+
const sig = await this.sign(signer, 'SessionRevoke', { owner: this.owner, nonce: n });
|
|
338
|
+
const r = await this.client.post('/session/revoke', (0, account_js_1.sessionRevokeBody)(this.owner, held.session_key, n, sig));
|
|
339
|
+
const ok = r.status >= 200 && r.status < 300 && (await waitFor(async () => (await this.client.session(this.owner)) === null, waitMs));
|
|
340
|
+
return { applied: ok, status: r.status, answer: r.body, detail: ok ? '' : 'the chain still holds the session' };
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Give `sessionKey` (a session key, or a registered agent's key) this account's session until `endMs`. A session the
|
|
344
|
+
* chain holds for another key is revoked first (one more signature). Applied only once the chain shows it confirmed.
|
|
345
|
+
*/
|
|
346
|
+
async createSession(sessionKey, endMs, waitMs = 60000) {
|
|
347
|
+
const signer = this.needOwner('A session');
|
|
348
|
+
const key = (0, ethers_1.getAddress)(sessionKey);
|
|
349
|
+
const held = await this.client.session(this.owner);
|
|
350
|
+
let after = 0;
|
|
351
|
+
if (held && held.session_key.toLowerCase() !== key.toLowerCase()) {
|
|
352
|
+
const r = await this.revokeSession();
|
|
353
|
+
if (!r.applied)
|
|
354
|
+
return { ...r, applied: false, detail: "the account's other session could not be ended" };
|
|
355
|
+
after = (0, account_js_1.nextNonce)();
|
|
356
|
+
}
|
|
357
|
+
const n = (0, account_js_1.nextNonce)(after), exp = (0, account_js_1.sessionEnd)(endMs);
|
|
358
|
+
const sig = await this.sign(signer, 'SessionCreate', { sessionKey: key, expiresAt: exp, nonce: n });
|
|
359
|
+
const r = await this.client.post('/session/create', (0, account_js_1.sessionCreateBody)(this.owner, key, exp, n, sig));
|
|
360
|
+
const mine = async () => { const s = await this.client.session(this.owner); return !!s && s.session_key.toLowerCase() === key.toLowerCase() && s.confirmed === true; };
|
|
361
|
+
const ok = r.status >= 200 && r.status < 300 && (await waitFor(mine, waitMs));
|
|
362
|
+
return { applied: ok, status: r.status, answer: r.body, detail: ok ? '' : 'the chain does not show the session as confirmed' };
|
|
363
|
+
}
|
|
364
|
+
// ── TWAP (owner key only) ─────────────────────────────────────────────────────────────────────────────────────────
|
|
365
|
+
async placeTwap(t, waitMs = 30000) {
|
|
366
|
+
const signer = this.needOwner('A TWAP order');
|
|
367
|
+
const rules = this.client.twapRules();
|
|
368
|
+
const slip = t.slippageBps ?? 100;
|
|
369
|
+
if (slip < 0 || slip > rules.maxSlippageBps)
|
|
370
|
+
throw new errors_js_1.InvalidRequest("The slippage is above the network's TWAP limit.");
|
|
371
|
+
const numSlices = t.numSlices ?? (0, account_js_1.planTwap)(t.durationMs, rules)[0];
|
|
372
|
+
const [total, lo, hi] = twapOnSteps(await this.pairSpec(t.pairId), (0, units_js_1.uint)(t.totalSize18, units_js_1.U128, 'totalSize18'), numSlices, t.minPrice8 ?? 0n, t.maxPrice8 ?? 0n);
|
|
373
|
+
const clientNonce = (0, account_js_1.nextNonce)();
|
|
374
|
+
const v = {
|
|
375
|
+
owner: this.owner, pairId: t.pairId, side: t.side === 'buy' ? 0 : 1, totalSize18dec: total,
|
|
376
|
+
numSlices, durationMs: t.durationMs, leverageBps: t.leverageBps ?? 10000, slippageBps: slip,
|
|
377
|
+
randomize: !!t.randomize, triggerPrice8dec: 0n, triggerDir: 0, minPrice8dec: lo, maxPrice8dec: hi, clientNonce,
|
|
378
|
+
};
|
|
379
|
+
const sig = await this.sign(signer, 'PlaceTwap', v);
|
|
380
|
+
const r = await this.client.post(rules.path, (0, account_js_1.twapBody)(v, sig));
|
|
381
|
+
const ok = r.status >= 200 && r.status < 300 && (await waitFor(async () => (await this.client.twaps(this.owner)).some((x) => String(x.client_nonce) === String(clientNonce)), waitMs));
|
|
382
|
+
return { applied: ok, status: r.status, answer: r.body, detail: ok ? '' : 'the chain does not list the TWAP', clientNonce };
|
|
383
|
+
}
|
|
384
|
+
async cancelTwap(twapId, waitMs = 30000) {
|
|
385
|
+
const signer = this.needOwner('Cancelling a TWAP');
|
|
386
|
+
const rules = this.client.twapRules();
|
|
387
|
+
const cn = (0, account_js_1.nextNonce)();
|
|
388
|
+
const sig = await this.sign(signer, 'CancelTwap', { owner: this.owner, twapId: (0, units_js_1.uint)(twapId, units_js_1.U64, 'twapId'), clientNonce: cn });
|
|
389
|
+
const r = await this.client.post(rules.cancelPath, (0, account_js_1.cancelTwapBody)(this.owner, twapId, cn, sig));
|
|
390
|
+
const gone = async () => (await this.client.twaps(this.owner)).every((x) => String(x.twap_id ?? x.id) !== String(twapId));
|
|
391
|
+
const ok = r.status >= 200 && r.status < 300 && (await waitFor(gone, waitMs));
|
|
392
|
+
return { applied: ok, status: r.status, answer: r.body, detail: ok ? '' : 'the chain still lists the TWAP' };
|
|
393
|
+
}
|
|
394
|
+
// ── margin mode (owner key only) ──────────────────────────────────────────────────────────────────────────────────
|
|
395
|
+
/** The market's margin mode as the chain reads it ("cross" | "isolated"), or null. */
|
|
396
|
+
async marginMode(pairId) {
|
|
397
|
+
const j = (await this.client.crossMargin(this.owner));
|
|
398
|
+
if (!j || !Array.isArray(j.modes))
|
|
399
|
+
return null;
|
|
400
|
+
const m = j.modes.find((x) => String(x.pair_id) === String(pairId))?.mode ?? j.default_mode;
|
|
401
|
+
const map = { 1: 'cross', 2: 'isolated', cross: 'cross', isolated: 'isolated' };
|
|
402
|
+
return map[String(m)] ?? null;
|
|
403
|
+
}
|
|
404
|
+
/** Cross or isolated for one market — only while it holds no open position and no resting order of the account. */
|
|
405
|
+
async setMarginMode(pairId, mode, waitMs = 30000) {
|
|
406
|
+
const signer = this.needOwner('The margin mode');
|
|
407
|
+
if (!(mode in account_js_1.MODE_CODE))
|
|
408
|
+
throw new errors_js_1.InvalidRequest('The mode is cross or isolated.');
|
|
409
|
+
const n = (0, account_js_1.nextNonce)();
|
|
410
|
+
const sig = await this.sign(signer, 'SetMarginMode', { owner: this.owner, pairId, mode: account_js_1.MODE_CODE[mode], nonce: n });
|
|
411
|
+
const r = await this.client.post('/margin/mode', (0, account_js_1.marginModeBody)(this.owner, pairId, account_js_1.MODE_CODE[mode], n, sig));
|
|
412
|
+
const ok = r.status === 202 && (await waitFor(async () => (await this.marginMode(pairId)) === mode, waitMs));
|
|
413
|
+
return { applied: ok, status: r.status, answer: r.body, detail: ok ? '' : 'the chain does not show the new mode' };
|
|
414
|
+
}
|
|
415
|
+
// ── agents (owner key; the agent may tighten its own policy) ──────────────────────────────────────────────────────
|
|
416
|
+
async submitAgentOp(signer, op, value, shows, agent, waitMs) {
|
|
417
|
+
const sig = await this.sign(signer, op, value);
|
|
418
|
+
const body = (0, agents_js_1.submitBody)(op, value, sig);
|
|
419
|
+
const r = await this.client.post('/submit', JSON.stringify(body));
|
|
420
|
+
const ok = r.status >= 200 && r.status < 300 && (await waitFor(async () => shows(await this.client.agent(this.owner, agent)), waitMs));
|
|
421
|
+
return { applied: ok, status: r.status, answer: r.body, detail: ok ? '' : 'the chain does not show the change' };
|
|
422
|
+
}
|
|
423
|
+
addr(a, op) {
|
|
424
|
+
const c = (0, ethers_1.getAddress)(a);
|
|
425
|
+
return agents_js_1.LOWERCASE_ADDRESS_OPS.has(op) ? c.toLowerCase() : c;
|
|
426
|
+
}
|
|
427
|
+
/** Register an agent key with a name (the chain's name rule) and a mandate; applied once the chain lists it. */
|
|
428
|
+
async registerAgent(a, waitMs = 30000) {
|
|
429
|
+
const signer = this.needOwner('Registering an agent');
|
|
430
|
+
const op = 'RegisterAgentV2';
|
|
431
|
+
const value = { owner: this.addr(this.owner, op), agent: this.addr(a.agent, op), name: (0, agents_js_1.requireName)(a.name), mandate: a.mandate, expiresMs: a.expiresMs, nonce: (0, account_js_1.nextNonce)() };
|
|
432
|
+
return this.submitAgentOp(signer, op, value, (rec) => !!rec && rec.status === 'active' && rec.name === value.name, value.agent, waitMs);
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Rename an agent. The chain applies one rename per wait (registering is the first naming) and ignores an earlier one
|
|
436
|
+
* silently: where the network publishes the wait it is refused here, before signing; otherwise the result says the
|
|
437
|
+
* chain did not show the new name.
|
|
438
|
+
*/
|
|
439
|
+
async renameAgent(agent, name, waitMs = 30000) {
|
|
440
|
+
const signer = this.needOwner('Renaming an agent');
|
|
441
|
+
const op = 'SetAgentName';
|
|
442
|
+
const value = { owner: this.addr(this.owner, op), agent: this.addr(agent, op), name: (0, agents_js_1.requireName)(name), nonce: (0, account_js_1.nextNonce)() };
|
|
443
|
+
const rec = await this.client.agent(this.owner, value.agent);
|
|
444
|
+
const named = typeof rec?.named_at_ms === 'number' ? rec.named_at_ms : null;
|
|
445
|
+
const wait = this.client.renameWaitMs();
|
|
446
|
+
if (rec?.name === value.name)
|
|
447
|
+
return { applied: true, status: 0, answer: null, detail: 'the agent already has this name' };
|
|
448
|
+
if (named !== null && wait !== null && Date.now() < named + wait) {
|
|
449
|
+
throw new errors_js_1.InvalidRequest(`The chain allows one rename per ${Math.round(wait / 3600000)} hours; this agent can be renamed after ${new Date(named + wait).toISOString()}.`);
|
|
450
|
+
}
|
|
451
|
+
const r = await this.submitAgentOp(signer, op, value, (x) => !!x && x.name === value.name, value.agent, waitMs);
|
|
452
|
+
return r.applied ? r : { ...r, detail: 'the chain does not show the new name (a rename within the wait after the last naming is ignored)' };
|
|
453
|
+
}
|
|
454
|
+
async revokeAgent(agent, waitMs = 30000) {
|
|
455
|
+
const signer = this.needOwner('Revoking an agent');
|
|
456
|
+
const op = 'RevokeAgent';
|
|
457
|
+
const value = { owner: this.addr(this.owner, op), agent: this.addr(agent, op), nonce: (0, account_js_1.nextNonce)() };
|
|
458
|
+
return this.submitAgentOp(signer, op, value, (rec) => !!rec && rec.status === 'revoked', value.agent, waitMs);
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Set an agent's policy. Refused before signing when the chain would refuse it at apply (it does so silently). The
|
|
462
|
+
* agent itself may sign only a tightening of the stored policy, with its own nonce; the owner may loosen (the chain
|
|
463
|
+
* allows that once per cooldown).
|
|
464
|
+
*/
|
|
465
|
+
async setAgentPolicy(agent, policy, waitMs = 30000) {
|
|
466
|
+
const op = 'SetAgentPolicyV1';
|
|
467
|
+
const problems = (0, agents_js_1.policyProblems)(policy, Date.now());
|
|
468
|
+
if (problems.length)
|
|
469
|
+
throw new errors_js_1.InvalidRequest('The chain would refuse this policy: ' + problems.join(' '));
|
|
470
|
+
const agentAddr = this.addr(agent, op);
|
|
471
|
+
let signer = this.ownerSigner;
|
|
472
|
+
if (!signer) {
|
|
473
|
+
signer = this.sessionSigner;
|
|
474
|
+
if (!signer || (0, ethers_1.getAddress)(await signer.getAddress()).toLowerCase() !== agentAddr)
|
|
475
|
+
throw new errors_js_1.InvalidRequest('Only the owner, or the agent itself, signs a policy.');
|
|
476
|
+
const stored = (0, agents_js_1.policyFromRead)((await this.client.agent(this.owner, agentAddr))?.policy_v1);
|
|
477
|
+
if (!stored)
|
|
478
|
+
throw new errors_js_1.InvalidRequest('An agent may only tighten a policy its owner has set; there is no stored policy to tighten.');
|
|
479
|
+
const wider = (0, agents_js_1.loosens)(stored, policy);
|
|
480
|
+
if (wider.length)
|
|
481
|
+
throw new errors_js_1.InvalidRequest(`An agent may only tighten its own policy; this ${wider.join(', ')} — only the owner may sign that.`);
|
|
482
|
+
}
|
|
483
|
+
const value = (0, agents_js_1.policyValue)(this.addr(this.owner, op), agentAddr, policy, (0, account_js_1.nextNonce)());
|
|
484
|
+
const same = (rec) => {
|
|
485
|
+
const p = (0, agents_js_1.policyFromRead)(rec?.policy_v1);
|
|
486
|
+
return !!p && JSON.stringify(p, (_, x) => (typeof x === 'bigint' ? x.toString() : x)) === JSON.stringify(policy, (_, x) => (typeof x === 'bigint' ? x.toString() : x));
|
|
487
|
+
};
|
|
488
|
+
return this.submitAgentOp(signer, op, value, same, agentAddr, waitMs);
|
|
489
|
+
}
|
|
490
|
+
/** Per-market limits for an agent (sizes in 18-decimal units; 0 leverage = the policy's). */
|
|
491
|
+
async setAgentMarketLimit(agent, m, waitMs = 30000) {
|
|
492
|
+
const signer = this.needOwner("An agent's market limit");
|
|
493
|
+
const op = 'SetAgentMarketLimit';
|
|
494
|
+
const value = { owner: this.addr(this.owner, op), agent: this.addr(agent, op), pair: m.pair, allowed: m.allowed,
|
|
495
|
+
maxOrder18dec: (0, units_js_1.uint)(m.maxOrder18, units_js_1.U128, 'maxOrder18'), maxPosition18dec: (0, units_js_1.uint)(m.maxPosition18, units_js_1.U128, 'maxPosition18'), maxLeverageBps: m.maxLeverageBps, nonce: (0, account_js_1.nextNonce)() };
|
|
496
|
+
const shows = (rec) => (Array.isArray(rec?.market_limits) ? rec.market_limits : [])
|
|
497
|
+
.some((l) => l.pair === m.pair && l.allowed === m.allowed && String(l.max_order_18dec) === value.maxOrder18dec.toString() && String(l.max_position_18dec) === value.maxPosition18dec.toString());
|
|
498
|
+
return this.submitAgentOp(signer, op, value, shows, value.agent.toLowerCase(), waitMs);
|
|
499
|
+
}
|
|
500
|
+
/** Clear the agent's active lock (owner only); `lockSinceMs` names the lock, so a clear signed early never clears a later one. */
|
|
501
|
+
async clearAgentLock(agent, lockSinceMs, waitMs = 30000) {
|
|
502
|
+
const signer = this.needOwner("Clearing an agent's lock");
|
|
503
|
+
if (!Number.isSafeInteger(lockSinceMs) || lockSinceMs <= 0)
|
|
504
|
+
throw new errors_js_1.InvalidRequest('lockSinceMs is the start of the active lock (from the agent read).');
|
|
505
|
+
const op = 'ClearAgentLock';
|
|
506
|
+
const value = { owner: this.addr(this.owner, op), agent: this.addr(agent, op), lockSinceMs, nonce: (0, account_js_1.nextNonce)() };
|
|
507
|
+
const shows = (rec) => rec?.risk_v1?.lock_active === false;
|
|
508
|
+
return this.submitAgentOp(signer, op, value, shows, value.agent, waitMs);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
exports.Account = Account;
|
|
512
|
+
/**
|
|
513
|
+
* A TWAP's signed size and price bounds on the market's lot and tick: the total DOWN to a whole number of lots per slice
|
|
514
|
+
* (every slice on the lot, never more than asked), the lowest price UP and the highest DOWN (the band never widens).
|
|
515
|
+
*/
|
|
516
|
+
function twapOnSteps(spec, total18, numSlices, minPrice8, maxPrice8) {
|
|
517
|
+
if (!Number.isSafeInteger(numSlices) || numSlices < 1)
|
|
518
|
+
throw new rounding_js_1.OrderValueError('A TWAP has at least one slice.');
|
|
519
|
+
const step = (0, rounding_js_1.sizeStep)(spec) * BigInt(numSlices);
|
|
520
|
+
const total = (0, rounding_js_1.checkSize)(total18 - (total18 % step), spec, total18);
|
|
521
|
+
const lo = minPrice8 ? (0, rounding_js_1.snapPrice8)(minPrice8, spec, 'sell') : 0n;
|
|
522
|
+
const hi = maxPrice8 ? (0, rounding_js_1.snapPrice8)(maxPrice8, spec, 'buy') : 0n;
|
|
523
|
+
if (lo && hi && hi < lo)
|
|
524
|
+
throw new rounding_js_1.OrderValueError("The TWAP's price band holds no tick of this market.");
|
|
525
|
+
return [total, lo, hi];
|
|
526
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VERIFYING_CONTRACT = void 0;
|
|
4
|
+
exports.genesisHex = genesisHex;
|
|
5
|
+
exports.salt = salt;
|
|
6
|
+
exports.domain = domain;
|
|
7
|
+
exports.separator = separator;
|
|
8
|
+
const ethers_1 = require("ethers");
|
|
9
|
+
const errors_js_1 = require("./errors.js");
|
|
10
|
+
/**
|
|
11
|
+
* The genesis-bound EIP-712 domains. Every signing domain on Vordium binds its `salt` to the chain's genesis:
|
|
12
|
+
* `salt = keccak256(tag ‖ genesis_sha256)`, so a signature made for one genesis can never verify on another chain that
|
|
13
|
+
* shares its chain id. The SDK derives each domain from the node's own `/status` and refuses to sign without a genesis.
|
|
14
|
+
*/
|
|
15
|
+
exports.VERIFYING_CONTRACT = '0x0000000000000000000000000000000000001002';
|
|
16
|
+
const TAGS = {
|
|
17
|
+
VordCore: 'VORDIUM_VORDCORE\u0001',
|
|
18
|
+
VordexSession: 'VORDIUM_VORDEXSESSION\u0001',
|
|
19
|
+
};
|
|
20
|
+
/** 32 bytes of genesis hash as lower-case hex without 0x; refused otherwise. */
|
|
21
|
+
function genesisHex(genesis) {
|
|
22
|
+
const g = genesis.startsWith('0x') || genesis.startsWith('0X') ? genesis.slice(2) : genesis;
|
|
23
|
+
if (!/^[0-9a-fA-F]{64}$/.test(g))
|
|
24
|
+
throw new errors_js_1.InvalidRequest('A genesis hash is 32 bytes of hex.');
|
|
25
|
+
return g.toLowerCase();
|
|
26
|
+
}
|
|
27
|
+
/** keccak256(tag ‖ genesis bytes): the domain's fifth field. */
|
|
28
|
+
function salt(cls, genesis) {
|
|
29
|
+
return (0, ethers_1.keccak256)((0, ethers_1.concat)([(0, ethers_1.toUtf8Bytes)(TAGS[cls]), (0, ethers_1.getBytes)('0x' + genesisHex(genesis))]));
|
|
30
|
+
}
|
|
31
|
+
function domain(cls, chainId, genesis) {
|
|
32
|
+
return { name: cls, version: '1', chainId: BigInt(chainId), verifyingContract: exports.VERIFYING_CONTRACT, salt: salt(cls, genesis) };
|
|
33
|
+
}
|
|
34
|
+
/** The domain separator, 0x-hex. */
|
|
35
|
+
function separator(cls, chainId, genesis) {
|
|
36
|
+
return ethers_1.TypedDataEncoder.hashDomain(domain(cls, chainId, genesis));
|
|
37
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotAvailable = exports.InvalidRequest = exports.NetworkMismatch = exports.VordiumError = void 0;
|
|
4
|
+
/** Every error the SDK raises is a {@link VordiumError}: a plain sentence, with the chain's code kept as a field. */
|
|
5
|
+
class VordiumError extends Error {
|
|
6
|
+
code;
|
|
7
|
+
detail;
|
|
8
|
+
constructor(code, message, detail = '') {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'VordiumError';
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.detail = detail;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.VordiumError = VordiumError;
|
|
16
|
+
/** The node, the network spec or the configured network disagree about which chain this is. Nothing was signed. */
|
|
17
|
+
class NetworkMismatch extends VordiumError {
|
|
18
|
+
constructor(message) {
|
|
19
|
+
super('network_mismatch', message);
|
|
20
|
+
this.name = 'NetworkMismatch';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.NetworkMismatch = NetworkMismatch;
|
|
24
|
+
/** An argument the chain would refuse, caught before anything was signed or sent. */
|
|
25
|
+
class InvalidRequest extends VordiumError {
|
|
26
|
+
constructor(message) {
|
|
27
|
+
super('invalid_request', message);
|
|
28
|
+
this.name = 'InvalidRequest';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.InvalidRequest = InvalidRequest;
|
|
32
|
+
/** Something the chain does not offer (stop orders, trigger fields, an op that is not in force). Nothing was signed. */
|
|
33
|
+
class NotAvailable extends VordiumError {
|
|
34
|
+
constructor(message) {
|
|
35
|
+
super('not_available', message);
|
|
36
|
+
this.name = 'NotAvailable';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.NotAvailable = NotAvailable;
|