@drift-labs/sdk 0.1.19-master.0 → 0.1.21-master.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/lib/accounts/bulkAccountLoader.d.ts +32 -0
- package/lib/accounts/bulkAccountLoader.d.ts.map +1 -0
- package/lib/accounts/bulkAccountLoader.js +175 -0
- package/lib/accounts/bulkUserSubscription.d.ts +8 -0
- package/lib/accounts/bulkUserSubscription.d.ts.map +1 -0
- package/lib/accounts/bulkUserSubscription.js +28 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +46 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +206 -0
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +26 -0
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/pollingTokenAccountSubscriber.js +79 -0
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +31 -0
- package/lib/accounts/pollingUserAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/pollingUserAccountSubscriber.js +123 -0
- package/lib/accounts/types.d.ts +30 -0
- package/lib/accounts/types.d.ts.map +1 -1
- package/lib/accounts/utils.d.ts +2 -0
- package/lib/accounts/utils.d.ts.map +1 -0
- package/lib/accounts/utils.js +7 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -2
- package/lib/accounts/webSocketAccountSubscriber.d.ts.map +1 -1
- package/lib/accounts/webSocketAccountSubscriber.js +43 -12
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.d.ts → webSocketClearingHouseAccountSubscriber.d.ts} +4 -2
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.js → webSocketClearingHouseAccountSubscriber.js} +5 -4
- package/lib/accounts/{defaultUserAccountSubscriber.d.ts → webSocketUserAccountSubscriber.d.ts} +4 -2
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/{defaultUserAccountSubscriber.js → webSocketUserAccountSubscriber.js} +5 -4
- package/lib/admin.d.ts +1 -0
- package/lib/admin.d.ts.map +1 -1
- package/lib/admin.js +23 -14
- package/lib/clearingHouse.d.ts +12 -2
- package/lib/clearingHouse.d.ts.map +1 -1
- package/lib/clearingHouse.js +28 -17
- package/lib/clearingHouseUser.d.ts +11 -3
- package/lib/clearingHouseUser.d.ts.map +1 -1
- package/lib/clearingHouseUser.js +46 -36
- package/lib/constants/markets.d.ts +2 -1
- package/lib/constants/markets.d.ts.map +1 -1
- package/lib/constants/markets.js +20 -15
- package/lib/constants/numericConstants.d.ts +3 -1
- package/lib/constants/numericConstants.d.ts.map +1 -1
- package/lib/constants/numericConstants.js +15 -17
- package/lib/examples/makeTradeExample.js +6 -6
- package/lib/factory/clearingHouse.d.ts +26 -0
- package/lib/factory/clearingHouse.d.ts.map +1 -0
- package/lib/factory/clearingHouse.js +64 -0
- package/lib/factory/clearingHouseUser.d.ts +20 -0
- package/lib/factory/clearingHouseUser.d.ts.map +1 -0
- package/lib/factory/clearingHouseUser.js +34 -0
- package/lib/index.d.ts +8 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +9 -2
- package/lib/math/amm.js +7 -7
- package/lib/math/conversion.d.ts +1 -1
- package/lib/math/conversion.d.ts.map +1 -1
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.js +1 -1
- package/lib/math/insuranceFund.d.ts +2 -1
- package/lib/math/insuranceFund.d.ts.map +1 -1
- package/lib/math/insuranceFund.js +3 -6
- package/lib/math/market.js +1 -1
- package/lib/math/position.d.ts +2 -1
- package/lib/math/position.d.ts.map +1 -1
- package/lib/math/position.js +3 -6
- package/lib/math/trade.js +16 -16
- package/lib/math/utils.d.ts +2 -1
- package/lib/math/utils.d.ts.map +1 -1
- package/lib/math/utils.js +3 -3
- package/lib/mockUSDCFaucet.d.ts +2 -1
- package/lib/mockUSDCFaucet.d.ts.map +1 -1
- package/lib/pythClient.js +1 -1
- package/lib/token/index.d.ts +4 -0
- package/lib/token/index.d.ts.map +1 -0
- package/lib/token/index.js +38 -0
- package/lib/types.d.ts +2 -1
- package/lib/types.d.ts.map +1 -1
- package/package.json +11 -3
- package/src/accounts/bulkAccountLoader.ts +215 -0
- package/src/accounts/bulkUserSubscription.ts +28 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +293 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +99 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +166 -0
- package/src/accounts/types.ts +38 -0
- package/src/accounts/utils.ts +3 -0
- package/src/accounts/webSocketAccountSubscriber.ts +67 -17
- package/src/accounts/{defaultClearingHouseAccountSubscriber.ts → webSocketClearingHouseAccountSubscriber.ts} +4 -1
- package/src/accounts/{defaultUserAccountSubscriber.ts → webSocketUserAccountSubscriber.ts} +4 -1
- package/src/admin.ts +27 -18
- package/src/clearingHouse.ts +25 -17
- package/src/clearingHouseUser.ts +26 -6
- package/src/constants/markets.ts +9 -1
- package/src/constants/numericConstants.ts +2 -1
- package/src/examples/makeTradeExample.ts +4 -1
- package/src/factory/clearingHouse.ts +125 -0
- package/src/factory/clearingHouseUser.ts +73 -0
- package/src/index.ts +8 -2
- package/src/math/conversion.ts +1 -1
- package/src/math/insuranceFund.ts +1 -1
- package/src/math/position.ts +1 -1
- package/src/math/utils.ts +1 -1
- package/src/mockUSDCFaucet.ts +1 -1
- package/src/token/index.ts +37 -0
- package/src/types.ts +1 -1
- package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +0 -1
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Commitment, Connection, PublicKey } from '@solana/web3.js';
|
|
3
|
+
import { AccountData } from './types';
|
|
4
|
+
declare type AccountToLoad = {
|
|
5
|
+
publicKey: PublicKey;
|
|
6
|
+
callbacks: Map<string, (buffer: Buffer) => void>;
|
|
7
|
+
};
|
|
8
|
+
export declare class BulkAccountLoader {
|
|
9
|
+
connection: Connection;
|
|
10
|
+
commitment: Commitment;
|
|
11
|
+
pollingFrequency: number;
|
|
12
|
+
accountsToLoad: Map<string, AccountToLoad>;
|
|
13
|
+
accountData: Map<string, AccountData>;
|
|
14
|
+
errorCallbacks: Map<string, (e: any) => void>;
|
|
15
|
+
intervalId?: NodeJS.Timer;
|
|
16
|
+
loadPromise?: Promise<void>;
|
|
17
|
+
loadPromiseResolver: () => void;
|
|
18
|
+
constructor(connection: Connection, commitment: Commitment, pollingFrequency: number);
|
|
19
|
+
addAccount(publicKey: PublicKey, callback: (buffer: Buffer) => void): string;
|
|
20
|
+
removeAccount(publicKey: PublicKey, callbackId: string): void;
|
|
21
|
+
addErrorCallbacks(callback: (error: Error) => void): string;
|
|
22
|
+
removeErrorCallbacks(callbackId: string): void;
|
|
23
|
+
chunks<T>(array: readonly T[], size: number): T[][];
|
|
24
|
+
load(): Promise<void>;
|
|
25
|
+
loadChunk(accountsToLoad: AccountToLoad[]): Promise<void>;
|
|
26
|
+
handleAccountCallbacks(accountToLoad: AccountToLoad, buffer: Buffer): void;
|
|
27
|
+
getAccountData(publicKey: PublicKey): Buffer | undefined;
|
|
28
|
+
startPolling(): void;
|
|
29
|
+
stopPolling(): void;
|
|
30
|
+
}
|
|
31
|
+
export {};
|
|
32
|
+
//# sourceMappingURL=bulkAccountLoader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bulkAccountLoader.d.ts","sourceRoot":"","sources":["../../src/accounts/bulkAccountLoader.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEpE,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEtC,aAAK,aAAa,GAAG;IACpB,SAAS,EAAE,SAAS,CAAC;IACrB,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC,CAAC;CACjD,CAAC;AAIF,qBAAa,iBAAiB;IAC7B,UAAU,EAAE,UAAU,CAAC;IACvB,UAAU,EAAE,UAAU,CAAC;IACvB,gBAAgB,EAAE,MAAM,CAAC;IACzB,cAAc,6BAAoC;IAClD,WAAW,2BAAkC;IAC7C,cAAc,0BAA0B,IAAI,EAAI;IAChD,UAAU,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC;IAE1B,WAAW,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,mBAAmB,EAAE,MAAM,IAAI,CAAC;gBAG/B,UAAU,EAAE,UAAU,EACtB,UAAU,EAAE,UAAU,EACtB,gBAAgB,EAAE,MAAM;IAOlB,UAAU,CAChB,SAAS,EAAE,SAAS,EACpB,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GAChC,MAAM;IA2BF,aAAa,CAAC,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI;IAc7D,iBAAiB,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,GAAG,MAAM;IAM3D,oBAAoB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAIrD,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,EAAE,EAAE;IAOtC,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IA+B5B,SAAS,CAAC,cAAc,EAAE,aAAa,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAgE/D,sBAAsB,CAAC,aAAa,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;IAMnE,cAAc,CAAC,SAAS,EAAE,SAAS,GAAG,MAAM,GAAG,SAAS;IAKxD,YAAY,IAAI,IAAI;IAQpB,WAAW,IAAI,IAAI;CAM1B"}
|
|
@@ -0,0 +1,175 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BulkAccountLoader = void 0;
|
|
13
|
+
const uuid_1 = require("uuid");
|
|
14
|
+
const GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE = 99;
|
|
15
|
+
class BulkAccountLoader {
|
|
16
|
+
constructor(connection, commitment, pollingFrequency) {
|
|
17
|
+
this.accountsToLoad = new Map();
|
|
18
|
+
this.accountData = new Map();
|
|
19
|
+
this.errorCallbacks = new Map();
|
|
20
|
+
this.connection = connection;
|
|
21
|
+
this.commitment = commitment;
|
|
22
|
+
this.pollingFrequency = pollingFrequency;
|
|
23
|
+
}
|
|
24
|
+
addAccount(publicKey, callback) {
|
|
25
|
+
const existingSize = this.accountsToLoad.size;
|
|
26
|
+
const callbackId = uuid_1.v4();
|
|
27
|
+
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
28
|
+
if (existingAccountToLoad) {
|
|
29
|
+
existingAccountToLoad.callbacks.set(callbackId, callback);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const callbacks = new Map();
|
|
33
|
+
callbacks.set(callbackId, callback);
|
|
34
|
+
const newAccountToLoad = {
|
|
35
|
+
publicKey,
|
|
36
|
+
callbacks,
|
|
37
|
+
};
|
|
38
|
+
this.accountsToLoad.set(publicKey.toString(), newAccountToLoad);
|
|
39
|
+
}
|
|
40
|
+
if (existingSize === 0) {
|
|
41
|
+
this.startPolling();
|
|
42
|
+
}
|
|
43
|
+
// if a new account needs to be polled, remove the cached loadPromise in case client calls load immediately after
|
|
44
|
+
this.loadPromise = undefined;
|
|
45
|
+
return callbackId;
|
|
46
|
+
}
|
|
47
|
+
removeAccount(publicKey, callbackId) {
|
|
48
|
+
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
49
|
+
if (existingAccountToLoad) {
|
|
50
|
+
existingAccountToLoad.callbacks.delete(callbackId);
|
|
51
|
+
if (existingAccountToLoad.callbacks.size === 0) {
|
|
52
|
+
this.accountsToLoad.delete(existingAccountToLoad.publicKey.toString());
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (this.accountsToLoad.size === 0) {
|
|
56
|
+
this.stopPolling();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
addErrorCallbacks(callback) {
|
|
60
|
+
const callbackId = uuid_1.v4();
|
|
61
|
+
this.errorCallbacks.set(callbackId, callback);
|
|
62
|
+
return callbackId;
|
|
63
|
+
}
|
|
64
|
+
removeErrorCallbacks(callbackId) {
|
|
65
|
+
this.errorCallbacks.delete(callbackId);
|
|
66
|
+
}
|
|
67
|
+
chunks(array, size) {
|
|
68
|
+
return new Array(Math.ceil(array.length / size))
|
|
69
|
+
.fill(null)
|
|
70
|
+
.map((_, index) => index * size)
|
|
71
|
+
.map((begin) => array.slice(begin, begin + size));
|
|
72
|
+
}
|
|
73
|
+
load() {
|
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
if (this.loadPromise) {
|
|
76
|
+
return this.loadPromise;
|
|
77
|
+
}
|
|
78
|
+
this.loadPromise = new Promise((resolver) => {
|
|
79
|
+
this.loadPromiseResolver = resolver;
|
|
80
|
+
});
|
|
81
|
+
try {
|
|
82
|
+
const chunks = this.chunks(Array.from(this.accountsToLoad.values()), GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE);
|
|
83
|
+
yield Promise.all(chunks.map((chunk) => {
|
|
84
|
+
return this.loadChunk(chunk);
|
|
85
|
+
}));
|
|
86
|
+
}
|
|
87
|
+
catch (e) {
|
|
88
|
+
console.error(`Error in bulkAccountLoader.load()`);
|
|
89
|
+
console.error(e);
|
|
90
|
+
for (const [_, callback] of this.errorCallbacks) {
|
|
91
|
+
callback(e);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
this.loadPromiseResolver();
|
|
95
|
+
this.loadPromise = undefined;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
loadChunk(accountsToLoad) {
|
|
99
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
+
if (accountsToLoad.length === 0) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const args = [
|
|
104
|
+
accountsToLoad.map((accountToLoad) => {
|
|
105
|
+
return accountToLoad.publicKey.toBase58();
|
|
106
|
+
}),
|
|
107
|
+
{ commitment: this.commitment },
|
|
108
|
+
];
|
|
109
|
+
let rpcResponse;
|
|
110
|
+
try {
|
|
111
|
+
// @ts-ignore
|
|
112
|
+
rpcResponse = yield this.connection._rpcRequest('getMultipleAccounts', args);
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
for (const [_, callback] of this.errorCallbacks) {
|
|
116
|
+
callback(e);
|
|
117
|
+
}
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const newSlot = rpcResponse.result.context.slot;
|
|
121
|
+
for (const i in accountsToLoad) {
|
|
122
|
+
const accountToLoad = accountsToLoad[i];
|
|
123
|
+
const key = accountToLoad.publicKey.toString();
|
|
124
|
+
const oldRPCResponse = this.accountData.get(key);
|
|
125
|
+
let newBuffer = undefined;
|
|
126
|
+
if (rpcResponse.result.value[i]) {
|
|
127
|
+
const raw = rpcResponse.result.value[i].data[0];
|
|
128
|
+
const dataType = rpcResponse.result.value[i].data[1];
|
|
129
|
+
newBuffer = Buffer.from(raw, dataType);
|
|
130
|
+
}
|
|
131
|
+
if (!oldRPCResponse) {
|
|
132
|
+
this.accountData.set(key, {
|
|
133
|
+
slot: newSlot,
|
|
134
|
+
buffer: newBuffer,
|
|
135
|
+
});
|
|
136
|
+
this.handleAccountCallbacks(accountToLoad, newBuffer);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (newSlot <= oldRPCResponse.slot) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const oldBuffer = oldRPCResponse.buffer;
|
|
143
|
+
if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
|
|
144
|
+
this.accountData.set(key, {
|
|
145
|
+
slot: newSlot,
|
|
146
|
+
buffer: newBuffer,
|
|
147
|
+
});
|
|
148
|
+
this.handleAccountCallbacks(accountToLoad, newBuffer);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
handleAccountCallbacks(accountToLoad, buffer) {
|
|
154
|
+
for (const [_, callback] of accountToLoad.callbacks) {
|
|
155
|
+
callback(buffer);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
getAccountData(publicKey) {
|
|
159
|
+
const accountData = this.accountData.get(publicKey.toString());
|
|
160
|
+
return accountData === null || accountData === void 0 ? void 0 : accountData.buffer;
|
|
161
|
+
}
|
|
162
|
+
startPolling() {
|
|
163
|
+
if (this.intervalId) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
this.intervalId = setInterval(this.load.bind(this), this.pollingFrequency);
|
|
167
|
+
}
|
|
168
|
+
stopPolling() {
|
|
169
|
+
if (this.intervalId) {
|
|
170
|
+
clearInterval(this.intervalId);
|
|
171
|
+
this.intervalId = undefined;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
exports.BulkAccountLoader = BulkAccountLoader;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ClearingHouseUser } from '../clearingHouseUser';
|
|
2
|
+
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
3
|
+
/**
|
|
4
|
+
* @param users
|
|
5
|
+
* @param accountLoader
|
|
6
|
+
*/
|
|
7
|
+
export declare function bulkPollingUserSubscribe(users: ClearingHouseUser[], accountLoader: BulkAccountLoader): Promise<void>;
|
|
8
|
+
//# sourceMappingURL=bulkUserSubscription.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bulkUserSubscription.d.ts","sourceRoot":"","sources":["../../src/accounts/bulkUserSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAGxD;;;GAGG;AACH,wBAAsB,wBAAwB,CAC7C,KAAK,EAAE,iBAAiB,EAAE,EAC1B,aAAa,EAAE,iBAAiB,GAC9B,OAAO,CAAC,IAAI,CAAC,CAgBf"}
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.bulkPollingUserSubscribe = void 0;
|
|
13
|
+
/**
|
|
14
|
+
* @param users
|
|
15
|
+
* @param accountLoader
|
|
16
|
+
*/
|
|
17
|
+
function bulkPollingUserSubscribe(users, accountLoader) {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
yield Promise.all(users.map((user) => {
|
|
20
|
+
return user.accountSubscriber.addToAccountLoader();
|
|
21
|
+
}));
|
|
22
|
+
yield accountLoader.load();
|
|
23
|
+
yield Promise.all(users.map((user) => __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
return user.subscribe();
|
|
25
|
+
})));
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
exports.bulkPollingUserSubscribe = bulkPollingUserSubscribe;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { AccountToPoll, ClearingHouseAccountEvents, ClearingHouseAccountSubscriber, ClearingHouseAccountTypes } from './types';
|
|
3
|
+
import { Program } from '@project-serum/anchor';
|
|
4
|
+
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
|
+
import { EventEmitter } from 'events';
|
|
6
|
+
import { DepositHistoryAccount, ExtendedCurveHistoryAccount, FundingPaymentHistoryAccount, FundingRateHistoryAccount, LiquidationHistoryAccount, MarketsAccount, StateAccount, TradeHistoryAccount } from '../types';
|
|
7
|
+
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
8
|
+
import { ClearingHouseConfigType } from '../factory/clearingHouse';
|
|
9
|
+
export declare class PollingClearingHouseAccountSubscriber implements ClearingHouseAccountSubscriber {
|
|
10
|
+
isSubscribed: boolean;
|
|
11
|
+
program: Program;
|
|
12
|
+
eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
|
|
13
|
+
accountLoader: BulkAccountLoader;
|
|
14
|
+
accountsToPoll: Map<string, AccountToPoll>;
|
|
15
|
+
errorCallbackId?: string;
|
|
16
|
+
state?: StateAccount;
|
|
17
|
+
markets?: MarketsAccount;
|
|
18
|
+
tradeHistory?: TradeHistoryAccount;
|
|
19
|
+
depositHistory?: DepositHistoryAccount;
|
|
20
|
+
fundingPaymentHistory?: FundingPaymentHistoryAccount;
|
|
21
|
+
fundingRateHistory?: FundingRateHistoryAccount;
|
|
22
|
+
liquidationHistory?: LiquidationHistoryAccount;
|
|
23
|
+
extendedCurveHistory: ExtendedCurveHistoryAccount;
|
|
24
|
+
optionalExtraSubscriptions: ClearingHouseAccountTypes[];
|
|
25
|
+
type: ClearingHouseConfigType;
|
|
26
|
+
private isSubscribing;
|
|
27
|
+
private subscriptionPromise;
|
|
28
|
+
private subscriptionPromiseResolver;
|
|
29
|
+
constructor(program: Program, accountLoader: BulkAccountLoader);
|
|
30
|
+
subscribe(optionalSubscriptions?: ClearingHouseAccountTypes[]): Promise<boolean>;
|
|
31
|
+
updateAccountsToPoll(): Promise<void>;
|
|
32
|
+
addToAccountLoader(): Promise<void>;
|
|
33
|
+
fetch(): Promise<void>;
|
|
34
|
+
unsubscribe(): Promise<void>;
|
|
35
|
+
assertIsSubscribed(): void;
|
|
36
|
+
assertOptionalIsSubscribed(optionalSubscription: ClearingHouseAccountTypes): void;
|
|
37
|
+
getStateAccount(): StateAccount;
|
|
38
|
+
getMarketsAccount(): MarketsAccount;
|
|
39
|
+
getTradeHistoryAccount(): TradeHistoryAccount;
|
|
40
|
+
getDepositHistoryAccount(): DepositHistoryAccount;
|
|
41
|
+
getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount;
|
|
42
|
+
getFundingRateHistoryAccount(): FundingRateHistoryAccount;
|
|
43
|
+
getCurveHistoryAccount(): ExtendedCurveHistoryAccount;
|
|
44
|
+
getLiquidationHistoryAccount(): LiquidationHistoryAccount;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=pollingClearingHouseAccountSubscriber.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pollingClearingHouseAccountSubscriber.d.ts","sourceRoot":"","sources":["../../src/accounts/pollingClearingHouseAccountSubscriber.ts"],"names":[],"mappings":";AAAA,OAAO,EACN,aAAa,EACb,0BAA0B,EAC1B,8BAA8B,EAC9B,yBAAyB,EAEzB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,kBAAkB,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EACN,qBAAqB,EACrB,2BAA2B,EAC3B,4BAA4B,EAC5B,yBAAyB,EACzB,yBAAyB,EACzB,cAAc,EACd,YAAY,EACZ,mBAAmB,EACnB,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,OAAO,EAAE,uBAAuB,EAAE,MAAM,0BAA0B,CAAC;AAEnE,qBAAa,qCACZ,YAAW,8BAA8B;IAEzC,YAAY,EAAE,OAAO,CAAC;IACtB,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,EAAE,kBAAkB,CAAC,YAAY,EAAE,0BAA0B,CAAC,CAAC;IAE3E,aAAa,EAAE,iBAAiB,CAAC;IACjC,cAAc,6BAAoC;IAClD,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB,KAAK,CAAC,EAAE,YAAY,CAAC;IACrB,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,YAAY,CAAC,EAAE,mBAAmB,CAAC;IACnC,cAAc,CAAC,EAAE,qBAAqB,CAAC;IACvC,qBAAqB,CAAC,EAAE,4BAA4B,CAAC;IACrD,kBAAkB,CAAC,EAAE,yBAAyB,CAAC;IAC/C,kBAAkB,CAAC,EAAE,yBAAyB,CAAC;IAC/C,oBAAoB,EAAE,2BAA2B,CAAC;IAElD,0BAA0B,EAAE,yBAAyB,EAAE,CAAM;IAE7D,IAAI,EAAE,uBAAuB,CAAa;IAE1C,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,mBAAmB,CAAmB;IAC9C,OAAO,CAAC,2BAA2B,CAAyB;gBAEzC,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,iBAAiB;IAOxD,SAAS,CACrB,qBAAqB,CAAC,EAAE,yBAAyB,EAAE,GACjD,OAAO,CAAC,OAAO,CAAC;IA6Bb,oBAAoB,IAAI,OAAO,CAAC,IAAI,CAAC;IAgFrC,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;IAqB5B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAYtB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAmBzC,kBAAkB,IAAI,IAAI;IAQ1B,0BAA0B,CACzB,oBAAoB,EAAE,yBAAyB,GAC7C,IAAI;IAcA,eAAe,IAAI,YAAY;IAK/B,iBAAiB,IAAI,cAAc;IAKnC,sBAAsB,IAAI,mBAAmB;IAM7C,wBAAwB,IAAI,qBAAqB;IAMjD,+BAA+B,IAAI,4BAA4B;IAM/D,4BAA4B,IAAI,yBAAyB;IAMzD,sBAAsB,IAAI,2BAA2B;IAMrD,4BAA4B,IAAI,yBAAyB;CAKhE"}
|
|
@@ -0,0 +1,206 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.PollingClearingHouseAccountSubscriber = void 0;
|
|
13
|
+
const types_1 = require("./types");
|
|
14
|
+
const events_1 = require("events");
|
|
15
|
+
const addresses_1 = require("../addresses");
|
|
16
|
+
const utils_1 = require("./utils");
|
|
17
|
+
class PollingClearingHouseAccountSubscriber {
|
|
18
|
+
constructor(program, accountLoader) {
|
|
19
|
+
this.accountsToPoll = new Map();
|
|
20
|
+
this.optionalExtraSubscriptions = [];
|
|
21
|
+
this.type = 'polling';
|
|
22
|
+
this.isSubscribing = false;
|
|
23
|
+
this.isSubscribed = false;
|
|
24
|
+
this.program = program;
|
|
25
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
26
|
+
this.accountLoader = accountLoader;
|
|
27
|
+
}
|
|
28
|
+
subscribe(optionalSubscriptions) {
|
|
29
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
+
if (this.isSubscribed) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
if (this.isSubscribing) {
|
|
34
|
+
return yield this.subscriptionPromise;
|
|
35
|
+
}
|
|
36
|
+
this.optionalExtraSubscriptions = optionalSubscriptions;
|
|
37
|
+
this.isSubscribing = true;
|
|
38
|
+
this.subscriptionPromise = new Promise((res) => {
|
|
39
|
+
this.subscriptionPromiseResolver = res;
|
|
40
|
+
});
|
|
41
|
+
yield this.updateAccountsToPoll();
|
|
42
|
+
yield this.addToAccountLoader();
|
|
43
|
+
yield this.fetch();
|
|
44
|
+
this.eventEmitter.emit('update');
|
|
45
|
+
this.isSubscribing = false;
|
|
46
|
+
this.isSubscribed = true;
|
|
47
|
+
this.subscriptionPromiseResolver(true);
|
|
48
|
+
return true;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
updateAccountsToPoll() {
|
|
52
|
+
var _a, _b, _c, _d, _e, _f;
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
if (this.accountsToPoll.size > 0) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const statePublicKey = yield addresses_1.getClearingHouseStateAccountPublicKey(this.program.programId);
|
|
58
|
+
const state = (yield this.program.account.state.fetch(statePublicKey));
|
|
59
|
+
this.accountsToPoll.set(statePublicKey.toString(), {
|
|
60
|
+
key: 'state',
|
|
61
|
+
publicKey: statePublicKey,
|
|
62
|
+
eventType: 'stateAccountUpdate',
|
|
63
|
+
});
|
|
64
|
+
this.accountsToPoll.set(state.markets.toString(), {
|
|
65
|
+
key: 'markets',
|
|
66
|
+
publicKey: state.markets,
|
|
67
|
+
eventType: 'marketsAccountUpdate',
|
|
68
|
+
});
|
|
69
|
+
if ((_a = this.optionalExtraSubscriptions) === null || _a === void 0 ? void 0 : _a.includes('tradeHistoryAccount')) {
|
|
70
|
+
this.accountsToPoll.set(state.tradeHistory.toString(), {
|
|
71
|
+
key: 'tradeHistory',
|
|
72
|
+
publicKey: state.tradeHistory,
|
|
73
|
+
eventType: 'tradeHistoryAccountUpdate',
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if ((_b = this.optionalExtraSubscriptions) === null || _b === void 0 ? void 0 : _b.includes('depositHistoryAccount')) {
|
|
77
|
+
this.accountsToPoll.set(state.depositHistory.toString(), {
|
|
78
|
+
key: 'depositHistory',
|
|
79
|
+
publicKey: state.depositHistory,
|
|
80
|
+
eventType: 'depositHistoryAccountUpdate',
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
if ((_c = this.optionalExtraSubscriptions) === null || _c === void 0 ? void 0 : _c.includes('fundingPaymentHistoryAccount')) {
|
|
84
|
+
this.accountsToPoll.set(state.fundingPaymentHistory.toString(), {
|
|
85
|
+
key: 'fundingPaymentHistory',
|
|
86
|
+
publicKey: state.fundingPaymentHistory,
|
|
87
|
+
eventType: 'fundingPaymentHistoryAccountUpdate',
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
if ((_d = this.optionalExtraSubscriptions) === null || _d === void 0 ? void 0 : _d.includes('fundingRateHistoryAccount')) {
|
|
91
|
+
this.accountsToPoll.set(state.fundingRateHistory.toString(), {
|
|
92
|
+
key: 'fundingRateHistory',
|
|
93
|
+
publicKey: state.fundingRateHistory,
|
|
94
|
+
eventType: 'fundingRateHistoryAccountUpdate',
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
if ((_e = this.optionalExtraSubscriptions) === null || _e === void 0 ? void 0 : _e.includes('curveHistoryAccount')) {
|
|
98
|
+
this.accountsToPoll.set(state.extendedCurveHistory.toString(), {
|
|
99
|
+
key: 'extendedCurveHistory',
|
|
100
|
+
publicKey: state.extendedCurveHistory,
|
|
101
|
+
eventType: 'curveHistoryAccountUpdate',
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
if ((_f = this.optionalExtraSubscriptions) === null || _f === void 0 ? void 0 : _f.includes('liquidationHistoryAccount')) {
|
|
105
|
+
this.accountsToPoll.set(state.liquidationHistory.toString(), {
|
|
106
|
+
key: 'liquidationHistory',
|
|
107
|
+
publicKey: state.liquidationHistory,
|
|
108
|
+
eventType: 'liquidationHistoryAccountUpdate',
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
addToAccountLoader() {
|
|
114
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
116
|
+
accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, (buffer) => {
|
|
117
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
118
|
+
this[accountToPoll.key] = account;
|
|
119
|
+
// @ts-ignore
|
|
120
|
+
this.eventEmitter.emit(accountToPoll.eventType, account);
|
|
121
|
+
this.eventEmitter.emit('update');
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
|
|
125
|
+
this.eventEmitter.emit('error', error);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
fetch() {
|
|
130
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
131
|
+
yield this.accountLoader.load();
|
|
132
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
133
|
+
const buffer = this.accountLoader.getAccountData(accountToPoll.publicKey);
|
|
134
|
+
if (buffer) {
|
|
135
|
+
this[accountToPoll.key] = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
unsubscribe() {
|
|
141
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
142
|
+
if (!this.isSubscribed) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
146
|
+
this.accountLoader.removeAccount(accountToPoll.publicKey, accountToPoll.callbackId);
|
|
147
|
+
}
|
|
148
|
+
this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
|
|
149
|
+
this.errorCallbackId = undefined;
|
|
150
|
+
this.accountsToPoll.clear();
|
|
151
|
+
this.isSubscribed = false;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
assertIsSubscribed() {
|
|
155
|
+
if (!this.isSubscribed) {
|
|
156
|
+
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
assertOptionalIsSubscribed(optionalSubscription) {
|
|
160
|
+
if (!this.isSubscribed) {
|
|
161
|
+
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
162
|
+
}
|
|
163
|
+
if (!this.optionalExtraSubscriptions.includes(optionalSubscription)) {
|
|
164
|
+
throw new types_1.NotSubscribedError(`You need to subscribe to the optional Clearing House account "${optionalSubscription}" to use this method`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
getStateAccount() {
|
|
168
|
+
this.assertIsSubscribed();
|
|
169
|
+
return this.state;
|
|
170
|
+
}
|
|
171
|
+
getMarketsAccount() {
|
|
172
|
+
this.assertIsSubscribed();
|
|
173
|
+
return this.markets;
|
|
174
|
+
}
|
|
175
|
+
getTradeHistoryAccount() {
|
|
176
|
+
this.assertIsSubscribed();
|
|
177
|
+
this.assertOptionalIsSubscribed('tradeHistoryAccount');
|
|
178
|
+
return this.tradeHistory;
|
|
179
|
+
}
|
|
180
|
+
getDepositHistoryAccount() {
|
|
181
|
+
this.assertIsSubscribed();
|
|
182
|
+
this.assertOptionalIsSubscribed('depositHistoryAccount');
|
|
183
|
+
return this.depositHistory;
|
|
184
|
+
}
|
|
185
|
+
getFundingPaymentHistoryAccount() {
|
|
186
|
+
this.assertIsSubscribed();
|
|
187
|
+
this.assertOptionalIsSubscribed('fundingPaymentHistoryAccount');
|
|
188
|
+
return this.fundingPaymentHistory;
|
|
189
|
+
}
|
|
190
|
+
getFundingRateHistoryAccount() {
|
|
191
|
+
this.assertIsSubscribed();
|
|
192
|
+
this.assertOptionalIsSubscribed('fundingRateHistoryAccount');
|
|
193
|
+
return this.fundingRateHistory;
|
|
194
|
+
}
|
|
195
|
+
getCurveHistoryAccount() {
|
|
196
|
+
this.assertIsSubscribed();
|
|
197
|
+
this.assertOptionalIsSubscribed('curveHistoryAccount');
|
|
198
|
+
return this.extendedCurveHistory;
|
|
199
|
+
}
|
|
200
|
+
getLiquidationHistoryAccount() {
|
|
201
|
+
this.assertIsSubscribed();
|
|
202
|
+
this.assertOptionalIsSubscribed('liquidationHistoryAccount');
|
|
203
|
+
return this.liquidationHistory;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
exports.PollingClearingHouseAccountSubscriber = PollingClearingHouseAccountSubscriber;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { TokenAccountEvents, TokenAccountSubscriber } from './types';
|
|
3
|
+
import { Program } from '@project-serum/anchor';
|
|
4
|
+
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
|
+
import { EventEmitter } from 'events';
|
|
6
|
+
import { PublicKey } from '@solana/web3.js';
|
|
7
|
+
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
8
|
+
import { AccountInfo } from '@solana/spl-token';
|
|
9
|
+
export declare class PollingTokenAccountSubscriber implements TokenAccountSubscriber {
|
|
10
|
+
isSubscribed: boolean;
|
|
11
|
+
program: Program;
|
|
12
|
+
eventEmitter: StrictEventEmitter<EventEmitter, TokenAccountEvents>;
|
|
13
|
+
publicKey: PublicKey;
|
|
14
|
+
accountLoader: BulkAccountLoader;
|
|
15
|
+
callbackId?: string;
|
|
16
|
+
errorCallbackId?: string;
|
|
17
|
+
tokenAccount?: AccountInfo;
|
|
18
|
+
constructor(publicKey: PublicKey, accountLoader: BulkAccountLoader);
|
|
19
|
+
subscribe(): Promise<boolean>;
|
|
20
|
+
addToAccountLoader(): void;
|
|
21
|
+
fetch(): Promise<void>;
|
|
22
|
+
unsubscribe(): Promise<void>;
|
|
23
|
+
assertIsSubscribed(): void;
|
|
24
|
+
getTokenAccount(): AccountInfo;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=pollingTokenAccountSubscriber.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pollingTokenAccountSubscriber.d.ts","sourceRoot":"","sources":["../../src/accounts/pollingTokenAccountSubscriber.ts"],"names":[],"mappings":";AAAA,OAAO,EAEN,kBAAkB,EAClB,sBAAsB,EACtB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,kBAAkB,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGhD,qBAAa,6BAA8B,YAAW,sBAAsB;IAC3E,YAAY,EAAE,OAAO,CAAC;IACtB,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,EAAE,kBAAkB,CAAC,YAAY,EAAE,kBAAkB,CAAC,CAAC;IACnE,SAAS,EAAE,SAAS,CAAC;IAErB,aAAa,EAAE,iBAAiB,CAAC;IACjC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB,YAAY,CAAC,EAAE,WAAW,CAAC;gBAER,SAAS,EAAE,SAAS,EAAE,aAAa,EAAE,iBAAiB;IAOnE,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAanC,kBAAkB,IAAI,IAAI;IAqBpB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAMtB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAclC,kBAAkB,IAAI,IAAI;IAQnB,eAAe,IAAI,WAAW;CAIrC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.PollingTokenAccountSubscriber = void 0;
|
|
13
|
+
const types_1 = require("./types");
|
|
14
|
+
const events_1 = require("events");
|
|
15
|
+
const token_1 = require("../token");
|
|
16
|
+
class PollingTokenAccountSubscriber {
|
|
17
|
+
constructor(publicKey, accountLoader) {
|
|
18
|
+
this.isSubscribed = false;
|
|
19
|
+
this.publicKey = publicKey;
|
|
20
|
+
this.accountLoader = accountLoader;
|
|
21
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
22
|
+
}
|
|
23
|
+
subscribe() {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
if (this.isSubscribed) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
this.addToAccountLoader();
|
|
29
|
+
yield this.fetch();
|
|
30
|
+
this.eventEmitter.emit('update');
|
|
31
|
+
this.isSubscribed = true;
|
|
32
|
+
return true;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
addToAccountLoader() {
|
|
36
|
+
if (this.callbackId) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
this.callbackId = this.accountLoader.addAccount(this.publicKey, (buffer) => {
|
|
40
|
+
const tokenAccount = token_1.parseTokenAccount(buffer);
|
|
41
|
+
this.tokenAccount = tokenAccount;
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
this.eventEmitter.emit('tokenAccountUpdate', tokenAccount);
|
|
44
|
+
this.eventEmitter.emit('update');
|
|
45
|
+
});
|
|
46
|
+
this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
|
|
47
|
+
this.eventEmitter.emit('error', error);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
fetch() {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
yield this.accountLoader.load();
|
|
53
|
+
const buffer = this.accountLoader.getAccountData(this.publicKey);
|
|
54
|
+
this.tokenAccount = token_1.parseTokenAccount(buffer);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
unsubscribe() {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
if (!this.isSubscribed) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
this.accountLoader.removeAccount(this.publicKey, this.callbackId);
|
|
63
|
+
this.callbackId = undefined;
|
|
64
|
+
this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
|
|
65
|
+
this.errorCallbackId = undefined;
|
|
66
|
+
this.isSubscribed = false;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
assertIsSubscribed() {
|
|
70
|
+
if (!this.isSubscribed) {
|
|
71
|
+
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
getTokenAccount() {
|
|
75
|
+
this.assertIsSubscribed();
|
|
76
|
+
return this.tokenAccount;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.PollingTokenAccountSubscriber = PollingTokenAccountSubscriber;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { AccountToPoll, UserAccountEvents, UserAccountSubscriber } from './types';
|
|
3
|
+
import { Program } from '@project-serum/anchor';
|
|
4
|
+
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
|
+
import { EventEmitter } from 'events';
|
|
6
|
+
import { PublicKey } from '@solana/web3.js';
|
|
7
|
+
import { UserAccount, UserPositionsAccount } from '../types';
|
|
8
|
+
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
9
|
+
import { ClearingHouseConfigType } from '../factory/clearingHouse';
|
|
10
|
+
export declare class PollingUserAccountSubscriber implements UserAccountSubscriber {
|
|
11
|
+
isSubscribed: boolean;
|
|
12
|
+
program: Program;
|
|
13
|
+
eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
|
|
14
|
+
authority: PublicKey;
|
|
15
|
+
accountLoader: BulkAccountLoader;
|
|
16
|
+
accountsToPoll: Map<string, AccountToPoll>;
|
|
17
|
+
errorCallbackId?: string;
|
|
18
|
+
user?: UserAccount;
|
|
19
|
+
userPositions?: UserPositionsAccount;
|
|
20
|
+
type: ClearingHouseConfigType;
|
|
21
|
+
constructor(program: Program, authority: PublicKey, accountLoader: BulkAccountLoader);
|
|
22
|
+
subscribe(): Promise<boolean>;
|
|
23
|
+
addToAccountLoader(): Promise<void>;
|
|
24
|
+
fetchIfUnloaded(): Promise<void>;
|
|
25
|
+
fetch(): Promise<void>;
|
|
26
|
+
unsubscribe(): Promise<void>;
|
|
27
|
+
assertIsSubscribed(): void;
|
|
28
|
+
getUserAccount(): UserAccount;
|
|
29
|
+
getUserPositionsAccount(): UserPositionsAccount;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=pollingUserAccountSubscriber.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pollingUserAccountSubscriber.d.ts","sourceRoot":"","sources":["../../src/accounts/pollingUserAccountSubscriber.ts"],"names":[],"mappings":";AAAA,OAAO,EACN,aAAa,EAEb,iBAAiB,EACjB,qBAAqB,EACrB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,kBAAkB,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,OAAO,EAAE,uBAAuB,EAAE,MAAM,0BAA0B,CAAC;AAEnE,qBAAa,4BAA6B,YAAW,qBAAqB;IACzE,YAAY,EAAE,OAAO,CAAC;IACtB,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,EAAE,kBAAkB,CAAC,YAAY,EAAE,iBAAiB,CAAC,CAAC;IAClE,SAAS,EAAE,SAAS,CAAC;IAErB,aAAa,EAAE,iBAAiB,CAAC;IACjC,cAAc,6BAAoC;IAClD,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB,IAAI,CAAC,EAAE,WAAW,CAAC;IACnB,aAAa,CAAC,EAAE,oBAAoB,CAAC;IAErC,IAAI,EAAE,uBAAuB,CAAa;gBAGzC,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,SAAS,EACpB,aAAa,EAAE,iBAAiB;IAS3B,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAa7B,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;IA8CnC,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAchC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAYtB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAoBlC,kBAAkB,IAAI,IAAI;IAQnB,cAAc,IAAI,WAAW;IAK7B,uBAAuB,IAAI,oBAAoB;CAItD"}
|