@lifi/perps-sdk-provider-lighter 5.1.1 → 7.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/dist/cjs/LighterProvider.d.ts.map +1 -1
- package/dist/cjs/LighterProvider.js +18 -11
- package/dist/cjs/LighterProvider.js.map +1 -1
- package/dist/cjs/accountConfig.d.ts.map +1 -1
- package/dist/cjs/accountConfig.js +1 -0
- package/dist/cjs/accountConfig.js.map +1 -1
- package/dist/cjs/depositFlow.d.ts +4 -0
- package/dist/cjs/depositFlow.d.ts.map +1 -0
- package/dist/cjs/depositFlow.js +32 -0
- package/dist/cjs/depositFlow.js.map +1 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/utils/index.d.ts +1 -2
- package/dist/cjs/utils/index.d.ts.map +1 -1
- package/dist/cjs/utils/index.js +1 -2
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/cjs/utils/wireEncoding.d.ts +0 -2
- package/dist/cjs/utils/wireEncoding.d.ts.map +1 -1
- package/dist/cjs/utils/wireEncoding.js +1 -25
- package/dist/cjs/utils/wireEncoding.js.map +1 -1
- package/dist/esm/LighterProvider.d.ts.map +1 -1
- package/dist/esm/LighterProvider.js +18 -11
- package/dist/esm/LighterProvider.js.map +1 -1
- package/dist/esm/accountConfig.d.ts.map +1 -1
- package/dist/esm/accountConfig.js +1 -0
- package/dist/esm/accountConfig.js.map +1 -1
- package/dist/esm/depositFlow.d.ts +12 -0
- package/dist/esm/depositFlow.d.ts.map +1 -0
- package/dist/esm/depositFlow.js +37 -0
- package/dist/esm/depositFlow.js.map +1 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/index.d.ts +1 -2
- package/dist/esm/utils/index.d.ts.map +1 -1
- package/dist/esm/utils/index.js +1 -1
- package/dist/esm/utils/index.js.map +1 -1
- package/dist/esm/utils/wireEncoding.d.ts +0 -22
- package/dist/esm/utils/wireEncoding.d.ts.map +1 -1
- package/dist/esm/utils/wireEncoding.js +0 -32
- package/dist/esm/utils/wireEncoding.js.map +1 -1
- package/dist/types/LighterProvider.d.ts.map +1 -1
- package/dist/types/accountConfig.d.ts.map +1 -1
- package/dist/types/depositFlow.d.ts +12 -0
- package/dist/types/depositFlow.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/utils/index.d.ts +1 -2
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/types/utils/wireEncoding.d.ts +0 -22
- package/dist/types/utils/wireEncoding.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/LighterProvider.ts +32 -12
- package/src/accountConfig.ts +1 -0
- package/src/depositFlow.ts +59 -0
- package/src/index.ts +1 -0
- package/src/utils/index.ts +0 -2
- package/src/utils/wireEncoding.ts +0 -55
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type DeclaredDepositAsset,
|
|
3
|
+
type DepositFlow,
|
|
4
|
+
ETHEREUM_NATIVE_GAS,
|
|
5
|
+
ETHEREUM_USDC,
|
|
6
|
+
LIGHTER_USDC,
|
|
7
|
+
ROBINHOOD_NATIVE_GAS,
|
|
8
|
+
ROBINHOOD_USDG,
|
|
9
|
+
} from '@lifi/perps-sdk'
|
|
10
|
+
import { ActionType, type LighterProviderKey } from '@lifi/perps-types'
|
|
11
|
+
import { LIGHTER_PROVIDER_KEY, LIGHTER_RH_PROVIDER_KEY } from './constants.js'
|
|
12
|
+
|
|
13
|
+
interface LighterDepositAssets {
|
|
14
|
+
/** Collateral token on the venue chain, credited to an account that exists. */
|
|
15
|
+
venueCollateral: DeclaredDepositAsset
|
|
16
|
+
/** Collateral the account-opening deposit is made in, on the chain that deposit runs on. */
|
|
17
|
+
firstDepositCollateral: DeclaredDepositAsset
|
|
18
|
+
firstDepositGas: DeclaredDepositAsset
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const DEPOSIT_ASSETS: Record<LighterProviderKey, LighterDepositAssets> = {
|
|
22
|
+
[LIGHTER_PROVIDER_KEY]: {
|
|
23
|
+
venueCollateral: LIGHTER_USDC,
|
|
24
|
+
firstDepositCollateral: ETHEREUM_USDC,
|
|
25
|
+
firstDepositGas: ETHEREUM_NATIVE_GAS,
|
|
26
|
+
},
|
|
27
|
+
[LIGHTER_RH_PROVIDER_KEY]: {
|
|
28
|
+
venueCollateral: ROBINHOOD_USDG,
|
|
29
|
+
firstDepositCollateral: ROBINHOOD_USDG,
|
|
30
|
+
firstDepositGas: ROBINHOOD_NATIVE_GAS,
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Resolve one Lighter instance's deposit flow from whether the account already
|
|
36
|
+
* exists: an existing account takes a swap into the instance's venue collateral,
|
|
37
|
+
* while an address with no account runs the account-opening deposit on the chain
|
|
38
|
+
* that instance's collateral is deposited from.
|
|
39
|
+
*
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
export function lighterDepositFlow(
|
|
43
|
+
providerKey: LighterProviderKey,
|
|
44
|
+
accountExists: boolean
|
|
45
|
+
): DepositFlow {
|
|
46
|
+
const assets = DEPOSIT_ASSETS[providerKey]
|
|
47
|
+
|
|
48
|
+
if (accountExists) {
|
|
49
|
+
return { kind: 'lifiSwap', destination: assets.venueCollateral }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
kind: 'firstDepositPipeline',
|
|
54
|
+
chainId: assets.firstDepositCollateral.chainId,
|
|
55
|
+
gasAsset: assets.firstDepositGas,
|
|
56
|
+
collateral: assets.firstDepositCollateral,
|
|
57
|
+
bridgeAction: ActionType.DEPOSIT,
|
|
58
|
+
}
|
|
59
|
+
}
|
package/src/index.ts
CHANGED
package/src/utils/index.ts
CHANGED
|
@@ -28,7 +28,6 @@ export { mapPosition } from './mapPosition.js'
|
|
|
28
28
|
export { mapInterval } from './ohlcvInterval.js'
|
|
29
29
|
export { formatOrderPrice, formatOrderSize } from './orderFormatting.js'
|
|
30
30
|
export { toIsoFromMs, toIsoFromSeconds } from './time.js'
|
|
31
|
-
export type { ScaleToIntegerPolicy } from './wireEncoding.js'
|
|
32
31
|
export {
|
|
33
32
|
leverageToFraction,
|
|
34
33
|
mapOrderTypeToInt,
|
|
@@ -36,5 +35,4 @@ export {
|
|
|
36
35
|
marginFractionToMaxLeverage,
|
|
37
36
|
orderExpiryForTif,
|
|
38
37
|
resolveTimeInForce,
|
|
39
|
-
scaleToInteger,
|
|
40
38
|
} from './wireEncoding.js'
|
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
|
|
7
7
|
import { PerpsError } from '@lifi/perps-sdk'
|
|
8
8
|
import { PerpsErrorCode } from '@lifi/perps-types'
|
|
9
|
-
import Big from 'big.js'
|
|
10
9
|
import {
|
|
11
10
|
LT_DEFAULT_ORDER_EXPIRY,
|
|
12
11
|
LT_ORDER_TYPE_LIMIT,
|
|
@@ -46,60 +45,6 @@ export const leverageToFraction = (leverage: number): number => {
|
|
|
46
45
|
return Math.round(10_000 / leverage)
|
|
47
46
|
}
|
|
48
47
|
|
|
49
|
-
/**
|
|
50
|
-
* Off-grid resolution for {@link scaleToInteger}:
|
|
51
|
-
* - `truncate` — toward zero, for sizes and collateral amounts. The wire
|
|
52
|
-
* value never exceeds the caller's intent.
|
|
53
|
-
* - `round` — half away from zero, for prices, which snap to the nearest
|
|
54
|
-
* tick.
|
|
55
|
-
* @public
|
|
56
|
-
*/
|
|
57
|
-
export type ScaleToIntegerPolicy = 'truncate' | 'round'
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Scale a human-readable decimal string to a scaled integer for the WASM
|
|
61
|
-
* signer, in exact decimal arithmetic — an on-grid input maps to its exact
|
|
62
|
-
* scaled integer with no binary float artifacts (`'0.29'` at 2 decimals is
|
|
63
|
-
* 29, never 28). Off-grid input resolves per `policy`; there is no silent
|
|
64
|
-
* default.
|
|
65
|
-
*
|
|
66
|
-
* @throws {PerpsError} `ValidationError` when `value` is not a decimal
|
|
67
|
-
* numeric string, `decimals` is not a non-negative integer, or the scaled
|
|
68
|
-
* result's magnitude exceeds `Number.MAX_SAFE_INTEGER`.
|
|
69
|
-
* @public
|
|
70
|
-
*/
|
|
71
|
-
export const scaleToInteger = (
|
|
72
|
-
value: string,
|
|
73
|
-
decimals: number,
|
|
74
|
-
policy: ScaleToIntegerPolicy
|
|
75
|
-
): number => {
|
|
76
|
-
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
77
|
-
throw new PerpsError(
|
|
78
|
-
PerpsErrorCode.ValidationError,
|
|
79
|
-
`Invalid decimals for Lighter integer scaling: ${decimals}`
|
|
80
|
-
)
|
|
81
|
-
}
|
|
82
|
-
let parsed: Big
|
|
83
|
-
try {
|
|
84
|
-
parsed = new Big(value)
|
|
85
|
-
} catch {
|
|
86
|
-
throw new PerpsError(
|
|
87
|
-
PerpsErrorCode.ValidationError,
|
|
88
|
-
`Invalid decimal string for Lighter integer scaling: '${value}'`
|
|
89
|
-
)
|
|
90
|
-
}
|
|
91
|
-
const scaled = parsed
|
|
92
|
-
.times(new Big(10).pow(decimals))
|
|
93
|
-
.round(0, policy === 'truncate' ? Big.roundDown : Big.roundHalfUp)
|
|
94
|
-
if (scaled.abs().gt(Number.MAX_SAFE_INTEGER)) {
|
|
95
|
-
throw new PerpsError(
|
|
96
|
-
PerpsErrorCode.ValidationError,
|
|
97
|
-
`Scaled value ${scaled.toFixed()} exceeds Number.MAX_SAFE_INTEGER and cannot be encoded exactly.`
|
|
98
|
-
)
|
|
99
|
-
}
|
|
100
|
-
return scaled.toNumber()
|
|
101
|
-
}
|
|
102
|
-
|
|
103
48
|
/**
|
|
104
49
|
* Map our OrderType enum to Lighter's integer order type.
|
|
105
50
|
* @public
|