@vordium/sdk 0.2.0 → 0.2.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.
@@ -105,6 +105,8 @@ class Vordium {
105
105
  const r = await this.transport.get(this.network.api + path, this.timeoutMs);
106
106
  if (r.status === 404)
107
107
  return null;
108
+ if (r.status >= 300 && r.status < 400)
109
+ throw new errors_js_1.VordiumError('redirect_refused', `The read ${path} was answered with a redirect, which is not followed: use the https address of the node.`);
108
110
  if (r.status !== 200)
109
111
  throw new errors_js_1.VordiumError('read_failed', `The read ${path} answered HTTP ${r.status}.`);
110
112
  return r.body;
package/dist/cjs/http.js CHANGED
@@ -1,14 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fetchTransport = void 0;
3
+ exports.fetchTransport = exports.REDIRECT_REFUSAL = void 0;
4
4
  exports.parseExactJson = parseExactJson;
5
5
  const errors_js_1 = require("./errors.js");
6
6
  const RETRY_STATUS = new Set([429, 502, 503, 504]);
7
+ /**
8
+ * One fetch with a time limit. Redirects are never followed (`redirect: 'manual'`), for reads or writes: a 3xx comes back
9
+ * as the answer it is, so a node (or anything in front of it) cannot send a signed body, or a read, to another address.
10
+ */
7
11
  async function fetchWithTimeout(url, init, timeoutMs) {
8
12
  const ctl = new AbortController();
9
13
  const t = setTimeout(() => ctl.abort(), timeoutMs);
10
14
  try {
11
- return await fetch(url, { ...init, signal: ctl.signal });
15
+ return await fetch(url, { ...init, redirect: 'manual', signal: ctl.signal });
12
16
  }
13
17
  finally {
14
18
  clearTimeout(t);
@@ -26,6 +30,9 @@ async function readBody(r) {
26
30
  }
27
31
  }
28
32
  const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
33
+ const isRedirect = (status) => status >= 300 && status < 400;
34
+ /** What a redirect answer reads as: never the redirect's own body. */
35
+ exports.REDIRECT_REFUSAL = Object.freeze({ code: 'redirect_refused', error: 'The node answered with a redirect, which is not followed: use the https address of the node.' });
29
36
  /**
30
37
  * The default transport: `fetch`. A read (GET) is tried up to three times on a connection error, a timeout, 429 or
31
38
  * 502–504, with a random wait and the server's Retry-After. A write (POST) is never retried: a second attempt could
@@ -42,7 +49,7 @@ exports.fetchTransport = {
42
49
  await sleep(Number.isFinite(ra) && ra > 0 ? Math.min(ra, 5) * 1000 : 250 * 2 ** attempt * (0.5 + Math.random() / 2));
43
50
  continue;
44
51
  }
45
- return { status: r.status, body: await readBody(r) };
52
+ return { status: r.status, body: isRedirect(r.status) ? { ...exports.REDIRECT_REFUSAL } : await readBody(r) };
46
53
  }
47
54
  catch (e) {
48
55
  last = e;
@@ -54,7 +61,8 @@ exports.fetchTransport = {
54
61
  },
55
62
  async post(url, body, timeoutMs) {
56
63
  const r = await fetchWithTimeout(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body }, timeoutMs);
57
- return { status: r.status, body: await readBody(r), retryAfter: r.headers.get('retry-after') };
64
+ // a redirect is not an answer about the write: no body is read from it
65
+ return { status: r.status, body: isRedirect(r.status) ? { ...exports.REDIRECT_REFUSAL } : await readBody(r), retryAfter: r.headers.get('retry-after') };
58
66
  },
59
67
  };
60
68
  /**
@@ -220,6 +220,10 @@ function pairSpecs(read) {
220
220
  if (!s || typeof s !== 'object')
221
221
  throw new OrderValueError('the market rules read has no specs; nothing was signed.');
222
222
  const sp = s;
223
+ if (Array.isArray(sp.pairs) && sp.pairs.length && (sp.min_order_notional_6dec === undefined || sp.min_order_notional_6dec === null || sp.min_order_notional_6dec === '')) {
224
+ // a read that lists markets but not the smallest order value is trimmed or out of date: never read it as "no minimum"
225
+ throw new SpecsUnavailable('the market rules do not name the smallest order value (min_order_notional_6dec); nothing was signed.');
226
+ }
223
227
  const minNotional6 = count(sp.min_order_notional_6dec);
224
228
  const out = new Map();
225
229
  for (const p of Array.isArray(sp.pairs) ? sp.pairs : []) {
@@ -98,6 +98,8 @@ export class Vordium {
98
98
  const r = await this.transport.get(this.network.api + path, this.timeoutMs);
99
99
  if (r.status === 404)
100
100
  return null;
101
+ if (r.status >= 300 && r.status < 400)
102
+ throw new VordiumError('redirect_refused', `The read ${path} was answered with a redirect, which is not followed: use the https address of the node.`);
101
103
  if (r.status !== 200)
102
104
  throw new VordiumError('read_failed', `The read ${path} answered HTTP ${r.status}.`);
103
105
  return r.body;
@@ -11,6 +11,11 @@ export interface Transport {
11
11
  retryAfter: string | null;
12
12
  }>;
13
13
  }
14
+ /** What a redirect answer reads as: never the redirect's own body. */
15
+ export declare const REDIRECT_REFUSAL: Readonly<{
16
+ code: "redirect_refused";
17
+ error: "The node answered with a redirect, which is not followed: use the https address of the node.";
18
+ }>;
14
19
  /**
15
20
  * The default transport: `fetch`. A read (GET) is tried up to three times on a connection error, a timeout, 429 or
16
21
  * 502–504, with a random wait and the server's Retry-After. A write (POST) is never retried: a second attempt could
package/dist/esm/http.js CHANGED
@@ -1,10 +1,14 @@
1
1
  import { VordiumError } from './errors.js';
2
2
  const RETRY_STATUS = new Set([429, 502, 503, 504]);
3
+ /**
4
+ * One fetch with a time limit. Redirects are never followed (`redirect: 'manual'`), for reads or writes: a 3xx comes back
5
+ * as the answer it is, so a node (or anything in front of it) cannot send a signed body, or a read, to another address.
6
+ */
3
7
  async function fetchWithTimeout(url, init, timeoutMs) {
4
8
  const ctl = new AbortController();
5
9
  const t = setTimeout(() => ctl.abort(), timeoutMs);
6
10
  try {
7
- return await fetch(url, { ...init, signal: ctl.signal });
11
+ return await fetch(url, { ...init, redirect: 'manual', signal: ctl.signal });
8
12
  }
9
13
  finally {
10
14
  clearTimeout(t);
@@ -22,6 +26,9 @@ async function readBody(r) {
22
26
  }
23
27
  }
24
28
  const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
29
+ const isRedirect = (status) => status >= 300 && status < 400;
30
+ /** What a redirect answer reads as: never the redirect's own body. */
31
+ export const REDIRECT_REFUSAL = Object.freeze({ code: 'redirect_refused', error: 'The node answered with a redirect, which is not followed: use the https address of the node.' });
25
32
  /**
26
33
  * The default transport: `fetch`. A read (GET) is tried up to three times on a connection error, a timeout, 429 or
27
34
  * 502–504, with a random wait and the server's Retry-After. A write (POST) is never retried: a second attempt could
@@ -38,7 +45,7 @@ export const fetchTransport = {
38
45
  await sleep(Number.isFinite(ra) && ra > 0 ? Math.min(ra, 5) * 1000 : 250 * 2 ** attempt * (0.5 + Math.random() / 2));
39
46
  continue;
40
47
  }
41
- return { status: r.status, body: await readBody(r) };
48
+ return { status: r.status, body: isRedirect(r.status) ? { ...REDIRECT_REFUSAL } : await readBody(r) };
42
49
  }
43
50
  catch (e) {
44
51
  last = e;
@@ -50,7 +57,8 @@ export const fetchTransport = {
50
57
  },
51
58
  async post(url, body, timeoutMs) {
52
59
  const r = await fetchWithTimeout(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body }, timeoutMs);
53
- return { status: r.status, body: await readBody(r), retryAfter: r.headers.get('retry-after') };
60
+ // a redirect is not an answer about the write: no body is read from it
61
+ return { status: r.status, body: isRedirect(r.status) ? { ...REDIRECT_REFUSAL } : await readBody(r), retryAfter: r.headers.get('retry-after') };
54
62
  },
55
63
  };
56
64
  /**
@@ -197,6 +197,10 @@ export function pairSpecs(read) {
197
197
  if (!s || typeof s !== 'object')
198
198
  throw new OrderValueError('the market rules read has no specs; nothing was signed.');
199
199
  const sp = s;
200
+ if (Array.isArray(sp.pairs) && sp.pairs.length && (sp.min_order_notional_6dec === undefined || sp.min_order_notional_6dec === null || sp.min_order_notional_6dec === '')) {
201
+ // a read that lists markets but not the smallest order value is trimmed or out of date: never read it as "no minimum"
202
+ throw new SpecsUnavailable('the market rules do not name the smallest order value (min_order_notional_6dec); nothing was signed.');
203
+ }
200
204
  const minNotional6 = count(sp.min_order_notional_6dec);
201
205
  const out = new Map();
202
206
  for (const p of Array.isArray(sp.pairs) ? sp.pairs : []) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vordium/sdk",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "TypeScript and JavaScript SDK for Vordium Chain and the Vordex perpetuals exchange: signing, orders, session keys, agents, TWAP, margin mode and live feeds.",
5
5
  "license": "MIT",
6
6
  "author": "Vordium <dev@vordium.com>",