@lifi/perps-sdk-provider-ondo 0.0.0-preview-7ff840c
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 +201 -0
- package/README.md +38 -0
- package/dist/cjs/OndoProvider.d.ts +8 -0
- package/dist/cjs/OndoProvider.d.ts.map +1 -0
- package/dist/cjs/OndoProvider.js +338 -0
- package/dist/cjs/OndoProvider.js.map +1 -0
- package/dist/cjs/accountConfig.d.ts +3 -0
- package/dist/cjs/accountConfig.d.ts.map +1 -0
- package/dist/cjs/accountConfig.js +59 -0
- package/dist/cjs/accountConfig.js.map +1 -0
- package/dist/cjs/accountSummary.d.ts +3 -0
- package/dist/cjs/accountSummary.d.ts.map +1 -0
- package/dist/cjs/accountSummary.js +8 -0
- package/dist/cjs/accountSummary.js.map +1 -0
- package/dist/cjs/auth/OndoTokenStore.d.ts +13 -0
- package/dist/cjs/auth/OndoTokenStore.d.ts.map +1 -0
- package/dist/cjs/auth/OndoTokenStore.js +53 -0
- package/dist/cjs/auth/OndoTokenStore.js.map +1 -0
- package/dist/cjs/auth/completeSiweLogin.d.ts +13 -0
- package/dist/cjs/auth/completeSiweLogin.d.ts.map +1 -0
- package/dist/cjs/auth/completeSiweLogin.js +15 -0
- package/dist/cjs/auth/completeSiweLogin.js.map +1 -0
- package/dist/cjs/auth/index.d.ts +4 -0
- package/dist/cjs/auth/index.d.ts.map +1 -0
- package/dist/cjs/auth/index.js +11 -0
- package/dist/cjs/auth/index.js.map +1 -0
- package/dist/cjs/auth/signActions.d.ts +13 -0
- package/dist/cjs/auth/signActions.d.ts.map +1 -0
- package/dist/cjs/auth/signActions.js +86 -0
- package/dist/cjs/auth/signActions.js.map +1 -0
- package/dist/cjs/constants.d.ts +9 -0
- package/dist/cjs/constants.d.ts.map +1 -0
- package/dist/cjs/constants.js +12 -0
- package/dist/cjs/constants.js.map +1 -0
- package/dist/cjs/index.d.ts +9 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +37 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/types/auth.d.ts +16 -0
- package/dist/cjs/types/auth.d.ts.map +1 -0
- package/dist/cjs/types/auth.js +3 -0
- package/dist/cjs/types/auth.js.map +1 -0
- package/dist/cjs/types/index.d.ts +4 -0
- package/dist/cjs/types/index.d.ts.map +1 -0
- package/dist/cjs/types/index.js +3 -0
- package/dist/cjs/types/index.js.map +1 -0
- package/dist/cjs/types/wire.d.ts +154 -0
- package/dist/cjs/types/wire.d.ts.map +1 -0
- package/dist/cjs/types/wire.js +3 -0
- package/dist/cjs/types/wire.js.map +1 -0
- package/dist/cjs/types/ws.d.ts +92 -0
- package/dist/cjs/types/ws.d.ts.map +1 -0
- package/dist/cjs/types/ws.js +3 -0
- package/dist/cjs/types/ws.js.map +1 -0
- package/dist/cjs/utils/activityCursor.d.ts +9 -0
- package/dist/cjs/utils/activityCursor.d.ts.map +1 -0
- package/dist/cjs/utils/activityCursor.js +79 -0
- package/dist/cjs/utils/activityCursor.js.map +1 -0
- package/dist/cjs/utils/apiClient.d.ts +43 -0
- package/dist/cjs/utils/apiClient.d.ts.map +1 -0
- package/dist/cjs/utils/apiClient.js +134 -0
- package/dist/cjs/utils/apiClient.js.map +1 -0
- package/dist/cjs/utils/index.d.ts +11 -0
- package/dist/cjs/utils/index.d.ts.map +1 -0
- package/dist/cjs/utils/index.js +39 -0
- package/dist/cjs/utils/index.js.map +1 -0
- package/dist/cjs/utils/liquidation.d.ts +4 -0
- package/dist/cjs/utils/liquidation.d.ts.map +1 -0
- package/dist/cjs/utils/liquidation.js +16 -0
- package/dist/cjs/utils/liquidation.js.map +1 -0
- package/dist/cjs/utils/mapActivity.d.ts +5 -0
- package/dist/cjs/utils/mapActivity.d.ts.map +1 -0
- package/dist/cjs/utils/mapActivity.js +34 -0
- package/dist/cjs/utils/mapActivity.js.map +1 -0
- package/dist/cjs/utils/mapFill.d.ts +4 -0
- package/dist/cjs/utils/mapFill.d.ts.map +1 -0
- package/dist/cjs/utils/mapFill.js +36 -0
- package/dist/cjs/utils/mapFill.js.map +1 -0
- package/dist/cjs/utils/mapOrder.d.ts +16 -0
- package/dist/cjs/utils/mapOrder.d.ts.map +1 -0
- package/dist/cjs/utils/mapOrder.js +135 -0
- package/dist/cjs/utils/mapOrder.js.map +1 -0
- package/dist/cjs/utils/mapPosition.d.ts +5 -0
- package/dist/cjs/utils/mapPosition.d.ts.map +1 -0
- package/dist/cjs/utils/mapPosition.js +26 -0
- package/dist/cjs/utils/mapPosition.js.map +1 -0
- package/dist/cjs/utils/ohlcvInterval.d.ts +4 -0
- package/dist/cjs/utils/ohlcvInterval.d.ts.map +1 -0
- package/dist/cjs/utils/ohlcvInterval.js +49 -0
- package/dist/cjs/utils/ohlcvInterval.js.map +1 -0
- package/dist/cjs/utils/ondoAsset.d.ts +3 -0
- package/dist/cjs/utils/ondoAsset.d.ts.map +1 -0
- package/dist/cjs/utils/ondoAsset.js +12 -0
- package/dist/cjs/utils/ondoAsset.js.map +1 -0
- package/dist/cjs/utils/orderFormatting.d.ts +4 -0
- package/dist/cjs/utils/orderFormatting.d.ts.map +1 -0
- package/dist/cjs/utils/orderFormatting.js +40 -0
- package/dist/cjs/utils/orderFormatting.js.map +1 -0
- package/dist/cjs/websocket/OndoWsProvider.d.ts +56 -0
- package/dist/cjs/websocket/OndoWsProvider.d.ts.map +1 -0
- package/dist/cjs/websocket/OndoWsProvider.js +515 -0
- package/dist/cjs/websocket/OndoWsProvider.js.map +1 -0
- package/dist/esm/OndoProvider.d.ts +39 -0
- package/dist/esm/OndoProvider.d.ts.map +1 -0
- package/dist/esm/OndoProvider.js +366 -0
- package/dist/esm/OndoProvider.js.map +1 -0
- package/dist/esm/accountConfig.d.ts +13 -0
- package/dist/esm/accountConfig.d.ts.map +1 -0
- package/dist/esm/accountConfig.js +77 -0
- package/dist/esm/accountConfig.js.map +1 -0
- package/dist/esm/accountSummary.d.ts +12 -0
- package/dist/esm/accountSummary.d.ts.map +1 -0
- package/dist/esm/accountSummary.js +14 -0
- package/dist/esm/accountSummary.js.map +1 -0
- package/dist/esm/auth/OndoTokenStore.d.ts +20 -0
- package/dist/esm/auth/OndoTokenStore.d.ts.map +1 -0
- package/dist/esm/auth/OndoTokenStore.js +61 -0
- package/dist/esm/auth/OndoTokenStore.js.map +1 -0
- package/dist/esm/auth/completeSiweLogin.d.ts +28 -0
- package/dist/esm/auth/completeSiweLogin.d.ts.map +1 -0
- package/dist/esm/auth/completeSiweLogin.js +21 -0
- package/dist/esm/auth/completeSiweLogin.js.map +1 -0
- package/dist/esm/auth/index.d.ts +4 -0
- package/dist/esm/auth/index.d.ts.map +1 -0
- package/dist/esm/auth/index.js +5 -0
- package/dist/esm/auth/index.js.map +1 -0
- package/dist/esm/auth/signActions.d.ts +35 -0
- package/dist/esm/auth/signActions.d.ts.map +1 -0
- package/dist/esm/auth/signActions.js +103 -0
- package/dist/esm/auth/signActions.js.map +1 -0
- package/dist/esm/constants.d.ts +35 -0
- package/dist/esm/constants.d.ts.map +1 -0
- package/dist/esm/constants.js +35 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/index.d.ts +19 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +21 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/types/auth.d.ts +32 -0
- package/dist/esm/types/auth.d.ts.map +1 -0
- package/dist/esm/types/auth.js +2 -0
- package/dist/esm/types/auth.js.map +1 -0
- package/dist/esm/types/index.d.ts +4 -0
- package/dist/esm/types/index.d.ts.map +1 -0
- package/dist/esm/types/index.js +3 -0
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/types/wire.d.ts +195 -0
- package/dist/esm/types/wire.d.ts.map +1 -0
- package/dist/esm/types/wire.js +7 -0
- package/dist/esm/types/wire.js.map +1 -0
- package/dist/esm/types/ws.d.ts +108 -0
- package/dist/esm/types/ws.d.ts.map +1 -0
- package/dist/esm/types/ws.js +7 -0
- package/dist/esm/types/ws.js.map +1 -0
- package/dist/esm/utils/activityCursor.d.ts +47 -0
- package/dist/esm/utils/activityCursor.d.ts.map +1 -0
- package/dist/esm/utils/activityCursor.js +93 -0
- package/dist/esm/utils/activityCursor.js.map +1 -0
- package/dist/esm/utils/apiClient.d.ts +90 -0
- package/dist/esm/utils/apiClient.d.ts.map +1 -0
- package/dist/esm/utils/apiClient.js +169 -0
- package/dist/esm/utils/apiClient.js.map +1 -0
- package/dist/esm/utils/index.d.ts +11 -0
- package/dist/esm/utils/index.d.ts.map +1 -0
- package/dist/esm/utils/index.js +12 -0
- package/dist/esm/utils/index.js.map +1 -0
- package/dist/esm/utils/liquidation.d.ts +20 -0
- package/dist/esm/utils/liquidation.d.ts.map +1 -0
- package/dist/esm/utils/liquidation.js +29 -0
- package/dist/esm/utils/liquidation.js.map +1 -0
- package/dist/esm/utils/mapActivity.d.ts +21 -0
- package/dist/esm/utils/mapActivity.d.ts.map +1 -0
- package/dist/esm/utils/mapActivity.js +45 -0
- package/dist/esm/utils/mapActivity.js.map +1 -0
- package/dist/esm/utils/mapFill.d.ts +12 -0
- package/dist/esm/utils/mapFill.d.ts.map +1 -0
- package/dist/esm/utils/mapFill.js +37 -0
- package/dist/esm/utils/mapFill.js.map +1 -0
- package/dist/esm/utils/mapOrder.d.ts +50 -0
- package/dist/esm/utils/mapOrder.d.ts.map +1 -0
- package/dist/esm/utils/mapOrder.js +155 -0
- package/dist/esm/utils/mapOrder.js.map +1 -0
- package/dist/esm/utils/mapPosition.d.ts +19 -0
- package/dist/esm/utils/mapPosition.d.ts.map +1 -0
- package/dist/esm/utils/mapPosition.js +32 -0
- package/dist/esm/utils/mapPosition.js.map +1 -0
- package/dist/esm/utils/ohlcvInterval.d.ts +11 -0
- package/dist/esm/utils/ohlcvInterval.d.ts.map +1 -0
- package/dist/esm/utils/ohlcvInterval.js +59 -0
- package/dist/esm/utils/ohlcvInterval.js.map +1 -0
- package/dist/esm/utils/ondoAsset.d.ts +4 -0
- package/dist/esm/utils/ondoAsset.d.ts.map +1 -0
- package/dist/esm/utils/ondoAsset.js +9 -0
- package/dist/esm/utils/ondoAsset.js.map +1 -0
- package/dist/esm/utils/orderFormatting.d.ts +33 -0
- package/dist/esm/utils/orderFormatting.d.ts.map +1 -0
- package/dist/esm/utils/orderFormatting.js +62 -0
- package/dist/esm/utils/orderFormatting.js.map +1 -0
- package/dist/esm/websocket/OndoWsProvider.d.ts +122 -0
- package/dist/esm/websocket/OndoWsProvider.d.ts.map +1 -0
- package/dist/esm/websocket/OndoWsProvider.js +591 -0
- package/dist/esm/websocket/OndoWsProvider.js.map +1 -0
- package/dist/types/OndoProvider.d.ts +39 -0
- package/dist/types/OndoProvider.d.ts.map +1 -0
- package/dist/types/accountConfig.d.ts +13 -0
- package/dist/types/accountConfig.d.ts.map +1 -0
- package/dist/types/accountSummary.d.ts +12 -0
- package/dist/types/accountSummary.d.ts.map +1 -0
- package/dist/types/auth/OndoTokenStore.d.ts +20 -0
- package/dist/types/auth/OndoTokenStore.d.ts.map +1 -0
- package/dist/types/auth/completeSiweLogin.d.ts +28 -0
- package/dist/types/auth/completeSiweLogin.d.ts.map +1 -0
- package/dist/types/auth/index.d.ts +4 -0
- package/dist/types/auth/index.d.ts.map +1 -0
- package/dist/types/auth/signActions.d.ts +35 -0
- package/dist/types/auth/signActions.d.ts.map +1 -0
- package/dist/types/constants.d.ts +35 -0
- package/dist/types/constants.d.ts.map +1 -0
- package/dist/types/index.d.ts +19 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/types/auth.d.ts +32 -0
- package/dist/types/types/auth.d.ts.map +1 -0
- package/dist/types/types/index.d.ts +4 -0
- package/dist/types/types/index.d.ts.map +1 -0
- package/dist/types/types/wire.d.ts +195 -0
- package/dist/types/types/wire.d.ts.map +1 -0
- package/dist/types/types/ws.d.ts +108 -0
- package/dist/types/types/ws.d.ts.map +1 -0
- package/dist/types/utils/activityCursor.d.ts +47 -0
- package/dist/types/utils/activityCursor.d.ts.map +1 -0
- package/dist/types/utils/apiClient.d.ts +90 -0
- package/dist/types/utils/apiClient.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +11 -0
- package/dist/types/utils/index.d.ts.map +1 -0
- package/dist/types/utils/liquidation.d.ts +20 -0
- package/dist/types/utils/liquidation.d.ts.map +1 -0
- package/dist/types/utils/mapActivity.d.ts +21 -0
- package/dist/types/utils/mapActivity.d.ts.map +1 -0
- package/dist/types/utils/mapFill.d.ts +12 -0
- package/dist/types/utils/mapFill.d.ts.map +1 -0
- package/dist/types/utils/mapOrder.d.ts +50 -0
- package/dist/types/utils/mapOrder.d.ts.map +1 -0
- package/dist/types/utils/mapPosition.d.ts +19 -0
- package/dist/types/utils/mapPosition.d.ts.map +1 -0
- package/dist/types/utils/ohlcvInterval.d.ts +11 -0
- package/dist/types/utils/ohlcvInterval.d.ts.map +1 -0
- package/dist/types/utils/ondoAsset.d.ts +4 -0
- package/dist/types/utils/ondoAsset.d.ts.map +1 -0
- package/dist/types/utils/orderFormatting.d.ts +33 -0
- package/dist/types/utils/orderFormatting.d.ts.map +1 -0
- package/dist/types/websocket/OndoWsProvider.d.ts +122 -0
- package/dist/types/websocket/OndoWsProvider.d.ts.map +1 -0
- package/package.json +64 -0
- package/src/OndoProvider.ts +635 -0
- package/src/accountConfig.ts +105 -0
- package/src/accountSummary.ts +22 -0
- package/src/auth/OndoTokenStore.ts +83 -0
- package/src/auth/completeSiweLogin.ts +43 -0
- package/src/auth/index.ts +12 -0
- package/src/auth/signActions.ts +160 -0
- package/src/constants.ts +38 -0
- package/src/index.ts +36 -0
- package/src/types/auth.ts +32 -0
- package/src/types/index.ts +5 -0
- package/src/types/wire.ts +228 -0
- package/src/types/ws.ts +79 -0
- package/src/utils/activityCursor.ts +140 -0
- package/src/utils/apiClient.ts +257 -0
- package/src/utils/index.ts +35 -0
- package/src/utils/liquidation.ts +37 -0
- package/src/utils/mapActivity.ts +61 -0
- package/src/utils/mapFill.ts +47 -0
- package/src/utils/mapOrder.ts +201 -0
- package/src/utils/mapPosition.ts +44 -0
- package/src/utils/ohlcvInterval.ts +69 -0
- package/src/utils/ondoAsset.ts +10 -0
- package/src/utils/orderFormatting.ts +70 -0
- package/src/websocket/OndoWsProvider.ts +752 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { PerpsError } from '@lifi/perps-sdk'
|
|
2
|
+
import type {
|
|
3
|
+
AccountConfig,
|
|
4
|
+
AccountConfigSetting,
|
|
5
|
+
OndoAccountConfig,
|
|
6
|
+
ProviderAction,
|
|
7
|
+
} from '@lifi/perps-types'
|
|
8
|
+
import { ActionType, PerpsErrorCode } from '@lifi/perps-types'
|
|
9
|
+
|
|
10
|
+
function assertNever(value: never): never {
|
|
11
|
+
throw new Error(
|
|
12
|
+
`Unreachable: exhaustiveness check failed for value ${JSON.stringify(value)}`
|
|
13
|
+
)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Project a single Ondo descriptor against the typed `OndoAccountConfig`
|
|
18
|
+
* into an `AccountConfigSetting`. Ondo has two setup descriptors:
|
|
19
|
+
* `SIWE_LOGIN`, satisfied by the presence of a live session token, and
|
|
20
|
+
* `SET_REFERRAL`, satisfied when a referral code is already applied to the
|
|
21
|
+
* account.
|
|
22
|
+
*
|
|
23
|
+
* The switch is exhaustive over `ActionType` so enum additions force a
|
|
24
|
+
* compile error in the `default` arm. ActionTypes that are not valid on
|
|
25
|
+
* `Provider.setup` / `Provider.options` throw at runtime.
|
|
26
|
+
*/
|
|
27
|
+
function projectOndoDescriptor(
|
|
28
|
+
descriptor: ProviderAction,
|
|
29
|
+
config: OndoAccountConfig
|
|
30
|
+
): AccountConfigSetting {
|
|
31
|
+
switch (descriptor.type) {
|
|
32
|
+
case ActionType.SIWE_LOGIN:
|
|
33
|
+
return {
|
|
34
|
+
type: descriptor.type,
|
|
35
|
+
values: [
|
|
36
|
+
{ name: 'authTokenExpiry', value: config.authTokenExpiry ?? null },
|
|
37
|
+
],
|
|
38
|
+
satisfied: config.loggedIn,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
case ActionType.SET_REFERRAL:
|
|
42
|
+
return {
|
|
43
|
+
type: descriptor.type,
|
|
44
|
+
values: [],
|
|
45
|
+
satisfied: config.referralSet,
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
case ActionType.APPROVE_AGENT:
|
|
49
|
+
case ActionType.APPROVE_BUILDER_FEE:
|
|
50
|
+
case ActionType.APPROVE_INTEGRATOR:
|
|
51
|
+
case ActionType.ACCOUNT_MODE:
|
|
52
|
+
case ActionType.ACCOUNT_TYPE:
|
|
53
|
+
case ActionType.SEND_ASSET:
|
|
54
|
+
case ActionType.WITHDRAWAL:
|
|
55
|
+
case ActionType.TRANSFER:
|
|
56
|
+
case ActionType.PLACE_ORDER:
|
|
57
|
+
case ActionType.PLACE_TRIGGER_ORDER:
|
|
58
|
+
case ActionType.CANCEL_ORDER:
|
|
59
|
+
case ActionType.CANCEL_ALL_ORDERS:
|
|
60
|
+
case ActionType.MODIFY_ORDER:
|
|
61
|
+
case ActionType.UPDATE_LEVERAGE:
|
|
62
|
+
case ActionType.UPDATE_POSITION_MARGIN:
|
|
63
|
+
case ActionType.UPDATE_ASSET_COLLATERAL:
|
|
64
|
+
case ActionType.REGISTER_API_KEY:
|
|
65
|
+
case ActionType.APPROVE_READ_ONLY_TOKEN:
|
|
66
|
+
case ActionType.DEPOSIT:
|
|
67
|
+
case ActionType.META_VOTE:
|
|
68
|
+
case ActionType.META_ACCEPT_TERMS:
|
|
69
|
+
throw new PerpsError(
|
|
70
|
+
PerpsErrorCode.SDKError,
|
|
71
|
+
`Ondo account-config mapper has no projection for descriptor type ` +
|
|
72
|
+
`'${descriptor.type}' — this ActionType is not valid on ` +
|
|
73
|
+
`Provider.setup / Provider.options for Ondo.`
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
default:
|
|
77
|
+
return assertNever(descriptor.type)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Project the union of Ondo setup + options descriptors against the typed
|
|
83
|
+
* `OndoAccountConfig`. Produces exactly one `AccountConfigSetting` per
|
|
84
|
+
* descriptor, in `setup`-then-`options` order.
|
|
85
|
+
*
|
|
86
|
+
* @param config Typed account state; a non-`ondo` config throws `SDKError`.
|
|
87
|
+
* @param setup `Provider.setup` array as emitted by `/providers`.
|
|
88
|
+
* @param options `Provider.options` array as emitted by `/providers`.
|
|
89
|
+
* @public
|
|
90
|
+
*/
|
|
91
|
+
export function projectOndoConfigSettings(
|
|
92
|
+
config: AccountConfig,
|
|
93
|
+
setup: ProviderAction[],
|
|
94
|
+
options: ProviderAction[]
|
|
95
|
+
): AccountConfigSetting[] {
|
|
96
|
+
if (config.provider !== 'ondo') {
|
|
97
|
+
throw new PerpsError(
|
|
98
|
+
PerpsErrorCode.SDKError,
|
|
99
|
+
`Ondo account-config mapper received a '${config.provider}' config.`
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
return [...setup, ...options].map((descriptor) =>
|
|
103
|
+
projectOndoDescriptor(descriptor, config)
|
|
104
|
+
)
|
|
105
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { summarizeAccount } from '@lifi/perps-sdk'
|
|
2
|
+
import type {
|
|
3
|
+
AccountResponse,
|
|
4
|
+
AccountSummary,
|
|
5
|
+
Position,
|
|
6
|
+
} from '@lifi/perps-types'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Roll an Ondo {@link AccountResponse} up into an {@link AccountSummary}.
|
|
10
|
+
* Ondo's collateral row holds `walletBalance`, which includes locked margin
|
|
11
|
+
* but not unrealized PnL (the positions carry it) — gross semantics: the
|
|
12
|
+
* venue counts unrealized PnL toward buying power
|
|
13
|
+
* (`availableMargin = marginBalance − usedMargin`).
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
export function getAccountSummary(
|
|
18
|
+
account: AccountResponse,
|
|
19
|
+
positions: Position[]
|
|
20
|
+
): AccountSummary {
|
|
21
|
+
return summarizeAccount(account, positions, 'gross')
|
|
22
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { readValidatedRecord, type StorageAdapter } from '@lifi/perps-sdk'
|
|
2
|
+
import type { Address } from 'viem'
|
|
3
|
+
import type { OndoAuthToken } from '../types/auth.js'
|
|
4
|
+
|
|
5
|
+
// Keys are namespaced by environment host as well as address: a production
|
|
6
|
+
// JWT is meaningless against the sandbox, and both stores may share one
|
|
7
|
+
// localStorage.
|
|
8
|
+
|
|
9
|
+
const STORAGE_PREFIX = 'lifi-perps-ondo-session'
|
|
10
|
+
|
|
11
|
+
const isOnAuthToken = (value: unknown): value is OndoAuthToken => {
|
|
12
|
+
if (typeof value !== 'object' || value === null) {
|
|
13
|
+
return false
|
|
14
|
+
}
|
|
15
|
+
const {
|
|
16
|
+
identifier,
|
|
17
|
+
authType,
|
|
18
|
+
accountId,
|
|
19
|
+
issuedAtSecs,
|
|
20
|
+
expirationSecs,
|
|
21
|
+
token,
|
|
22
|
+
newAccount,
|
|
23
|
+
} = value as Record<string, unknown>
|
|
24
|
+
return (
|
|
25
|
+
typeof identifier === 'string' &&
|
|
26
|
+
typeof authType === 'string' &&
|
|
27
|
+
typeof accountId === 'string' &&
|
|
28
|
+
typeof issuedAtSecs === 'number' &&
|
|
29
|
+
Number.isFinite(issuedAtSecs) &&
|
|
30
|
+
typeof expirationSecs === 'number' &&
|
|
31
|
+
Number.isFinite(expirationSecs) &&
|
|
32
|
+
typeof token === 'string' &&
|
|
33
|
+
token.length > 0 &&
|
|
34
|
+
typeof newAccount === 'boolean'
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const isExpired = (token: OndoAuthToken): boolean =>
|
|
39
|
+
token.expirationSecs * 1000 <= Date.now()
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Persists the Ondo session token per wallet address and environment via a
|
|
43
|
+
* `StorageAdapter`. An expired token reads back as absent (and is evicted),
|
|
44
|
+
* so callers can treat `null` uniformly as "run the SIWE login".
|
|
45
|
+
*
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
export class OndoTokenStore {
|
|
49
|
+
private readonly storage: StorageAdapter
|
|
50
|
+
private readonly host: string
|
|
51
|
+
|
|
52
|
+
constructor(storage: StorageAdapter, baseUrl: string) {
|
|
53
|
+
this.storage = storage
|
|
54
|
+
this.host = new URL(baseUrl).host
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private storageKey(address: Address): string {
|
|
58
|
+
return `${STORAGE_PREFIX}:${this.host}:${address.toLowerCase()}`
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Reads go straight to storage — no in-memory cache — so every store
|
|
62
|
+
// instance sharing a storage backend observes evictions immediately.
|
|
63
|
+
async get(address: Address): Promise<OndoAuthToken | null> {
|
|
64
|
+
const key = this.storageKey(address)
|
|
65
|
+
const parsed = await readValidatedRecord(this.storage, key, isOnAuthToken)
|
|
66
|
+
if (!parsed) {
|
|
67
|
+
return null
|
|
68
|
+
}
|
|
69
|
+
if (isExpired(parsed)) {
|
|
70
|
+
await this.remove(address)
|
|
71
|
+
return null
|
|
72
|
+
}
|
|
73
|
+
return parsed
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async set(address: Address, token: OndoAuthToken): Promise<void> {
|
|
77
|
+
await this.storage.set(this.storageKey(address), JSON.stringify(token))
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async remove(address: Address): Promise<void> {
|
|
81
|
+
await this.storage.remove(this.storageKey(address))
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { PerpsClientSigner } from '@lifi/perps-sdk'
|
|
2
|
+
import type { Hex } from 'viem'
|
|
3
|
+
import type { OndoAuthToken } from '../types/auth.js'
|
|
4
|
+
import type { OndoApiClient } from '../utils/apiClient.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* SIWE challenge to sign, as issued by Ondo's `get_challenge`.
|
|
8
|
+
*
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
export interface OndoSiweChallenge {
|
|
12
|
+
id: string
|
|
13
|
+
/** Plain-text ERC-4361 message; signed as-is via `personal_sign`. */
|
|
14
|
+
message: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Sign a SIWE challenge with the user's wallet and exchange it for an Ondo
|
|
19
|
+
* session token, directly against Ondo — the returned JWT never transits the
|
|
20
|
+
* LI.FI backend. viem's `signMessage` performs the EIP-191 `personal_sign`
|
|
21
|
+
* encoding Ondo verifies against. The signature is returned alongside the
|
|
22
|
+
* token so callers can hand it back as a `SiweSignedActionStep`.
|
|
23
|
+
*
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export async function completeSiweLogin(
|
|
27
|
+
client: OndoApiClient,
|
|
28
|
+
signer: PerpsClientSigner,
|
|
29
|
+
challenge: OndoSiweChallenge
|
|
30
|
+
): Promise<{ token: OndoAuthToken; signature: Hex }> {
|
|
31
|
+
const signature = await signer.signMessage({
|
|
32
|
+
account: signer.account,
|
|
33
|
+
message: challenge.message,
|
|
34
|
+
})
|
|
35
|
+
const token = await client.post<OndoAuthToken>(
|
|
36
|
+
'/v1/auth/erc-4361/login/complete_challenge',
|
|
37
|
+
{
|
|
38
|
+
id: challenge.id,
|
|
39
|
+
signature,
|
|
40
|
+
}
|
|
41
|
+
)
|
|
42
|
+
return { token, signature }
|
|
43
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// biome-ignore-all lint/performance/noBarrelFile: module public entry point.
|
|
2
|
+
|
|
3
|
+
export {
|
|
4
|
+
completeSiweLogin,
|
|
5
|
+
type OndoSiweChallenge,
|
|
6
|
+
} from './completeSiweLogin.js'
|
|
7
|
+
export { OndoTokenStore } from './OndoTokenStore.js'
|
|
8
|
+
export {
|
|
9
|
+
executeOndoRestCallActions,
|
|
10
|
+
type OndoSignActionsDeps,
|
|
11
|
+
ondoSignActions,
|
|
12
|
+
} from './signActions.js'
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { PerpsError, type SignActionsContext } from '@lifi/perps-sdk'
|
|
2
|
+
import type {
|
|
3
|
+
ActionResult,
|
|
4
|
+
ActionStep,
|
|
5
|
+
RestCallSignedActionStep,
|
|
6
|
+
SignedActionStep,
|
|
7
|
+
SiweActionStep,
|
|
8
|
+
} from '@lifi/perps-types'
|
|
9
|
+
import { PerpsErrorCode, SigningMethod } from '@lifi/perps-types'
|
|
10
|
+
import type { Address } from 'viem'
|
|
11
|
+
import {
|
|
12
|
+
type OndoApiClient,
|
|
13
|
+
type OndoHttpMethod,
|
|
14
|
+
OndoSessionExpiredError,
|
|
15
|
+
} from '../utils/apiClient.js'
|
|
16
|
+
import { completeSiweLogin } from './completeSiweLogin.js'
|
|
17
|
+
import type { OndoTokenStore } from './OndoTokenStore.js'
|
|
18
|
+
|
|
19
|
+
/** @internal */
|
|
20
|
+
export interface OndoSignActionsDeps {
|
|
21
|
+
client: OndoApiClient
|
|
22
|
+
tokenStore: OndoTokenStore
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const isSiweStep = (step: ActionStep): step is SiweActionStep => 'siwe' in step
|
|
26
|
+
|
|
27
|
+
const hasRequest = (
|
|
28
|
+
step: ActionStep
|
|
29
|
+
): step is Extract<ActionStep, { request: unknown }> => 'request' in step
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Ondo's `signActions` arms.
|
|
33
|
+
*
|
|
34
|
+
* `SIWE` signs the backend-built ERC-4361 challenge with the user's wallet
|
|
35
|
+
* and completes the login directly against Ondo — the returned session JWT is
|
|
36
|
+
* persisted in the token store and never transits the LI.FI backend.
|
|
37
|
+
*
|
|
38
|
+
* `AUTH_TOKEN` attaches the stored session JWT as an `Authorization: Bearer`
|
|
39
|
+
* header on each REST-call step; an absent or expired token throws
|
|
40
|
+
* {@link OndoSessionExpiredError} so callers re-run the SIWE login.
|
|
41
|
+
*
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
export async function ondoSignActions(
|
|
45
|
+
deps: OndoSignActionsDeps,
|
|
46
|
+
method: SigningMethod,
|
|
47
|
+
steps: ActionStep[],
|
|
48
|
+
address: Address,
|
|
49
|
+
ctx?: SignActionsContext
|
|
50
|
+
): Promise<SignedActionStep[]> {
|
|
51
|
+
switch (method) {
|
|
52
|
+
case SigningMethod.SIWE: {
|
|
53
|
+
const userWallet = ctx?.userWallet
|
|
54
|
+
if (userWallet === undefined) {
|
|
55
|
+
throw new PerpsError(
|
|
56
|
+
PerpsErrorCode.SDKError,
|
|
57
|
+
'Ondo SIWE login requires the user wallet. Pass `userWallet` when ' +
|
|
58
|
+
'creating the perps client.'
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
const signed: SignedActionStep[] = []
|
|
62
|
+
for (const step of steps) {
|
|
63
|
+
if (!isSiweStep(step)) {
|
|
64
|
+
throw new PerpsError(
|
|
65
|
+
PerpsErrorCode.SDKError,
|
|
66
|
+
`Ondo received a non-SIWE step ('${step.action}') under the SIWE signing method.`
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
const { token, signature } = await completeSiweLogin(
|
|
70
|
+
deps.client,
|
|
71
|
+
userWallet,
|
|
72
|
+
{ id: step.siwe.challengeId, message: step.siwe.message }
|
|
73
|
+
)
|
|
74
|
+
await deps.tokenStore.set(address, token)
|
|
75
|
+
signed.push({ action: step.action, siwe: step.siwe, signature })
|
|
76
|
+
}
|
|
77
|
+
return signed
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
case SigningMethod.AUTH_TOKEN: {
|
|
81
|
+
const token = await deps.tokenStore.get(address)
|
|
82
|
+
if (token === null) {
|
|
83
|
+
throw new OndoSessionExpiredError(
|
|
84
|
+
`No valid Ondo session token stored for ${address}. Run the SIWE login first.`
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
return steps.map((step): RestCallSignedActionStep => {
|
|
88
|
+
if (!hasRequest(step)) {
|
|
89
|
+
throw new PerpsError(
|
|
90
|
+
PerpsErrorCode.SDKError,
|
|
91
|
+
`Ondo received a non-REST step ('${step.action}') under the AUTH_TOKEN signing method.`
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
action: step.action,
|
|
96
|
+
request: step.request,
|
|
97
|
+
headers: { Authorization: `Bearer ${token.token}` },
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
default:
|
|
103
|
+
throw new PerpsError(
|
|
104
|
+
PerpsErrorCode.SDKError,
|
|
105
|
+
`Ondo does not sign via '${method}'. Supported methods: siwe, authToken.`
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Execute credential-bearing REST-call steps directly against Ondo,
|
|
112
|
+
* sequentially — later steps in a batch may depend on earlier ones (e.g.
|
|
113
|
+
* leverage update before order placement), so after the first failure the
|
|
114
|
+
* remainder is skipped rather than executed out of order.
|
|
115
|
+
*
|
|
116
|
+
* @public
|
|
117
|
+
*/
|
|
118
|
+
export async function executeOndoRestCallActions(
|
|
119
|
+
client: OndoApiClient,
|
|
120
|
+
steps: RestCallSignedActionStep[]
|
|
121
|
+
): Promise<ActionResult[]> {
|
|
122
|
+
const results: ActionResult[] = []
|
|
123
|
+
let failed = false
|
|
124
|
+
for (const step of steps) {
|
|
125
|
+
if (failed) {
|
|
126
|
+
results.push({
|
|
127
|
+
action: step.action,
|
|
128
|
+
success: false,
|
|
129
|
+
error: 'Skipped: a preceding step in the batch failed.',
|
|
130
|
+
})
|
|
131
|
+
continue
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
const result = await client.send<unknown>(
|
|
135
|
+
step.request.method as OndoHttpMethod,
|
|
136
|
+
step.request.path,
|
|
137
|
+
{ body: step.request.body, headers: step.headers }
|
|
138
|
+
)
|
|
139
|
+
const orderId =
|
|
140
|
+
typeof result === 'object' &&
|
|
141
|
+
result !== null &&
|
|
142
|
+
typeof (result as { orderId?: unknown }).orderId === 'string'
|
|
143
|
+
? (result as { orderId: string }).orderId
|
|
144
|
+
: undefined
|
|
145
|
+
results.push({
|
|
146
|
+
action: step.action,
|
|
147
|
+
success: true,
|
|
148
|
+
...(orderId === undefined ? {} : { orderId }),
|
|
149
|
+
})
|
|
150
|
+
} catch (err) {
|
|
151
|
+
failed = true
|
|
152
|
+
results.push({
|
|
153
|
+
action: step.action,
|
|
154
|
+
success: false,
|
|
155
|
+
error: err instanceof Error ? err.message : String(err),
|
|
156
|
+
})
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return results
|
|
160
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ondo provider key as it appears on `Provider.key` from the backend.
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export const ONDO_PROVIDER_KEY = 'ondo'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Default (production) REST base URL for Ondo Perps.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export const DEFAULT_ONDO_API_URL = 'https://api.ondoperps.xyz'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Sandbox REST base URL for Ondo Perps.
|
|
17
|
+
*
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export const ONDO_SANDBOX_API_URL = 'https://api.ondoperps-sandbox.xyz'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Default (production) WebSocket URL for Ondo Perps.
|
|
24
|
+
*
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export const DEFAULT_ONDO_WS_URL = 'wss://api.ondoperps.xyz/ws'
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Ondo's public base fee schedule (2 bps maker / 5 bps taker). Ondo exposes no
|
|
31
|
+
* per-account fee endpoint, so quotes and account snapshots use this schedule.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export const ONDO_BASE_FEE_TIER = {
|
|
36
|
+
maker: '0.0002',
|
|
37
|
+
taker: '0.0005',
|
|
38
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// biome-ignore-all lint/performance/noBarrelFile: package public entry point.
|
|
2
|
+
// biome-ignore-all lint/performance/noReExportAll: package public entry point.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Ondo Perps provider plugin for `@lifi/perps-sdk`. Ondo authenticates with a
|
|
6
|
+
* client-held session JWT obtained through a SIWE (ERC-4361) login — this
|
|
7
|
+
* package owns the venue HTTP boundary (`OndoApiClient`), the login completion
|
|
8
|
+
* (`completeSiweLogin`), and browser-side persistence of the session token
|
|
9
|
+
* (`OndoTokenStore`). The JWT never transits the LI.FI backend.
|
|
10
|
+
*
|
|
11
|
+
* @packageDocumentation
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export { projectOndoConfigSettings } from './accountConfig.js'
|
|
16
|
+
export { getAccountSummary } from './accountSummary.js'
|
|
17
|
+
export * from './auth/index.js'
|
|
18
|
+
export {
|
|
19
|
+
DEFAULT_ONDO_API_URL,
|
|
20
|
+
DEFAULT_ONDO_WS_URL,
|
|
21
|
+
ONDO_BASE_FEE_TIER,
|
|
22
|
+
ONDO_PROVIDER_KEY,
|
|
23
|
+
ONDO_SANDBOX_API_URL,
|
|
24
|
+
} from './constants.js'
|
|
25
|
+
export {
|
|
26
|
+
Ondo,
|
|
27
|
+
type OndoProviderOptions,
|
|
28
|
+
ondoProvider,
|
|
29
|
+
} from './OndoProvider.js'
|
|
30
|
+
export * from './types/index.js'
|
|
31
|
+
export * from './utils/index.js'
|
|
32
|
+
export {
|
|
33
|
+
OndoWsProvider,
|
|
34
|
+
type OndoWsProviderOptions,
|
|
35
|
+
ondoWsProvider,
|
|
36
|
+
} from './websocket/OndoWsProvider.js'
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Envelope wrapping every Ondo REST response: `result` is present on
|
|
3
|
+
* success, `error`/`error_code` on failure.
|
|
4
|
+
*
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export interface OndoGenericResponse<T> {
|
|
8
|
+
success: boolean
|
|
9
|
+
error?: string
|
|
10
|
+
/** Machine-readable failure discriminator (per-endpoint enum). */
|
|
11
|
+
error_code?: string
|
|
12
|
+
result?: T
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Ondo session credential returned by `complete_challenge`. `token` is the
|
|
17
|
+
* JWT presented as `Authorization: Bearer` on subsequent calls.
|
|
18
|
+
*
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
export interface OndoAuthToken {
|
|
22
|
+
identifier: string
|
|
23
|
+
authType: string
|
|
24
|
+
accountId: string
|
|
25
|
+
/** Absolute unix timestamp (seconds). */
|
|
26
|
+
issuedAtSecs: number
|
|
27
|
+
/** Absolute unix timestamp (seconds) — not a duration. */
|
|
28
|
+
expirationSecs: number
|
|
29
|
+
token: string
|
|
30
|
+
/** True when this login created the Ondo account. */
|
|
31
|
+
newAccount: boolean
|
|
32
|
+
}
|