@drift-labs/sdk 0.1.36-master.7 → 0.2.0-master.10
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/lib/accounts/bulkAccountLoader.d.ts +6 -6
- package/lib/accounts/bulkAccountLoader.js +81 -95
- package/lib/accounts/bulkUserSubscription.js +13 -57
- package/lib/accounts/fetch.d.ts +4 -0
- package/lib/accounts/fetch.js +18 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +34 -38
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +224 -224
- package/lib/accounts/pollingOracleSubscriber.d.ts +3 -3
- package/lib/accounts/pollingOracleSubscriber.js +37 -49
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
- package/lib/accounts/pollingTokenAccountSubscriber.js +35 -50
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +7 -13
- package/lib/accounts/pollingUserAccountSubscriber.js +71 -134
- package/lib/accounts/types.d.ts +34 -41
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -4
- package/lib/accounts/webSocketAccountSubscriber.js +39 -35
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +33 -28
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +153 -185
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +5 -11
- package/lib/accounts/webSocketUserAccountSubscriber.js +22 -67
- package/lib/addresses/marketAddresses.d.ts +4 -0
- package/lib/addresses/marketAddresses.js +15 -0
- package/lib/addresses/pda.d.ts +12 -0
- package/lib/addresses/pda.js +83 -0
- package/lib/admin.d.ts +12 -18
- package/lib/admin.js +398 -558
- package/lib/clearingHouse.d.ts +83 -109
- package/lib/clearingHouse.js +832 -893
- package/lib/clearingHouseConfig.d.ts +34 -0
- package/lib/clearingHouseConfig.js +2 -0
- package/lib/clearingHouseUser.d.ts +19 -22
- package/lib/clearingHouseUser.js +155 -141
- package/lib/clearingHouseUserConfig.d.ts +14 -0
- package/lib/clearingHouseUserConfig.js +2 -0
- package/lib/config.d.ts +12 -0
- package/lib/config.js +35 -4
- package/lib/constants/banks.d.ts +16 -0
- package/lib/constants/banks.js +41 -0
- package/lib/constants/markets.d.ts +8 -3
- package/lib/constants/markets.js +13 -206
- package/lib/constants/numericConstants.d.ts +17 -0
- package/lib/constants/numericConstants.js +23 -6
- package/lib/events/eventList.d.ts +22 -0
- package/lib/events/eventList.js +77 -0
- package/lib/events/eventSubscriber.d.ts +34 -0
- package/lib/events/eventSubscriber.js +126 -0
- package/lib/events/fetchLogs.d.ts +13 -0
- package/lib/events/fetchLogs.js +39 -0
- package/lib/events/pollingLogProvider.d.ts +15 -0
- package/lib/events/pollingLogProvider.js +53 -0
- package/lib/events/sort.d.ts +2 -0
- package/lib/events/sort.js +44 -0
- package/lib/events/txEventCache.d.ts +24 -0
- package/lib/events/txEventCache.js +71 -0
- package/lib/events/types.d.ts +49 -0
- package/lib/events/types.js +20 -0
- package/lib/events/webSocketLogProvider.d.ts +12 -0
- package/lib/events/webSocketLogProvider.js +30 -0
- package/lib/examples/makeTradeExample.js +26 -27
- package/lib/factory/bigNum.d.ts +118 -0
- package/lib/factory/bigNum.js +364 -0
- package/lib/factory/oracleClient.d.ts +1 -2
- package/lib/factory/oracleClient.js +6 -2
- package/lib/idl/clearing_house.json +2258 -2774
- package/lib/index.d.ts +14 -5
- package/lib/index.js +18 -5
- package/lib/math/amm.d.ts +24 -29
- package/lib/math/amm.js +209 -176
- package/lib/math/auction.d.ts +5 -0
- package/lib/math/auction.js +42 -0
- package/lib/math/bankBalance.d.ts +9 -0
- package/lib/math/bankBalance.js +75 -0
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/conversion.js +1 -5
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +158 -175
- package/lib/math/market.d.ts +6 -6
- package/lib/math/market.js +10 -9
- package/lib/math/orders.d.ts +6 -1
- package/lib/math/orders.js +38 -1
- package/lib/math/position.d.ts +7 -5
- package/lib/math/position.js +29 -27
- package/lib/math/repeg.d.ts +22 -0
- package/lib/math/repeg.js +128 -0
- package/lib/math/trade.d.ts +5 -4
- package/lib/math/trade.js +29 -24
- package/lib/mockUSDCFaucet.js +87 -116
- package/lib/oracles/oracleClientCache.d.ts +8 -0
- package/lib/oracles/oracleClientCache.js +19 -0
- package/lib/oracles/pythClient.d.ts +3 -5
- package/lib/oracles/pythClient.js +12 -31
- package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
- package/lib/oracles/quoteAssetOracleClient.js +21 -0
- package/lib/oracles/switchboardClient.d.ts +3 -5
- package/lib/oracles/switchboardClient.js +29 -50
- package/lib/oracles/types.d.ts +6 -1
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +8 -96
- package/lib/orders.d.ts +6 -7
- package/lib/orders.js +11 -89
- package/lib/slot/SlotSubscriber.d.ts +19 -0
- package/lib/slot/SlotSubscriber.js +26 -0
- package/lib/tx/retryTxSender.d.ts +2 -2
- package/lib/tx/retryTxSender.js +108 -123
- package/lib/tx/types.d.ts +5 -1
- package/lib/tx/utils.d.ts +1 -1
- package/lib/tx/utils.js +11 -2
- package/lib/types.d.ts +180 -110
- package/lib/types.js +54 -1
- package/lib/userName.d.ts +4 -0
- package/lib/userName.js +20 -0
- package/lib/util/computeUnits.js +10 -21
- package/lib/util/tps.js +11 -22
- package/lib/wallet.js +7 -20
- package/package.json +11 -4
- package/src/accounts/bulkAccountLoader.js +197 -0
- package/src/accounts/bulkAccountLoader.ts +21 -15
- package/src/accounts/bulkUserSubscription.js +33 -0
- package/src/accounts/bulkUserSubscription.ts +1 -45
- package/src/accounts/fetch.js +29 -0
- package/src/accounts/fetch.ts +33 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
- package/src/accounts/pollingOracleSubscriber.js +93 -0
- package/src/accounts/pollingOracleSubscriber.ts +16 -8
- package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
- package/src/accounts/pollingUserAccountSubscriber.js +132 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
- package/src/accounts/types.js +10 -0
- package/src/accounts/types.ts +41 -70
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +93 -0
- package/src/accounts/webSocketAccountSubscriber.ts +33 -16
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
- package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
- package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/marketAddresses.ts +18 -0
- package/{lib/addresses.js → src/addresses/pda.js} +45 -34
- package/src/addresses/pda.ts +118 -0
- package/src/admin.ts +247 -336
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +1023 -1026
- package/src/clearingHouseConfig.ts +37 -0
- package/src/clearingHouseUser.ts +275 -185
- package/src/clearingHouseUserConfig.ts +18 -0
- package/src/config.ts +54 -1
- package/src/constants/banks.ts +50 -0
- package/src/constants/markets.ts +16 -207
- package/src/constants/numericConstants.ts +34 -5
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +94 -0
- package/src/events/eventSubscriber.js +139 -0
- package/src/events/eventSubscriber.ts +194 -0
- package/src/events/fetchLogs.js +50 -0
- package/src/events/fetchLogs.ts +80 -0
- package/src/events/pollingLogProvider.js +64 -0
- package/src/events/pollingLogProvider.ts +79 -0
- package/src/events/sort.js +44 -0
- package/src/events/sort.ts +65 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/txEventCache.ts +74 -0
- package/src/events/types.js +20 -0
- package/src/events/types.ts +98 -0
- package/src/events/webSocketLogProvider.js +41 -0
- package/src/events/webSocketLogProvider.ts +38 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/examples/makeTradeExample.ts +20 -11
- package/src/factory/bigNum.js +364 -0
- package/src/factory/bigNum.ts +524 -0
- package/src/factory/oracleClient.js +20 -0
- package/src/factory/oracleClient.ts +7 -4
- package/src/idl/clearing_house.json +2258 -2774
- package/src/index.js +69 -0
- package/src/index.ts +14 -5
- package/src/math/amm.js +369 -0
- package/src/math/amm.ts +382 -243
- package/src/math/auction.js +42 -0
- package/src/math/auction.ts +43 -0
- package/src/math/bankBalance.js +75 -0
- package/src/math/bankBalance.ts +112 -0
- package/src/math/conversion.js +11 -0
- package/src/math/conversion.ts +1 -11
- package/src/math/funding.js +248 -0
- package/src/math/funding.ts +12 -9
- package/src/math/market.js +57 -0
- package/src/math/market.ts +37 -30
- package/src/math/oracles.js +26 -0
- package/src/math/orders.js +110 -0
- package/src/math/orders.ts +43 -1
- package/src/math/position.js +140 -0
- package/src/math/position.ts +52 -35
- package/src/math/repeg.js +128 -0
- package/src/math/repeg.ts +176 -0
- package/src/math/state.js +15 -0
- package/src/math/trade.js +253 -0
- package/src/math/trade.ts +55 -47
- package/src/math/utils.js +26 -0
- package/src/math/utils.js.map +1 -0
- package/src/mockUSDCFaucet.js +171 -0
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/oracleClientCache.ts +20 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/pythClient.ts +4 -11
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/quoteAssetOracleClient.ts +25 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/switchboardClient.ts +11 -24
- package/src/oracles/types.js +2 -0
- package/src/oracles/types.ts +7 -1
- package/src/orderParams.js +20 -0
- package/src/orderParams.ts +20 -141
- package/src/orders.js +134 -0
- package/src/orders.ts +25 -134
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/slot/SlotSubscriber.ts +42 -0
- package/src/token/index.js +38 -0
- package/src/tx/retryTxSender.js +188 -0
- package/src/tx/retryTxSender.ts +6 -4
- package/src/tx/types.js +2 -0
- package/src/tx/types.ts +6 -1
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +22 -3
- package/src/types.js +114 -0
- package/src/types.ts +176 -124
- package/src/userName.js +20 -0
- package/src/userName.ts +20 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/tests/bn/test.ts +255 -0
- package/tsconfig.json +12 -12
- package/lib/addresses.d.ts +0 -10
- package/lib/constants/accounts.d.ts +0 -15
- package/lib/constants/accounts.js +0 -18
- package/lib/factory/clearingHouse.d.ts +0 -35
- package/lib/factory/clearingHouse.js +0 -81
- package/lib/factory/clearingHouseUser.d.ts +0 -19
- package/lib/factory/clearingHouseUser.js +0 -34
- package/lib/math/insuranceFund.d.ts +0 -15
- package/lib/math/insuranceFund.js +0 -33
- package/lib/settlement.d.ts +0 -4
- package/lib/settlement.js +0 -10
- package/lib/tx/defaultTxSender.d.ts +0 -8
- package/lib/tx/defaultTxSender.js +0 -12
- package/src/addresses.ts +0 -82
- package/src/constants/accounts.ts +0 -26
- package/src/factory/clearingHouse.ts +0 -173
- package/src/factory/clearingHouseUser.ts +0 -73
- package/src/math/insuranceFund.ts +0 -29
- package/src/settlement.ts +0 -9
- package/src/tx/defaultTxSender.ts +0 -24
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.PollingTokenAccountSubscriber = void 0;
|
|
13
4
|
const types_1 = require("./types");
|
|
@@ -20,33 +11,31 @@ class PollingTokenAccountSubscriber {
|
|
|
20
11
|
this.accountLoader = accountLoader;
|
|
21
12
|
this.eventEmitter = new events_1.EventEmitter();
|
|
22
13
|
}
|
|
23
|
-
subscribe() {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return subscriptionSucceeded;
|
|
41
|
-
});
|
|
14
|
+
async subscribe() {
|
|
15
|
+
if (this.isSubscribed) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
this.addToAccountLoader();
|
|
19
|
+
let subscriptionSucceeded = false;
|
|
20
|
+
let retries = 0;
|
|
21
|
+
while (!subscriptionSucceeded && retries < 5) {
|
|
22
|
+
await this.fetch();
|
|
23
|
+
subscriptionSucceeded = this.didSubscriptionSucceed();
|
|
24
|
+
retries++;
|
|
25
|
+
}
|
|
26
|
+
if (subscriptionSucceeded) {
|
|
27
|
+
this.eventEmitter.emit('update');
|
|
28
|
+
}
|
|
29
|
+
this.isSubscribed = subscriptionSucceeded;
|
|
30
|
+
return subscriptionSucceeded;
|
|
42
31
|
}
|
|
43
32
|
addToAccountLoader() {
|
|
44
33
|
if (this.callbackId) {
|
|
45
34
|
return;
|
|
46
35
|
}
|
|
47
|
-
this.callbackId = this.accountLoader.addAccount(this.publicKey, (buffer) => {
|
|
36
|
+
this.callbackId = this.accountLoader.addAccount(this.publicKey, (buffer, slot) => {
|
|
48
37
|
const tokenAccount = (0, token_1.parseTokenAccount)(buffer);
|
|
49
|
-
this.
|
|
38
|
+
this.tokenAccountAndSlot = { data: tokenAccount, slot };
|
|
50
39
|
// @ts-ignore
|
|
51
40
|
this.eventEmitter.emit('tokenAccountUpdate', tokenAccount);
|
|
52
41
|
this.eventEmitter.emit('update');
|
|
@@ -55,36 +44,32 @@ class PollingTokenAccountSubscriber {
|
|
|
55
44
|
this.eventEmitter.emit('error', error);
|
|
56
45
|
});
|
|
57
46
|
}
|
|
58
|
-
fetch() {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
this.tokenAccount = (0, token_1.parseTokenAccount)(buffer);
|
|
63
|
-
});
|
|
47
|
+
async fetch() {
|
|
48
|
+
await this.accountLoader.load();
|
|
49
|
+
const { buffer, slot } = this.accountLoader.getBufferAndSlot(this.publicKey);
|
|
50
|
+
this.tokenAccountAndSlot = { data: (0, token_1.parseTokenAccount)(buffer), slot };
|
|
64
51
|
}
|
|
65
|
-
unsubscribe() {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
this.isSubscribed = false;
|
|
75
|
-
});
|
|
52
|
+
async unsubscribe() {
|
|
53
|
+
if (!this.isSubscribed) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
this.accountLoader.removeAccount(this.publicKey, this.callbackId);
|
|
57
|
+
this.callbackId = undefined;
|
|
58
|
+
this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
|
|
59
|
+
this.errorCallbackId = undefined;
|
|
60
|
+
this.isSubscribed = false;
|
|
76
61
|
}
|
|
77
62
|
assertIsSubscribed() {
|
|
78
63
|
if (!this.isSubscribed) {
|
|
79
64
|
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
80
65
|
}
|
|
81
66
|
}
|
|
82
|
-
|
|
67
|
+
getTokenAccountAndSlot() {
|
|
83
68
|
this.assertIsSubscribed();
|
|
84
|
-
return this.
|
|
69
|
+
return this.tokenAccountAndSlot;
|
|
85
70
|
}
|
|
86
71
|
didSubscriptionSucceed() {
|
|
87
|
-
return !!this.
|
|
72
|
+
return !!this.tokenAccountAndSlot;
|
|
88
73
|
}
|
|
89
74
|
}
|
|
90
75
|
exports.PollingTokenAccountSubscriber = PollingTokenAccountSubscriber;
|
|
@@ -1,33 +1,27 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { AccountToPoll, UserAccountEvents, UserAccountSubscriber
|
|
2
|
+
import { DataAndSlot, AccountToPoll, UserAccountEvents, UserAccountSubscriber } from './types';
|
|
3
3
|
import { Program } from '@project-serum/anchor';
|
|
4
4
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
5
|
import { EventEmitter } from 'events';
|
|
6
6
|
import { PublicKey } from '@solana/web3.js';
|
|
7
|
-
import { UserAccount
|
|
7
|
+
import { UserAccount } from '../types';
|
|
8
8
|
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
9
|
-
import { ClearingHouseConfigType } from '../factory/clearingHouse';
|
|
10
9
|
export declare class PollingUserAccountSubscriber implements UserAccountSubscriber {
|
|
11
10
|
isSubscribed: boolean;
|
|
12
11
|
program: Program;
|
|
13
12
|
eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
|
|
14
|
-
|
|
13
|
+
userAccountPublicKey: PublicKey;
|
|
15
14
|
accountLoader: BulkAccountLoader;
|
|
16
15
|
accountsToPoll: Map<string, AccountToPoll>;
|
|
17
16
|
errorCallbackId?: string;
|
|
18
|
-
user?: UserAccount
|
|
19
|
-
|
|
20
|
-
userOrders?: UserOrdersAccount;
|
|
21
|
-
type: ClearingHouseConfigType;
|
|
22
|
-
constructor(program: Program, authority: PublicKey, accountLoader: BulkAccountLoader);
|
|
17
|
+
user?: DataAndSlot<UserAccount>;
|
|
18
|
+
constructor(program: Program, userAccountPublicKey: PublicKey, accountLoader: BulkAccountLoader);
|
|
23
19
|
subscribe(): Promise<boolean>;
|
|
24
|
-
addToAccountLoader(
|
|
20
|
+
addToAccountLoader(): Promise<void>;
|
|
25
21
|
fetchIfUnloaded(): Promise<void>;
|
|
26
22
|
fetch(): Promise<void>;
|
|
27
23
|
didSubscriptionSucceed(): boolean;
|
|
28
24
|
unsubscribe(): Promise<void>;
|
|
29
25
|
assertIsSubscribed(): void;
|
|
30
|
-
|
|
31
|
-
getUserPositionsAccount(): UserPositionsAccount;
|
|
32
|
-
getUserOrdersAccount(): UserOrdersAccount;
|
|
26
|
+
getUserAccountAndSlot(): DataAndSlot<UserAccount>;
|
|
33
27
|
}
|
|
@@ -1,176 +1,113 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.PollingUserAccountSubscriber = void 0;
|
|
13
4
|
const types_1 = require("./types");
|
|
14
5
|
const events_1 = require("events");
|
|
15
|
-
const addresses_1 = require("../addresses");
|
|
16
6
|
const utils_1 = require("./utils");
|
|
17
7
|
class PollingUserAccountSubscriber {
|
|
18
|
-
constructor(program,
|
|
8
|
+
constructor(program, userAccountPublicKey, accountLoader) {
|
|
19
9
|
this.accountsToPoll = new Map();
|
|
20
|
-
this.type = 'polling';
|
|
21
10
|
this.isSubscribed = false;
|
|
22
11
|
this.program = program;
|
|
23
|
-
this.authority = authority;
|
|
24
12
|
this.accountLoader = accountLoader;
|
|
25
13
|
this.eventEmitter = new events_1.EventEmitter();
|
|
14
|
+
this.userAccountPublicKey = userAccountPublicKey;
|
|
26
15
|
}
|
|
27
|
-
subscribe() {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return subscriptionSucceeded;
|
|
45
|
-
});
|
|
16
|
+
async subscribe() {
|
|
17
|
+
if (this.isSubscribed) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
await this.addToAccountLoader();
|
|
21
|
+
let subscriptionSucceeded = false;
|
|
22
|
+
let retries = 0;
|
|
23
|
+
while (!subscriptionSucceeded && retries < 5) {
|
|
24
|
+
await this.fetchIfUnloaded();
|
|
25
|
+
subscriptionSucceeded = this.didSubscriptionSucceed();
|
|
26
|
+
retries++;
|
|
27
|
+
}
|
|
28
|
+
if (subscriptionSucceeded) {
|
|
29
|
+
this.eventEmitter.emit('update');
|
|
30
|
+
}
|
|
31
|
+
this.isSubscribed = subscriptionSucceeded;
|
|
32
|
+
return subscriptionSucceeded;
|
|
46
33
|
}
|
|
47
|
-
addToAccountLoader(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
this.accountsToPoll.set(userAccount.positions.toString(), {
|
|
61
|
-
key: 'userPositions',
|
|
62
|
-
publicKey: userAccount.positions,
|
|
63
|
-
eventType: 'userPositionsData',
|
|
64
|
-
});
|
|
65
|
-
const userOrdersPublicKey = yield (0, addresses_1.getUserOrdersAccountPublicKey)(this.program.programId, userPublicKey);
|
|
66
|
-
this.accountsToPoll.set(userOrdersPublicKey.toString(), {
|
|
67
|
-
key: 'userOrders',
|
|
68
|
-
publicKey: userOrdersPublicKey,
|
|
69
|
-
eventType: 'userOrdersData',
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
this.accountsToPoll.set(userPublicKeys.user.toString(), {
|
|
74
|
-
key: 'user',
|
|
75
|
-
publicKey: userPublicKeys.user,
|
|
76
|
-
eventType: 'userAccountData',
|
|
77
|
-
});
|
|
78
|
-
this.accountsToPoll.set(userPublicKeys.userPositions.toString(), {
|
|
79
|
-
key: 'userPositions',
|
|
80
|
-
publicKey: userPublicKeys.userPositions,
|
|
81
|
-
eventType: 'userPositionsData',
|
|
82
|
-
});
|
|
83
|
-
if (userPublicKeys.userOrders) {
|
|
84
|
-
this.accountsToPoll.set(userPublicKeys.userOrders.toString(), {
|
|
85
|
-
key: 'userOrders',
|
|
86
|
-
publicKey: userPublicKeys.userOrders,
|
|
87
|
-
eventType: 'userOrdersData',
|
|
88
|
-
});
|
|
34
|
+
async addToAccountLoader() {
|
|
35
|
+
if (this.accountsToPoll.size > 0) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
this.accountsToPoll.set(this.userAccountPublicKey.toString(), {
|
|
39
|
+
key: 'user',
|
|
40
|
+
publicKey: this.userAccountPublicKey,
|
|
41
|
+
eventType: 'userAccountUpdate',
|
|
42
|
+
});
|
|
43
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
44
|
+
accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, (buffer, slot) => {
|
|
45
|
+
if (!buffer) {
|
|
46
|
+
return;
|
|
89
47
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
const account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
97
|
-
this[accountToPoll.key] = account;
|
|
98
|
-
// @ts-ignore
|
|
99
|
-
this.eventEmitter.emit(accountToPoll.eventType, account);
|
|
100
|
-
this.eventEmitter.emit('update');
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
|
|
104
|
-
this.eventEmitter.emit('error', error);
|
|
48
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
49
|
+
this[accountToPoll.key] = { data: account, slot };
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
this.eventEmitter.emit(accountToPoll.eventType, account);
|
|
52
|
+
this.eventEmitter.emit('update');
|
|
105
53
|
});
|
|
54
|
+
}
|
|
55
|
+
this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
|
|
56
|
+
this.eventEmitter.emit('error', error);
|
|
106
57
|
});
|
|
107
58
|
}
|
|
108
|
-
fetchIfUnloaded() {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
break;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
if (shouldFetch) {
|
|
118
|
-
yield this.fetch();
|
|
59
|
+
async fetchIfUnloaded() {
|
|
60
|
+
let shouldFetch = false;
|
|
61
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
62
|
+
if (this[accountToPoll.key] === undefined) {
|
|
63
|
+
shouldFetch = true;
|
|
64
|
+
break;
|
|
119
65
|
}
|
|
120
|
-
}
|
|
66
|
+
}
|
|
67
|
+
if (shouldFetch) {
|
|
68
|
+
await this.fetch();
|
|
69
|
+
}
|
|
121
70
|
}
|
|
122
|
-
fetch() {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
71
|
+
async fetch() {
|
|
72
|
+
await this.accountLoader.load();
|
|
73
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
74
|
+
const { buffer, slot } = this.accountLoader.getBufferAndSlot(accountToPoll.publicKey);
|
|
75
|
+
if (buffer) {
|
|
76
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
77
|
+
this[accountToPoll.key] = { data: account, slot };
|
|
130
78
|
}
|
|
131
|
-
}
|
|
79
|
+
}
|
|
132
80
|
}
|
|
133
81
|
didSubscriptionSucceed() {
|
|
134
82
|
let success = true;
|
|
135
83
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
136
|
-
|
|
137
|
-
if (accountToPoll.key !== 'userOrders' && !this[accountToPoll.key]) {
|
|
84
|
+
if (!this[accountToPoll.key]) {
|
|
138
85
|
success = false;
|
|
139
86
|
break;
|
|
140
87
|
}
|
|
141
88
|
}
|
|
142
89
|
return success;
|
|
143
90
|
}
|
|
144
|
-
unsubscribe() {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
this.isSubscribed = false;
|
|
156
|
-
});
|
|
91
|
+
async unsubscribe() {
|
|
92
|
+
if (!this.isSubscribed) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
96
|
+
this.accountLoader.removeAccount(accountToPoll.publicKey, accountToPoll.callbackId);
|
|
97
|
+
}
|
|
98
|
+
this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
|
|
99
|
+
this.errorCallbackId = undefined;
|
|
100
|
+
this.accountsToPoll.clear();
|
|
101
|
+
this.isSubscribed = false;
|
|
157
102
|
}
|
|
158
103
|
assertIsSubscribed() {
|
|
159
104
|
if (!this.isSubscribed) {
|
|
160
105
|
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
161
106
|
}
|
|
162
107
|
}
|
|
163
|
-
|
|
108
|
+
getUserAccountAndSlot() {
|
|
164
109
|
this.assertIsSubscribed();
|
|
165
110
|
return this.user;
|
|
166
111
|
}
|
|
167
|
-
getUserPositionsAccount() {
|
|
168
|
-
this.assertIsSubscribed();
|
|
169
|
-
return this.userPositions;
|
|
170
|
-
}
|
|
171
|
-
getUserOrdersAccount() {
|
|
172
|
-
this.assertIsSubscribed();
|
|
173
|
-
return this.userOrders;
|
|
174
|
-
}
|
|
175
112
|
}
|
|
176
113
|
exports.PollingUserAccountSubscriber = PollingUserAccountSubscriber;
|
package/lib/accounts/types.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
|
|
2
|
+
/// <reference types="bn.js" />
|
|
3
|
+
/// <reference types="node" />
|
|
4
|
+
import { BankAccount, MarketAccount, OracleSource, StateAccount, UserAccount } from '../types';
|
|
3
5
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
4
6
|
import { EventEmitter } from 'events';
|
|
5
7
|
import { PublicKey } from '@solana/web3.js';
|
|
6
8
|
import { AccountInfo } from '@solana/spl-token';
|
|
7
|
-
import {
|
|
9
|
+
import { OracleInfo, OraclePriceData } from '..';
|
|
10
|
+
import { BN } from '@project-serum/anchor';
|
|
8
11
|
export interface AccountSubscriber<T> {
|
|
9
|
-
|
|
12
|
+
dataAndSlot?: DataAndSlot<T>;
|
|
10
13
|
subscribe(onChange: (data: T) => void): Promise<void>;
|
|
11
14
|
fetch(): Promise<void>;
|
|
12
15
|
unsubscribe(): Promise<void>;
|
|
@@ -16,47 +19,30 @@ export declare class NotSubscribedError extends Error {
|
|
|
16
19
|
}
|
|
17
20
|
export interface ClearingHouseAccountEvents {
|
|
18
21
|
stateAccountUpdate: (payload: StateAccount) => void;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
liquidationHistoryAccountUpdate: (payload: LiquidationHistoryAccount) => void;
|
|
24
|
-
depositHistoryAccountUpdate: (payload: DepositHistoryAccount) => void;
|
|
25
|
-
curveHistoryAccountUpdate: (payload: ExtendedCurveHistoryAccount) => void;
|
|
26
|
-
orderHistoryAccountUpdate: (payload: OrderHistoryAccount) => void;
|
|
27
|
-
orderStateAccountUpdate: (payload: OrderStateAccount) => void;
|
|
22
|
+
marketAccountUpdate: (payload: MarketAccount) => void;
|
|
23
|
+
bankAccountUpdate: (payload: BankAccount) => void;
|
|
24
|
+
oraclePriceUpdate: (publicKey: PublicKey, data: OraclePriceData) => void;
|
|
25
|
+
userAccountUpdate: (payload: UserAccount) => void;
|
|
28
26
|
update: void;
|
|
29
27
|
error: (e: Error) => void;
|
|
30
28
|
}
|
|
31
|
-
export declare type ClearingHouseAccountTypes = 'tradeHistoryAccount' | 'depositHistoryAccount' | 'fundingPaymentHistoryAccount' | 'fundingRateHistoryAccount' | 'curveHistoryAccount' | 'liquidationHistoryAccount' | 'orderHistoryAccount';
|
|
32
29
|
export interface ClearingHouseAccountSubscriber {
|
|
33
30
|
eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
|
|
34
31
|
isSubscribed: boolean;
|
|
35
|
-
|
|
36
|
-
subscribe(optionalSubscriptions?: ClearingHouseAccountTypes[]): Promise<boolean>;
|
|
32
|
+
subscribe(): Promise<boolean>;
|
|
37
33
|
fetch(): Promise<void>;
|
|
38
34
|
unsubscribe(): Promise<void>;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
getOrderStateAccount(): OrderStateAccount;
|
|
48
|
-
getOrderHistoryAccount(): OrderHistoryAccount;
|
|
49
|
-
type: ClearingHouseConfigType;
|
|
35
|
+
addMarket(marketIndex: BN): Promise<boolean>;
|
|
36
|
+
addBank(bankIndex: BN): Promise<boolean>;
|
|
37
|
+
addOracle(oracleInfo: OracleInfo): Promise<boolean>;
|
|
38
|
+
getStateAccountAndSlot(): DataAndSlot<StateAccount>;
|
|
39
|
+
getMarketAccountAndSlot(marketIndex: BN): DataAndSlot<MarketAccount> | undefined;
|
|
40
|
+
getMarketAccountsAndSlots(): DataAndSlot<MarketAccount>[];
|
|
41
|
+
getBankAccountAndSlot(bankIndex: BN): DataAndSlot<BankAccount> | undefined;
|
|
42
|
+
getOraclePriceDataAndSlot(oraclePublicKey: PublicKey): DataAndSlot<OraclePriceData> | undefined;
|
|
50
43
|
}
|
|
51
|
-
export declare type UserPublicKeys = {
|
|
52
|
-
user: PublicKey;
|
|
53
|
-
userPositions: PublicKey;
|
|
54
|
-
userOrders: PublicKey | undefined;
|
|
55
|
-
};
|
|
56
44
|
export interface UserAccountEvents {
|
|
57
|
-
|
|
58
|
-
userPositionsData: (payload: UserPositionsAccount) => void;
|
|
59
|
-
userOrdersData: (payload: UserOrdersAccount) => void;
|
|
45
|
+
userAccountUpdate: (payload: UserAccount) => void;
|
|
60
46
|
update: void;
|
|
61
47
|
error: (e: Error) => void;
|
|
62
48
|
}
|
|
@@ -66,10 +52,7 @@ export interface UserAccountSubscriber {
|
|
|
66
52
|
subscribe(): Promise<boolean>;
|
|
67
53
|
fetch(): Promise<void>;
|
|
68
54
|
unsubscribe(): Promise<void>;
|
|
69
|
-
|
|
70
|
-
getUserPositionsAccount(): UserPositionsAccount;
|
|
71
|
-
getUserOrdersAccount(): UserOrdersAccount;
|
|
72
|
-
type: ClearingHouseUserConfigType;
|
|
55
|
+
getUserAccountAndSlot(): DataAndSlot<UserAccount>;
|
|
73
56
|
}
|
|
74
57
|
export interface TokenAccountEvents {
|
|
75
58
|
tokenAccountUpdate: (payload: AccountInfo) => void;
|
|
@@ -82,7 +65,7 @@ export interface TokenAccountSubscriber {
|
|
|
82
65
|
subscribe(): Promise<boolean>;
|
|
83
66
|
fetch(): Promise<void>;
|
|
84
67
|
unsubscribe(): Promise<void>;
|
|
85
|
-
|
|
68
|
+
getTokenAccountAndSlot(): DataAndSlot<AccountInfo>;
|
|
86
69
|
}
|
|
87
70
|
export interface OracleEvents {
|
|
88
71
|
oracleUpdate: (payload: OraclePriceData) => void;
|
|
@@ -95,15 +78,25 @@ export interface OracleSubscriber {
|
|
|
95
78
|
subscribe(): Promise<boolean>;
|
|
96
79
|
fetch(): Promise<void>;
|
|
97
80
|
unsubscribe(): Promise<void>;
|
|
98
|
-
getOraclePriceData(): OraclePriceData
|
|
81
|
+
getOraclePriceData(): DataAndSlot<OraclePriceData>;
|
|
99
82
|
}
|
|
100
83
|
export declare type AccountToPoll = {
|
|
101
84
|
key: string;
|
|
102
85
|
publicKey: PublicKey;
|
|
103
86
|
eventType: string;
|
|
104
87
|
callbackId?: string;
|
|
88
|
+
mapKey?: number;
|
|
89
|
+
};
|
|
90
|
+
export declare type OraclesToPoll = {
|
|
91
|
+
publicKey: PublicKey;
|
|
92
|
+
source: OracleSource;
|
|
93
|
+
callbackId?: string;
|
|
105
94
|
};
|
|
106
|
-
export declare type
|
|
95
|
+
export declare type BufferAndSlot = {
|
|
107
96
|
slot: number;
|
|
108
97
|
buffer: Buffer | undefined;
|
|
109
98
|
};
|
|
99
|
+
export declare type DataAndSlot<T> = {
|
|
100
|
+
data: T;
|
|
101
|
+
slot: number;
|
|
102
|
+
};
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import {
|
|
2
|
+
import { DataAndSlot, BufferAndSlot, AccountSubscriber } from './types';
|
|
3
3
|
import { Program } from '@project-serum/anchor';
|
|
4
4
|
import { AccountInfo, Context, PublicKey } from '@solana/web3.js';
|
|
5
5
|
export declare class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
dataAndSlot?: DataAndSlot<T>;
|
|
7
|
+
bufferAndSlot?: BufferAndSlot;
|
|
8
8
|
accountName: string;
|
|
9
9
|
program: Program;
|
|
10
10
|
accountPublicKey: PublicKey;
|
|
11
|
+
decodeBufferFn: (buffer: Buffer) => T;
|
|
11
12
|
onChange: (data: T) => void;
|
|
12
13
|
listenerId?: number;
|
|
13
|
-
constructor(accountName: string, program: Program, accountPublicKey: PublicKey);
|
|
14
|
+
constructor(accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => T);
|
|
14
15
|
subscribe(onChange: (data: T) => void): Promise<void>;
|
|
15
16
|
fetch(): Promise<void>;
|
|
16
17
|
handleRpcResponse(context: Context, accountInfo?: AccountInfo<Buffer>): void;
|
|
18
|
+
decodeBuffer(buffer: Buffer): T;
|
|
17
19
|
unsubscribe(): Promise<void>;
|
|
18
20
|
}
|