@lifi/perps-sdk-provider-ondo 35.0.0 → 36.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/README.md +57 -0
- package/dist/cjs/OndoProvider.js +1 -1
- package/dist/cjs/OndoProvider.js.map +1 -1
- package/dist/cjs/auth/OndoTokenStore.d.ts +1 -1
- package/dist/cjs/auth/OndoTokenStore.d.ts.map +1 -1
- package/dist/cjs/auth/OndoTokenStore.js +9 -2
- package/dist/cjs/auth/OndoTokenStore.js.map +1 -1
- package/dist/cjs/types/wire.d.ts.map +1 -1
- package/dist/cjs/types/ws.d.ts +0 -4
- package/dist/cjs/types/ws.d.ts.map +1 -1
- package/dist/cjs/utils/buildFee.d.ts +4 -0
- package/dist/cjs/utils/buildFee.d.ts.map +1 -0
- package/dist/cjs/utils/buildFee.js +22 -0
- package/dist/cjs/utils/buildFee.js.map +1 -0
- package/dist/cjs/utils/mapFill.d.ts.map +1 -1
- package/dist/cjs/utils/mapFill.js +2 -6
- package/dist/cjs/utils/mapFill.js.map +1 -1
- package/dist/cjs/utils/mapPortfolioHistory.d.ts.map +1 -1
- package/dist/cjs/utils/mapPortfolioHistory.js +18 -3
- package/dist/cjs/utils/mapPortfolioHistory.js.map +1 -1
- package/dist/cjs/utils/mapPosition.d.ts.map +1 -1
- package/dist/cjs/utils/mapPosition.js +9 -2
- package/dist/cjs/utils/mapPosition.js.map +1 -1
- package/dist/cjs/websocket/OndoWsProvider.d.ts +4 -2
- package/dist/cjs/websocket/OndoWsProvider.d.ts.map +1 -1
- package/dist/cjs/websocket/OndoWsProvider.js +88 -49
- package/dist/cjs/websocket/OndoWsProvider.js.map +1 -1
- package/dist/esm/OndoProvider.js +1 -1
- package/dist/esm/OndoProvider.js.map +1 -1
- package/dist/esm/auth/OndoTokenStore.d.ts +1 -1
- package/dist/esm/auth/OndoTokenStore.d.ts.map +1 -1
- package/dist/esm/auth/OndoTokenStore.js +9 -2
- package/dist/esm/auth/OndoTokenStore.js.map +1 -1
- package/dist/esm/types/wire.d.ts +4 -0
- package/dist/esm/types/wire.d.ts.map +1 -1
- package/dist/esm/types/ws.d.ts +1 -5
- package/dist/esm/types/ws.d.ts.map +1 -1
- package/dist/esm/utils/buildFee.d.ts +8 -0
- package/dist/esm/utils/buildFee.d.ts.map +1 -0
- package/dist/esm/utils/buildFee.js +22 -0
- package/dist/esm/utils/buildFee.js.map +1 -0
- package/dist/esm/utils/mapFill.d.ts.map +1 -1
- package/dist/esm/utils/mapFill.js +2 -7
- package/dist/esm/utils/mapFill.js.map +1 -1
- package/dist/esm/utils/mapPortfolioHistory.d.ts.map +1 -1
- package/dist/esm/utils/mapPortfolioHistory.js +19 -4
- package/dist/esm/utils/mapPortfolioHistory.js.map +1 -1
- package/dist/esm/utils/mapPosition.d.ts +2 -6
- package/dist/esm/utils/mapPosition.d.ts.map +1 -1
- package/dist/esm/utils/mapPosition.js +12 -9
- package/dist/esm/utils/mapPosition.js.map +1 -1
- package/dist/esm/websocket/OndoWsProvider.d.ts +5 -3
- package/dist/esm/websocket/OndoWsProvider.d.ts.map +1 -1
- package/dist/esm/websocket/OndoWsProvider.js +94 -56
- package/dist/esm/websocket/OndoWsProvider.js.map +1 -1
- package/dist/types/auth/OndoTokenStore.d.ts +1 -1
- package/dist/types/auth/OndoTokenStore.d.ts.map +1 -1
- package/dist/types/types/wire.d.ts +4 -0
- package/dist/types/types/wire.d.ts.map +1 -1
- package/dist/types/types/ws.d.ts +1 -5
- package/dist/types/types/ws.d.ts.map +1 -1
- package/dist/types/utils/buildFee.d.ts +8 -0
- package/dist/types/utils/buildFee.d.ts.map +1 -0
- package/dist/types/utils/mapFill.d.ts.map +1 -1
- package/dist/types/utils/mapPortfolioHistory.d.ts.map +1 -1
- package/dist/types/utils/mapPosition.d.ts +2 -6
- package/dist/types/utils/mapPosition.d.ts.map +1 -1
- package/dist/types/websocket/OndoWsProvider.d.ts +5 -3
- package/dist/types/websocket/OndoWsProvider.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/OndoProvider.ts +1 -1
- package/src/auth/OndoTokenStore.ts +13 -2
- package/src/types/wire.ts +4 -0
- package/src/types/ws.ts +1 -2
- package/src/utils/buildFee.ts +35 -0
- package/src/utils/mapFill.ts +2 -10
- package/src/utils/mapPortfolioHistory.ts +29 -4
- package/src/utils/mapPosition.ts +14 -8
- package/src/websocket/OndoWsProvider.ts +132 -63
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
addDecimalString,
|
|
3
2
|
DecodeChain,
|
|
4
|
-
divideDecimalString,
|
|
5
3
|
getMarketRegistry,
|
|
6
4
|
isDecimalString,
|
|
7
5
|
localStorageAdapter,
|
|
@@ -65,10 +63,8 @@ import { intervalFromBarSpan, mapInterval } from '../utils/ohlcvInterval.js'
|
|
|
65
63
|
|
|
66
64
|
// Public channels (per market, demuxed from the frame's `market` field):
|
|
67
65
|
// orderbook → depthBooksPerps, trades → tradesPerps, candle → kLinePerps.
|
|
68
|
-
// `marketsContext`/`marketContext` merge
|
|
69
|
-
//
|
|
70
|
-
// `midPrice` is the top-of-book mid; the mark price stands in until a
|
|
71
|
-
// two-sided book arrives for the market.
|
|
66
|
+
// `marketsContext`/`marketContext` merge two wire channels — markPricesPerps
|
|
67
|
+
// and fundingRatesPerps — into the aggregated context emit, with no `midPrice`.
|
|
72
68
|
// Authenticated channels (account-wide, no market filter):
|
|
73
69
|
// orderUpdates → ordersPerps, fills → fillsPerps, positions → positionsPerps.
|
|
74
70
|
//
|
|
@@ -94,6 +90,9 @@ const ONDO_AUTH_FANOUT: readonly string[] = [
|
|
|
94
90
|
'accountSummary',
|
|
95
91
|
]
|
|
96
92
|
|
|
93
|
+
/** Bound on the venue's answer to a `login` op. An unmapped error leaves no other exit. */
|
|
94
|
+
const ONDO_LOGIN_ACK_TIMEOUT_MS = 10_000
|
|
95
|
+
|
|
97
96
|
const isAuthChannel = (
|
|
98
97
|
channel: Subscription['channel']
|
|
99
98
|
): channel is AuthChannel => channel in ONDO_AUTH_CHANNEL
|
|
@@ -170,12 +169,21 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
170
169
|
string,
|
|
171
170
|
{ rate: string; nextFundingTime: number }
|
|
172
171
|
>()
|
|
173
|
-
/** Latest two-sided top-of-book mid, keyed by venue market symbol. */
|
|
174
|
-
private bookMids = new Map<string, string>()
|
|
175
172
|
|
|
176
173
|
/** The single address this connection's login is bound to. */
|
|
177
174
|
private accountAddress: string | undefined
|
|
178
175
|
private loginPromise: Promise<void> | undefined
|
|
176
|
+
private loginAttempt:
|
|
177
|
+
| {
|
|
178
|
+
address: Address
|
|
179
|
+
token: string
|
|
180
|
+
accepted: boolean
|
|
181
|
+
timer: ReturnType<typeof setTimeout>
|
|
182
|
+
resolve: () => void
|
|
183
|
+
reject: (error: unknown) => void
|
|
184
|
+
}
|
|
185
|
+
| undefined
|
|
186
|
+
private readonly rejectedTokens = new Set<string>()
|
|
179
187
|
private authGeneration = 0
|
|
180
188
|
private authBindingRefs = 0
|
|
181
189
|
private closeGeneration = 0
|
|
@@ -200,6 +208,9 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
200
208
|
// The venue's login lives and dies with the connection, so a drop must
|
|
201
209
|
// forget both the in-flight login and the address it bound.
|
|
202
210
|
this.rws.on('close', () => {
|
|
211
|
+
this.takeLoginAttempt()?.reject(
|
|
212
|
+
new Error('Ondo WebSocket closed during login')
|
|
213
|
+
)
|
|
203
214
|
this.loginPromise = undefined
|
|
204
215
|
this.accountAddress = undefined
|
|
205
216
|
})
|
|
@@ -478,8 +489,10 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
478
489
|
protected override onClose(): void {
|
|
479
490
|
this.contexts = {}
|
|
480
491
|
this.pendingFunding.clear()
|
|
481
|
-
this.bookMids.clear()
|
|
482
492
|
this.loginPromise = undefined
|
|
493
|
+
this.takeLoginAttempt()?.reject(
|
|
494
|
+
new Error('Ondo WebSocket closed during login')
|
|
495
|
+
)
|
|
483
496
|
this.accountAddress = undefined
|
|
484
497
|
this.authGeneration += 1
|
|
485
498
|
this.closeGeneration += 1
|
|
@@ -501,10 +514,6 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
501
514
|
'fundingRatesPerps',
|
|
502
515
|
{ frame: { channel: 'fundingRatesPerps' }, needsAuth: false },
|
|
503
516
|
],
|
|
504
|
-
[
|
|
505
|
-
'topOfBooksPerps',
|
|
506
|
-
{ frame: { channel: 'topOfBooksPerps' }, needsAuth: false },
|
|
507
|
-
],
|
|
508
517
|
]
|
|
509
518
|
case 'marketContext':
|
|
510
519
|
return [
|
|
@@ -522,13 +531,6 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
522
531
|
needsAuth: false,
|
|
523
532
|
},
|
|
524
533
|
],
|
|
525
|
-
[
|
|
526
|
-
`topOfBooksPerps:${sub.marketId}`,
|
|
527
|
-
{
|
|
528
|
-
frame: { channel: 'topOfBooksPerps', markets: [sub.marketId] },
|
|
529
|
-
needsAuth: false,
|
|
530
|
-
},
|
|
531
|
-
],
|
|
532
534
|
]
|
|
533
535
|
case 'orderbook':
|
|
534
536
|
return [
|
|
@@ -620,6 +622,12 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
620
622
|
`No Ondo session for ${address}. Run the SIWE login before subscribing to account channels.`
|
|
621
623
|
)
|
|
622
624
|
}
|
|
625
|
+
if (this.rejectedTokens.has(token.token)) {
|
|
626
|
+
throw ondoSessionRejectedError(
|
|
627
|
+
'WebSocket subscription',
|
|
628
|
+
new OndoSessionExpiredError('Ondo rejected the session token')
|
|
629
|
+
)
|
|
630
|
+
}
|
|
623
631
|
return token
|
|
624
632
|
}
|
|
625
633
|
|
|
@@ -632,11 +640,36 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
632
640
|
if (this.loginPromise === undefined) {
|
|
633
641
|
const login: Promise<void> = this.requireSession(address).then(
|
|
634
642
|
(token) => {
|
|
635
|
-
if (this.loginPromise
|
|
643
|
+
if (this.loginPromise !== login) {
|
|
644
|
+
return
|
|
645
|
+
}
|
|
646
|
+
return new Promise<void>((resolve, reject) => {
|
|
636
647
|
this.rws.send(
|
|
637
648
|
JSON.stringify({ op: 'login', args: { token: token.token } })
|
|
638
649
|
)
|
|
639
|
-
|
|
650
|
+
const attempt = {
|
|
651
|
+
address,
|
|
652
|
+
token: token.token,
|
|
653
|
+
accepted: false,
|
|
654
|
+
timer: setTimeout(() => {
|
|
655
|
+
if (this.loginAttempt !== attempt) {
|
|
656
|
+
return
|
|
657
|
+
}
|
|
658
|
+
this.rejectLogin(
|
|
659
|
+
new PerpsError(
|
|
660
|
+
PerpsErrorCode.TimeoutError,
|
|
661
|
+
'Ondo did not acknowledge the WebSocket login.'
|
|
662
|
+
),
|
|
663
|
+
false
|
|
664
|
+
)
|
|
665
|
+
// The venue may still hold a half-open login on this socket.
|
|
666
|
+
this.rws.reconnect()
|
|
667
|
+
}, ONDO_LOGIN_ACK_TIMEOUT_MS),
|
|
668
|
+
resolve,
|
|
669
|
+
reject,
|
|
670
|
+
}
|
|
671
|
+
this.loginAttempt = attempt
|
|
672
|
+
})
|
|
640
673
|
}
|
|
641
674
|
)
|
|
642
675
|
this.loginPromise = login
|
|
@@ -726,7 +759,7 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
726
759
|
if (!(err instanceof OndoSessionExpiredError)) {
|
|
727
760
|
throw err
|
|
728
761
|
}
|
|
729
|
-
await this.tokenStore.remove(address)
|
|
762
|
+
await this.tokenStore.remove(address, token.token)
|
|
730
763
|
throw ondoSessionRejectedError('account summary', err)
|
|
731
764
|
}
|
|
732
765
|
return {
|
|
@@ -756,6 +789,50 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
756
789
|
})
|
|
757
790
|
}
|
|
758
791
|
|
|
792
|
+
/** Detach the pending login attempt, if any, and stop its acknowledgement timer. */
|
|
793
|
+
private takeLoginAttempt(): typeof this.loginAttempt {
|
|
794
|
+
const attempt = this.loginAttempt
|
|
795
|
+
if (attempt !== undefined) {
|
|
796
|
+
clearTimeout(attempt.timer)
|
|
797
|
+
this.loginAttempt = undefined
|
|
798
|
+
}
|
|
799
|
+
return attempt
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
private rejectLogin(error: PerpsError, unauthorized: boolean): void {
|
|
803
|
+
const attempt = this.takeLoginAttempt()
|
|
804
|
+
if (attempt === undefined) {
|
|
805
|
+
wsLog.serverError(
|
|
806
|
+
this.providerKey,
|
|
807
|
+
`Login error without a pending login: ${error.message}`
|
|
808
|
+
)
|
|
809
|
+
return
|
|
810
|
+
}
|
|
811
|
+
this.loginPromise = undefined
|
|
812
|
+
const address = attempt.address.toLowerCase()
|
|
813
|
+
if (unauthorized) {
|
|
814
|
+
this.rejectedTokens.add(attempt.token)
|
|
815
|
+
void this.tokenStore
|
|
816
|
+
.remove(attempt.address, attempt.token)
|
|
817
|
+
.catch((cause) => wsLog.handlerFailure(this.providerKey, cause))
|
|
818
|
+
}
|
|
819
|
+
for (const [key, state] of this.wireSubs) {
|
|
820
|
+
if (state.needsAuth) {
|
|
821
|
+
this.unregisterSub(key)
|
|
822
|
+
this.rws.send(JSON.stringify({ op: 'unsubscribe', ...state.frame }))
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
this.authWireRefs.clear()
|
|
826
|
+
this.authBindingRefs = 0
|
|
827
|
+
this.authGeneration += 1
|
|
828
|
+
this.accountAddress = undefined
|
|
829
|
+
this.clearAccountSummary()
|
|
830
|
+
attempt.reject(error)
|
|
831
|
+
for (const channel of ONDO_AUTH_FANOUT) {
|
|
832
|
+
this.rejectChannel(`${channel}:${address}`, error)
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
|
|
759
836
|
protected handleMessage(raw: string): void {
|
|
760
837
|
let msg: OndoWsMessage
|
|
761
838
|
try {
|
|
@@ -764,8 +841,38 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
764
841
|
wsLog.parseFailure(this.providerKey, raw)
|
|
765
842
|
return
|
|
766
843
|
}
|
|
844
|
+
if (msg.type === 'loggedIn') {
|
|
845
|
+
if (this.loginAttempt !== undefined) {
|
|
846
|
+
clearTimeout(this.loginAttempt.timer)
|
|
847
|
+
this.loginAttempt.accepted = true
|
|
848
|
+
this.loginAttempt.resolve()
|
|
849
|
+
}
|
|
850
|
+
return
|
|
851
|
+
}
|
|
767
852
|
if (msg.type === 'error') {
|
|
768
|
-
|
|
853
|
+
if (msg.msg?.startsWith('error parsing jwt: ')) {
|
|
854
|
+
this.rejectLogin(
|
|
855
|
+
ondoSessionRejectedError(
|
|
856
|
+
'WebSocket subscription',
|
|
857
|
+
new OndoSessionExpiredError('Ondo rejected the session token')
|
|
858
|
+
),
|
|
859
|
+
true
|
|
860
|
+
)
|
|
861
|
+
return
|
|
862
|
+
}
|
|
863
|
+
if (
|
|
864
|
+
this.loginAttempt !== undefined &&
|
|
865
|
+
!this.loginAttempt.accepted &&
|
|
866
|
+
(msg.msg === 'already logged in on this connection' ||
|
|
867
|
+
msg.msg === 'account already has too many connections')
|
|
868
|
+
) {
|
|
869
|
+
this.rejectLogin(
|
|
870
|
+
new PerpsError(PerpsErrorCode.ThirdPartyError, msg.msg),
|
|
871
|
+
false
|
|
872
|
+
)
|
|
873
|
+
return
|
|
874
|
+
}
|
|
875
|
+
wsLog.serverError(this.providerKey, 'Venue rejected a WebSocket request')
|
|
769
876
|
return
|
|
770
877
|
}
|
|
771
878
|
if (msg.type !== 'update') {
|
|
@@ -776,9 +883,6 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
776
883
|
case 'depthBooksPerps':
|
|
777
884
|
this.handleBooks(msg.data as OndoBookSnapshot[])
|
|
778
885
|
break
|
|
779
|
-
case 'topOfBooksPerps':
|
|
780
|
-
this.handleTopOfBooks(msg.data as OndoBookSnapshot[])
|
|
781
|
-
break
|
|
782
886
|
case 'tradesPerps':
|
|
783
887
|
this.handleTrades(msg.data as OndoWsTrade[])
|
|
784
888
|
break
|
|
@@ -856,36 +960,6 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
856
960
|
}
|
|
857
961
|
}
|
|
858
962
|
|
|
859
|
-
private handleTopOfBooks(snapshots: OndoBookSnapshot[]): void {
|
|
860
|
-
const touched: string[] = []
|
|
861
|
-
for (const { market, bids, asks } of snapshots) {
|
|
862
|
-
const bestBid = this.toBookLevels(market, bids, -1)[0]
|
|
863
|
-
const bestAsk = this.toBookLevels(market, asks, 1)[0]
|
|
864
|
-
if (bestBid === undefined || bestAsk === undefined) {
|
|
865
|
-
this.bookMids.delete(market)
|
|
866
|
-
} else {
|
|
867
|
-
this.bookMids.set(
|
|
868
|
-
market,
|
|
869
|
-
divideDecimalString(
|
|
870
|
-
addDecimalString(bestBid.price, bestAsk.price),
|
|
871
|
-
'2'
|
|
872
|
-
)
|
|
873
|
-
)
|
|
874
|
-
}
|
|
875
|
-
const context = this.contexts[market]
|
|
876
|
-
if (context !== undefined) {
|
|
877
|
-
this.contexts[market] = {
|
|
878
|
-
...context,
|
|
879
|
-
midPrice: this.bookMids.get(market) ?? context.markPrice,
|
|
880
|
-
}
|
|
881
|
-
touched.push(market)
|
|
882
|
-
}
|
|
883
|
-
}
|
|
884
|
-
if (touched.length > 0) {
|
|
885
|
-
this.emitContexts(touched)
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
|
|
889
963
|
private handleTrades(trades: OndoWsTrade[]): void {
|
|
890
964
|
const byMarket = new Map<string, OndoWsTrade[]>()
|
|
891
965
|
for (const trade of trades) {
|
|
@@ -936,8 +1010,6 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
936
1010
|
...this.contexts[market],
|
|
937
1011
|
provider: this.providerKey,
|
|
938
1012
|
marketId: market,
|
|
939
|
-
// The mark price stands in until a two-sided top-of-book arrives.
|
|
940
|
-
midPrice: this.bookMids.get(market) ?? markPrice,
|
|
941
1013
|
markPrice,
|
|
942
1014
|
oraclePrice,
|
|
943
1015
|
}
|
|
@@ -969,10 +1041,7 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
|
|
|
969
1041
|
}
|
|
970
1042
|
|
|
971
1043
|
private emitContexts(markets: string[]): void {
|
|
972
|
-
this.
|
|
973
|
-
channel: 'marketsContext',
|
|
974
|
-
data: { ...this.contexts },
|
|
975
|
-
})
|
|
1044
|
+
this.emitMarketsContext(() => ({ ...this.contexts }))
|
|
976
1045
|
for (const market of markets) {
|
|
977
1046
|
const context = this.contexts[market]
|
|
978
1047
|
if (context !== undefined) {
|