@drift-labs/sdk 0.2.0-master.1 → 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/types.d.ts +1 -0
- package/lib/admin.d.ts +6 -3
- package/lib/admin.js +39 -7
- package/lib/clearingHouse.d.ts +13 -14
- package/lib/clearingHouse.js +166 -106
- package/lib/config.js +1 -1
- package/lib/constants/banks.js +8 -1
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.js +2 -1
- package/lib/factory/bigNum.d.ts +8 -2
- package/lib/factory/bigNum.js +14 -6
- package/lib/idl/clearing_house.json +277 -55
- package/lib/index.d.ts +2 -1
- package/lib/index.js +6 -1
- package/lib/math/amm.d.ts +6 -1
- package/lib/math/amm.js +124 -41
- package/lib/math/auction.js +4 -1
- package/lib/math/orders.d.ts +2 -2
- package/lib/math/orders.js +18 -11
- package/lib/math/position.js +3 -1
- package/lib/math/repeg.js +1 -1
- package/lib/math/trade.d.ts +1 -1
- package/lib/math/trade.js +7 -10
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +8 -96
- package/lib/orders.d.ts +1 -2
- package/lib/orders.js +6 -85
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +75 -1
- package/lib/types.js +42 -1
- package/package.json +3 -3
- package/src/accounts/bulkAccountLoader.js +197 -0
- package/src/accounts/bulkUserSubscription.js +33 -0
- package/src/accounts/fetch.js +29 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
- package/src/accounts/pollingOracleSubscriber.js +93 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
- package/src/accounts/pollingUserAccountSubscriber.js +132 -0
- package/src/accounts/types.js +10 -0
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +93 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
- package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/pda.js +104 -0
- package/src/admin.ts +60 -8
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +223 -183
- package/src/config.ts +1 -1
- package/src/constants/banks.ts +8 -1
- package/src/constants/numericConstants.ts +1 -0
- package/src/events/eventList.js +77 -0
- package/src/events/eventSubscriber.js +139 -0
- package/src/events/fetchLogs.js +50 -0
- package/src/events/pollingLogProvider.js +64 -0
- package/src/events/sort.js +44 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/types.js +20 -0
- package/src/events/webSocketLogProvider.js +41 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/factory/bigNum.js +364 -0
- package/src/factory/bigNum.ts +26 -9
- package/src/factory/oracleClient.js +20 -0
- package/src/idl/clearing_house.json +277 -55
- package/src/index.js +69 -0
- package/src/index.ts +2 -1
- package/src/math/amm.js +369 -0
- package/src/math/amm.ts +207 -52
- package/src/math/auction.js +42 -0
- package/src/math/auction.ts +5 -1
- package/src/math/bankBalance.js +75 -0
- package/src/math/conversion.js +11 -0
- package/src/math/funding.js +248 -0
- package/src/math/market.js +57 -0
- package/src/math/oracles.js +26 -0
- package/src/math/orders.js +110 -0
- package/src/math/orders.ts +17 -13
- package/src/math/position.js +140 -0
- package/src/math/position.ts +5 -1
- package/src/math/repeg.js +128 -0
- package/src/math/repeg.ts +2 -1
- package/src/math/state.js +15 -0
- package/src/math/trade.js +253 -0
- package/src/math/trade.ts +23 -25
- package/src/math/utils.js +0 -1
- package/src/mockUSDCFaucet.js +171 -0
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/types.js +2 -0
- package/src/orderParams.js +20 -0
- package/src/orderParams.ts +20 -141
- package/src/orders.js +134 -0
- package/src/orders.ts +7 -131
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/token/index.js +38 -0
- package/src/tx/retryTxSender.js +188 -0
- package/src/tx/types.js +2 -0
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +1 -1
- package/src/types.js +114 -0
- package/src/types.ts +69 -3
- package/src/userName.js +20 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/src/util/computeUnits.js +0 -17
- package/src/util/computeUnits.js.map +0 -1
|
@@ -0,0 +1,93 @@
|
|
|
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.WebSocketAccountSubscriber = void 0;
|
|
13
|
+
const utils_1 = require("./utils");
|
|
14
|
+
class WebSocketAccountSubscriber {
|
|
15
|
+
constructor(accountName, program, accountPublicKey, decodeBuffer) {
|
|
16
|
+
this.accountName = accountName;
|
|
17
|
+
this.program = program;
|
|
18
|
+
this.accountPublicKey = accountPublicKey;
|
|
19
|
+
this.decodeBufferFn = decodeBuffer;
|
|
20
|
+
}
|
|
21
|
+
subscribe(onChange) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
if (this.listenerId) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
this.onChange = onChange;
|
|
27
|
+
yield this.fetch();
|
|
28
|
+
this.listenerId = this.program.provider.connection.onAccountChange(this.accountPublicKey, (accountInfo, context) => {
|
|
29
|
+
this.handleRpcResponse(context, accountInfo);
|
|
30
|
+
}, this.program.provider.opts.commitment);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
fetch() {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
const rpcResponse = yield this.program.provider.connection.getAccountInfoAndContext(this.accountPublicKey, this.program.provider.opts.commitment);
|
|
36
|
+
this.handleRpcResponse(rpcResponse.context, rpcResponse === null || rpcResponse === void 0 ? void 0 : rpcResponse.value);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
handleRpcResponse(context, accountInfo) {
|
|
40
|
+
const newSlot = context.slot;
|
|
41
|
+
let newBuffer = undefined;
|
|
42
|
+
if (accountInfo) {
|
|
43
|
+
newBuffer = accountInfo.data;
|
|
44
|
+
}
|
|
45
|
+
if (!this.bufferAndSlot) {
|
|
46
|
+
this.bufferAndSlot = {
|
|
47
|
+
buffer: newBuffer,
|
|
48
|
+
slot: newSlot,
|
|
49
|
+
};
|
|
50
|
+
if (newBuffer) {
|
|
51
|
+
const account = this.decodeBuffer(newBuffer);
|
|
52
|
+
this.dataAndSlot = {
|
|
53
|
+
data: account,
|
|
54
|
+
slot: newSlot,
|
|
55
|
+
};
|
|
56
|
+
this.onChange(account);
|
|
57
|
+
}
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (newSlot <= this.bufferAndSlot.slot) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const oldBuffer = this.bufferAndSlot.buffer;
|
|
64
|
+
if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
|
|
65
|
+
this.bufferAndSlot = {
|
|
66
|
+
buffer: newBuffer,
|
|
67
|
+
slot: newSlot,
|
|
68
|
+
};
|
|
69
|
+
const account = this.decodeBuffer(newBuffer);
|
|
70
|
+
this.dataAndSlot = {
|
|
71
|
+
data: account,
|
|
72
|
+
slot: newSlot,
|
|
73
|
+
};
|
|
74
|
+
this.onChange(account);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
decodeBuffer(buffer) {
|
|
78
|
+
if (this.decodeBufferFn) {
|
|
79
|
+
return this.decodeBufferFn(buffer);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
return this.program.account[this.accountName].coder.accounts.decode(utils_1.capitalize(this.accountName), buffer);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
unsubscribe() {
|
|
86
|
+
if (this.listenerId) {
|
|
87
|
+
const promise = this.program.provider.connection.removeAccountChangeListener(this.listenerId);
|
|
88
|
+
this.listenerId = undefined;
|
|
89
|
+
return promise;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
exports.WebSocketAccountSubscriber = WebSocketAccountSubscriber;
|
|
@@ -0,0 +1,233 @@
|
|
|
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.WebSocketClearingHouseAccountSubscriber = void 0;
|
|
13
|
+
const types_1 = require("./types");
|
|
14
|
+
const events_1 = require("events");
|
|
15
|
+
const pda_1 = require("../addresses/pda");
|
|
16
|
+
const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
|
|
17
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
18
|
+
const oracleClientCache_1 = require("../oracles/oracleClientCache");
|
|
19
|
+
const quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
|
|
20
|
+
class WebSocketClearingHouseAccountSubscriber {
|
|
21
|
+
constructor(program, marketIndexes, bankIndexes, oracleInfos) {
|
|
22
|
+
this.oracleClientCache = new oracleClientCache_1.OracleClientCache();
|
|
23
|
+
this.marketAccountSubscribers = new Map();
|
|
24
|
+
this.bankAccountSubscribers = new Map();
|
|
25
|
+
this.oracleSubscribers = new Map();
|
|
26
|
+
this.isSubscribing = false;
|
|
27
|
+
this.isSubscribed = false;
|
|
28
|
+
this.program = program;
|
|
29
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
30
|
+
this.marketIndexes = marketIndexes;
|
|
31
|
+
this.bankIndexes = bankIndexes;
|
|
32
|
+
this.oracleInfos = oracleInfos;
|
|
33
|
+
}
|
|
34
|
+
subscribe() {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
if (this.isSubscribed) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
if (this.isSubscribing) {
|
|
40
|
+
return yield this.subscriptionPromise;
|
|
41
|
+
}
|
|
42
|
+
this.isSubscribing = true;
|
|
43
|
+
this.subscriptionPromise = new Promise((res) => {
|
|
44
|
+
this.subscriptionPromiseResolver = res;
|
|
45
|
+
});
|
|
46
|
+
const statePublicKey = yield pda_1.getClearingHouseStateAccountPublicKey(this.program.programId);
|
|
47
|
+
// create and activate main state account subscription
|
|
48
|
+
this.stateAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('state', this.program, statePublicKey);
|
|
49
|
+
yield this.stateAccountSubscriber.subscribe((data) => {
|
|
50
|
+
this.eventEmitter.emit('stateAccountUpdate', data);
|
|
51
|
+
this.eventEmitter.emit('update');
|
|
52
|
+
});
|
|
53
|
+
// subscribe to market accounts
|
|
54
|
+
yield this.subscribeToMarketAccounts();
|
|
55
|
+
// subscribe to bank accounts
|
|
56
|
+
yield this.subscribeToBankAccounts();
|
|
57
|
+
// subscribe to oracles
|
|
58
|
+
yield this.subscribeToOracles();
|
|
59
|
+
this.eventEmitter.emit('update');
|
|
60
|
+
this.isSubscribing = false;
|
|
61
|
+
this.isSubscribed = true;
|
|
62
|
+
this.subscriptionPromiseResolver(true);
|
|
63
|
+
return true;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
subscribeToMarketAccounts() {
|
|
67
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
for (const marketIndex of this.marketIndexes) {
|
|
69
|
+
yield this.subscribeToMarketAccount(marketIndex);
|
|
70
|
+
}
|
|
71
|
+
return true;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
subscribeToMarketAccount(marketIndex) {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
const marketPublicKey = yield pda_1.getMarketPublicKey(this.program.programId, marketIndex);
|
|
77
|
+
const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('market', this.program, marketPublicKey);
|
|
78
|
+
yield accountSubscriber.subscribe((data) => {
|
|
79
|
+
this.eventEmitter.emit('marketAccountUpdate', data);
|
|
80
|
+
this.eventEmitter.emit('update');
|
|
81
|
+
});
|
|
82
|
+
this.marketAccountSubscribers.set(marketIndex.toNumber(), accountSubscriber);
|
|
83
|
+
return true;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
subscribeToBankAccounts() {
|
|
87
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
for (const bankIndex of this.bankIndexes) {
|
|
89
|
+
yield this.subscribeToBankAccount(bankIndex);
|
|
90
|
+
}
|
|
91
|
+
return true;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
subscribeToBankAccount(bankIndex) {
|
|
95
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
const bankPublicKey = yield pda_1.getBankPublicKey(this.program.programId, bankIndex);
|
|
97
|
+
const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('bank', this.program, bankPublicKey);
|
|
98
|
+
yield accountSubscriber.subscribe((data) => {
|
|
99
|
+
this.eventEmitter.emit('bankAccountUpdate', data);
|
|
100
|
+
this.eventEmitter.emit('update');
|
|
101
|
+
});
|
|
102
|
+
this.bankAccountSubscribers.set(bankIndex.toNumber(), accountSubscriber);
|
|
103
|
+
return true;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
subscribeToOracles() {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
for (const oracleInfo of this.oracleInfos) {
|
|
109
|
+
if (!oracleInfo.publicKey.equals(web3_js_1.PublicKey.default)) {
|
|
110
|
+
yield this.subscribeToOracle(oracleInfo);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
subscribeToOracle(oracleInfo) {
|
|
117
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
118
|
+
const client = this.oracleClientCache.get(oracleInfo.source, this.program.provider.connection);
|
|
119
|
+
const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('oracle', this.program, oracleInfo.publicKey, (buffer) => {
|
|
120
|
+
return client.getOraclePriceDataFromBuffer(buffer);
|
|
121
|
+
});
|
|
122
|
+
yield accountSubscriber.subscribe((data) => {
|
|
123
|
+
this.eventEmitter.emit('oraclePriceUpdate', oracleInfo.publicKey, data);
|
|
124
|
+
this.eventEmitter.emit('update');
|
|
125
|
+
});
|
|
126
|
+
this.oracleSubscribers.set(oracleInfo.publicKey.toString(), accountSubscriber);
|
|
127
|
+
return true;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
unsubscribeFromMarketAccounts() {
|
|
131
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
for (const accountSubscriber of this.marketAccountSubscribers.values()) {
|
|
133
|
+
yield accountSubscriber.unsubscribe();
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
unsubscribeFromBankAccounts() {
|
|
138
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
139
|
+
for (const accountSubscriber of this.bankAccountSubscribers.values()) {
|
|
140
|
+
yield accountSubscriber.unsubscribe();
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
unsubscribeFromOracles() {
|
|
145
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
146
|
+
for (const accountSubscriber of this.oracleSubscribers.values()) {
|
|
147
|
+
yield accountSubscriber.unsubscribe();
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
fetch() {
|
|
152
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
153
|
+
if (!this.isSubscribed) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const promises = [this.stateAccountSubscriber.fetch()]
|
|
157
|
+
.concat(Array.from(this.marketAccountSubscribers.values()).map((subscriber) => subscriber.fetch()))
|
|
158
|
+
.concat(Array.from(this.bankAccountSubscribers.values()).map((subscriber) => subscriber.fetch()));
|
|
159
|
+
yield Promise.all(promises);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
unsubscribe() {
|
|
163
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
164
|
+
if (!this.isSubscribed) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
yield this.stateAccountSubscriber.unsubscribe();
|
|
168
|
+
yield this.unsubscribeFromMarketAccounts();
|
|
169
|
+
yield this.unsubscribeFromBankAccounts();
|
|
170
|
+
yield this.unsubscribeFromOracles();
|
|
171
|
+
this.isSubscribed = false;
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
addBank(bankIndex) {
|
|
175
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
if (this.bankAccountSubscribers.has(bankIndex.toNumber())) {
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
return this.subscribeToBankAccount(bankIndex);
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
addMarket(marketIndex) {
|
|
183
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
184
|
+
if (this.marketAccountSubscribers.has(marketIndex.toNumber())) {
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
return this.subscribeToMarketAccount(marketIndex);
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
addOracle(oracleInfo) {
|
|
191
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
+
if (this.oracleSubscribers.has(oracleInfo.publicKey.toString())) {
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
if (oracleInfo.publicKey.equals(web3_js_1.PublicKey.default)) {
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
return this.subscribeToOracle(oracleInfo);
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
assertIsSubscribed() {
|
|
202
|
+
if (!this.isSubscribed) {
|
|
203
|
+
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
getStateAccountAndSlot() {
|
|
207
|
+
this.assertIsSubscribed();
|
|
208
|
+
return this.stateAccountSubscriber.dataAndSlot;
|
|
209
|
+
}
|
|
210
|
+
getMarketAccountAndSlot(marketIndex) {
|
|
211
|
+
this.assertIsSubscribed();
|
|
212
|
+
return this.marketAccountSubscribers.get(marketIndex.toNumber())
|
|
213
|
+
.dataAndSlot;
|
|
214
|
+
}
|
|
215
|
+
getMarketAccountsAndSlots() {
|
|
216
|
+
return Array.from(this.marketAccountSubscribers.values()).map((subscriber) => subscriber.dataAndSlot);
|
|
217
|
+
}
|
|
218
|
+
getBankAccountAndSlot(bankIndex) {
|
|
219
|
+
this.assertIsSubscribed();
|
|
220
|
+
return this.bankAccountSubscribers.get(bankIndex.toNumber()).dataAndSlot;
|
|
221
|
+
}
|
|
222
|
+
getOraclePriceDataAndSlot(oraclePublicKey) {
|
|
223
|
+
this.assertIsSubscribed();
|
|
224
|
+
if (oraclePublicKey.equals(web3_js_1.PublicKey.default)) {
|
|
225
|
+
return {
|
|
226
|
+
data: quoteAssetOracleClient_1.QUOTE_ORACLE_PRICE_DATA,
|
|
227
|
+
slot: 0,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
return this.oracleSubscribers.get(oraclePublicKey.toString()).dataAndSlot;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
exports.WebSocketClearingHouseAccountSubscriber = WebSocketClearingHouseAccountSubscriber;
|
|
@@ -0,0 +1,62 @@
|
|
|
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.WebSocketUserAccountSubscriber = void 0;
|
|
13
|
+
const types_1 = require("./types");
|
|
14
|
+
const events_1 = require("events");
|
|
15
|
+
const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
|
|
16
|
+
class WebSocketUserAccountSubscriber {
|
|
17
|
+
constructor(program, userAccountPublicKey) {
|
|
18
|
+
this.isSubscribed = false;
|
|
19
|
+
this.program = program;
|
|
20
|
+
this.userAccountPublicKey = userAccountPublicKey;
|
|
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.userDataAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('user', this.program, this.userAccountPublicKey);
|
|
29
|
+
yield this.userDataAccountSubscriber.subscribe((data) => {
|
|
30
|
+
this.eventEmitter.emit('userAccountUpdate', data);
|
|
31
|
+
this.eventEmitter.emit('update');
|
|
32
|
+
});
|
|
33
|
+
this.eventEmitter.emit('update');
|
|
34
|
+
this.isSubscribed = true;
|
|
35
|
+
return true;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
fetch() {
|
|
39
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
yield Promise.all([this.userDataAccountSubscriber.fetch()]);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
unsubscribe() {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
if (!this.isSubscribed) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
yield Promise.all([this.userDataAccountSubscriber.unsubscribe()]);
|
|
49
|
+
this.isSubscribed = false;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
assertIsSubscribed() {
|
|
53
|
+
if (!this.isSubscribed) {
|
|
54
|
+
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
getUserAccountAndSlot() {
|
|
58
|
+
this.assertIsSubscribed();
|
|
59
|
+
return this.userDataAccountSubscriber.dataAndSlot;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.WebSocketUserAccountSubscriber = WebSocketUserAccountSubscriber;
|
|
@@ -0,0 +1,26 @@
|
|
|
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.getMarketAddress = void 0;
|
|
13
|
+
const pda_1 = require("./pda");
|
|
14
|
+
const CACHE = new Map();
|
|
15
|
+
function getMarketAddress(programId, marketIndex) {
|
|
16
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
const cacheKey = `${programId.toString()}-${marketIndex.toString()}`;
|
|
18
|
+
if (CACHE.has(cacheKey)) {
|
|
19
|
+
return CACHE.get(cacheKey);
|
|
20
|
+
}
|
|
21
|
+
const publicKey = yield pda_1.getMarketPublicKey(programId, marketIndex);
|
|
22
|
+
CACHE.set(cacheKey, publicKey);
|
|
23
|
+
return publicKey;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
exports.getMarketAddress = getMarketAddress;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
22
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.getBankVaultAuthorityPublicKey = exports.getBankVaultPublicKey = exports.getBankPublicKey = exports.getMarketPublicKey = exports.getUserAccountPublicKeySync = exports.getUserAccountPublicKey = exports.getUserAccountPublicKeyAndNonce = exports.getClearingHouseStateAccountPublicKey = exports.getClearingHouseStateAccountPublicKeyAndNonce = void 0;
|
|
32
|
+
const anchor = __importStar(require("@project-serum/anchor"));
|
|
33
|
+
function getClearingHouseStateAccountPublicKeyAndNonce(programId) {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('clearing_house'))], programId);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
exports.getClearingHouseStateAccountPublicKeyAndNonce = getClearingHouseStateAccountPublicKeyAndNonce;
|
|
39
|
+
function getClearingHouseStateAccountPublicKey(programId) {
|
|
40
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
+
return (yield getClearingHouseStateAccountPublicKeyAndNonce(programId))[0];
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
exports.getClearingHouseStateAccountPublicKey = getClearingHouseStateAccountPublicKey;
|
|
45
|
+
function getUserAccountPublicKeyAndNonce(programId, authority, userId = 0) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
return anchor.web3.PublicKey.findProgramAddress([
|
|
48
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('user')),
|
|
49
|
+
authority.toBuffer(),
|
|
50
|
+
Uint8Array.from([userId]),
|
|
51
|
+
], programId);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
exports.getUserAccountPublicKeyAndNonce = getUserAccountPublicKeyAndNonce;
|
|
55
|
+
function getUserAccountPublicKey(programId, authority, userId = 0) {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
return (yield getUserAccountPublicKeyAndNonce(programId, authority, userId))[0];
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
exports.getUserAccountPublicKey = getUserAccountPublicKey;
|
|
61
|
+
function getUserAccountPublicKeySync(programId, authority, userId = 0) {
|
|
62
|
+
return anchor.web3.PublicKey.findProgramAddressSync([
|
|
63
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('user')),
|
|
64
|
+
authority.toBuffer(),
|
|
65
|
+
Uint8Array.from([userId]),
|
|
66
|
+
], programId)[0];
|
|
67
|
+
}
|
|
68
|
+
exports.getUserAccountPublicKeySync = getUserAccountPublicKeySync;
|
|
69
|
+
function getMarketPublicKey(programId, marketIndex) {
|
|
70
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
return (yield anchor.web3.PublicKey.findProgramAddress([
|
|
72
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('market')),
|
|
73
|
+
marketIndex.toArrayLike(Buffer, 'le', 8),
|
|
74
|
+
], programId))[0];
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
exports.getMarketPublicKey = getMarketPublicKey;
|
|
78
|
+
function getBankPublicKey(programId, bankIndex) {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
return (yield anchor.web3.PublicKey.findProgramAddress([
|
|
81
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('bank')),
|
|
82
|
+
bankIndex.toArrayLike(Buffer, 'le', 8),
|
|
83
|
+
], programId))[0];
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
exports.getBankPublicKey = getBankPublicKey;
|
|
87
|
+
function getBankVaultPublicKey(programId, bankIndex) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
return (yield anchor.web3.PublicKey.findProgramAddress([
|
|
90
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('bank_vault')),
|
|
91
|
+
bankIndex.toArrayLike(Buffer, 'le', 8),
|
|
92
|
+
], programId))[0];
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
exports.getBankVaultPublicKey = getBankVaultPublicKey;
|
|
96
|
+
function getBankVaultAuthorityPublicKey(programId, bankIndex) {
|
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
return (yield anchor.web3.PublicKey.findProgramAddress([
|
|
99
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('bank_vault_authority')),
|
|
100
|
+
bankIndex.toArrayLike(Buffer, 'le', 8),
|
|
101
|
+
], programId))[0];
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
exports.getBankVaultAuthorityPublicKey = getBankVaultAuthorityPublicKey;
|
package/src/admin.ts
CHANGED
|
@@ -85,7 +85,8 @@ export class Admin extends ClearingHouse {
|
|
|
85
85
|
initialAssetWeight: BN,
|
|
86
86
|
maintenanceAssetWeight: BN,
|
|
87
87
|
initialLiabilityWeight: BN,
|
|
88
|
-
maintenanceLiabilityWeight: BN
|
|
88
|
+
maintenanceLiabilityWeight: BN,
|
|
89
|
+
imfFactor = new BN(0)
|
|
89
90
|
): Promise<TransactionSignature> {
|
|
90
91
|
const bankIndex = this.getStateAccount().numberOfBanks;
|
|
91
92
|
const bank = await getBankPublicKey(this.program.programId, bankIndex);
|
|
@@ -109,6 +110,7 @@ export class Admin extends ClearingHouse {
|
|
|
109
110
|
maintenanceAssetWeight,
|
|
110
111
|
initialLiabilityWeight,
|
|
111
112
|
maintenanceLiabilityWeight,
|
|
113
|
+
imfFactor,
|
|
112
114
|
{
|
|
113
115
|
accounts: {
|
|
114
116
|
admin: this.wallet.publicKey,
|
|
@@ -329,10 +331,12 @@ export class Admin extends ClearingHouse {
|
|
|
329
331
|
recipient: PublicKey
|
|
330
332
|
): Promise<TransactionSignature> {
|
|
331
333
|
const state = await this.getStateAccount();
|
|
334
|
+
const bank = this.getQuoteAssetBankAccount();
|
|
332
335
|
return await this.program.rpc.withdrawFromInsuranceVault(amount, {
|
|
333
336
|
accounts: {
|
|
334
337
|
admin: this.wallet.publicKey,
|
|
335
338
|
state: await this.getStatePublicKey(),
|
|
339
|
+
bank: bank.pubkey,
|
|
336
340
|
insuranceVault: state.insuranceVault,
|
|
337
341
|
insuranceVaultAuthority: state.insuranceVaultAuthority,
|
|
338
342
|
recipient: recipient,
|
|
@@ -341,7 +345,7 @@ export class Admin extends ClearingHouse {
|
|
|
341
345
|
});
|
|
342
346
|
}
|
|
343
347
|
|
|
344
|
-
public async
|
|
348
|
+
public async withdrawFromMarketToInsuranceVault(
|
|
345
349
|
marketIndex: BN,
|
|
346
350
|
amount: BN,
|
|
347
351
|
recipient: PublicKey
|
|
@@ -351,7 +355,7 @@ export class Admin extends ClearingHouse {
|
|
|
351
355
|
marketIndex
|
|
352
356
|
);
|
|
353
357
|
const bank = this.getQuoteAssetBankAccount();
|
|
354
|
-
return await this.program.rpc.
|
|
358
|
+
return await this.program.rpc.withdrawFromMarketToInsuranceVault(amount, {
|
|
355
359
|
accounts: {
|
|
356
360
|
admin: this.wallet.publicKey,
|
|
357
361
|
state: await this.getStatePublicKey(),
|
|
@@ -370,6 +374,8 @@ export class Admin extends ClearingHouse {
|
|
|
370
374
|
amount: BN
|
|
371
375
|
): Promise<TransactionSignature> {
|
|
372
376
|
const state = await this.getStateAccount();
|
|
377
|
+
const bank = this.getQuoteAssetBankAccount();
|
|
378
|
+
|
|
373
379
|
return await this.program.rpc.withdrawFromInsuranceVaultToMarket(amount, {
|
|
374
380
|
accounts: {
|
|
375
381
|
admin: this.wallet.publicKey,
|
|
@@ -377,7 +383,9 @@ export class Admin extends ClearingHouse {
|
|
|
377
383
|
market: await getMarketPublicKey(this.program.programId, marketIndex),
|
|
378
384
|
insuranceVault: state.insuranceVault,
|
|
379
385
|
insuranceVaultAuthority: state.insuranceVaultAuthority,
|
|
380
|
-
|
|
386
|
+
bank: bank.pubkey,
|
|
387
|
+
bankVault: bank.vault,
|
|
388
|
+
bankVaultAuthority: bank.vaultAuthority,
|
|
381
389
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
382
390
|
},
|
|
383
391
|
});
|
|
@@ -444,6 +452,19 @@ export class Admin extends ClearingHouse {
|
|
|
444
452
|
});
|
|
445
453
|
}
|
|
446
454
|
|
|
455
|
+
public async updateMarketMaxSpread(
|
|
456
|
+
marketIndex: BN,
|
|
457
|
+
maxSpread: number
|
|
458
|
+
): Promise<TransactionSignature> {
|
|
459
|
+
return await this.program.rpc.updateMarketMaxSpread(maxSpread, {
|
|
460
|
+
accounts: {
|
|
461
|
+
admin: this.wallet.publicKey,
|
|
462
|
+
state: await this.getStatePublicKey(),
|
|
463
|
+
market: await getMarketPublicKey(this.program.programId, marketIndex),
|
|
464
|
+
},
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
|
|
447
468
|
public async updatePartialLiquidationClosePercentage(
|
|
448
469
|
numerator: BN,
|
|
449
470
|
denominator: BN
|
|
@@ -662,11 +683,13 @@ export class Admin extends ClearingHouse {
|
|
|
662
683
|
});
|
|
663
684
|
}
|
|
664
685
|
|
|
665
|
-
public async
|
|
666
|
-
|
|
686
|
+
public async updateAuctionDuration(
|
|
687
|
+
minDuration: BN | number,
|
|
688
|
+
maxDuration: BN | number
|
|
667
689
|
): Promise<TransactionSignature> {
|
|
668
|
-
return await this.program.rpc.
|
|
669
|
-
typeof
|
|
690
|
+
return await this.program.rpc.updateAuctionDuration(
|
|
691
|
+
typeof minDuration === 'number' ? minDuration : minDuration.toNumber(),
|
|
692
|
+
typeof maxDuration === 'number' ? maxDuration : maxDuration.toNumber(),
|
|
670
693
|
{
|
|
671
694
|
accounts: {
|
|
672
695
|
admin: this.wallet.publicKey,
|
|
@@ -675,4 +698,33 @@ export class Admin extends ClearingHouse {
|
|
|
675
698
|
}
|
|
676
699
|
);
|
|
677
700
|
}
|
|
701
|
+
|
|
702
|
+
public async updateMaxBaseAssetAmountRatio(
|
|
703
|
+
marketIndex: BN,
|
|
704
|
+
maxBaseAssetAmountRatio: number
|
|
705
|
+
): Promise<TransactionSignature> {
|
|
706
|
+
return await this.program.rpc.updateMaxBaseAssetAmountRatio(
|
|
707
|
+
maxBaseAssetAmountRatio,
|
|
708
|
+
{
|
|
709
|
+
accounts: {
|
|
710
|
+
admin: this.wallet.publicKey,
|
|
711
|
+
state: await this.getStatePublicKey(),
|
|
712
|
+
market: this.getMarketAccount(marketIndex).pubkey,
|
|
713
|
+
},
|
|
714
|
+
}
|
|
715
|
+
);
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
public async updateMaxSlippageRatio(
|
|
719
|
+
marketIndex: BN,
|
|
720
|
+
maxSlippageRatio: number
|
|
721
|
+
): Promise<TransactionSignature> {
|
|
722
|
+
return await this.program.rpc.updateMaxSlippageRatio(maxSlippageRatio, {
|
|
723
|
+
accounts: {
|
|
724
|
+
admin: this.wallet.publicKey,
|
|
725
|
+
state: await this.getStatePublicKey(),
|
|
726
|
+
market: this.getMarketAccount(marketIndex).pubkey,
|
|
727
|
+
},
|
|
728
|
+
});
|
|
729
|
+
}
|
|
678
730
|
}
|