@lifi/perps-sdk-provider-lighter 8.2.1 → 10.0.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/README.md +14 -10
- package/dist/cjs/LighterProvider.d.ts +5 -10
- package/dist/cjs/LighterProvider.d.ts.map +1 -1
- package/dist/cjs/LighterProvider.js +31 -26
- package/dist/cjs/LighterProvider.js.map +1 -1
- package/dist/cjs/constants.d.ts +5 -8
- package/dist/cjs/constants.d.ts.map +1 -1
- package/dist/cjs/constants.js +15 -12
- package/dist/cjs/constants.js.map +1 -1
- package/dist/cjs/index.d.ts +3 -4
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +6 -5
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/signers/LighterSigner.d.ts +5 -7
- package/dist/cjs/signers/LighterSigner.d.ts.map +1 -1
- package/dist/cjs/signers/LighterSigner.js +30 -17
- package/dist/cjs/signers/LighterSigner.js.map +1 -1
- package/dist/cjs/signers/generated/wasmExecRuntime.d.ts +2 -0
- package/dist/cjs/signers/generated/wasmExecRuntime.d.ts.map +1 -0
- package/dist/cjs/signers/generated/wasmExecRuntime.js +569 -0
- package/dist/cjs/signers/generated/wasmExecRuntime.js.map +1 -0
- package/dist/cjs/signers/signActions.d.ts +2 -2
- package/dist/cjs/signers/signActions.d.ts.map +1 -1
- package/dist/cjs/signers/signActions.js +4 -10
- package/dist/cjs/signers/signActions.js.map +1 -1
- package/dist/cjs/signers/wasmBinaryUrl.js +11 -0
- package/dist/cjs/signers/wasmLoader.d.ts +1 -6
- package/dist/cjs/signers/wasmLoader.d.ts.map +1 -1
- package/dist/cjs/signers/wasmLoader.js +15 -69
- package/dist/cjs/signers/wasmLoader.js.map +1 -1
- package/dist/cjs/types/account.d.ts +4 -1
- package/dist/cjs/types/account.d.ts.map +1 -1
- package/dist/cjs/types/action.d.ts +4 -1
- package/dist/cjs/types/action.d.ts.map +1 -1
- package/dist/cjs/types/action.js.map +1 -1
- package/dist/cjs/utils/assetCollateral.d.ts +2 -1
- package/dist/cjs/utils/assetCollateral.d.ts.map +1 -1
- package/dist/cjs/utils/assetCollateral.js +1 -1
- package/dist/cjs/utils/assetCollateral.js.map +1 -1
- package/dist/cjs/utils/index.d.ts +1 -0
- package/dist/cjs/utils/index.d.ts.map +1 -1
- package/dist/cjs/utils/index.js +3 -1
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/cjs/utils/withdrawableBalances.d.ts +4 -0
- package/dist/cjs/utils/withdrawableBalances.d.ts.map +1 -0
- package/dist/cjs/utils/withdrawableBalances.js +21 -0
- package/dist/cjs/utils/withdrawableBalances.js.map +1 -0
- package/dist/esm/LighterProvider.d.ts +53 -56
- package/dist/esm/LighterProvider.d.ts.map +1 -1
- package/dist/esm/LighterProvider.js +74 -49
- package/dist/esm/LighterProvider.js.map +1 -1
- package/dist/esm/constants.d.ts +25 -36
- package/dist/esm/constants.d.ts.map +1 -1
- package/dist/esm/constants.js +31 -18
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/index.d.ts +6 -7
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +5 -6
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/signers/LighterSigner.d.ts +10 -18
- package/dist/esm/signers/LighterSigner.d.ts.map +1 -1
- package/dist/esm/signers/LighterSigner.js +32 -25
- package/dist/esm/signers/LighterSigner.js.map +1 -1
- package/dist/esm/signers/generated/wasmExecRuntime.d.ts +8 -0
- package/dist/esm/signers/generated/wasmExecRuntime.d.ts.map +1 -0
- package/dist/esm/signers/generated/wasmExecRuntime.js +574 -0
- package/dist/esm/signers/generated/wasmExecRuntime.js.map +1 -0
- package/dist/esm/signers/signActions.d.ts +3 -5
- package/dist/esm/signers/signActions.d.ts.map +1 -1
- package/dist/esm/signers/signActions.js +4 -10
- package/dist/esm/signers/signActions.js.map +1 -1
- package/dist/esm/signers/wasmBinaryUrl.js +10 -0
- package/dist/esm/signers/wasmLoader.d.ts +7 -31
- package/dist/esm/signers/wasmLoader.d.ts.map +1 -1
- package/dist/esm/signers/wasmLoader.js +32 -108
- package/dist/esm/signers/wasmLoader.js.map +1 -1
- package/dist/esm/types/account.d.ts +17 -6
- package/dist/esm/types/account.d.ts.map +1 -1
- package/dist/esm/types/action.d.ts +14 -2
- package/dist/esm/types/action.d.ts.map +1 -1
- package/dist/esm/types/action.js.map +1 -1
- package/dist/esm/utils/assetCollateral.d.ts +3 -2
- package/dist/esm/utils/assetCollateral.d.ts.map +1 -1
- package/dist/esm/utils/assetCollateral.js +5 -5
- package/dist/esm/utils/assetCollateral.js.map +1 -1
- package/dist/esm/utils/index.d.ts +1 -0
- package/dist/esm/utils/index.d.ts.map +1 -1
- package/dist/esm/utils/index.js +1 -0
- package/dist/esm/utils/index.js.map +1 -1
- package/dist/esm/utils/withdrawableBalances.d.ts +12 -0
- package/dist/esm/utils/withdrawableBalances.d.ts.map +1 -0
- package/dist/esm/utils/withdrawableBalances.js +25 -0
- package/dist/esm/utils/withdrawableBalances.js.map +1 -0
- package/dist/types/LighterProvider.d.ts +53 -56
- package/dist/types/LighterProvider.d.ts.map +1 -1
- package/dist/types/constants.d.ts +25 -36
- package/dist/types/constants.d.ts.map +1 -1
- package/dist/types/index.d.ts +6 -7
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/signers/LighterSigner.d.ts +10 -18
- package/dist/types/signers/LighterSigner.d.ts.map +1 -1
- package/dist/types/signers/generated/wasmExecRuntime.d.ts +8 -0
- package/dist/types/signers/generated/wasmExecRuntime.d.ts.map +1 -0
- package/dist/types/signers/signActions.d.ts +3 -5
- package/dist/types/signers/signActions.d.ts.map +1 -1
- package/dist/types/signers/wasmLoader.d.ts +7 -31
- package/dist/types/signers/wasmLoader.d.ts.map +1 -1
- package/dist/types/types/account.d.ts +17 -6
- package/dist/types/types/account.d.ts.map +1 -1
- package/dist/types/types/action.d.ts +14 -2
- package/dist/types/types/action.d.ts.map +1 -1
- package/dist/types/utils/assetCollateral.d.ts +3 -2
- package/dist/types/utils/assetCollateral.d.ts.map +1 -1
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/types/utils/withdrawableBalances.d.ts +12 -0
- package/dist/types/utils/withdrawableBalances.d.ts.map +1 -0
- package/package.json +3 -6
- package/src/LighterProvider.ts +112 -101
- package/src/constants.ts +35 -45
- package/src/index.ts +13 -10
- package/src/signers/LighterSigner.ts +47 -51
- package/src/signers/generated/wasmExecRuntime.ts +575 -0
- package/src/signers/signActions.ts +12 -30
- package/src/signers/wasmBinaryUrl.d.ts +7 -0
- package/src/signers/wasmLoader.ts +33 -152
- package/src/types/account.ts +18 -6
- package/src/types/action.ts +14 -2
- package/src/utils/assetCollateral.ts +8 -6
- package/src/utils/index.ts +1 -0
- package/src/utils/withdrawableBalances.ts +31 -0
- package/dist/cjs/signers/index.d.ts +0 -11
- package/dist/cjs/signers/index.d.ts.map +0 -1
- package/dist/cjs/signers/index.js +0 -18
- package/dist/cjs/signers/index.js.map +0 -1
- package/dist/esm/signers/index.d.ts +0 -11
- package/dist/esm/signers/index.d.ts.map +0 -1
- package/dist/esm/signers/index.js +0 -7
- package/dist/esm/signers/index.js.map +0 -1
- package/dist/types/signers/index.d.ts +0 -11
- package/dist/types/signers/index.d.ts.map +0 -1
- package/dist/wasm/wasm_exec.js +0 -561
- package/src/signers/index.ts +0 -35
- package/wasm/lighter-signer.wasm +0 -0
- package/wasm/wasm_exec.js +0 -561
|
@@ -1,78 +1,8 @@
|
|
|
1
1
|
// The Go runtime installs functions (SignCreateOrder, SignCancelOrder, etc.)
|
|
2
2
|
// onto `globalThis` when `go.run(instance)` starts the main goroutine.
|
|
3
|
-
//
|
|
4
|
-
// The default asset URLs resolve relative to this module's emitted JS file:
|
|
5
|
-
// src/signers/../../wasm/lighter-signer.wasm → package-root wasm/
|
|
6
|
-
// dist/{esm,cjs}/signers/../../wasm/… → dist/wasm/ (shared)
|
|
7
|
-
// The build script copies the wasm assets into both dist subtrees so the same
|
|
8
|
-
// relative path resolves at runtime regardless of which bundle is loaded.
|
|
9
|
-
//
|
|
10
|
-
// To resolve the module's own URL in both ESM (where `__filename` is absent)
|
|
11
|
-
// and CJS (where `import.meta.url` is a syntax error), we parse a stack trace
|
|
12
|
-
// — `currentModuleUrl` always appears as the first frame, and V8 includes the
|
|
13
|
-
// emitting file path regardless of module system. Callers can override both
|
|
14
|
-
// asset URLs via {@link LoadLighterWasmOptions} to bypass this heuristic.
|
|
15
3
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (typeof cjsFilename === 'string' && cjsFilename.length > 0) {
|
|
19
|
-
return `file://${cjsFilename}`
|
|
20
|
-
}
|
|
21
|
-
const stack = new Error().stack ?? ''
|
|
22
|
-
// Stack frames look like one of:
|
|
23
|
-
// " at currentModuleUrl (file:///abs/path/wasmLoader.js:29:10)"
|
|
24
|
-
// " at currentModuleUrl (/abs/path/wasmLoader.js:29:10)"
|
|
25
|
-
// " at currentModuleUrl (file:///abs/path/wasmLoader.ts:29:10)" (vitest)
|
|
26
|
-
// " at currentModuleUrl (http://localhost:5173/.../wasmLoader.ts?v=…:29:10)" (vite dev)
|
|
27
|
-
const match = stack.match(/\(([^)]+wasmLoader\.[tj]s[^)]*)\)/)
|
|
28
|
-
const frame = match?.[1] ?? ''
|
|
29
|
-
const withoutPos = frame.replace(/:\d+:\d+$/, '')
|
|
30
|
-
if (
|
|
31
|
-
withoutPos.startsWith('file://') ||
|
|
32
|
-
withoutPos.startsWith('http://') ||
|
|
33
|
-
withoutPos.startsWith('https://')
|
|
34
|
-
) {
|
|
35
|
-
return withoutPos
|
|
36
|
-
}
|
|
37
|
-
if (withoutPos.length > 0) {
|
|
38
|
-
// Bare absolute path (Node CJS without `file://` prefix in some runners).
|
|
39
|
-
return `file://${withoutPos}`
|
|
40
|
-
}
|
|
41
|
-
throw new Error(
|
|
42
|
-
'Could not resolve the Lighter WASM loader module URL. Pass an explicit ' +
|
|
43
|
-
'`wasmBinaryUrl` and `wasmExecJsUrl` to loadLighterWasm().'
|
|
44
|
-
)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function defaultAssetUrl(filename: string): URL {
|
|
48
|
-
return new URL(`../../wasm/${filename}`, currentModuleUrl())
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Overrides for locating the Lighter Go WASM binary and its runtime loader.
|
|
53
|
-
* Supplying explicit URLs or source is useful when a bundler does not preserve
|
|
54
|
-
* the package's relative asset layout.
|
|
55
|
-
*
|
|
56
|
-
* @public
|
|
57
|
-
*/
|
|
58
|
-
export interface LoadLighterWasmOptions {
|
|
59
|
-
/** Override URL for the `.wasm` binary (browser: fetch; Node: fs). */
|
|
60
|
-
wasmBinaryUrl?: string | URL
|
|
61
|
-
/** Override URL for Go's `wasm_exec.js` runtime. */
|
|
62
|
-
wasmExecJsUrl?: string | URL
|
|
63
|
-
/**
|
|
64
|
-
* Pre-fetched source of Go's `wasm_exec.js`. Preferred over
|
|
65
|
-
* `wasmExecJsUrl` for bundlers (Vite/webpack) that may transform a `.js`
|
|
66
|
-
* URL on serve, breaking the IIFE that installs `globalThis.Go`. Pass via
|
|
67
|
-
* a `?raw` import (Vite) or equivalent so the original source survives
|
|
68
|
-
* intact.
|
|
69
|
-
*
|
|
70
|
-
* @security This string is evaluated via `new Function(...)`. The caller is
|
|
71
|
-
* responsible for ensuring it originates from the bundled `wasm_exec.js`;
|
|
72
|
-
* attacker-controlled content would execute arbitrary code in the host.
|
|
73
|
-
*/
|
|
74
|
-
wasmExecJsSource?: string
|
|
75
|
-
}
|
|
4
|
+
import { WASM_EXEC_JS } from './generated/wasmExecRuntime.js'
|
|
5
|
+
import { lighterWasmBinaryUrl } from './wasmBinaryUrl.js'
|
|
76
6
|
|
|
77
7
|
/**
|
|
78
8
|
* Function table installed by the Lighter Go WASM runtime. Methods mirror the
|
|
@@ -230,109 +160,60 @@ type GoClass = new () => {
|
|
|
230
160
|
|
|
231
161
|
let cachedExports: Promise<LighterWasmExports> | undefined
|
|
232
162
|
|
|
233
|
-
// `node:fs/promises`
|
|
234
|
-
//
|
|
235
|
-
// `
|
|
236
|
-
//
|
|
163
|
+
// `node:fs/promises` is Node-only and reached solely for a `file://` asset URL,
|
|
164
|
+
// which never happens in a browser. Both ignore comments are required: without
|
|
165
|
+
// `webpackIgnore` a Next.js build fails with `UnhandledSchemeError: Reading
|
|
166
|
+
// from "node:fs/promises" is not handled by plugins`, and Vite would otherwise
|
|
167
|
+
// warn while externalising it. Node's `readFile` accepts the `file://` URL
|
|
168
|
+
// directly, so no `node:url` hop is needed.
|
|
237
169
|
async function readNodeFile(url: URL): Promise<Uint8Array> {
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
return readFile(fileURLToPath(url))
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Resolve a possibly-relative URL string against the current document/page
|
|
247
|
-
* origin. Bundlers like Vite hand out absolute paths (e.g. `/node_modules/…`)
|
|
248
|
-
* for `?url` imports, which `new URL(string)` rejects without a base.
|
|
249
|
-
*/
|
|
250
|
-
function toResolvedUrl(url: string | URL): URL {
|
|
251
|
-
if (url instanceof URL) {
|
|
252
|
-
return url
|
|
253
|
-
}
|
|
254
|
-
const base =
|
|
255
|
-
typeof globalThis !== 'undefined' &&
|
|
256
|
-
(globalThis as { location?: { href?: string } }).location?.href
|
|
257
|
-
? (globalThis as { location: { href: string } }).location.href
|
|
258
|
-
: undefined
|
|
259
|
-
return new URL(url, base)
|
|
170
|
+
const { readFile } = await import(
|
|
171
|
+
/* webpackIgnore: true */ /* @vite-ignore */ 'node:fs/promises'
|
|
172
|
+
)
|
|
173
|
+
return readFile(url)
|
|
260
174
|
}
|
|
261
175
|
|
|
262
|
-
async function
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
const buf = await readNodeFile(u)
|
|
176
|
+
async function readWasmBinary(url: URL): Promise<ArrayBuffer> {
|
|
177
|
+
if (url.protocol === 'file:') {
|
|
178
|
+
const bytes = await readNodeFile(url)
|
|
266
179
|
// Copy the view into a fresh ArrayBuffer — `Buffer.buffer` is a shared
|
|
267
180
|
// ArrayBuffer in Node and the wasm instantiator expects a plain one.
|
|
268
|
-
return
|
|
181
|
+
return bytes.slice().buffer as ArrayBuffer
|
|
269
182
|
}
|
|
270
|
-
const response = await fetch(
|
|
183
|
+
const response = await fetch(url)
|
|
271
184
|
if (!response.ok) {
|
|
272
185
|
throw new Error(
|
|
273
|
-
`Failed to fetch ${
|
|
186
|
+
`Failed to fetch ${url.href}: ${response.status} ${response.statusText}`
|
|
274
187
|
)
|
|
275
188
|
}
|
|
276
189
|
return response.arrayBuffer()
|
|
277
190
|
}
|
|
278
191
|
|
|
279
|
-
async function readUrlText(url: string | URL): Promise<string> {
|
|
280
|
-
const u = toResolvedUrl(url)
|
|
281
|
-
if (u.protocol === 'file:') {
|
|
282
|
-
const buf = await readNodeFile(u)
|
|
283
|
-
return new TextDecoder('utf-8').decode(buf)
|
|
284
|
-
}
|
|
285
|
-
const response = await fetch(u)
|
|
286
|
-
if (!response.ok) {
|
|
287
|
-
throw new Error(
|
|
288
|
-
`Failed to fetch ${u.href}: ${response.status} ${response.statusText}`
|
|
289
|
-
)
|
|
290
|
-
}
|
|
291
|
-
return response.text()
|
|
292
|
-
}
|
|
293
|
-
|
|
294
192
|
/**
|
|
295
|
-
* Load the Lighter WASM signer
|
|
296
|
-
* the cached exports. The Go
|
|
297
|
-
* JS calls — we start it
|
|
298
|
-
*
|
|
193
|
+
* Load the Lighter WASM signer from the binary shipped with this package.
|
|
194
|
+
* Memoized per-process: subsequent calls return the cached exports. The Go
|
|
195
|
+
* runtime keeps a long-running goroutine to service JS calls — we start it
|
|
196
|
+
* once and never stop it.
|
|
197
|
+
* @internal
|
|
299
198
|
*/
|
|
300
|
-
export async function loadLighterWasm(
|
|
301
|
-
options?: LoadLighterWasmOptions
|
|
302
|
-
): Promise<LighterWasmExports> {
|
|
199
|
+
export async function loadLighterWasm(): Promise<LighterWasmExports> {
|
|
303
200
|
if (!cachedExports) {
|
|
304
|
-
cachedExports = loadWasmUncached(
|
|
201
|
+
cachedExports = loadWasmUncached()
|
|
305
202
|
}
|
|
306
203
|
return cachedExports
|
|
307
204
|
}
|
|
308
205
|
|
|
309
|
-
async function loadWasmUncached(
|
|
310
|
-
|
|
311
|
-
): Promise<LighterWasmExports> {
|
|
312
|
-
const wasmBinaryUrl =
|
|
313
|
-
options?.wasmBinaryUrl ?? defaultAssetUrl('lighter-signer.wasm')
|
|
314
|
-
|
|
315
|
-
// Prefer a pre-fetched source string (bundler `?raw` imports) over a URL —
|
|
316
|
-
// bundlers may transform a `.js` URL on serve, breaking the IIFE that sets
|
|
317
|
-
// `globalThis.Go`.
|
|
318
|
-
const wasmExecSourcePromise: Promise<string> =
|
|
319
|
-
options?.wasmExecJsSource !== undefined
|
|
320
|
-
? Promise.resolve(options.wasmExecJsSource)
|
|
321
|
-
: readUrlText(options?.wasmExecJsUrl ?? defaultAssetUrl('wasm_exec.js'))
|
|
322
|
-
|
|
323
|
-
const [wasmExecSource, wasmBytes] = await Promise.all([
|
|
324
|
-
wasmExecSourcePromise,
|
|
325
|
-
readUrlBytes(wasmBinaryUrl),
|
|
326
|
-
])
|
|
206
|
+
async function loadWasmUncached(): Promise<LighterWasmExports> {
|
|
207
|
+
const wasmBytes = await readWasmBinary(lighterWasmBinaryUrl)
|
|
327
208
|
|
|
328
209
|
// wasm_exec.js is an IIFE that installs `globalThis.Go = class { ... }` plus
|
|
329
|
-
// fs/process/crypto polyfills. Evaluating
|
|
210
|
+
// fs/process/crypto polyfills. Evaluating the packaged text keeps Go's runtime
|
|
330
211
|
// opaque to consumer bundlers — it references require/process/fs/crypto, which
|
|
331
|
-
// Vite/webpack choke on if they try to parse it as a module. The
|
|
332
|
-
//
|
|
333
|
-
//
|
|
212
|
+
// Vite/webpack choke on if they try to parse it as a module. The text is
|
|
213
|
+
// generated from this package's vendored wasm_exec.js at build time and is
|
|
214
|
+
// never caller-supplied.
|
|
334
215
|
// nosec
|
|
335
|
-
const installGo = new Function(`${
|
|
216
|
+
const installGo = new Function(`${WASM_EXEC_JS}; return globalThis.Go`)
|
|
336
217
|
const Go = installGo() as GoClass
|
|
337
218
|
|
|
338
219
|
const go = new Go()
|
|
@@ -374,7 +255,7 @@ async function yieldToGoRuntime(): Promise<void> {
|
|
|
374
255
|
/**
|
|
375
256
|
* Testing helper — drop the cached WASM instance so the next load reinitializes.
|
|
376
257
|
*
|
|
377
|
-
* @
|
|
258
|
+
* @internal
|
|
378
259
|
*/
|
|
379
260
|
export function resetLighterWasmCache(): void {
|
|
380
261
|
cachedExports = undefined
|
package/src/types/account.ts
CHANGED
|
@@ -33,24 +33,36 @@ export type LtAccountPosition = {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
|
|
36
|
+
* Cross-margin collateral flag Lighter reports on an account asset row.
|
|
37
|
+
*
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export type LtAssetMarginMode = 'enabled' | 'disabled'
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Held asset row returned in a Lighter account response. The two balances are
|
|
44
|
+
* the asset's two withdrawal routes: `balance` is the spot route
|
|
45
|
+
* (`AssetRouteType_Spot`) and `margin_balance` the perps route
|
|
46
|
+
* (`AssetRouteType_Perps`). All amounts are decimal strings.
|
|
40
47
|
*
|
|
41
48
|
* @public
|
|
42
49
|
*/
|
|
43
50
|
export type LtAccountAsset = {
|
|
44
51
|
symbol: string
|
|
45
52
|
asset_id: number
|
|
53
|
+
/** Spot-route balance, `locked_balance` included. */
|
|
46
54
|
balance: string
|
|
55
|
+
/** Portion of `balance` reserved by pending activity and not withdrawable. */
|
|
47
56
|
locked_balance: string
|
|
57
|
+
/** Perps-route balance — the asset's collateral leg. */
|
|
58
|
+
margin_balance: string
|
|
59
|
+
/** Collateral weighting Lighter applies to the asset, as a decimal factor. */
|
|
60
|
+
multiplier: string
|
|
48
61
|
/**
|
|
49
|
-
* Cross-margin collateral flag: 0 = MarginDisabled, 1 = MarginEnabled.
|
|
50
62
|
* Lighter surfaces it via `additional_properties`, so it may be absent —
|
|
51
63
|
* chiefly outside Unified Trading Account mode.
|
|
52
64
|
*/
|
|
53
|
-
margin_mode?:
|
|
65
|
+
margin_mode?: LtAssetMarginMode
|
|
54
66
|
}
|
|
55
67
|
|
|
56
68
|
/**
|
package/src/types/action.ts
CHANGED
|
@@ -72,14 +72,26 @@ export type LtUpdateMarginWasmParams = {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
/**
|
|
75
|
-
* Backend-provided params for SignWithdraw.
|
|
75
|
+
* Backend-provided params for SignWithdraw. The asset and route are the
|
|
76
|
+
* caller's selection, not a signer default, and the remaining fields are the
|
|
77
|
+
* venue metadata the signer needs to scale and bounds-check `amount` without
|
|
78
|
+
* an asset registry of its own.
|
|
76
79
|
*
|
|
77
80
|
* @public
|
|
78
81
|
*/
|
|
79
82
|
export type LtWithdrawWasmParams = {
|
|
83
|
+
/** Lighter L2 `asset_id` being withdrawn. */
|
|
80
84
|
asset_index: number
|
|
85
|
+
/** {@link LT_ROUTE_PERP} or {@link LT_ROUTE_SPOT}; anything else is rejected. */
|
|
81
86
|
route_type: number
|
|
82
|
-
amount
|
|
87
|
+
/** Decimal amount in the asset's own units, unscaled. */
|
|
88
|
+
amount: string
|
|
89
|
+
/** The asset's L2 precision — the exponent `amount` is scaled by. */
|
|
90
|
+
decimals: number
|
|
91
|
+
/** Venue minimum for a single withdrawal, in the asset's own units. */
|
|
92
|
+
min_withdrawal_amount: string
|
|
93
|
+
/** Asset symbol, named in a sub-minimum rejection. */
|
|
94
|
+
symbol: string
|
|
83
95
|
}
|
|
84
96
|
|
|
85
97
|
/**
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import type { LtAssetMarginMode } from '../types/account.js'
|
|
2
|
+
|
|
1
3
|
// Lighter's `AssetMarginMode` wire ints (lighter-go
|
|
2
|
-
// `types/txtypes/update_account_asset_config.go`), used
|
|
3
|
-
//
|
|
4
|
-
//
|
|
4
|
+
// `types/txtypes/update_account_asset_config.go`), used to sign
|
|
5
|
+
// `L2UpdateAccountAssetConfigTx` (tx type 42). The read side is not symmetric:
|
|
6
|
+
// `/api/v1/account` reports the same flag as a string.
|
|
5
7
|
const ASSET_MARGIN_DISABLED = 0
|
|
6
8
|
const ASSET_MARGIN_ENABLED = 1
|
|
7
9
|
|
|
@@ -10,7 +12,7 @@ export function assetMarginModeInt(enabled: boolean): number {
|
|
|
10
12
|
return enabled ? ASSET_MARGIN_ENABLED : ASSET_MARGIN_DISABLED
|
|
11
13
|
}
|
|
12
14
|
|
|
13
|
-
/** Decode Lighter's `margin_mode`
|
|
14
|
-
export function isAssetMarginEnabled(marginMode:
|
|
15
|
-
return marginMode ===
|
|
15
|
+
/** Decode Lighter's `margin_mode` on an account asset back to `enabled`. */
|
|
16
|
+
export function isAssetMarginEnabled(marginMode: LtAssetMarginMode): boolean {
|
|
17
|
+
return marginMode === 'enabled'
|
|
16
18
|
}
|
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ProviderWithdrawableBalance } from '@lifi/perps-sdk'
|
|
2
|
+
import type { LtAccountAsset } from '../types/account.js'
|
|
3
|
+
import { toRequiredBig } from './decimal.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Split each held asset into the two routes a Lighter withdrawal can name:
|
|
7
|
+
* spot draws on `balance` net of `locked_balance`, perps draws on
|
|
8
|
+
* `margin_balance`. Routes with nothing left to draw are dropped; the
|
|
9
|
+
* per-asset venue minimum is applied by the caller holding the asset registry.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export const lighterWithdrawableBalances = (
|
|
14
|
+
assets: LtAccountAsset[]
|
|
15
|
+
): ProviderWithdrawableBalance[] => {
|
|
16
|
+
const rows: ProviderWithdrawableBalance[] = []
|
|
17
|
+
for (const asset of assets) {
|
|
18
|
+
const assetId = String(asset.asset_id)
|
|
19
|
+
const spot = toRequiredBig(asset.balance, 'balance').minus(
|
|
20
|
+
toRequiredBig(asset.locked_balance, 'locked_balance')
|
|
21
|
+
)
|
|
22
|
+
if (spot.gt(0)) {
|
|
23
|
+
rows.push({ assetId, route: 'spot', available: spot.toFixed() })
|
|
24
|
+
}
|
|
25
|
+
const perps = toRequiredBig(asset.margin_balance, 'margin_balance')
|
|
26
|
+
if (perps.gt(0)) {
|
|
27
|
+
rows.push({ assetId, route: 'perps', available: perps.toFixed() })
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return rows
|
|
31
|
+
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export type { CreateAuthTokenInputs } from './createAuthToken.js';
|
|
2
|
-
export { createAuthToken, isReadOnlyTokenExpiringSoon, } from './createAuthToken.js';
|
|
3
|
-
export type { LighterApiKey } from './LighterKeyStore.js';
|
|
4
|
-
export { LighterKeyStore } from './LighterKeyStore.js';
|
|
5
|
-
export type { ApproveReadOnlyTokenInputs, ApproveReadOnlyTokenResult, LighterCreateTokenResponse, LighterReadOnlyToken, LighterReadOnlyTokenManagerOptions, LighterTokenFetcher, } from './LighterReadOnlyTokenManager.js';
|
|
6
|
-
export { DEFAULT_READ_ONLY_TOKEN_NAME, defaultLighterTokenFetcher, LighterReadOnlyTokenManager, } from './LighterReadOnlyTokenManager.js';
|
|
7
|
-
export type { ApiKeyPair, ChangePubKeyResult, LighterSignedBlob, LighterSignerConfig, LighterSignerContext, } from './LighterSigner.js';
|
|
8
|
-
export { LighterSigner } from './LighterSigner.js';
|
|
9
|
-
export type { LighterWasmExports, LoadLighterWasmOptions, } from './wasmLoader.js';
|
|
10
|
-
export { loadLighterWasm, resetLighterWasmCache } from './wasmLoader.js';
|
|
11
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/signers/index.ts"],"names":[],"mappings":"AAEA,YAAY,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AACjE,OAAO,EACL,eAAe,EACf,2BAA2B,GAC5B,MAAM,sBAAsB,CAAA;AAC7B,YAAY,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AACtD,YAAY,EACV,0BAA0B,EAC1B,0BAA0B,EAC1B,0BAA0B,EAC1B,oBAAoB,EACpB,kCAAkC,EAClC,mBAAmB,GACpB,MAAM,kCAAkC,CAAA;AACzC,OAAO,EACL,4BAA4B,EAC5B,0BAA0B,EAC1B,2BAA2B,GAC5B,MAAM,kCAAkC,CAAA;AACzC,YAAY,EACV,UAAU,EACV,kBAAkB,EAClB,iBAAiB,EACjB,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,YAAY,EACV,kBAAkB,EAClB,sBAAsB,GACvB,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAA"}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.resetLighterWasmCache = exports.loadLighterWasm = exports.LighterSigner = exports.LighterReadOnlyTokenManager = exports.defaultLighterTokenFetcher = exports.DEFAULT_READ_ONLY_TOKEN_NAME = exports.LighterKeyStore = exports.isReadOnlyTokenExpiringSoon = exports.createAuthToken = void 0;
|
|
4
|
-
var createAuthToken_js_1 = require("./createAuthToken.js");
|
|
5
|
-
Object.defineProperty(exports, "createAuthToken", { enumerable: true, get: function () { return createAuthToken_js_1.createAuthToken; } });
|
|
6
|
-
Object.defineProperty(exports, "isReadOnlyTokenExpiringSoon", { enumerable: true, get: function () { return createAuthToken_js_1.isReadOnlyTokenExpiringSoon; } });
|
|
7
|
-
var LighterKeyStore_js_1 = require("./LighterKeyStore.js");
|
|
8
|
-
Object.defineProperty(exports, "LighterKeyStore", { enumerable: true, get: function () { return LighterKeyStore_js_1.LighterKeyStore; } });
|
|
9
|
-
var LighterReadOnlyTokenManager_js_1 = require("./LighterReadOnlyTokenManager.js");
|
|
10
|
-
Object.defineProperty(exports, "DEFAULT_READ_ONLY_TOKEN_NAME", { enumerable: true, get: function () { return LighterReadOnlyTokenManager_js_1.DEFAULT_READ_ONLY_TOKEN_NAME; } });
|
|
11
|
-
Object.defineProperty(exports, "defaultLighterTokenFetcher", { enumerable: true, get: function () { return LighterReadOnlyTokenManager_js_1.defaultLighterTokenFetcher; } });
|
|
12
|
-
Object.defineProperty(exports, "LighterReadOnlyTokenManager", { enumerable: true, get: function () { return LighterReadOnlyTokenManager_js_1.LighterReadOnlyTokenManager; } });
|
|
13
|
-
var LighterSigner_js_1 = require("./LighterSigner.js");
|
|
14
|
-
Object.defineProperty(exports, "LighterSigner", { enumerable: true, get: function () { return LighterSigner_js_1.LighterSigner; } });
|
|
15
|
-
var wasmLoader_js_1 = require("./wasmLoader.js");
|
|
16
|
-
Object.defineProperty(exports, "loadLighterWasm", { enumerable: true, get: function () { return wasmLoader_js_1.loadLighterWasm; } });
|
|
17
|
-
Object.defineProperty(exports, "resetLighterWasmCache", { enumerable: true, get: function () { return wasmLoader_js_1.resetLighterWasmCache; } });
|
|
18
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/signers/index.ts"],"names":[],"mappings":";;;AAGA,2DAG6B;AAF3B,qHAAA,eAAe,OAAA;AACf,iIAAA,2BAA2B,OAAA;AAG7B,2DAAsD;AAA7C,qHAAA,eAAe,OAAA;AASxB,mFAIyC;AAHvC,8IAAA,4BAA4B,OAAA;AAC5B,4IAAA,0BAA0B,OAAA;AAC1B,6IAAA,2BAA2B,OAAA;AAS7B,uDAAkD;AAAzC,iHAAA,aAAa,OAAA;AAKtB,iDAAwE;AAA/D,gHAAA,eAAe,OAAA;AAAE,sHAAA,qBAAqB,OAAA"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export type { CreateAuthTokenInputs } from './createAuthToken.js';
|
|
2
|
-
export { createAuthToken, isReadOnlyTokenExpiringSoon, } from './createAuthToken.js';
|
|
3
|
-
export type { LighterApiKey } from './LighterKeyStore.js';
|
|
4
|
-
export { LighterKeyStore } from './LighterKeyStore.js';
|
|
5
|
-
export type { ApproveReadOnlyTokenInputs, ApproveReadOnlyTokenResult, LighterCreateTokenResponse, LighterReadOnlyToken, LighterReadOnlyTokenManagerOptions, LighterTokenFetcher, } from './LighterReadOnlyTokenManager.js';
|
|
6
|
-
export { DEFAULT_READ_ONLY_TOKEN_NAME, defaultLighterTokenFetcher, LighterReadOnlyTokenManager, } from './LighterReadOnlyTokenManager.js';
|
|
7
|
-
export type { ApiKeyPair, ChangePubKeyResult, LighterSignedBlob, LighterSignerConfig, LighterSignerContext, } from './LighterSigner.js';
|
|
8
|
-
export { LighterSigner } from './LighterSigner.js';
|
|
9
|
-
export type { LighterWasmExports, LoadLighterWasmOptions, } from './wasmLoader.js';
|
|
10
|
-
export { loadLighterWasm, resetLighterWasmCache } from './wasmLoader.js';
|
|
11
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/signers/index.ts"],"names":[],"mappings":"AAEA,YAAY,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AACjE,OAAO,EACL,eAAe,EACf,2BAA2B,GAC5B,MAAM,sBAAsB,CAAA;AAC7B,YAAY,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AACtD,YAAY,EACV,0BAA0B,EAC1B,0BAA0B,EAC1B,0BAA0B,EAC1B,oBAAoB,EACpB,kCAAkC,EAClC,mBAAmB,GACpB,MAAM,kCAAkC,CAAA;AACzC,OAAO,EACL,4BAA4B,EAC5B,0BAA0B,EAC1B,2BAA2B,GAC5B,MAAM,kCAAkC,CAAA;AACzC,YAAY,EACV,UAAU,EACV,kBAAkB,EAClB,iBAAiB,EACjB,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,YAAY,EACV,kBAAkB,EAClB,sBAAsB,GACvB,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAA"}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
// biome-ignore-all lint/performance/noBarrelFile: package-internal barrel.
|
|
2
|
-
export { createAuthToken, isReadOnlyTokenExpiringSoon, } from './createAuthToken.js';
|
|
3
|
-
export { LighterKeyStore } from './LighterKeyStore.js';
|
|
4
|
-
export { DEFAULT_READ_ONLY_TOKEN_NAME, defaultLighterTokenFetcher, LighterReadOnlyTokenManager, } from './LighterReadOnlyTokenManager.js';
|
|
5
|
-
export { LighterSigner } from './LighterSigner.js';
|
|
6
|
-
export { loadLighterWasm, resetLighterWasmCache } from './wasmLoader.js';
|
|
7
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/signers/index.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAG3E,OAAO,EACL,eAAe,EACf,2BAA2B,GAC5B,MAAM,sBAAsB,CAAA;AAE7B,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAStD,OAAO,EACL,4BAA4B,EAC5B,0BAA0B,EAC1B,2BAA2B,GAC5B,MAAM,kCAAkC,CAAA;AAQzC,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAKlD,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAA"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export type { CreateAuthTokenInputs } from './createAuthToken.js';
|
|
2
|
-
export { createAuthToken, isReadOnlyTokenExpiringSoon, } from './createAuthToken.js';
|
|
3
|
-
export type { LighterApiKey } from './LighterKeyStore.js';
|
|
4
|
-
export { LighterKeyStore } from './LighterKeyStore.js';
|
|
5
|
-
export type { ApproveReadOnlyTokenInputs, ApproveReadOnlyTokenResult, LighterCreateTokenResponse, LighterReadOnlyToken, LighterReadOnlyTokenManagerOptions, LighterTokenFetcher, } from './LighterReadOnlyTokenManager.js';
|
|
6
|
-
export { DEFAULT_READ_ONLY_TOKEN_NAME, defaultLighterTokenFetcher, LighterReadOnlyTokenManager, } from './LighterReadOnlyTokenManager.js';
|
|
7
|
-
export type { ApiKeyPair, ChangePubKeyResult, LighterSignedBlob, LighterSignerConfig, LighterSignerContext, } from './LighterSigner.js';
|
|
8
|
-
export { LighterSigner } from './LighterSigner.js';
|
|
9
|
-
export type { LighterWasmExports, LoadLighterWasmOptions, } from './wasmLoader.js';
|
|
10
|
-
export { loadLighterWasm, resetLighterWasmCache } from './wasmLoader.js';
|
|
11
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/signers/index.ts"],"names":[],"mappings":"AAEA,YAAY,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AACjE,OAAO,EACL,eAAe,EACf,2BAA2B,GAC5B,MAAM,sBAAsB,CAAA;AAC7B,YAAY,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AACtD,YAAY,EACV,0BAA0B,EAC1B,0BAA0B,EAC1B,0BAA0B,EAC1B,oBAAoB,EACpB,kCAAkC,EAClC,mBAAmB,GACpB,MAAM,kCAAkC,CAAA;AACzC,OAAO,EACL,4BAA4B,EAC5B,0BAA0B,EAC1B,2BAA2B,GAC5B,MAAM,kCAAkC,CAAA;AACzC,YAAY,EACV,UAAU,EACV,kBAAkB,EAClB,iBAAiB,EACjB,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,YAAY,EACV,kBAAkB,EAClB,sBAAsB,GACvB,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAA"}
|