dsh-hyperliquid 0.0.1 → 0.1.0
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/NOTICE +27 -0
- package/README.md +120 -19
- package/client.js +18396 -0
- package/cordis.patch.yml +7 -0
- package/icon.svg +7 -0
- package/index.js +118 -0
- package/lib/hl.js +372 -0
- package/lib/indicators.js +88 -0
- package/lib/market-symbol.js +5 -0
- package/lib/msgpack.js +123 -0
- package/lib/remote.js +1393 -0
- package/lib/signing.js +215 -0
- package/lib/skill.js +19 -0
- package/lib/store.js +341 -0
- package/lib/tools.js +411 -0
- package/lib/trading-guard.js +86 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +74 -9
- package/skills/hyperliquid-trading/SKILL.md +34 -0
package/lib/msgpack.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-hyperliquid — canonical msgpack encoder.
|
|
3
|
+
*
|
|
4
|
+
* Hyperliquid's L1 action hash is keccak256(msgpack(action) ‖ nonce ‖ flags).
|
|
5
|
+
* The hash input is never sent on the wire (the POST body is JSON), but it
|
|
6
|
+
* must match the server's canonical encoding byte-for-byte, so this encoder
|
|
7
|
+
* mirrors python-msgpack `packb` defaults:
|
|
8
|
+
* - nil/bool/int (fixint … uint64/int64)
|
|
9
|
+
* - float64 (0xcb) for non-integer numbers
|
|
10
|
+
* - str as fixstr/str8/16/32 (UTF-8) — order px/sz travel as strings
|
|
11
|
+
* - bin as bin8/16/32
|
|
12
|
+
* - array fixarray/array16/32, map fixmap/map16/32 (insertion order;
|
|
13
|
+
* the SDK builds actions with a fixed field order — keep it)
|
|
14
|
+
* @module lib/msgpack.js
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export function packb(value) {
|
|
18
|
+
const out = []
|
|
19
|
+
writeValue(out, value)
|
|
20
|
+
return Uint8Array.from(out)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function writeValue(out, value) {
|
|
24
|
+
if (value === null || value === undefined) return out.push(0xc0)
|
|
25
|
+
if (typeof value === 'boolean') return out.push(value ? 0xc3 : 0xc2)
|
|
26
|
+
if (typeof value === 'number') return writeNumber(out, value)
|
|
27
|
+
if (typeof value === 'string') return writeString(out, value)
|
|
28
|
+
if (typeof value === 'bigint') return writeBigInt(out, value)
|
|
29
|
+
if (value instanceof Uint8Array) return writeBin(out, value)
|
|
30
|
+
if (Array.isArray(value)) return writeArray(out, value)
|
|
31
|
+
if (typeof value === 'object') return writeMap(out, value)
|
|
32
|
+
throw new TypeError(`msgpack: unsupported type ${typeof value}`)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function pushBytes(out, bytes) {
|
|
36
|
+
for (const b of bytes) out.push(b)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function writeNumber(out, n) {
|
|
40
|
+
if (!Number.isFinite(n)) throw new TypeError(`msgpack: non-finite number ${n}`)
|
|
41
|
+
if (Number.isInteger(n)) {
|
|
42
|
+
if (n >= 0) {
|
|
43
|
+
if (n <= 0x7f) return out.push(n)
|
|
44
|
+
if (n <= 0xff) return out.push(0xcc, n)
|
|
45
|
+
if (n <= 0xffff) return out.push(0xcd, n >> 8, n & 0xff)
|
|
46
|
+
if (n <= 0xffffffff) return out.push(0xce, (n >>> 24) & 0xff, (n >>> 16) & 0xff, (n >>> 8) & 0xff, n & 0xff)
|
|
47
|
+
return writeBigInt(out, BigInt(n))
|
|
48
|
+
}
|
|
49
|
+
if (n >= -32) return out.push(0xe0 | (n + 32))
|
|
50
|
+
if (n >= -128) return out.push(0xd0, n & 0xff)
|
|
51
|
+
if (n >= -32768) return out.push(0xd1, (n >> 8) & 0xff, n & 0xff)
|
|
52
|
+
if (n >= -2147483648) {
|
|
53
|
+
const v = n < 0 ? n + 0x1_0000_0000 : n
|
|
54
|
+
return out.push(0xd2, (v >>> 24) & 0xff, (v >>> 16) & 0xff, (v >>> 8) & 0xff, v & 0xff)
|
|
55
|
+
}
|
|
56
|
+
return writeBigInt(out, BigInt(n))
|
|
57
|
+
}
|
|
58
|
+
return writeF64(out, n)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function writeBigInt(out, n) {
|
|
62
|
+
if (n >= 0n) {
|
|
63
|
+
if (n <= 0xffff_ffff_ffff_ffffn) {
|
|
64
|
+
const hex = n.toString(16).padStart(16, '0')
|
|
65
|
+
out.push(0xcf, ...hexToBytes(hex))
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
throw new TypeError('msgpack: uint64 overflow')
|
|
69
|
+
}
|
|
70
|
+
if (n >= -0x8000_0000_0000_0000n) {
|
|
71
|
+
const hex = (n + 0x1_0000_0000_0000_0000n).toString(16).padStart(16, '0')
|
|
72
|
+
out.push(0xd3, ...hexToBytes(hex))
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
throw new TypeError('msgpack: int64 overflow')
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function writeF64(out, n) {
|
|
79
|
+
const buf = new DataView(new ArrayBuffer(8))
|
|
80
|
+
buf.setFloat64(0, n, false)
|
|
81
|
+
out.push(0xcb)
|
|
82
|
+
for (let i = 0; i < 8; i += 1) out.push(buf.getUint8(i))
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function writeString(out, s) {
|
|
86
|
+
const bytes = new TextEncoder().encode(s)
|
|
87
|
+
if (bytes.length <= 31) out.push(0xa0 | bytes.length)
|
|
88
|
+
else if (bytes.length <= 0xff) out.push(0xd9, bytes.length)
|
|
89
|
+
else if (bytes.length <= 0xffff) out.push(0xda, bytes.length >> 8, bytes.length & 0xff)
|
|
90
|
+
else out.push(0xdb, (bytes.length >>> 24) & 0xff, (bytes.length >>> 16) & 0xff, (bytes.length >>> 8) & 0xff, bytes.length & 0xff)
|
|
91
|
+
pushBytes(out, bytes)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function writeBin(out, bytes) {
|
|
95
|
+
if (bytes.length <= 0xff) out.push(0xc4, bytes.length)
|
|
96
|
+
else if (bytes.length <= 0xffff) out.push(0xc5, bytes.length >> 8, bytes.length & 0xff)
|
|
97
|
+
else out.push(0xc6, (bytes.length >>> 24) & 0xff, (bytes.length >>> 16) & 0xff, (bytes.length >>> 8) & 0xff, bytes.length & 0xff)
|
|
98
|
+
pushBytes(out, bytes)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function writeArray(out, arr) {
|
|
102
|
+
if (arr.length <= 15) out.push(0x90 | arr.length)
|
|
103
|
+
else if (arr.length <= 0xffff) out.push(0xdc, arr.length >> 8, arr.length & 0xff)
|
|
104
|
+
else out.push(0xdd, (arr.length >>> 24) & 0xff, (arr.length >>> 16) & 0xff, (arr.length >>> 8) & 0xff, arr.length & 0xff)
|
|
105
|
+
for (const v of arr) writeValue(out, v)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function writeMap(out, obj) {
|
|
109
|
+
const entries = Object.entries(obj)
|
|
110
|
+
if (entries.length <= 15) out.push(0x80 | entries.length)
|
|
111
|
+
else if (entries.length <= 0xffff) out.push(0xde, entries.length >> 8, entries.length & 0xff)
|
|
112
|
+
else out.push(0xdf, (entries.length >>> 24) & 0xff, (entries.length >>> 16) & 0xff, (entries.length >>> 8) & 0xff, entries.length & 0xff)
|
|
113
|
+
for (const [k, v] of entries) {
|
|
114
|
+
writeString(out, k)
|
|
115
|
+
writeValue(out, v)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function hexToBytes(hex) {
|
|
120
|
+
const out = []
|
|
121
|
+
for (let i = 0; i < hex.length; i += 2) out.push(parseInt(hex.slice(i, i + 2), 16))
|
|
122
|
+
return out
|
|
123
|
+
}
|