@drift-labs/sdk 0.1.24 → 0.1.27
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 +1 -0
- package/lib/accounts/bulkAccountLoader.js +16 -2
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +3 -3
- package/lib/accounts/pollingOracleSubscriber.d.ts +26 -0
- package/lib/accounts/pollingOracleSubscriber.js +79 -0
- package/lib/accounts/pollingTokenAccountSubscriber.js +2 -2
- package/lib/accounts/pollingUserAccountSubscriber.js +4 -4
- package/lib/accounts/types.d.ts +14 -1
- package/lib/accounts/webSocketAccountSubscriber.js +2 -2
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +1 -1
- package/lib/accounts/webSocketUserAccountSubscriber.js +2 -2
- package/lib/addresses.js +5 -1
- package/lib/admin.d.ts +1 -1
- package/lib/admin.js +15 -10
- package/lib/clearingHouse.d.ts +4 -4
- package/lib/clearingHouse.js +56 -25
- package/lib/clearingHouseUser.js +18 -18
- package/lib/constants/markets.d.ts +8 -6
- package/lib/constants/markets.js +85 -26
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.js +2 -1
- package/lib/examples/makeTradeExample.js +6 -6
- package/lib/factory/oracleClient.d.ts +5 -0
- package/lib/factory/oracleClient.js +16 -0
- package/lib/idl/clearing_house.json +22 -2
- package/lib/idl/switchboard_v2.json +4663 -0
- package/lib/index.d.ts +5 -1
- package/lib/index.js +10 -2
- package/lib/math/amm.js +12 -12
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +5 -17
- package/lib/math/market.d.ts +2 -0
- package/lib/math/market.js +8 -3
- package/lib/math/orders.js +9 -9
- package/lib/math/position.js +1 -1
- package/lib/math/trade.js +18 -18
- package/lib/mockUSDCFaucet.js +5 -1
- package/lib/oracles/pythClient.d.ts +14 -0
- package/lib/oracles/pythClient.js +53 -0
- package/lib/oracles/switchboardClient.d.ts +13 -0
- package/lib/oracles/switchboardClient.js +76 -0
- package/lib/oracles/types.d.ts +15 -0
- package/lib/oracles/types.js +2 -0
- package/lib/orderParams.d.ts +1 -1
- package/lib/orderParams.js +3 -3
- package/lib/orders.d.ts +3 -2
- package/lib/orders.js +29 -22
- package/lib/tx/retryTxSender.js +1 -1
- package/lib/types.d.ts +1 -0
- package/package.json +2 -1
- package/src/accounts/bulkAccountLoader.js +180 -0
- package/src/accounts/bulkAccountLoader.js.map +1 -0
- package/src/accounts/bulkAccountLoader.ts +19 -0
- package/src/accounts/bulkUserSubscription.js +56 -0
- package/src/accounts/bulkUserSubscription.js.map +1 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +210 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js.map +1 -0
- package/src/accounts/pollingOracleSubscriber.js +65 -0
- package/src/accounts/pollingOracleSubscriber.js.map +1 -0
- package/src/accounts/pollingOracleSubscriber.ts +103 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +65 -0
- package/src/accounts/pollingTokenAccountSubscriber.js.map +1 -0
- package/src/accounts/pollingUserAccountSubscriber.js +139 -0
- package/src/accounts/pollingUserAccountSubscriber.js.map +1 -0
- package/src/accounts/types.js +1 -0
- package/src/accounts/types.js.map +1 -0
- package/src/accounts/types.ts +22 -1
- package/src/accounts/utils.js +1 -0
- package/src/accounts/utils.js.map +1 -0
- package/src/accounts/webSocketAccountSubscriber.js +15 -27
- package/src/accounts/webSocketAccountSubscriber.js.map +1 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +212 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js.map +1 -0
- package/src/accounts/webSocketUserAccountSubscriber.js +78 -0
- package/src/accounts/webSocketUserAccountSubscriber.js.map +1 -0
- package/src/addresses.js +20 -44
- package/src/addresses.js.map +1 -0
- package/src/admin.js +443 -0
- package/src/admin.js.map +1 -0
- package/src/admin.ts +2 -0
- package/src/assert/assert.js +10 -0
- package/src/assert/assert.js.map +1 -0
- package/src/clearingHouse.js +769 -0
- package/src/clearingHouse.js.map +1 -0
- package/src/clearingHouse.ts +47 -7
- package/src/clearingHouseUser.js +581 -0
- package/src/clearingHouseUser.js.map +1 -0
- package/src/config.js +37 -0
- package/src/config.js.map +1 -0
- package/src/constants/markets.js +167 -0
- package/src/constants/markets.js.map +1 -0
- package/src/constants/markets.ts +93 -31
- package/src/constants/numericConstants.js +22 -0
- package/src/constants/numericConstants.js.map +1 -0
- package/src/constants/numericConstants.ts +1 -0
- package/src/factory/clearingHouse.js +65 -0
- package/src/factory/clearingHouse.js.map +1 -0
- package/src/factory/clearingHouseUser.js +35 -0
- package/src/factory/clearingHouseUser.js.map +1 -0
- package/src/factory/oracleClient.js +17 -0
- package/src/factory/oracleClient.js.map +1 -0
- package/src/factory/oracleClient.ts +22 -0
- package/src/idl/clearing_house.json +22 -2
- package/src/idl/switchboard_v2.json +4663 -0
- package/src/index.js +56 -0
- package/src/index.js.map +1 -0
- package/src/index.ts +5 -1
- package/src/math/amm.js +285 -0
- package/src/math/amm.js.map +1 -0
- package/src/math/conversion.js +16 -0
- package/src/math/conversion.js.map +1 -0
- package/src/math/funding.js +223 -0
- package/src/math/funding.js.map +1 -0
- package/src/math/funding.ts +9 -25
- package/src/math/insuranceFund.js +23 -0
- package/src/math/insuranceFund.js.map +1 -0
- package/src/math/market.js +30 -0
- package/src/math/market.js.map +1 -0
- package/src/math/market.ts +9 -0
- package/src/math/orders.js +73 -0
- package/src/math/orders.js.map +1 -0
- package/src/math/position.js +121 -0
- package/src/math/position.js.map +1 -0
- package/src/math/trade.js +182 -0
- package/src/math/trade.js.map +1 -0
- package/src/math/utils.js +27 -0
- package/src/math/utils.js.map +1 -0
- package/src/mockUSDCFaucet.js +88 -116
- package/src/mockUSDCFaucet.js.map +1 -0
- package/src/oracles/pythClient.js +39 -0
- package/src/oracles/pythClient.js.map +1 -0
- package/src/oracles/pythClient.ts +49 -0
- package/src/oracles/switchboardClient.js +60 -0
- package/src/oracles/switchboardClient.js.map +1 -0
- package/src/oracles/switchboardClient.ts +87 -0
- package/src/oracles/types.js +3 -0
- package/src/oracles/types.js.map +1 -0
- package/src/oracles/types.ts +15 -0
- package/src/orderParams.js +109 -0
- package/src/orderParams.js.map +1 -0
- package/src/orderParams.ts +5 -3
- package/src/orders.js +172 -0
- package/src/orders.js.map +1 -0
- package/src/orders.ts +17 -4
- package/src/token/index.js +39 -0
- package/src/token/index.js.map +1 -0
- package/src/tx/defaultTxSender.js +13 -0
- package/src/tx/defaultTxSender.js.map +1 -0
- package/src/tx/retryTxSender.js +137 -0
- package/src/tx/retryTxSender.js.map +1 -0
- package/src/tx/types.js +3 -0
- package/src/tx/types.js.map +1 -0
- package/src/tx/utils.js +9 -0
- package/src/tx/utils.js.map +1 -0
- package/src/types.js +1 -0
- package/src/types.js.map +1 -0
- package/src/types.ts +1 -0
- package/src/util/computeUnits.js +17 -0
- package/src/util/computeUnits.js.map +1 -0
- package/src/util/tps.js +17 -0
- package/src/util/tps.js.map +1 -0
- package/src/wallet.js +23 -0
- package/src/wallet.js.map +1 -0
- package/lib/pythClient.d.ts +0 -7
- package/lib/pythClient.js +0 -25
- package/src/pythClient.ts +0 -15
|
@@ -16,6 +16,7 @@ export declare class BulkAccountLoader {
|
|
|
16
16
|
loadPromise?: Promise<void>;
|
|
17
17
|
loadPromiseResolver: () => void;
|
|
18
18
|
loggingEnabled: boolean;
|
|
19
|
+
lastUpdate: number;
|
|
19
20
|
constructor(connection: Connection, commitment: Commitment, pollingFrequency: number);
|
|
20
21
|
addAccount(publicKey: PublicKey, callback: (buffer: Buffer) => void): string;
|
|
21
22
|
removeAccount(publicKey: PublicKey, callbackId: string): void;
|
|
@@ -10,21 +10,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.BulkAccountLoader = void 0;
|
|
13
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
13
14
|
const uuid_1 = require("uuid");
|
|
14
15
|
const GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE = 99;
|
|
16
|
+
const fiveMinutes = 5 * 60 * 1000;
|
|
15
17
|
class BulkAccountLoader {
|
|
16
18
|
constructor(connection, commitment, pollingFrequency) {
|
|
17
19
|
this.accountsToLoad = new Map();
|
|
18
20
|
this.accountData = new Map();
|
|
19
21
|
this.errorCallbacks = new Map();
|
|
20
22
|
this.loggingEnabled = false;
|
|
23
|
+
this.lastUpdate = Date.now();
|
|
21
24
|
this.connection = connection;
|
|
22
25
|
this.commitment = commitment;
|
|
23
26
|
this.pollingFrequency = pollingFrequency;
|
|
24
27
|
}
|
|
25
28
|
addAccount(publicKey, callback) {
|
|
26
29
|
const existingSize = this.accountsToLoad.size;
|
|
27
|
-
const callbackId = uuid_1.v4();
|
|
30
|
+
const callbackId = (0, uuid_1.v4)();
|
|
28
31
|
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
29
32
|
if (existingAccountToLoad) {
|
|
30
33
|
existingAccountToLoad.callbacks.set(callbackId, callback);
|
|
@@ -58,7 +61,7 @@ class BulkAccountLoader {
|
|
|
58
61
|
}
|
|
59
62
|
}
|
|
60
63
|
addErrorCallbacks(callback) {
|
|
61
|
-
const callbackId = uuid_1.v4();
|
|
64
|
+
const callbackId = (0, uuid_1.v4)();
|
|
62
65
|
this.errorCallbacks.set(callbackId, callback);
|
|
63
66
|
return callbackId;
|
|
64
67
|
}
|
|
@@ -98,6 +101,15 @@ class BulkAccountLoader {
|
|
|
98
101
|
finally {
|
|
99
102
|
this.loadPromiseResolver();
|
|
100
103
|
this.loadPromise = undefined;
|
|
104
|
+
const now = Date.now();
|
|
105
|
+
if (now - this.lastUpdate > fiveMinutes) {
|
|
106
|
+
if (this.loggingEnabled) {
|
|
107
|
+
console.log("Haven't seen updated account in five minutes. Bulk account loader creating new Connection Object");
|
|
108
|
+
}
|
|
109
|
+
this.connection = new web3_js_1.Connection(
|
|
110
|
+
// @ts-ignore
|
|
111
|
+
this.connection._rpcEndpoint, this.connection.commitment);
|
|
112
|
+
}
|
|
101
113
|
}
|
|
102
114
|
});
|
|
103
115
|
}
|
|
@@ -131,6 +143,7 @@ class BulkAccountLoader {
|
|
|
131
143
|
buffer: newBuffer,
|
|
132
144
|
});
|
|
133
145
|
this.handleAccountCallbacks(accountToLoad, newBuffer);
|
|
146
|
+
this.lastUpdate = Date.now();
|
|
134
147
|
continue;
|
|
135
148
|
}
|
|
136
149
|
if (newSlot <= oldRPCResponse.slot) {
|
|
@@ -143,6 +156,7 @@ class BulkAccountLoader {
|
|
|
143
156
|
buffer: newBuffer,
|
|
144
157
|
});
|
|
145
158
|
this.handleAccountCallbacks(accountToLoad, newBuffer);
|
|
159
|
+
this.lastUpdate = Date.now();
|
|
146
160
|
}
|
|
147
161
|
}
|
|
148
162
|
});
|
|
@@ -54,7 +54,7 @@ class PollingClearingHouseAccountSubscriber {
|
|
|
54
54
|
if (this.accountsToPoll.size > 0) {
|
|
55
55
|
return;
|
|
56
56
|
}
|
|
57
|
-
const statePublicKey = yield addresses_1.getClearingHouseStateAccountPublicKey(this.program.programId);
|
|
57
|
+
const statePublicKey = yield (0, addresses_1.getClearingHouseStateAccountPublicKey)(this.program.programId);
|
|
58
58
|
const state = (yield this.program.account.state.fetch(statePublicKey));
|
|
59
59
|
this.accountsToPoll.set(statePublicKey.toString(), {
|
|
60
60
|
key: 'state',
|
|
@@ -127,7 +127,7 @@ class PollingClearingHouseAccountSubscriber {
|
|
|
127
127
|
return __awaiter(this, void 0, void 0, function* () {
|
|
128
128
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
129
129
|
accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, (buffer) => {
|
|
130
|
-
const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
130
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
131
131
|
this[accountToPoll.key] = account;
|
|
132
132
|
// @ts-ignore
|
|
133
133
|
this.eventEmitter.emit(accountToPoll.eventType, account);
|
|
@@ -145,7 +145,7 @@ class PollingClearingHouseAccountSubscriber {
|
|
|
145
145
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
146
146
|
const buffer = this.accountLoader.getAccountData(accountToPoll.publicKey);
|
|
147
147
|
if (buffer) {
|
|
148
|
-
this[accountToPoll.key] = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
148
|
+
this[accountToPoll.key] = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
151
|
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { OracleEvents, OracleSubscriber } 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 { OracleClient, OraclePriceData } from '../oracles/types';
|
|
9
|
+
export declare class PollingOracleSubscriber implements OracleSubscriber {
|
|
10
|
+
isSubscribed: boolean;
|
|
11
|
+
program: Program;
|
|
12
|
+
eventEmitter: StrictEventEmitter<EventEmitter, OracleEvents>;
|
|
13
|
+
publicKey: PublicKey;
|
|
14
|
+
accountLoader: BulkAccountLoader;
|
|
15
|
+
oracleClient: OracleClient;
|
|
16
|
+
callbackId?: string;
|
|
17
|
+
errorCallbackId?: string;
|
|
18
|
+
oraclePriceData?: OraclePriceData;
|
|
19
|
+
constructor(publicKey: PublicKey, oracleClient: OracleClient, accountLoader: BulkAccountLoader);
|
|
20
|
+
subscribe(): Promise<boolean>;
|
|
21
|
+
addToAccountLoader(): void;
|
|
22
|
+
fetch(): Promise<void>;
|
|
23
|
+
unsubscribe(): Promise<void>;
|
|
24
|
+
assertIsSubscribed(): void;
|
|
25
|
+
getOraclePriceData(): OraclePriceData;
|
|
26
|
+
}
|
|
@@ -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.PollingOracleSubscriber = void 0;
|
|
13
|
+
const types_1 = require("./types");
|
|
14
|
+
const events_1 = require("events");
|
|
15
|
+
class PollingOracleSubscriber {
|
|
16
|
+
constructor(publicKey, oracleClient, accountLoader) {
|
|
17
|
+
this.isSubscribed = false;
|
|
18
|
+
this.publicKey = publicKey;
|
|
19
|
+
this.oracleClient = oracleClient;
|
|
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) => __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
const oraclePriceData = yield this.oracleClient.getOraclePriceDataFromBuffer(buffer);
|
|
41
|
+
this.oraclePriceData = oraclePriceData;
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
this.eventEmitter.emit('oracleUpdate', oraclePriceData);
|
|
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.oraclePriceData = yield this.oracleClient.getOraclePriceDataFromBuffer(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
|
+
getOraclePriceData() {
|
|
75
|
+
this.assertIsSubscribed();
|
|
76
|
+
return this.oraclePriceData;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.PollingOracleSubscriber = PollingOracleSubscriber;
|
|
@@ -37,7 +37,7 @@ class PollingTokenAccountSubscriber {
|
|
|
37
37
|
return;
|
|
38
38
|
}
|
|
39
39
|
this.callbackId = this.accountLoader.addAccount(this.publicKey, (buffer) => {
|
|
40
|
-
const tokenAccount = token_1.parseTokenAccount(buffer);
|
|
40
|
+
const tokenAccount = (0, token_1.parseTokenAccount)(buffer);
|
|
41
41
|
this.tokenAccount = tokenAccount;
|
|
42
42
|
// @ts-ignore
|
|
43
43
|
this.eventEmitter.emit('tokenAccountUpdate', tokenAccount);
|
|
@@ -51,7 +51,7 @@ class PollingTokenAccountSubscriber {
|
|
|
51
51
|
return __awaiter(this, void 0, void 0, function* () {
|
|
52
52
|
yield this.accountLoader.load();
|
|
53
53
|
const buffer = this.accountLoader.getAccountData(this.publicKey);
|
|
54
|
-
this.tokenAccount = token_1.parseTokenAccount(buffer);
|
|
54
|
+
this.tokenAccount = (0, token_1.parseTokenAccount)(buffer);
|
|
55
55
|
});
|
|
56
56
|
}
|
|
57
57
|
unsubscribe() {
|
|
@@ -42,7 +42,7 @@ class PollingUserAccountSubscriber {
|
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
44
|
if (!userPublicKeys) {
|
|
45
|
-
const userPublicKey = yield addresses_1.getUserAccountPublicKey(this.program.programId, this.authority);
|
|
45
|
+
const userPublicKey = yield (0, addresses_1.getUserAccountPublicKey)(this.program.programId, this.authority);
|
|
46
46
|
const userAccount = (yield this.program.account.user.fetch(userPublicKey));
|
|
47
47
|
this.accountsToPoll.set(userPublicKey.toString(), {
|
|
48
48
|
key: 'user',
|
|
@@ -54,7 +54,7 @@ class PollingUserAccountSubscriber {
|
|
|
54
54
|
publicKey: userAccount.positions,
|
|
55
55
|
eventType: 'userPositionsData',
|
|
56
56
|
});
|
|
57
|
-
const userOrdersPublicKey = yield addresses_1.getUserOrdersAccountPublicKey(this.program.programId, userPublicKey);
|
|
57
|
+
const userOrdersPublicKey = yield (0, addresses_1.getUserOrdersAccountPublicKey)(this.program.programId, userPublicKey);
|
|
58
58
|
this.accountsToPoll.set(userOrdersPublicKey.toString(), {
|
|
59
59
|
key: 'userOrders',
|
|
60
60
|
publicKey: userOrdersPublicKey,
|
|
@@ -85,7 +85,7 @@ class PollingUserAccountSubscriber {
|
|
|
85
85
|
if (!buffer) {
|
|
86
86
|
return;
|
|
87
87
|
}
|
|
88
|
-
const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
88
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
89
89
|
this[accountToPoll.key] = account;
|
|
90
90
|
// @ts-ignore
|
|
91
91
|
this.eventEmitter.emit(accountToPoll.eventType, account);
|
|
@@ -117,7 +117,7 @@ class PollingUserAccountSubscriber {
|
|
|
117
117
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
118
118
|
const buffer = this.accountLoader.getAccountData(accountToPoll.publicKey);
|
|
119
119
|
if (buffer) {
|
|
120
|
-
this[accountToPoll.key] = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
120
|
+
this[accountToPoll.key] = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
});
|
package/lib/accounts/types.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import StrictEventEmitter from 'strict-event-emitter-types';
|
|
|
4
4
|
import { EventEmitter } from 'events';
|
|
5
5
|
import { PublicKey } from '@solana/web3.js';
|
|
6
6
|
import { AccountInfo } from '@solana/spl-token';
|
|
7
|
-
import { ClearingHouseConfigType, ClearingHouseUserConfigType } from '..';
|
|
7
|
+
import { ClearingHouseConfigType, ClearingHouseUserConfigType, OraclePriceData } from '..';
|
|
8
8
|
export interface AccountSubscriber<T> {
|
|
9
9
|
data?: T;
|
|
10
10
|
subscribe(onChange: (data: T) => void): Promise<void>;
|
|
@@ -84,6 +84,19 @@ export interface TokenAccountSubscriber {
|
|
|
84
84
|
unsubscribe(): Promise<void>;
|
|
85
85
|
getTokenAccount(): AccountInfo;
|
|
86
86
|
}
|
|
87
|
+
export interface OracleEvents {
|
|
88
|
+
oracleUpdate: (payload: OraclePriceData) => void;
|
|
89
|
+
update: void;
|
|
90
|
+
error: (e: Error) => void;
|
|
91
|
+
}
|
|
92
|
+
export interface OracleSubscriber {
|
|
93
|
+
eventEmitter: StrictEventEmitter<EventEmitter, OracleEvents>;
|
|
94
|
+
isSubscribed: boolean;
|
|
95
|
+
subscribe(): Promise<boolean>;
|
|
96
|
+
fetch(): Promise<void>;
|
|
97
|
+
unsubscribe(): Promise<void>;
|
|
98
|
+
getOraclePriceData(): OraclePriceData;
|
|
99
|
+
}
|
|
87
100
|
export declare type AccountToPoll = {
|
|
88
101
|
key: string;
|
|
89
102
|
publicKey: PublicKey;
|
|
@@ -47,7 +47,7 @@ class WebSocketAccountSubscriber {
|
|
|
47
47
|
slot: newSlot,
|
|
48
48
|
};
|
|
49
49
|
if (newBuffer) {
|
|
50
|
-
this.data = this.program.account[this.accountName].coder.accounts.decode(utils_1.capitalize(this.accountName), newBuffer);
|
|
50
|
+
this.data = this.program.account[this.accountName].coder.accounts.decode((0, utils_1.capitalize)(this.accountName), newBuffer);
|
|
51
51
|
this.onChange(this.data);
|
|
52
52
|
}
|
|
53
53
|
return;
|
|
@@ -61,7 +61,7 @@ class WebSocketAccountSubscriber {
|
|
|
61
61
|
buffer: newBuffer,
|
|
62
62
|
slot: newSlot,
|
|
63
63
|
};
|
|
64
|
-
this.data = this.program.account[this.accountName].coder.accounts.decode(utils_1.capitalize(this.accountName), newBuffer);
|
|
64
|
+
this.data = this.program.account[this.accountName].coder.accounts.decode((0, utils_1.capitalize)(this.accountName), newBuffer);
|
|
65
65
|
this.onChange(this.data);
|
|
66
66
|
}
|
|
67
67
|
}
|
|
@@ -35,7 +35,7 @@ class WebSocketClearingHouseAccountSubscriber {
|
|
|
35
35
|
this.subscriptionPromise = new Promise((res) => {
|
|
36
36
|
this.subscriptionPromiseResolver = res;
|
|
37
37
|
});
|
|
38
|
-
const statePublicKey = yield addresses_1.getClearingHouseStateAccountPublicKey(this.program.programId);
|
|
38
|
+
const statePublicKey = yield (0, addresses_1.getClearingHouseStateAccountPublicKey)(this.program.programId);
|
|
39
39
|
// create and activate main state account subscription
|
|
40
40
|
this.stateAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('state', this.program, statePublicKey);
|
|
41
41
|
yield this.stateAccountSubscriber.subscribe((data) => {
|
|
@@ -27,7 +27,7 @@ class WebSocketUserAccountSubscriber {
|
|
|
27
27
|
if (this.isSubscribed) {
|
|
28
28
|
return true;
|
|
29
29
|
}
|
|
30
|
-
const userPublicKey = yield addresses_1.getUserAccountPublicKey(this.program.programId, this.authority);
|
|
30
|
+
const userPublicKey = yield (0, addresses_1.getUserAccountPublicKey)(this.program.programId, this.authority);
|
|
31
31
|
this.userDataAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('user', this.program, userPublicKey);
|
|
32
32
|
yield this.userDataAccountSubscriber.subscribe((data) => {
|
|
33
33
|
this.eventEmitter.emit('userAccountData', data);
|
|
@@ -39,7 +39,7 @@ class WebSocketUserAccountSubscriber {
|
|
|
39
39
|
this.eventEmitter.emit('userPositionsData', data);
|
|
40
40
|
this.eventEmitter.emit('update');
|
|
41
41
|
});
|
|
42
|
-
const userOrdersPublicKey = yield addresses_1.getUserOrdersAccountPublicKey(this.program.programId, userPublicKey);
|
|
42
|
+
const userOrdersPublicKey = yield (0, addresses_1.getUserOrdersAccountPublicKey)(this.program.programId, userPublicKey);
|
|
43
43
|
this.userOrdersAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('userOrders', this.program, userOrdersPublicKey);
|
|
44
44
|
yield this.userOrdersAccountSubscriber.subscribe((data) => {
|
|
45
45
|
this.eventEmitter.emit('userOrdersData', data);
|
package/lib/addresses.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
package/lib/admin.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export declare class Admin extends ClearingHouse {
|
|
|
11
11
|
TransactionSignature
|
|
12
12
|
]>;
|
|
13
13
|
initializeOrderState(): Promise<TransactionSignature>;
|
|
14
|
-
initializeMarket(marketIndex: BN, priceOracle: PublicKey, baseAssetReserve: BN, quoteAssetReserve: BN, periodicity: BN, pegMultiplier?: BN, marginRatioInitial?: number, marginRatioPartial?: number, marginRatioMaintenance?: number): Promise<TransactionSignature>;
|
|
14
|
+
initializeMarket(marketIndex: BN, priceOracle: PublicKey, baseAssetReserve: BN, quoteAssetReserve: BN, periodicity: BN, pegMultiplier?: BN, oracleSource?: OracleSource, marginRatioInitial?: number, marginRatioPartial?: number, marginRatioMaintenance?: number): Promise<TransactionSignature>;
|
|
15
15
|
moveAmmPrice(baseAssetReserve: BN, quoteAssetReserve: BN, marketIndex: BN): Promise<TransactionSignature>;
|
|
16
16
|
updateK(sqrtK: BN, marketIndex: BN): Promise<TransactionSignature>;
|
|
17
17
|
updateCurveHistory(): Promise<TransactionSignature>;
|
package/lib/admin.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -30,6 +34,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
30
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
35
|
exports.Admin = void 0;
|
|
32
36
|
const web3_js_1 = require("@solana/web3.js");
|
|
37
|
+
const types_1 = require("./types");
|
|
33
38
|
const anchor_1 = require("@project-serum/anchor");
|
|
34
39
|
const anchor = __importStar(require("@project-serum/anchor"));
|
|
35
40
|
const addresses_1 = require("./addresses");
|
|
@@ -41,8 +46,8 @@ const amm_1 = require("./math/amm");
|
|
|
41
46
|
const clearingHouse_2 = require("./factory/clearingHouse");
|
|
42
47
|
class Admin extends clearingHouse_1.ClearingHouse {
|
|
43
48
|
static from(connection, wallet, clearingHouseProgramId, opts = anchor_1.Provider.defaultOptions()) {
|
|
44
|
-
const config = clearingHouse_2.getWebSocketClearingHouseConfig(connection, wallet, clearingHouseProgramId, opts);
|
|
45
|
-
return clearingHouse_2.getAdmin(config);
|
|
49
|
+
const config = (0, clearingHouse_2.getWebSocketClearingHouseConfig)(connection, wallet, clearingHouseProgramId, opts);
|
|
50
|
+
return (0, clearingHouse_2.getAdmin)(config);
|
|
46
51
|
}
|
|
47
52
|
initialize(usdcMint, adminControlsPrices) {
|
|
48
53
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -61,7 +66,7 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
61
66
|
const tradeHistory = anchor.web3.Keypair.generate();
|
|
62
67
|
const liquidationHistory = anchor.web3.Keypair.generate();
|
|
63
68
|
const curveHistory = anchor.web3.Keypair.generate();
|
|
64
|
-
const [clearingHouseStatePublicKey, clearingHouseNonce] = yield addresses_1.getClearingHouseStateAccountPublicKeyAndNonce(this.program.programId);
|
|
69
|
+
const [clearingHouseStatePublicKey, clearingHouseNonce] = yield (0, addresses_1.getClearingHouseStateAccountPublicKeyAndNonce)(this.program.programId);
|
|
65
70
|
const initializeTx = yield this.program.transaction.initialize(clearingHouseNonce, collateralVaultNonce, insuranceVaultNonce, adminControlsPrices, {
|
|
66
71
|
accounts: {
|
|
67
72
|
admin: this.wallet.publicKey,
|
|
@@ -118,8 +123,8 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
118
123
|
initializeOrderState() {
|
|
119
124
|
return __awaiter(this, void 0, void 0, function* () {
|
|
120
125
|
const orderHistory = anchor.web3.Keypair.generate();
|
|
121
|
-
const [orderStatePublicKey, orderStateNonce] = yield addresses_1.getOrderStateAccountPublicKeyAndNonce(this.program.programId);
|
|
122
|
-
const clearingHouseStatePublicKey = yield addresses_1.getClearingHouseStateAccountPublicKey(this.program.programId);
|
|
126
|
+
const [orderStatePublicKey, orderStateNonce] = yield (0, addresses_1.getOrderStateAccountPublicKeyAndNonce)(this.program.programId);
|
|
127
|
+
const clearingHouseStatePublicKey = yield (0, addresses_1.getClearingHouseStateAccountPublicKey)(this.program.programId);
|
|
123
128
|
const initializeOrderStateTx = yield this.program.transaction.initializeOrderState(orderStateNonce, {
|
|
124
129
|
accounts: {
|
|
125
130
|
admin: this.wallet.publicKey,
|
|
@@ -136,12 +141,12 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
136
141
|
return yield this.txSender.send(initializeOrderStateTx, [orderHistory], this.opts);
|
|
137
142
|
});
|
|
138
143
|
}
|
|
139
|
-
initializeMarket(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, marginRatioInitial = 2000, marginRatioPartial = 625, marginRatioMaintenance = 500) {
|
|
144
|
+
initializeMarket(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH, marginRatioInitial = 2000, marginRatioPartial = 625, marginRatioMaintenance = 500) {
|
|
140
145
|
return __awaiter(this, void 0, void 0, function* () {
|
|
141
146
|
if (this.getMarketsAccount().markets[marketIndex.toNumber()].initialized) {
|
|
142
147
|
throw Error(`MarketIndex ${marketIndex.toNumber()} already initialized`);
|
|
143
148
|
}
|
|
144
|
-
const initializeMarketTx = yield this.program.transaction.initializeMarket(marketIndex, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, marginRatioInitial, marginRatioPartial, marginRatioMaintenance, {
|
|
149
|
+
const initializeMarketTx = yield this.program.transaction.initializeMarket(marketIndex, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, marginRatioInitial, marginRatioPartial, marginRatioMaintenance, {
|
|
145
150
|
accounts: {
|
|
146
151
|
state: yield this.getStatePublicKey(),
|
|
147
152
|
admin: this.wallet.publicKey,
|
|
@@ -202,8 +207,8 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
202
207
|
moveAmmToPrice(marketIndex, targetPrice) {
|
|
203
208
|
return __awaiter(this, void 0, void 0, function* () {
|
|
204
209
|
const market = this.getMarket(marketIndex);
|
|
205
|
-
const [direction, tradeSize, _] = trade_1.calculateTargetPriceTrade(market, targetPrice);
|
|
206
|
-
const [newQuoteAssetAmount, newBaseAssetAmount] = amm_1.calculateAmmReservesAfterSwap(market.amm, 'quote', tradeSize, amm_1.getSwapDirection('quote', direction));
|
|
210
|
+
const [direction, tradeSize, _] = (0, trade_1.calculateTargetPriceTrade)(market, targetPrice);
|
|
211
|
+
const [newQuoteAssetAmount, newBaseAssetAmount] = (0, amm_1.calculateAmmReservesAfterSwap)(market.amm, 'quote', tradeSize, (0, amm_1.getSwapDirection)('quote', direction));
|
|
207
212
|
const state = this.getStateAccount();
|
|
208
213
|
return yield this.program.rpc.moveAmmPrice(newBaseAssetAmount, newQuoteAssetAmount, marketIndex, {
|
|
209
214
|
accounts: {
|
package/lib/clearingHouse.d.ts
CHANGED
|
@@ -121,10 +121,10 @@ export declare class ClearingHouse {
|
|
|
121
121
|
initializeUserOrdersThenPlaceOrder(orderParams: OrderParams, discountToken?: PublicKey, referrer?: PublicKey): Promise<TransactionSignature>;
|
|
122
122
|
placeOrder(orderParams: OrderParams, discountToken?: PublicKey, referrer?: PublicKey): Promise<TransactionSignature>;
|
|
123
123
|
getPlaceOrderIx(orderParams: OrderParams, discountToken?: PublicKey, referrer?: PublicKey): Promise<TransactionInstruction>;
|
|
124
|
-
cancelOrder(orderId: BN): Promise<TransactionSignature>;
|
|
125
|
-
getCancelOrderIx(orderId: BN): Promise<TransactionInstruction>;
|
|
126
|
-
cancelOrderByUserId(userOrderId: number): Promise<TransactionSignature>;
|
|
127
|
-
getCancelOrderByUserIdIx(userOrderId: number): Promise<TransactionInstruction>;
|
|
124
|
+
cancelOrder(orderId: BN, oracle?: PublicKey): Promise<TransactionSignature>;
|
|
125
|
+
getCancelOrderIx(orderId: BN, oracle?: PublicKey): Promise<TransactionInstruction>;
|
|
126
|
+
cancelOrderByUserId(userOrderId: number, oracle?: PublicKey): Promise<TransactionSignature>;
|
|
127
|
+
getCancelOrderByUserIdIx(userOrderId: number, oracle?: PublicKey): Promise<TransactionInstruction>;
|
|
128
128
|
fillOrder(userAccountPublicKey: PublicKey, userOrdersAccountPublicKey: PublicKey, order: Order): Promise<TransactionSignature>;
|
|
129
129
|
getFillOrderIx(userAccountPublicKey: PublicKey, userOrdersAccountPublicKey: PublicKey, order: Order): Promise<TransactionInstruction>;
|
|
130
130
|
initializeUserOrdersThenPlaceAndFillOrder(orderParams: OrderParams, discountToken?: PublicKey, referrer?: PublicKey): Promise<TransactionSignature>;
|