@drift-labs/sdk 0.2.0-master.26 → 0.2.0-master.28
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/pollingClearingHouseAccountSubscriber.d.ts +1 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +3 -0
- package/lib/accounts/types.d.ts +2 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +1 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +3 -0
- package/lib/admin.d.ts +5 -2
- package/lib/admin.js +30 -4
- package/lib/clearingHouse.d.ts +3 -0
- package/lib/clearingHouse.js +19 -1
- package/lib/clearingHouseUser.d.ts +11 -9
- package/lib/clearingHouseUser.js +184 -108
- package/lib/config.js +1 -1
- package/lib/dlob/DLOB.d.ts +73 -0
- package/lib/dlob/DLOB.js +557 -0
- package/lib/dlob/DLOBNode.d.ts +52 -0
- package/lib/dlob/DLOBNode.js +82 -0
- package/lib/dlob/NodeList.d.ts +26 -0
- package/lib/dlob/NodeList.js +138 -0
- package/lib/idl/clearing_house.json +123 -203
- package/lib/index.d.ts +5 -0
- package/lib/index.js +5 -0
- package/lib/math/market.d.ts +3 -2
- package/lib/math/market.js +17 -12
- package/lib/math/orders.d.ts +3 -3
- package/lib/math/orders.js +31 -16
- package/lib/math/position.d.ts +3 -3
- package/lib/math/position.js +23 -16
- package/lib/math/repeg.js +8 -0
- package/lib/math/spotBalance.d.ts +3 -0
- package/lib/math/spotBalance.js +18 -1
- package/lib/math/spotPosition.d.ts +5 -1
- package/lib/math/spotPosition.js +16 -1
- package/lib/types.d.ts +25 -24
- package/lib/types.js +9 -3
- package/lib/userMap/userMap.d.ts +25 -0
- package/lib/userMap/userMap.js +73 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/package.json +6 -3
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +4 -0
- package/src/accounts/types.ts +1 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +6 -0
- package/src/admin.ts +50 -8
- package/src/clearingHouse.ts +30 -1
- package/src/clearingHouseUser.ts +366 -167
- package/src/config.ts +1 -1
- package/src/dlob/DLOB.ts +884 -0
- package/src/dlob/DLOBNode.ts +163 -0
- package/src/dlob/NodeList.ts +185 -0
- package/src/idl/clearing_house.json +123 -203
- package/src/index.ts +5 -0
- package/src/math/market.ts +22 -13
- package/src/math/orders.ts +29 -21
- package/src/math/position.ts +36 -22
- package/src/math/repeg.ts +9 -0
- package/src/math/spotBalance.ts +26 -0
- package/src/math/spotPosition.ts +42 -1
- package/src/types.ts +26 -24
- package/src/userMap/userMap.ts +100 -0
- package/src/userMap/userStatsMap.ts +110 -0
- package/tests/bn/test.ts +2 -3
- package/tests/dlob/helpers.ts +322 -0
- package/tests/dlob/test.ts +2865 -0
- package/my-script/.env +0 -7
- package/my-script/getUserStats.ts +0 -106
- package/my-script/multiConnections.ts +0 -119
- package/my-script/test-regex.ts +0 -11
- package/my-script/utils.ts +0 -52
- package/src/accounts/bulkAccountLoader.js +0 -249
- package/src/accounts/bulkUserStatsSubscription.js +0 -75
- package/src/accounts/bulkUserSubscription.js +0 -75
- package/src/accounts/fetch.js +0 -92
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -465
- package/src/accounts/pollingOracleSubscriber.js +0 -156
- package/src/accounts/pollingTokenAccountSubscriber.js +0 -141
- package/src/accounts/pollingUserAccountSubscriber.js +0 -208
- package/src/accounts/pollingUserStatsAccountSubscriber.js +0 -208
- package/src/accounts/types.js +0 -28
- package/src/accounts/utils.js +0 -7
- package/src/accounts/webSocketAccountSubscriber.js +0 -138
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -433
- package/src/accounts/webSocketUserAccountSubscriber.js +0 -113
- package/src/accounts/webSocketUserStatsAccountSubsriber.js +0 -113
- package/src/addresses/pda.js +0 -186
- package/src/admin.js +0 -1284
- package/src/assert/assert.js +0 -9
- package/src/clearingHouse.js +0 -3433
- package/src/clearingHouseConfig.js +0 -2
- package/src/clearingHouseUser.js +0 -874
- package/src/clearingHouseUserConfig.js +0 -2
- package/src/clearingHouseUserStats.js +0 -115
- package/src/clearingHouseUserStatsConfig.js +0 -2
- package/src/config.js +0 -80
- package/src/constants/numericConstants.js +0 -48
- package/src/constants/perpMarkets.js +0 -42
- package/src/constants/spotMarkets.js +0 -51
- package/src/events/eventList.js +0 -120
- package/src/events/eventSubscriber.js +0 -202
- package/src/events/fetchLogs.js +0 -117
- package/src/events/pollingLogProvider.js +0 -113
- package/src/events/sort.js +0 -41
- package/src/events/txEventCache.js +0 -74
- package/src/events/types.js +0 -25
- package/src/events/webSocketLogProvider.js +0 -76
- package/src/factory/bigNum.js +0 -393
- package/src/factory/oracleClient.js +0 -20
- package/src/index.js +0 -75
- package/src/math/amm.js +0 -422
- package/src/math/auction.js +0 -42
- package/src/math/conversion.js +0 -12
- package/src/math/funding.js +0 -296
- package/src/math/insurance.js +0 -27
- package/src/math/margin.js +0 -77
- package/src/math/market.js +0 -105
- package/src/math/oracles.js +0 -56
- package/src/math/orders.js +0 -153
- package/src/math/position.js +0 -172
- package/src/math/repeg.js +0 -128
- package/src/math/spotBalance.js +0 -176
- package/src/math/spotMarket.js +0 -8
- package/src/math/spotPosition.js +0 -8
- package/src/math/trade.js +0 -260
- package/src/math/utils.js +0 -27
- package/src/oracles/oracleClientCache.js +0 -20
- package/src/oracles/pythClient.js +0 -81
- package/src/oracles/quoteAssetOracleClient.js +0 -63
- package/src/oracles/switchboardClient.js +0 -101
- package/src/oracles/types.js +0 -2
- package/src/orderParams.js +0 -28
- package/src/serum/serumSubscriber.js +0 -102
- package/src/serum/types.js +0 -2
- package/src/slot/SlotSubscriber.js +0 -86
- package/src/token/index.js +0 -38
- package/src/tokenFaucet.js +0 -323
- package/src/tx/retryTxSender.js +0 -280
- package/src/tx/types.js +0 -2
- package/src/tx/utils.js +0 -18
- package/src/types.js +0 -216
- package/src/userName.js +0 -20
- package/src/util/computeUnits.js +0 -62
- package/src/util/promiseTimeout.js +0 -14
- package/src/util/tps.js +0 -61
- package/src/wallet.js +0 -72
package/src/clearingHouse.js
DELETED
|
@@ -1,3433 +0,0 @@
|
|
|
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
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
39
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
40
|
-
if (ar || !(i in from)) {
|
|
41
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
42
|
-
ar[i] = from[i];
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
46
|
-
};
|
|
47
|
-
exports.__esModule = true;
|
|
48
|
-
exports.ClearingHouse = void 0;
|
|
49
|
-
var anchor_1 = require("@project-serum/anchor");
|
|
50
|
-
var spl_token_1 = require("@solana/spl-token");
|
|
51
|
-
var types_1 = require("./types");
|
|
52
|
-
var anchor = require("@project-serum/anchor");
|
|
53
|
-
var clearing_house_json_1 = require("./idl/clearing_house.json");
|
|
54
|
-
var web3_js_1 = require("@solana/web3.js");
|
|
55
|
-
var pda_1 = require("./addresses/pda");
|
|
56
|
-
var utils_1 = require("./tx/utils");
|
|
57
|
-
var numericConstants_1 = require("./constants/numericConstants");
|
|
58
|
-
var position_1 = require("./math/position");
|
|
59
|
-
var spotBalance_1 = require("./math/spotBalance");
|
|
60
|
-
var userName_1 = require("./userName");
|
|
61
|
-
var pollingClearingHouseAccountSubscriber_1 = require("./accounts/pollingClearingHouseAccountSubscriber");
|
|
62
|
-
var webSocketClearingHouseAccountSubscriber_1 = require("./accounts/webSocketClearingHouseAccountSubscriber");
|
|
63
|
-
var retryTxSender_1 = require("./tx/retryTxSender");
|
|
64
|
-
var clearingHouseUser_1 = require("./clearingHouseUser");
|
|
65
|
-
var config_1 = require("./config");
|
|
66
|
-
var spotMarkets_1 = require("./constants/spotMarkets");
|
|
67
|
-
var clearingHouseUserStats_1 = require("./clearingHouseUserStats");
|
|
68
|
-
var spotPosition_1 = require("./math/spotPosition");
|
|
69
|
-
/**
|
|
70
|
-
* # ClearingHouse
|
|
71
|
-
* This class is the main way to interact with Drift Protocol. It allows you to subscribe to the various accounts where the Market's state is stored, as well as: opening positions, liquidating, settling funding, depositing & withdrawing, and more.
|
|
72
|
-
*/
|
|
73
|
-
var ClearingHouse = /** @class */ (function () {
|
|
74
|
-
function ClearingHouse(config) {
|
|
75
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
76
|
-
this.users = new Map();
|
|
77
|
-
this._isSubscribed = false;
|
|
78
|
-
this.marketLastSlotCache = new Map();
|
|
79
|
-
this.connection = config.connection;
|
|
80
|
-
this.wallet = config.wallet;
|
|
81
|
-
this.opts = config.opts || anchor_1.AnchorProvider.defaultOptions();
|
|
82
|
-
this.provider = new anchor_1.AnchorProvider(config.connection, config.wallet, this.opts);
|
|
83
|
-
this.program = new anchor_1.Program(clearing_house_json_1["default"], config.programID, this.provider);
|
|
84
|
-
var userIds = (_a = config.userIds) !== null && _a !== void 0 ? _a : [0];
|
|
85
|
-
this.activeUserId = (_b = config.activeUserId) !== null && _b !== void 0 ? _b : userIds[0];
|
|
86
|
-
this.userAccountSubscriptionConfig =
|
|
87
|
-
((_c = config.accountSubscription) === null || _c === void 0 ? void 0 : _c.type) === 'polling'
|
|
88
|
-
? {
|
|
89
|
-
type: 'polling',
|
|
90
|
-
accountLoader: config.accountSubscription.accountLoader
|
|
91
|
-
}
|
|
92
|
-
: {
|
|
93
|
-
type: 'websocket'
|
|
94
|
-
};
|
|
95
|
-
this.createUsers(userIds, this.userAccountSubscriptionConfig);
|
|
96
|
-
if (config.userStats) {
|
|
97
|
-
this.userStats = new clearingHouseUserStats_1.ClearingHouseUserStats({
|
|
98
|
-
clearingHouse: this,
|
|
99
|
-
userStatsAccountPublicKey: (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, this.wallet.publicKey),
|
|
100
|
-
accountSubscription: this.userAccountSubscriptionConfig
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
var perpMarketIndexes = config.perpMarketIndexes;
|
|
104
|
-
var spotMarketIndexes = config.spotMarketIndexes;
|
|
105
|
-
var oracleInfos = config.oracleInfos;
|
|
106
|
-
if (config.env) {
|
|
107
|
-
var _l = (0, config_1.getMarketsAndOraclesForSubscription)(config.env), envPerpMarketIndexes = _l.perpMarketIndexes, envSpotMarketIndexes = _l.spotMarketIndexes, envOralceInfos = _l.oracleInfos;
|
|
108
|
-
perpMarketIndexes = perpMarketIndexes
|
|
109
|
-
? perpMarketIndexes
|
|
110
|
-
: envPerpMarketIndexes;
|
|
111
|
-
spotMarketIndexes = spotMarketIndexes
|
|
112
|
-
? spotMarketIndexes
|
|
113
|
-
: envSpotMarketIndexes;
|
|
114
|
-
oracleInfos = oracleInfos ? oracleInfos : envOralceInfos;
|
|
115
|
-
}
|
|
116
|
-
if (((_d = config.accountSubscription) === null || _d === void 0 ? void 0 : _d.type) === 'polling') {
|
|
117
|
-
this.accountSubscriber = new pollingClearingHouseAccountSubscriber_1.PollingClearingHouseAccountSubscriber(this.program, config.accountSubscription.accountLoader, perpMarketIndexes !== null && perpMarketIndexes !== void 0 ? perpMarketIndexes : [], spotMarketIndexes !== null && spotMarketIndexes !== void 0 ? spotMarketIndexes : [], oracleInfos !== null && oracleInfos !== void 0 ? oracleInfos : []);
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
this.accountSubscriber = new webSocketClearingHouseAccountSubscriber_1.WebSocketClearingHouseAccountSubscriber(this.program, (_e = config.perpMarketIndexes) !== null && _e !== void 0 ? _e : [], (_f = config.spotMarketIndexes) !== null && _f !== void 0 ? _f : [], (_g = config.oracleInfos) !== null && _g !== void 0 ? _g : []);
|
|
121
|
-
}
|
|
122
|
-
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
123
|
-
this.txSender = new retryTxSender_1.RetryTxSender(this.provider, (_h = config.txSenderConfig) === null || _h === void 0 ? void 0 : _h.timeout, (_j = config.txSenderConfig) === null || _j === void 0 ? void 0 : _j.retrySleep, (_k = config.txSenderConfig) === null || _k === void 0 ? void 0 : _k.additionalConnections);
|
|
124
|
-
}
|
|
125
|
-
Object.defineProperty(ClearingHouse.prototype, "isSubscribed", {
|
|
126
|
-
get: function () {
|
|
127
|
-
return this._isSubscribed && this.accountSubscriber.isSubscribed;
|
|
128
|
-
},
|
|
129
|
-
set: function (val) {
|
|
130
|
-
this._isSubscribed = val;
|
|
131
|
-
},
|
|
132
|
-
enumerable: false,
|
|
133
|
-
configurable: true
|
|
134
|
-
});
|
|
135
|
-
ClearingHouse.prototype.createUsers = function (userIds, accountSubscriptionConfig) {
|
|
136
|
-
for (var _i = 0, userIds_1 = userIds; _i < userIds_1.length; _i++) {
|
|
137
|
-
var userId = userIds_1[_i];
|
|
138
|
-
var user = this.createUser(userId, accountSubscriptionConfig);
|
|
139
|
-
this.users.set(userId, user);
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
ClearingHouse.prototype.createUser = function (userId, accountSubscriptionConfig) {
|
|
143
|
-
var userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.wallet.publicKey, userId);
|
|
144
|
-
return new clearingHouseUser_1.ClearingHouseUser({
|
|
145
|
-
clearingHouse: this,
|
|
146
|
-
userAccountPublicKey: userAccountPublicKey,
|
|
147
|
-
accountSubscription: accountSubscriptionConfig
|
|
148
|
-
});
|
|
149
|
-
};
|
|
150
|
-
ClearingHouse.prototype.subscribe = function () {
|
|
151
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
152
|
-
var subscribePromises, _a;
|
|
153
|
-
return __generator(this, function (_b) {
|
|
154
|
-
switch (_b.label) {
|
|
155
|
-
case 0:
|
|
156
|
-
subscribePromises = this.subscribeUsers().concat(this.accountSubscriber.subscribe());
|
|
157
|
-
if (this.userStats !== undefined) {
|
|
158
|
-
subscribePromises.concat(this.userStats.subscribe());
|
|
159
|
-
}
|
|
160
|
-
_a = this;
|
|
161
|
-
return [4 /*yield*/, Promise.all(subscribePromises)];
|
|
162
|
-
case 1:
|
|
163
|
-
_a.isSubscribed = (_b.sent()).reduce(function (success, prevSuccess) { return success && prevSuccess; });
|
|
164
|
-
return [2 /*return*/, this.isSubscribed];
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
});
|
|
168
|
-
};
|
|
169
|
-
ClearingHouse.prototype.subscribeUsers = function () {
|
|
170
|
-
return __spreadArray([], this.users.values(), true).map(function (user) { return user.subscribe(); });
|
|
171
|
-
};
|
|
172
|
-
/**
|
|
173
|
-
* Forces the accountSubscriber to fetch account updates from rpc
|
|
174
|
-
*/
|
|
175
|
-
ClearingHouse.prototype.fetchAccounts = function () {
|
|
176
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
177
|
-
var promises;
|
|
178
|
-
return __generator(this, function (_a) {
|
|
179
|
-
switch (_a.label) {
|
|
180
|
-
case 0:
|
|
181
|
-
promises = __spreadArray([], this.users.values(), true).map(function (user) { return user.fetchAccounts(); })
|
|
182
|
-
.concat(this.accountSubscriber.fetch());
|
|
183
|
-
if (this.userStats) {
|
|
184
|
-
promises.concat(this.userStats.fetchAccounts());
|
|
185
|
-
}
|
|
186
|
-
return [4 /*yield*/, Promise.all(promises)];
|
|
187
|
-
case 1:
|
|
188
|
-
_a.sent();
|
|
189
|
-
return [2 /*return*/];
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
});
|
|
193
|
-
};
|
|
194
|
-
ClearingHouse.prototype.unsubscribe = function () {
|
|
195
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
196
|
-
var unsubscribePromises;
|
|
197
|
-
return __generator(this, function (_a) {
|
|
198
|
-
switch (_a.label) {
|
|
199
|
-
case 0:
|
|
200
|
-
unsubscribePromises = this.unsubscribeUsers().concat(this.accountSubscriber.unsubscribe());
|
|
201
|
-
if (this.userStats !== undefined) {
|
|
202
|
-
unsubscribePromises.concat(this.userStats.unsubscribe());
|
|
203
|
-
}
|
|
204
|
-
return [4 /*yield*/, Promise.all(unsubscribePromises)];
|
|
205
|
-
case 1:
|
|
206
|
-
_a.sent();
|
|
207
|
-
this.isSubscribed = false;
|
|
208
|
-
return [2 /*return*/];
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
};
|
|
213
|
-
ClearingHouse.prototype.unsubscribeUsers = function () {
|
|
214
|
-
return __spreadArray([], this.users.values(), true).map(function (user) { return user.unsubscribe(); });
|
|
215
|
-
};
|
|
216
|
-
ClearingHouse.prototype.getStatePublicKey = function () {
|
|
217
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
218
|
-
var _a;
|
|
219
|
-
return __generator(this, function (_b) {
|
|
220
|
-
switch (_b.label) {
|
|
221
|
-
case 0:
|
|
222
|
-
if (this.statePublicKey) {
|
|
223
|
-
return [2 /*return*/, this.statePublicKey];
|
|
224
|
-
}
|
|
225
|
-
_a = this;
|
|
226
|
-
return [4 /*yield*/, (0, pda_1.getClearingHouseStateAccountPublicKey)(this.program.programId)];
|
|
227
|
-
case 1:
|
|
228
|
-
_a.statePublicKey = _b.sent();
|
|
229
|
-
return [2 /*return*/, this.statePublicKey];
|
|
230
|
-
}
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
};
|
|
234
|
-
ClearingHouse.prototype.getSignerPublicKey = function () {
|
|
235
|
-
if (this.signerPublicKey) {
|
|
236
|
-
return this.signerPublicKey;
|
|
237
|
-
}
|
|
238
|
-
this.signerPublicKey = (0, pda_1.getClearingHouseSignerPublicKey)(this.program.programId);
|
|
239
|
-
return this.signerPublicKey;
|
|
240
|
-
};
|
|
241
|
-
ClearingHouse.prototype.getStateAccount = function () {
|
|
242
|
-
return this.accountSubscriber.getStateAccountAndSlot().data;
|
|
243
|
-
};
|
|
244
|
-
ClearingHouse.prototype.getPerpMarketAccount = function (marketIndex) {
|
|
245
|
-
var _a;
|
|
246
|
-
marketIndex = marketIndex instanceof anchor_1.BN ? marketIndex : new anchor_1.BN(marketIndex);
|
|
247
|
-
return (_a = this.accountSubscriber.getMarketAccountAndSlot(marketIndex)) === null || _a === void 0 ? void 0 : _a.data;
|
|
248
|
-
};
|
|
249
|
-
ClearingHouse.prototype.getPerpMarketAccounts = function () {
|
|
250
|
-
return this.accountSubscriber
|
|
251
|
-
.getMarketAccountsAndSlots()
|
|
252
|
-
.map(function (value) { return value.data; });
|
|
253
|
-
};
|
|
254
|
-
ClearingHouse.prototype.getSpotMarketAccount = function (marketIndex) {
|
|
255
|
-
marketIndex = marketIndex instanceof anchor_1.BN ? marketIndex : new anchor_1.BN(marketIndex);
|
|
256
|
-
return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex).data;
|
|
257
|
-
};
|
|
258
|
-
ClearingHouse.prototype.getQuoteSpotMarketAccount = function () {
|
|
259
|
-
return this.accountSubscriber.getSpotMarketAccountAndSlot(numericConstants_1.QUOTE_SPOT_MARKET_INDEX).data;
|
|
260
|
-
};
|
|
261
|
-
ClearingHouse.prototype.getOraclePriceDataAndSlot = function (oraclePublicKey) {
|
|
262
|
-
return this.accountSubscriber.getOraclePriceDataAndSlot(oraclePublicKey);
|
|
263
|
-
};
|
|
264
|
-
ClearingHouse.prototype.getSerumV3FulfillmentConfig = function (serumMarket) {
|
|
265
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
266
|
-
var address;
|
|
267
|
-
return __generator(this, function (_a) {
|
|
268
|
-
switch (_a.label) {
|
|
269
|
-
case 0: return [4 /*yield*/, (0, pda_1.getSerumFulfillmentConfigPublicKey)(this.program.programId, serumMarket)];
|
|
270
|
-
case 1:
|
|
271
|
-
address = _a.sent();
|
|
272
|
-
return [4 /*yield*/, this.program.account.serumV3FulfillmentConfig.fetch(address)];
|
|
273
|
-
case 2: return [2 /*return*/, (_a.sent())];
|
|
274
|
-
}
|
|
275
|
-
});
|
|
276
|
-
});
|
|
277
|
-
};
|
|
278
|
-
/**
|
|
279
|
-
* Update the wallet to use for clearing house transactions and linked user account
|
|
280
|
-
* @param newWallet
|
|
281
|
-
* @param userIds
|
|
282
|
-
* @param activeUserId
|
|
283
|
-
*/
|
|
284
|
-
ClearingHouse.prototype.updateWallet = function (newWallet, userIds, activeUserId) {
|
|
285
|
-
if (userIds === void 0) { userIds = [0]; }
|
|
286
|
-
if (activeUserId === void 0) { activeUserId = 0; }
|
|
287
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
288
|
-
var newProvider, newProgram;
|
|
289
|
-
return __generator(this, function (_a) {
|
|
290
|
-
switch (_a.label) {
|
|
291
|
-
case 0:
|
|
292
|
-
newProvider = new anchor_1.AnchorProvider(this.connection, newWallet, this.opts);
|
|
293
|
-
newProgram = new anchor_1.Program(clearing_house_json_1["default"], this.program.programId, newProvider);
|
|
294
|
-
// Update provider for txSender with new wallet details
|
|
295
|
-
this.txSender.provider = newProvider;
|
|
296
|
-
this.wallet = newWallet;
|
|
297
|
-
this.provider = newProvider;
|
|
298
|
-
this.program = newProgram;
|
|
299
|
-
if (!this.isSubscribed) return [3 /*break*/, 2];
|
|
300
|
-
return [4 /*yield*/, Promise.all(this.unsubscribeUsers())];
|
|
301
|
-
case 1:
|
|
302
|
-
_a.sent();
|
|
303
|
-
_a.label = 2;
|
|
304
|
-
case 2:
|
|
305
|
-
this.users.clear();
|
|
306
|
-
this.createUsers(userIds, this.userAccountSubscriptionConfig);
|
|
307
|
-
if (!this.isSubscribed) return [3 /*break*/, 4];
|
|
308
|
-
return [4 /*yield*/, Promise.all(this.subscribeUsers())];
|
|
309
|
-
case 3:
|
|
310
|
-
_a.sent();
|
|
311
|
-
_a.label = 4;
|
|
312
|
-
case 4:
|
|
313
|
-
this.activeUserId = activeUserId;
|
|
314
|
-
this.userStatsAccountPublicKey = undefined;
|
|
315
|
-
return [2 /*return*/];
|
|
316
|
-
}
|
|
317
|
-
});
|
|
318
|
-
});
|
|
319
|
-
};
|
|
320
|
-
ClearingHouse.prototype.switchActiveUser = function (userId) {
|
|
321
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
322
|
-
return __generator(this, function (_a) {
|
|
323
|
-
this.activeUserId = userId;
|
|
324
|
-
return [2 /*return*/];
|
|
325
|
-
});
|
|
326
|
-
});
|
|
327
|
-
};
|
|
328
|
-
ClearingHouse.prototype.addUser = function (userId) {
|
|
329
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
330
|
-
var user;
|
|
331
|
-
return __generator(this, function (_a) {
|
|
332
|
-
switch (_a.label) {
|
|
333
|
-
case 0:
|
|
334
|
-
if (this.users.has(userId)) {
|
|
335
|
-
return [2 /*return*/];
|
|
336
|
-
}
|
|
337
|
-
user = this.createUser(userId, this.userAccountSubscriptionConfig);
|
|
338
|
-
return [4 /*yield*/, user.subscribe()];
|
|
339
|
-
case 1:
|
|
340
|
-
_a.sent();
|
|
341
|
-
this.users.set(userId, user);
|
|
342
|
-
return [2 /*return*/];
|
|
343
|
-
}
|
|
344
|
-
});
|
|
345
|
-
});
|
|
346
|
-
};
|
|
347
|
-
ClearingHouse.prototype.initializeUserAccount = function (userId, name, referrerInfo) {
|
|
348
|
-
if (userId === void 0) { userId = 0; }
|
|
349
|
-
if (name === void 0) { name = userName_1.DEFAULT_USER_NAME; }
|
|
350
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
351
|
-
var _a, userAccountPublicKey, initializeUserAccountIx, tx, _b, _c, txSig;
|
|
352
|
-
return __generator(this, function (_d) {
|
|
353
|
-
switch (_d.label) {
|
|
354
|
-
case 0: return [4 /*yield*/, this.getInitializeUserInstructions(userId, name, referrerInfo)];
|
|
355
|
-
case 1:
|
|
356
|
-
_a = _d.sent(), userAccountPublicKey = _a[0], initializeUserAccountIx = _a[1];
|
|
357
|
-
tx = new web3_js_1.Transaction();
|
|
358
|
-
if (!(userId === 0)) return [3 /*break*/, 3];
|
|
359
|
-
// not the safest assumption, can explicitly check if user stats account exists if it causes problems
|
|
360
|
-
_c = (_b = tx).add;
|
|
361
|
-
return [4 /*yield*/, this.getInitializeUserStatsIx()];
|
|
362
|
-
case 2:
|
|
363
|
-
// not the safest assumption, can explicitly check if user stats account exists if it causes problems
|
|
364
|
-
_c.apply(_b, [_d.sent()]);
|
|
365
|
-
_d.label = 3;
|
|
366
|
-
case 3:
|
|
367
|
-
tx.add(initializeUserAccountIx);
|
|
368
|
-
return [4 /*yield*/, this.txSender.send(tx, [], this.opts)];
|
|
369
|
-
case 4:
|
|
370
|
-
txSig = (_d.sent()).txSig;
|
|
371
|
-
return [2 /*return*/, [txSig, userAccountPublicKey]];
|
|
372
|
-
}
|
|
373
|
-
});
|
|
374
|
-
});
|
|
375
|
-
};
|
|
376
|
-
ClearingHouse.prototype.getInitializeUserInstructions = function (userId, name, referrerInfo) {
|
|
377
|
-
if (userId === void 0) { userId = 0; }
|
|
378
|
-
if (name === void 0) { name = userName_1.DEFAULT_USER_NAME; }
|
|
379
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
380
|
-
var userAccountPublicKey, remainingAccounts, nameBuffer, initializeUserAccountIx, _a, _b, _c;
|
|
381
|
-
var _d, _e;
|
|
382
|
-
return __generator(this, function (_f) {
|
|
383
|
-
switch (_f.label) {
|
|
384
|
-
case 0: return [4 /*yield*/, (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, userId)];
|
|
385
|
-
case 1:
|
|
386
|
-
userAccountPublicKey = _f.sent();
|
|
387
|
-
remainingAccounts = new Array();
|
|
388
|
-
if (referrerInfo !== undefined) {
|
|
389
|
-
remainingAccounts.push({
|
|
390
|
-
pubkey: referrerInfo.referrer,
|
|
391
|
-
isWritable: true,
|
|
392
|
-
isSigner: false
|
|
393
|
-
});
|
|
394
|
-
remainingAccounts.push({
|
|
395
|
-
pubkey: referrerInfo.referrerStats,
|
|
396
|
-
isWritable: true,
|
|
397
|
-
isSigner: false
|
|
398
|
-
});
|
|
399
|
-
}
|
|
400
|
-
nameBuffer = (0, userName_1.encodeName)(name);
|
|
401
|
-
_b = (_a = this.program.instruction).initializeUser;
|
|
402
|
-
_c = [userId, nameBuffer];
|
|
403
|
-
_d = {};
|
|
404
|
-
_e = {
|
|
405
|
-
user: userAccountPublicKey,
|
|
406
|
-
userStats: this.getUserStatsAccountPublicKey(),
|
|
407
|
-
authority: this.wallet.publicKey,
|
|
408
|
-
payer: this.wallet.publicKey,
|
|
409
|
-
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
410
|
-
systemProgram: anchor.web3.SystemProgram.programId
|
|
411
|
-
};
|
|
412
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
413
|
-
case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
414
|
-
_e),
|
|
415
|
-
_d.remainingAccounts = remainingAccounts,
|
|
416
|
-
_d)]))];
|
|
417
|
-
case 3:
|
|
418
|
-
initializeUserAccountIx = _f.sent();
|
|
419
|
-
return [2 /*return*/, [userAccountPublicKey, initializeUserAccountIx]];
|
|
420
|
-
}
|
|
421
|
-
});
|
|
422
|
-
});
|
|
423
|
-
};
|
|
424
|
-
ClearingHouse.prototype.getInitializeUserStatsIx = function () {
|
|
425
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
426
|
-
var _a, _b;
|
|
427
|
-
var _c, _d;
|
|
428
|
-
return __generator(this, function (_e) {
|
|
429
|
-
switch (_e.label) {
|
|
430
|
-
case 0:
|
|
431
|
-
_b = (_a = this.program.instruction).initializeUserStats;
|
|
432
|
-
_c = {};
|
|
433
|
-
_d = {
|
|
434
|
-
userStats: this.getUserStatsAccountPublicKey(),
|
|
435
|
-
authority: this.wallet.publicKey,
|
|
436
|
-
payer: this.wallet.publicKey,
|
|
437
|
-
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
438
|
-
systemProgram: anchor.web3.SystemProgram.programId
|
|
439
|
-
};
|
|
440
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
441
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [(_c.accounts = (_d.state = _e.sent(),
|
|
442
|
-
_d),
|
|
443
|
-
_c)])];
|
|
444
|
-
case 2: return [2 /*return*/, _e.sent()];
|
|
445
|
-
}
|
|
446
|
-
});
|
|
447
|
-
});
|
|
448
|
-
};
|
|
449
|
-
ClearingHouse.prototype.getUser = function (userId) {
|
|
450
|
-
userId = userId !== null && userId !== void 0 ? userId : this.activeUserId;
|
|
451
|
-
if (!this.users.has(userId)) {
|
|
452
|
-
throw new Error("Clearing House has no user for user id ".concat(userId));
|
|
453
|
-
}
|
|
454
|
-
return this.users.get(userId);
|
|
455
|
-
};
|
|
456
|
-
ClearingHouse.prototype.getUsers = function () {
|
|
457
|
-
return __spreadArray([], this.users.values(), true);
|
|
458
|
-
};
|
|
459
|
-
ClearingHouse.prototype.getUserStats = function () {
|
|
460
|
-
return this.userStats;
|
|
461
|
-
};
|
|
462
|
-
ClearingHouse.prototype.getUserStatsAccountPublicKey = function () {
|
|
463
|
-
if (this.userStatsAccountPublicKey) {
|
|
464
|
-
return this.userStatsAccountPublicKey;
|
|
465
|
-
}
|
|
466
|
-
this.userStatsAccountPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, this.wallet.publicKey);
|
|
467
|
-
return this.userStatsAccountPublicKey;
|
|
468
|
-
};
|
|
469
|
-
ClearingHouse.prototype.getUserAccountPublicKey = function () {
|
|
470
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
471
|
-
return __generator(this, function (_a) {
|
|
472
|
-
return [2 /*return*/, this.getUser().userAccountPublicKey];
|
|
473
|
-
});
|
|
474
|
-
});
|
|
475
|
-
};
|
|
476
|
-
ClearingHouse.prototype.getUserAccount = function (userId) {
|
|
477
|
-
return this.getUser(userId).getUserAccount();
|
|
478
|
-
};
|
|
479
|
-
ClearingHouse.prototype.getUserAccountAndSlot = function (userId) {
|
|
480
|
-
return this.getUser(userId).getUserAccountAndSlot();
|
|
481
|
-
};
|
|
482
|
-
ClearingHouse.prototype.getSpotPosition = function (marketIndex) {
|
|
483
|
-
var marketIndexBN = marketIndex instanceof anchor_1.BN ? marketIndex : new anchor_1.BN(marketIndex);
|
|
484
|
-
return this.getUserAccount().spotPositions.find(function (spotPosition) {
|
|
485
|
-
return spotPosition.marketIndex.eq(marketIndexBN);
|
|
486
|
-
});
|
|
487
|
-
};
|
|
488
|
-
ClearingHouse.prototype.getQuoteAssetTokenAmount = function () {
|
|
489
|
-
var spotMarket = this.getSpotMarketAccount(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
490
|
-
var spotPosition = this.getSpotPosition(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
491
|
-
return (0, spotBalance_1.getTokenAmount)(spotPosition.balance, spotMarket, spotPosition.balanceType);
|
|
492
|
-
};
|
|
493
|
-
ClearingHouse.prototype.getRemainingAccounts = function (params) {
|
|
494
|
-
var userAccountAndSlot = this.getUserAccountAndSlot();
|
|
495
|
-
if (!userAccountAndSlot) {
|
|
496
|
-
throw Error('No user account found. Most likely user account does not exist or failed to fetch account');
|
|
497
|
-
}
|
|
498
|
-
var userAccount = userAccountAndSlot.data, lastUserPositionsSlot = userAccountAndSlot.slot;
|
|
499
|
-
var oracleAccountMap = new Map();
|
|
500
|
-
var spotMarketAccountMap = new Map();
|
|
501
|
-
var perpMarketAccountMap = new Map();
|
|
502
|
-
for (var _i = 0, _a = this.marketLastSlotCache.entries(); _i < _a.length; _i++) {
|
|
503
|
-
var _b = _a[_i], marketIndexNum = _b[0], slot = _b[1];
|
|
504
|
-
// if cache has more recent slot than user positions account slot, add market to remaining accounts
|
|
505
|
-
// otherwise remove from slot
|
|
506
|
-
if (slot > lastUserPositionsSlot) {
|
|
507
|
-
var marketAccount = this.getPerpMarketAccount(marketIndexNum);
|
|
508
|
-
perpMarketAccountMap.set(marketIndexNum, {
|
|
509
|
-
pubkey: marketAccount.pubkey,
|
|
510
|
-
isSigner: false,
|
|
511
|
-
isWritable: false
|
|
512
|
-
});
|
|
513
|
-
oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
|
|
514
|
-
pubkey: marketAccount.amm.oracle,
|
|
515
|
-
isSigner: false,
|
|
516
|
-
isWritable: false
|
|
517
|
-
});
|
|
518
|
-
}
|
|
519
|
-
else {
|
|
520
|
-
this.marketLastSlotCache["delete"](marketIndexNum);
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
for (var _c = 0, _d = userAccount.perpPositions; _c < _d.length; _c++) {
|
|
524
|
-
var position = _d[_c];
|
|
525
|
-
if (!(0, position_1.positionIsAvailable)(position)) {
|
|
526
|
-
var marketIndexNum = position.marketIndex.toNumber();
|
|
527
|
-
var marketAccount = this.getPerpMarketAccount(marketIndexNum);
|
|
528
|
-
perpMarketAccountMap.set(marketIndexNum, {
|
|
529
|
-
pubkey: marketAccount.pubkey,
|
|
530
|
-
isSigner: false,
|
|
531
|
-
isWritable: false
|
|
532
|
-
});
|
|
533
|
-
oracleAccountMap.set(marketAccount.pubkey.toString(), {
|
|
534
|
-
pubkey: marketAccount.amm.oracle,
|
|
535
|
-
isSigner: false,
|
|
536
|
-
isWritable: false
|
|
537
|
-
});
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
if (params.readablePerpMarketIndex) {
|
|
541
|
-
var marketAccount = this.getPerpMarketAccount(params.readablePerpMarketIndex.toNumber());
|
|
542
|
-
perpMarketAccountMap.set(params.readablePerpMarketIndex.toNumber(), {
|
|
543
|
-
pubkey: marketAccount.pubkey,
|
|
544
|
-
isSigner: false,
|
|
545
|
-
isWritable: true
|
|
546
|
-
});
|
|
547
|
-
oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
|
|
548
|
-
pubkey: marketAccount.amm.oracle,
|
|
549
|
-
isSigner: false,
|
|
550
|
-
isWritable: false
|
|
551
|
-
});
|
|
552
|
-
}
|
|
553
|
-
if (params.writablePerpMarketIndex) {
|
|
554
|
-
var marketAccount = this.getPerpMarketAccount(params.writablePerpMarketIndex.toNumber());
|
|
555
|
-
perpMarketAccountMap.set(params.writablePerpMarketIndex.toNumber(), {
|
|
556
|
-
pubkey: marketAccount.pubkey,
|
|
557
|
-
isSigner: false,
|
|
558
|
-
isWritable: true
|
|
559
|
-
});
|
|
560
|
-
oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
|
|
561
|
-
pubkey: marketAccount.amm.oracle,
|
|
562
|
-
isSigner: false,
|
|
563
|
-
isWritable: false
|
|
564
|
-
});
|
|
565
|
-
}
|
|
566
|
-
for (var _e = 0, _f = userAccount.spotPositions; _e < _f.length; _e++) {
|
|
567
|
-
var spotPosition = _f[_e];
|
|
568
|
-
if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
|
|
569
|
-
var spotMarketAccount = this.getSpotMarketAccount(spotPosition.marketIndex);
|
|
570
|
-
spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
|
|
571
|
-
pubkey: spotMarketAccount.pubkey,
|
|
572
|
-
isSigner: false,
|
|
573
|
-
isWritable: false
|
|
574
|
-
});
|
|
575
|
-
if (!spotMarketAccount.marketIndex.eq(numericConstants_1.ZERO)) {
|
|
576
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
577
|
-
pubkey: spotMarketAccount.oracle,
|
|
578
|
-
isSigner: false,
|
|
579
|
-
isWritable: false
|
|
580
|
-
});
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
if (params.readableSpotMarketIndex) {
|
|
585
|
-
var spotMarketAccount = this.getSpotMarketAccount(params.readableSpotMarketIndex);
|
|
586
|
-
spotMarketAccountMap.set(params.readableSpotMarketIndex.toNumber(), {
|
|
587
|
-
pubkey: spotMarketAccount.pubkey,
|
|
588
|
-
isSigner: false,
|
|
589
|
-
isWritable: false
|
|
590
|
-
});
|
|
591
|
-
if (!spotMarketAccount.marketIndex.eq(numericConstants_1.ZERO)) {
|
|
592
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
593
|
-
pubkey: spotMarketAccount.oracle,
|
|
594
|
-
isSigner: false,
|
|
595
|
-
isWritable: false
|
|
596
|
-
});
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
if (params.writableSpotMarketIndex) {
|
|
600
|
-
var spotMarketAccount = this.getSpotMarketAccount(params.writableSpotMarketIndex);
|
|
601
|
-
spotMarketAccountMap.set(params.writableSpotMarketIndex.toNumber(), {
|
|
602
|
-
pubkey: spotMarketAccount.pubkey,
|
|
603
|
-
isSigner: false,
|
|
604
|
-
isWritable: true
|
|
605
|
-
});
|
|
606
|
-
if (!spotMarketAccount.marketIndex.eq(numericConstants_1.ZERO)) {
|
|
607
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
608
|
-
pubkey: spotMarketAccount.oracle,
|
|
609
|
-
isSigner: false,
|
|
610
|
-
isWritable: false
|
|
611
|
-
});
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
return __spreadArray(__spreadArray(__spreadArray([], oracleAccountMap.values(), true), spotMarketAccountMap.values(), true), perpMarketAccountMap.values(), true);
|
|
615
|
-
};
|
|
616
|
-
ClearingHouse.prototype.getOrder = function (orderId) {
|
|
617
|
-
var _a;
|
|
618
|
-
var orderIdBN = orderId instanceof anchor_1.BN ? orderId : new anchor_1.BN(orderId);
|
|
619
|
-
return (_a = this.getUserAccount()) === null || _a === void 0 ? void 0 : _a.orders.find(function (order) {
|
|
620
|
-
return order.orderId.eq(orderIdBN);
|
|
621
|
-
});
|
|
622
|
-
};
|
|
623
|
-
ClearingHouse.prototype.getOrderByUserId = function (userOrderId) {
|
|
624
|
-
var _a;
|
|
625
|
-
return (_a = this.getUserAccount()) === null || _a === void 0 ? void 0 : _a.orders.find(function (order) { return order.userOrderId === userOrderId; });
|
|
626
|
-
};
|
|
627
|
-
ClearingHouse.prototype.deposit = function (amount, marketIndex, collateralAccountPublicKey, userId, reduceOnly) {
|
|
628
|
-
if (reduceOnly === void 0) { reduceOnly = false; }
|
|
629
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
630
|
-
var tx, additionalSigners, spotMarketAccount, isSolMarket, authority, createWSOLTokenAccount, _a, ixs, signers, pubkey, depositCollateralIx, txSig;
|
|
631
|
-
return __generator(this, function (_b) {
|
|
632
|
-
switch (_b.label) {
|
|
633
|
-
case 0:
|
|
634
|
-
tx = new web3_js_1.Transaction();
|
|
635
|
-
additionalSigners = [];
|
|
636
|
-
spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
637
|
-
isSolMarket = spotMarketAccount.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
|
|
638
|
-
authority = this.wallet.publicKey;
|
|
639
|
-
createWSOLTokenAccount = isSolMarket && collateralAccountPublicKey.equals(authority);
|
|
640
|
-
if (!createWSOLTokenAccount) return [3 /*break*/, 2];
|
|
641
|
-
return [4 /*yield*/, this.getWrappedSolAccountCreationIxs(amount)];
|
|
642
|
-
case 1:
|
|
643
|
-
_a = _b.sent(), ixs = _a.ixs, signers = _a.signers, pubkey = _a.pubkey;
|
|
644
|
-
collateralAccountPublicKey = pubkey;
|
|
645
|
-
ixs.forEach(function (ix) {
|
|
646
|
-
tx.add(ix);
|
|
647
|
-
});
|
|
648
|
-
signers.forEach(function (signer) { return additionalSigners.push(signer); });
|
|
649
|
-
_b.label = 2;
|
|
650
|
-
case 2: return [4 /*yield*/, this.getDepositInstruction(amount, marketIndex, collateralAccountPublicKey, userId, reduceOnly, true)];
|
|
651
|
-
case 3:
|
|
652
|
-
depositCollateralIx = _b.sent();
|
|
653
|
-
tx.add(depositCollateralIx);
|
|
654
|
-
// Close the wrapped sol account at the end of the transaction
|
|
655
|
-
if (createWSOLTokenAccount) {
|
|
656
|
-
tx.add(spl_token_1.Token.createCloseAccountInstruction(spl_token_1.TOKEN_PROGRAM_ID, collateralAccountPublicKey, authority, authority, []));
|
|
657
|
-
}
|
|
658
|
-
return [4 /*yield*/, this.txSender.send(tx, additionalSigners, this.opts)];
|
|
659
|
-
case 4:
|
|
660
|
-
txSig = (_b.sent()).txSig;
|
|
661
|
-
return [2 /*return*/, txSig];
|
|
662
|
-
}
|
|
663
|
-
});
|
|
664
|
-
});
|
|
665
|
-
};
|
|
666
|
-
ClearingHouse.prototype.getDepositInstruction = function (amount, marketIndex, userTokenAccount, userId, reduceOnly, userInitialized) {
|
|
667
|
-
if (reduceOnly === void 0) { reduceOnly = false; }
|
|
668
|
-
if (userInitialized === void 0) { userInitialized = true; }
|
|
669
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
670
|
-
var userAccountPublicKey, _a, remainingAccounts, spotMarketAccount_1, spotMarketAccount, _b, _c, _d;
|
|
671
|
-
var _e, _f;
|
|
672
|
-
return __generator(this, function (_g) {
|
|
673
|
-
switch (_g.label) {
|
|
674
|
-
case 0:
|
|
675
|
-
if (!userId) return [3 /*break*/, 2];
|
|
676
|
-
return [4 /*yield*/, (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, userId)];
|
|
677
|
-
case 1:
|
|
678
|
-
_a = _g.sent();
|
|
679
|
-
return [3 /*break*/, 4];
|
|
680
|
-
case 2: return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
681
|
-
case 3:
|
|
682
|
-
_a = _g.sent();
|
|
683
|
-
_g.label = 4;
|
|
684
|
-
case 4:
|
|
685
|
-
userAccountPublicKey = _a;
|
|
686
|
-
remainingAccounts = [];
|
|
687
|
-
if (userInitialized) {
|
|
688
|
-
remainingAccounts = this.getRemainingAccounts({
|
|
689
|
-
writableSpotMarketIndex: marketIndex
|
|
690
|
-
});
|
|
691
|
-
}
|
|
692
|
-
else {
|
|
693
|
-
spotMarketAccount_1 = this.getSpotMarketAccount(marketIndex);
|
|
694
|
-
if (!spotMarketAccount_1.oracle.equals(web3_js_1.PublicKey["default"])) {
|
|
695
|
-
remainingAccounts.push({
|
|
696
|
-
pubkey: spotMarketAccount_1.oracle,
|
|
697
|
-
isSigner: false,
|
|
698
|
-
isWritable: false
|
|
699
|
-
});
|
|
700
|
-
}
|
|
701
|
-
remainingAccounts.push({
|
|
702
|
-
pubkey: spotMarketAccount_1.pubkey,
|
|
703
|
-
isSigner: false,
|
|
704
|
-
isWritable: true
|
|
705
|
-
});
|
|
706
|
-
}
|
|
707
|
-
spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
708
|
-
_c = (_b = this.program.instruction).deposit;
|
|
709
|
-
_d = [marketIndex,
|
|
710
|
-
amount,
|
|
711
|
-
reduceOnly];
|
|
712
|
-
_e = {};
|
|
713
|
-
_f = {};
|
|
714
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
715
|
-
case 5: return [4 /*yield*/, _c.apply(_b, _d.concat([(_e.accounts = (_f.state = _g.sent(),
|
|
716
|
-
_f.spotMarket = spotMarketAccount.pubkey,
|
|
717
|
-
_f.spotMarketVault = spotMarketAccount.vault,
|
|
718
|
-
_f.user = userAccountPublicKey,
|
|
719
|
-
_f.userStats = this.getUserStatsAccountPublicKey(),
|
|
720
|
-
_f.userTokenAccount = userTokenAccount,
|
|
721
|
-
_f.authority = this.wallet.publicKey,
|
|
722
|
-
_f.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
|
|
723
|
-
_f),
|
|
724
|
-
_e.remainingAccounts = remainingAccounts,
|
|
725
|
-
_e)]))];
|
|
726
|
-
case 6: return [2 /*return*/, _g.sent()];
|
|
727
|
-
}
|
|
728
|
-
});
|
|
729
|
-
});
|
|
730
|
-
};
|
|
731
|
-
ClearingHouse.prototype.checkIfAccountExists = function (account) {
|
|
732
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
733
|
-
var accountInfo, e_1;
|
|
734
|
-
return __generator(this, function (_a) {
|
|
735
|
-
switch (_a.label) {
|
|
736
|
-
case 0:
|
|
737
|
-
_a.trys.push([0, 2, , 3]);
|
|
738
|
-
return [4 /*yield*/, this.connection.getAccountInfo(account)];
|
|
739
|
-
case 1:
|
|
740
|
-
accountInfo = _a.sent();
|
|
741
|
-
return [2 /*return*/, accountInfo && true];
|
|
742
|
-
case 2:
|
|
743
|
-
e_1 = _a.sent();
|
|
744
|
-
// Doesn't already exist
|
|
745
|
-
return [2 /*return*/, false];
|
|
746
|
-
case 3: return [2 /*return*/];
|
|
747
|
-
}
|
|
748
|
-
});
|
|
749
|
-
});
|
|
750
|
-
};
|
|
751
|
-
ClearingHouse.prototype.getSolWithdrawalIxs = function (marketIndex, amount) {
|
|
752
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
753
|
-
var result, authority, _a, ixs, signers, pubkey, withdrawIx;
|
|
754
|
-
return __generator(this, function (_b) {
|
|
755
|
-
switch (_b.label) {
|
|
756
|
-
case 0:
|
|
757
|
-
result = {
|
|
758
|
-
ixs: [],
|
|
759
|
-
signers: [],
|
|
760
|
-
pubkey: web3_js_1.PublicKey["default"]
|
|
761
|
-
};
|
|
762
|
-
authority = this.wallet.publicKey;
|
|
763
|
-
return [4 /*yield*/, this.getWrappedSolAccountCreationIxs(amount)];
|
|
764
|
-
case 1:
|
|
765
|
-
_a = _b.sent(), ixs = _a.ixs, signers = _a.signers, pubkey = _a.pubkey;
|
|
766
|
-
result.pubkey = pubkey;
|
|
767
|
-
ixs.forEach(function (ix) {
|
|
768
|
-
result.ixs.push(ix);
|
|
769
|
-
});
|
|
770
|
-
signers.forEach(function (ix) {
|
|
771
|
-
result.signers.push(ix);
|
|
772
|
-
});
|
|
773
|
-
return [4 /*yield*/, this.getWithdrawIx(amount, marketIndex, pubkey, true)];
|
|
774
|
-
case 2:
|
|
775
|
-
withdrawIx = _b.sent();
|
|
776
|
-
result.ixs.push(withdrawIx);
|
|
777
|
-
result.ixs.push(spl_token_1.Token.createCloseAccountInstruction(spl_token_1.TOKEN_PROGRAM_ID, pubkey, authority, authority, []));
|
|
778
|
-
return [2 /*return*/, result];
|
|
779
|
-
}
|
|
780
|
-
});
|
|
781
|
-
});
|
|
782
|
-
};
|
|
783
|
-
ClearingHouse.prototype.getWrappedSolAccountCreationIxs = function (amount) {
|
|
784
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
785
|
-
var wrappedSolAccount, result, rentSpaceLamports, depositAmountLamports, authority;
|
|
786
|
-
return __generator(this, function (_a) {
|
|
787
|
-
wrappedSolAccount = new web3_js_1.Keypair();
|
|
788
|
-
result = {
|
|
789
|
-
ixs: [],
|
|
790
|
-
signers: [],
|
|
791
|
-
pubkey: wrappedSolAccount.publicKey
|
|
792
|
-
};
|
|
793
|
-
rentSpaceLamports = new anchor_1.BN(web3_js_1.LAMPORTS_PER_SOL / 100);
|
|
794
|
-
depositAmountLamports = amount.add(rentSpaceLamports);
|
|
795
|
-
authority = this.wallet.publicKey;
|
|
796
|
-
result.ixs.push(web3_js_1.SystemProgram.createAccount({
|
|
797
|
-
fromPubkey: authority,
|
|
798
|
-
newAccountPubkey: wrappedSolAccount.publicKey,
|
|
799
|
-
lamports: depositAmountLamports.toNumber(),
|
|
800
|
-
space: 165,
|
|
801
|
-
programId: spl_token_1.TOKEN_PROGRAM_ID
|
|
802
|
-
}));
|
|
803
|
-
result.ixs.push(spl_token_1.Token.createInitAccountInstruction(spl_token_1.TOKEN_PROGRAM_ID, spotMarkets_1.WRAPPED_SOL_MINT, wrappedSolAccount.publicKey, authority));
|
|
804
|
-
result.signers.push(wrappedSolAccount);
|
|
805
|
-
return [2 /*return*/, result];
|
|
806
|
-
});
|
|
807
|
-
});
|
|
808
|
-
};
|
|
809
|
-
/**
|
|
810
|
-
* Creates the Clearing House User account for a user, and deposits some initial collateral
|
|
811
|
-
* @param amount
|
|
812
|
-
* @param userTokenAccount
|
|
813
|
-
* @param marketIndex
|
|
814
|
-
* @param userId
|
|
815
|
-
* @param name
|
|
816
|
-
* @param fromUserId
|
|
817
|
-
* @returns
|
|
818
|
-
*/
|
|
819
|
-
ClearingHouse.prototype.initializeUserAccountAndDepositCollateral = function (amount, userTokenAccount, marketIndex, userId, name, fromUserId, referrerInfo) {
|
|
820
|
-
if (marketIndex === void 0) { marketIndex = new anchor_1.BN(0); }
|
|
821
|
-
if (userId === void 0) { userId = 0; }
|
|
822
|
-
if (name === void 0) { name = userName_1.DEFAULT_USER_NAME; }
|
|
823
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
824
|
-
var _a, userAccountPublicKey, initializeUserAccountIx, additionalSigners, spotMarket, isSolMarket, tx, authority, createWSOLTokenAccount, _b, startIxs, signers, pubkey, depositCollateralIx, _c, _d, _e, txSig;
|
|
825
|
-
return __generator(this, function (_f) {
|
|
826
|
-
switch (_f.label) {
|
|
827
|
-
case 0: return [4 /*yield*/, this.getInitializeUserInstructions(userId, name, referrerInfo)];
|
|
828
|
-
case 1:
|
|
829
|
-
_a = _f.sent(), userAccountPublicKey = _a[0], initializeUserAccountIx = _a[1];
|
|
830
|
-
additionalSigners = [];
|
|
831
|
-
spotMarket = this.getSpotMarketAccount(marketIndex);
|
|
832
|
-
isSolMarket = spotMarket.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
|
|
833
|
-
tx = new web3_js_1.Transaction();
|
|
834
|
-
authority = this.wallet.publicKey;
|
|
835
|
-
createWSOLTokenAccount = isSolMarket && userTokenAccount.equals(authority);
|
|
836
|
-
if (!createWSOLTokenAccount) return [3 /*break*/, 3];
|
|
837
|
-
return [4 /*yield*/, this.getWrappedSolAccountCreationIxs(amount)];
|
|
838
|
-
case 2:
|
|
839
|
-
_b = _f.sent(), startIxs = _b.ixs, signers = _b.signers, pubkey = _b.pubkey;
|
|
840
|
-
userTokenAccount = pubkey;
|
|
841
|
-
startIxs.forEach(function (ix) {
|
|
842
|
-
tx.add(ix);
|
|
843
|
-
});
|
|
844
|
-
signers.forEach(function (signer) { return additionalSigners.push(signer); });
|
|
845
|
-
_f.label = 3;
|
|
846
|
-
case 3:
|
|
847
|
-
if (!(fromUserId != null)) return [3 /*break*/, 5];
|
|
848
|
-
return [4 /*yield*/, this.getTransferDepositIx(amount, marketIndex, fromUserId, userId)];
|
|
849
|
-
case 4:
|
|
850
|
-
_c = _f.sent();
|
|
851
|
-
return [3 /*break*/, 7];
|
|
852
|
-
case 5: return [4 /*yield*/, this.getDepositInstruction(amount, marketIndex, userTokenAccount, userId, false, false)];
|
|
853
|
-
case 6:
|
|
854
|
-
_c = _f.sent();
|
|
855
|
-
_f.label = 7;
|
|
856
|
-
case 7:
|
|
857
|
-
depositCollateralIx = _c;
|
|
858
|
-
if (!(userId === 0)) return [3 /*break*/, 9];
|
|
859
|
-
_e = (_d = tx).add;
|
|
860
|
-
return [4 /*yield*/, this.getInitializeUserStatsIx()];
|
|
861
|
-
case 8:
|
|
862
|
-
_e.apply(_d, [_f.sent()]);
|
|
863
|
-
_f.label = 9;
|
|
864
|
-
case 9:
|
|
865
|
-
tx.add(initializeUserAccountIx).add(depositCollateralIx);
|
|
866
|
-
// Close the wrapped sol account at the end of the transaction
|
|
867
|
-
if (createWSOLTokenAccount) {
|
|
868
|
-
tx.add(spl_token_1.Token.createCloseAccountInstruction(spl_token_1.TOKEN_PROGRAM_ID, userTokenAccount, authority, authority, []));
|
|
869
|
-
}
|
|
870
|
-
return [4 /*yield*/, this.txSender.send(tx, additionalSigners, this.opts)];
|
|
871
|
-
case 10:
|
|
872
|
-
txSig = (_f.sent()).txSig;
|
|
873
|
-
return [2 /*return*/, [txSig, userAccountPublicKey]];
|
|
874
|
-
}
|
|
875
|
-
});
|
|
876
|
-
});
|
|
877
|
-
};
|
|
878
|
-
ClearingHouse.prototype.initializeUserAccountForDevnet = function (userId, name, marketIndex, tokenFaucet, amount, referrerInfo) {
|
|
879
|
-
if (userId === void 0) { userId = 0; }
|
|
880
|
-
if (name === void 0) { name = userName_1.DEFAULT_USER_NAME; }
|
|
881
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
882
|
-
var _a, associateTokenPublicKey, createAssociatedAccountIx, mintToIx, _b, userAccountPublicKey, initializeUserAccountIx, depositCollateralIx, tx, _c, _d, txSig;
|
|
883
|
-
return __generator(this, function (_e) {
|
|
884
|
-
switch (_e.label) {
|
|
885
|
-
case 0: return [4 /*yield*/, tokenFaucet.createAssociatedTokenAccountAndMintToInstructions(this.wallet.publicKey, amount)];
|
|
886
|
-
case 1:
|
|
887
|
-
_a = _e.sent(), associateTokenPublicKey = _a[0], createAssociatedAccountIx = _a[1], mintToIx = _a[2];
|
|
888
|
-
return [4 /*yield*/, this.getInitializeUserInstructions(userId, name, referrerInfo)];
|
|
889
|
-
case 2:
|
|
890
|
-
_b = _e.sent(), userAccountPublicKey = _b[0], initializeUserAccountIx = _b[1];
|
|
891
|
-
return [4 /*yield*/, this.getDepositInstruction(amount, marketIndex, associateTokenPublicKey, userId, false, false)];
|
|
892
|
-
case 3:
|
|
893
|
-
depositCollateralIx = _e.sent();
|
|
894
|
-
tx = new web3_js_1.Transaction().add(createAssociatedAccountIx).add(mintToIx);
|
|
895
|
-
if (!(userId === 0)) return [3 /*break*/, 5];
|
|
896
|
-
_d = (_c = tx).add;
|
|
897
|
-
return [4 /*yield*/, this.getInitializeUserStatsIx()];
|
|
898
|
-
case 4:
|
|
899
|
-
_d.apply(_c, [_e.sent()]);
|
|
900
|
-
_e.label = 5;
|
|
901
|
-
case 5:
|
|
902
|
-
tx.add(initializeUserAccountIx).add(depositCollateralIx);
|
|
903
|
-
return [4 /*yield*/, this.program.provider.sendAndConfirm(tx, [])];
|
|
904
|
-
case 6:
|
|
905
|
-
txSig = _e.sent();
|
|
906
|
-
return [2 /*return*/, [txSig, userAccountPublicKey]];
|
|
907
|
-
}
|
|
908
|
-
});
|
|
909
|
-
});
|
|
910
|
-
};
|
|
911
|
-
ClearingHouse.prototype.withdraw = function (amount, marketIndex, userTokenAccount, reduceOnly) {
|
|
912
|
-
if (reduceOnly === void 0) { reduceOnly = false; }
|
|
913
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
914
|
-
var tx, additionalSigners, spotMarketAccount, isSolMarket, authority, createWSOLTokenAccount, _a, ixs, signers, pubkey, withdrawCollateral, txSig;
|
|
915
|
-
return __generator(this, function (_b) {
|
|
916
|
-
switch (_b.label) {
|
|
917
|
-
case 0:
|
|
918
|
-
tx = new web3_js_1.Transaction();
|
|
919
|
-
additionalSigners = [];
|
|
920
|
-
spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
921
|
-
isSolMarket = spotMarketAccount.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
|
|
922
|
-
authority = this.wallet.publicKey;
|
|
923
|
-
createWSOLTokenAccount = isSolMarket && userTokenAccount.equals(authority);
|
|
924
|
-
if (!createWSOLTokenAccount) return [3 /*break*/, 2];
|
|
925
|
-
return [4 /*yield*/, this.getWrappedSolAccountCreationIxs(amount)];
|
|
926
|
-
case 1:
|
|
927
|
-
_a = _b.sent(), ixs = _a.ixs, signers = _a.signers, pubkey = _a.pubkey;
|
|
928
|
-
userTokenAccount = pubkey;
|
|
929
|
-
ixs.forEach(function (ix) {
|
|
930
|
-
tx.add(ix);
|
|
931
|
-
});
|
|
932
|
-
signers.forEach(function (signer) { return additionalSigners.push(signer); });
|
|
933
|
-
_b.label = 2;
|
|
934
|
-
case 2: return [4 /*yield*/, this.getWithdrawIx(amount, spotMarketAccount.marketIndex, userTokenAccount, reduceOnly)];
|
|
935
|
-
case 3:
|
|
936
|
-
withdrawCollateral = _b.sent();
|
|
937
|
-
tx.add(withdrawCollateral);
|
|
938
|
-
// Close the wrapped sol account at the end of the transaction
|
|
939
|
-
if (createWSOLTokenAccount) {
|
|
940
|
-
tx.add(spl_token_1.Token.createCloseAccountInstruction(spl_token_1.TOKEN_PROGRAM_ID, userTokenAccount, authority, authority, []));
|
|
941
|
-
}
|
|
942
|
-
return [4 /*yield*/, this.txSender.send(tx, additionalSigners, this.opts)];
|
|
943
|
-
case 4:
|
|
944
|
-
txSig = (_b.sent()).txSig;
|
|
945
|
-
return [2 /*return*/, txSig];
|
|
946
|
-
}
|
|
947
|
-
});
|
|
948
|
-
});
|
|
949
|
-
};
|
|
950
|
-
ClearingHouse.prototype.getWithdrawIx = function (amount, marketIndex, userTokenAccount, reduceOnly) {
|
|
951
|
-
if (reduceOnly === void 0) { reduceOnly = false; }
|
|
952
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
953
|
-
var userAccountPublicKey, remainingAccounts, spotMarketAccount, _a, _b, _c;
|
|
954
|
-
var _d, _e;
|
|
955
|
-
return __generator(this, function (_f) {
|
|
956
|
-
switch (_f.label) {
|
|
957
|
-
case 0: return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
958
|
-
case 1:
|
|
959
|
-
userAccountPublicKey = _f.sent();
|
|
960
|
-
remainingAccounts = this.getRemainingAccounts({
|
|
961
|
-
writableSpotMarketIndex: marketIndex
|
|
962
|
-
});
|
|
963
|
-
spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
964
|
-
_b = (_a = this.program.instruction).withdraw;
|
|
965
|
-
_c = [marketIndex,
|
|
966
|
-
amount,
|
|
967
|
-
reduceOnly];
|
|
968
|
-
_d = {};
|
|
969
|
-
_e = {};
|
|
970
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
971
|
-
case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
972
|
-
_e.spotMarket = spotMarketAccount.pubkey,
|
|
973
|
-
_e.spotMarketVault = spotMarketAccount.vault,
|
|
974
|
-
_e.clearingHouseSigner = this.getSignerPublicKey(),
|
|
975
|
-
_e.user = userAccountPublicKey,
|
|
976
|
-
_e.userStats = this.getUserStatsAccountPublicKey(),
|
|
977
|
-
_e.userTokenAccount = userTokenAccount,
|
|
978
|
-
_e.authority = this.wallet.publicKey,
|
|
979
|
-
_e.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
|
|
980
|
-
_e),
|
|
981
|
-
_d.remainingAccounts = remainingAccounts,
|
|
982
|
-
_d)]))];
|
|
983
|
-
case 3: return [2 /*return*/, _f.sent()];
|
|
984
|
-
}
|
|
985
|
-
});
|
|
986
|
-
});
|
|
987
|
-
};
|
|
988
|
-
ClearingHouse.prototype.transferDeposit = function (amount, marketIndex, fromUserId, toUserId) {
|
|
989
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
990
|
-
var txSig, _a, _b, _c;
|
|
991
|
-
return __generator(this, function (_d) {
|
|
992
|
-
switch (_d.label) {
|
|
993
|
-
case 0:
|
|
994
|
-
_b = (_a = this.txSender).send;
|
|
995
|
-
_c = utils_1.wrapInTx;
|
|
996
|
-
return [4 /*yield*/, this.getTransferDepositIx(amount, marketIndex, fromUserId, toUserId)];
|
|
997
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
998
|
-
[],
|
|
999
|
-
this.opts])];
|
|
1000
|
-
case 2:
|
|
1001
|
-
txSig = (_d.sent()).txSig;
|
|
1002
|
-
return [2 /*return*/, txSig];
|
|
1003
|
-
}
|
|
1004
|
-
});
|
|
1005
|
-
});
|
|
1006
|
-
};
|
|
1007
|
-
ClearingHouse.prototype.getTransferDepositIx = function (amount, marketIndex, fromUserId, toUserId) {
|
|
1008
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1009
|
-
var fromUser, toUser, remainingAccounts, _a, _b, _c;
|
|
1010
|
-
var _d, _e;
|
|
1011
|
-
return __generator(this, function (_f) {
|
|
1012
|
-
switch (_f.label) {
|
|
1013
|
-
case 0: return [4 /*yield*/, (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, fromUserId)];
|
|
1014
|
-
case 1:
|
|
1015
|
-
fromUser = _f.sent();
|
|
1016
|
-
return [4 /*yield*/, (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, toUserId)];
|
|
1017
|
-
case 2:
|
|
1018
|
-
toUser = _f.sent();
|
|
1019
|
-
remainingAccounts = this.getRemainingAccounts({
|
|
1020
|
-
writableSpotMarketIndex: marketIndex
|
|
1021
|
-
});
|
|
1022
|
-
_b = (_a = this.program.instruction).transferDeposit;
|
|
1023
|
-
_c = [marketIndex, amount];
|
|
1024
|
-
_d = {};
|
|
1025
|
-
_e = {
|
|
1026
|
-
authority: this.wallet.publicKey,
|
|
1027
|
-
fromUser: fromUser,
|
|
1028
|
-
toUser: toUser,
|
|
1029
|
-
userStats: this.getUserStatsAccountPublicKey()
|
|
1030
|
-
};
|
|
1031
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
1032
|
-
case 3: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
1033
|
-
_e),
|
|
1034
|
-
_d.remainingAccounts = remainingAccounts,
|
|
1035
|
-
_d)]))];
|
|
1036
|
-
case 4: return [2 /*return*/, _f.sent()];
|
|
1037
|
-
}
|
|
1038
|
-
});
|
|
1039
|
-
});
|
|
1040
|
-
};
|
|
1041
|
-
ClearingHouse.prototype.updateSpotMarketCumulativeInterest = function (marketIndex) {
|
|
1042
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1043
|
-
var txSig, _a, _b, _c;
|
|
1044
|
-
return __generator(this, function (_d) {
|
|
1045
|
-
switch (_d.label) {
|
|
1046
|
-
case 0:
|
|
1047
|
-
_b = (_a = this.txSender).send;
|
|
1048
|
-
_c = utils_1.wrapInTx;
|
|
1049
|
-
return [4 /*yield*/, this.updateSpotMarketCumulativeInterestIx(marketIndex)];
|
|
1050
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
1051
|
-
[],
|
|
1052
|
-
this.opts])];
|
|
1053
|
-
case 2:
|
|
1054
|
-
txSig = (_d.sent()).txSig;
|
|
1055
|
-
return [2 /*return*/, txSig];
|
|
1056
|
-
}
|
|
1057
|
-
});
|
|
1058
|
-
});
|
|
1059
|
-
};
|
|
1060
|
-
ClearingHouse.prototype.updateSpotMarketCumulativeInterestIx = function (marketIndex) {
|
|
1061
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1062
|
-
var spotMarket;
|
|
1063
|
-
return __generator(this, function (_a) {
|
|
1064
|
-
switch (_a.label) {
|
|
1065
|
-
case 0:
|
|
1066
|
-
spotMarket = this.getSpotMarketAccount(marketIndex);
|
|
1067
|
-
return [4 /*yield*/, this.program.instruction.updateSpotMarketCumulativeInterest({
|
|
1068
|
-
accounts: {
|
|
1069
|
-
spotMarket: spotMarket.pubkey
|
|
1070
|
-
}
|
|
1071
|
-
})];
|
|
1072
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1073
|
-
}
|
|
1074
|
-
});
|
|
1075
|
-
});
|
|
1076
|
-
};
|
|
1077
|
-
ClearingHouse.prototype.settleLP = function (settleeUserAccountPublicKey, marketIndex) {
|
|
1078
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1079
|
-
var txSig, _a, _b, _c;
|
|
1080
|
-
return __generator(this, function (_d) {
|
|
1081
|
-
switch (_d.label) {
|
|
1082
|
-
case 0:
|
|
1083
|
-
_b = (_a = this.txSender).send;
|
|
1084
|
-
_c = utils_1.wrapInTx;
|
|
1085
|
-
return [4 /*yield*/, this.settleLPIx(settleeUserAccountPublicKey, marketIndex)];
|
|
1086
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
1087
|
-
[],
|
|
1088
|
-
this.opts])];
|
|
1089
|
-
case 2:
|
|
1090
|
-
txSig = (_d.sent()).txSig;
|
|
1091
|
-
return [2 /*return*/, txSig];
|
|
1092
|
-
}
|
|
1093
|
-
});
|
|
1094
|
-
});
|
|
1095
|
-
};
|
|
1096
|
-
ClearingHouse.prototype.settleLPIx = function (settleeUserAccountPublicKey, marketIndex) {
|
|
1097
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1098
|
-
var settleeUserAccount, userPositions, remainingAccounts, foundMarket, _i, userPositions_1, position, marketPublicKey, _a, _b, _c;
|
|
1099
|
-
var _d, _e;
|
|
1100
|
-
return __generator(this, function (_f) {
|
|
1101
|
-
switch (_f.label) {
|
|
1102
|
-
case 0: return [4 /*yield*/, this.program.account.user.fetch(settleeUserAccountPublicKey)];
|
|
1103
|
-
case 1:
|
|
1104
|
-
settleeUserAccount = (_f.sent());
|
|
1105
|
-
userPositions = settleeUserAccount.perpPositions;
|
|
1106
|
-
remainingAccounts = [];
|
|
1107
|
-
foundMarket = false;
|
|
1108
|
-
_i = 0, userPositions_1 = userPositions;
|
|
1109
|
-
_f.label = 2;
|
|
1110
|
-
case 2:
|
|
1111
|
-
if (!(_i < userPositions_1.length)) return [3 /*break*/, 5];
|
|
1112
|
-
position = userPositions_1[_i];
|
|
1113
|
-
if (!!(0, position_1.positionIsAvailable)(position)) return [3 /*break*/, 4];
|
|
1114
|
-
return [4 /*yield*/, (0, pda_1.getMarketPublicKey)(this.program.programId, position.marketIndex)];
|
|
1115
|
-
case 3:
|
|
1116
|
-
marketPublicKey = _f.sent();
|
|
1117
|
-
remainingAccounts.push({
|
|
1118
|
-
pubkey: marketPublicKey,
|
|
1119
|
-
isWritable: true,
|
|
1120
|
-
isSigner: false
|
|
1121
|
-
});
|
|
1122
|
-
if (marketIndex.eq(position.marketIndex)) {
|
|
1123
|
-
foundMarket = true;
|
|
1124
|
-
}
|
|
1125
|
-
_f.label = 4;
|
|
1126
|
-
case 4:
|
|
1127
|
-
_i++;
|
|
1128
|
-
return [3 /*break*/, 2];
|
|
1129
|
-
case 5:
|
|
1130
|
-
if (!foundMarket) {
|
|
1131
|
-
console.log('Warning: lp is not in the market specified -- tx will likely fail');
|
|
1132
|
-
}
|
|
1133
|
-
_b = (_a = this.program.instruction).settleLp;
|
|
1134
|
-
_c = [marketIndex];
|
|
1135
|
-
_d = {};
|
|
1136
|
-
_e = {};
|
|
1137
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
1138
|
-
case 6: return [2 /*return*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
1139
|
-
_e.user = settleeUserAccountPublicKey,
|
|
1140
|
-
_e),
|
|
1141
|
-
_d.remainingAccounts = remainingAccounts,
|
|
1142
|
-
_d)]))];
|
|
1143
|
-
}
|
|
1144
|
-
});
|
|
1145
|
-
});
|
|
1146
|
-
};
|
|
1147
|
-
ClearingHouse.prototype.removeLiquidity = function (marketIndex, sharesToBurn) {
|
|
1148
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1149
|
-
var txSig, _a, _b, _c;
|
|
1150
|
-
return __generator(this, function (_d) {
|
|
1151
|
-
switch (_d.label) {
|
|
1152
|
-
case 0:
|
|
1153
|
-
_b = (_a = this.txSender).send;
|
|
1154
|
-
_c = utils_1.wrapInTx;
|
|
1155
|
-
return [4 /*yield*/, this.getRemoveLiquidityIx(marketIndex, sharesToBurn)];
|
|
1156
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
1157
|
-
[],
|
|
1158
|
-
this.opts])];
|
|
1159
|
-
case 2:
|
|
1160
|
-
txSig = (_d.sent()).txSig;
|
|
1161
|
-
return [2 /*return*/, txSig];
|
|
1162
|
-
}
|
|
1163
|
-
});
|
|
1164
|
-
});
|
|
1165
|
-
};
|
|
1166
|
-
ClearingHouse.prototype.getRemoveLiquidityIx = function (marketIndex, sharesToBurn) {
|
|
1167
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1168
|
-
var userAccountPublicKey, remainingAccounts, userAccount, perpPosition, _a, _b, _c;
|
|
1169
|
-
var _d, _e;
|
|
1170
|
-
return __generator(this, function (_f) {
|
|
1171
|
-
switch (_f.label) {
|
|
1172
|
-
case 0: return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
1173
|
-
case 1:
|
|
1174
|
-
userAccountPublicKey = _f.sent();
|
|
1175
|
-
remainingAccounts = this.getRemainingAccounts({
|
|
1176
|
-
writablePerpMarketIndex: marketIndex
|
|
1177
|
-
});
|
|
1178
|
-
if (sharesToBurn == undefined) {
|
|
1179
|
-
userAccount = this.getUserAccount();
|
|
1180
|
-
perpPosition = userAccount.perpPositions.filter(function (position) {
|
|
1181
|
-
return position.marketIndex.eq(marketIndex);
|
|
1182
|
-
})[0];
|
|
1183
|
-
sharesToBurn = perpPosition.lpShares;
|
|
1184
|
-
console.log('burning lp shares:', sharesToBurn.toString());
|
|
1185
|
-
}
|
|
1186
|
-
_b = (_a = this.program.instruction).removeLiquidity;
|
|
1187
|
-
_c = [sharesToBurn, marketIndex];
|
|
1188
|
-
_d = {};
|
|
1189
|
-
_e = {};
|
|
1190
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
1191
|
-
case 2: return [2 /*return*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
1192
|
-
_e.user = userAccountPublicKey,
|
|
1193
|
-
_e.authority = this.wallet.publicKey,
|
|
1194
|
-
_e),
|
|
1195
|
-
_d.remainingAccounts = remainingAccounts,
|
|
1196
|
-
_d)]))];
|
|
1197
|
-
}
|
|
1198
|
-
});
|
|
1199
|
-
});
|
|
1200
|
-
};
|
|
1201
|
-
ClearingHouse.prototype.addLiquidity = function (amount, marketIndex) {
|
|
1202
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1203
|
-
var _a, txSig, slot, _b, _c, _d;
|
|
1204
|
-
return __generator(this, function (_e) {
|
|
1205
|
-
switch (_e.label) {
|
|
1206
|
-
case 0:
|
|
1207
|
-
_c = (_b = this.txSender).send;
|
|
1208
|
-
_d = utils_1.wrapInTx;
|
|
1209
|
-
return [4 /*yield*/, this.getAddLiquidityIx(amount, marketIndex)];
|
|
1210
|
-
case 1: return [4 /*yield*/, _c.apply(_b, [_d.apply(void 0, [_e.sent()]),
|
|
1211
|
-
[],
|
|
1212
|
-
this.opts])];
|
|
1213
|
-
case 2:
|
|
1214
|
-
_a = _e.sent(), txSig = _a.txSig, slot = _a.slot;
|
|
1215
|
-
this.marketLastSlotCache.set(marketIndex.toNumber(), slot);
|
|
1216
|
-
return [2 /*return*/, txSig];
|
|
1217
|
-
}
|
|
1218
|
-
});
|
|
1219
|
-
});
|
|
1220
|
-
};
|
|
1221
|
-
ClearingHouse.prototype.getAddLiquidityIx = function (amount, marketIndex) {
|
|
1222
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1223
|
-
var userAccountPublicKey, remainingAccounts, _a, _b, _c;
|
|
1224
|
-
var _d, _e;
|
|
1225
|
-
return __generator(this, function (_f) {
|
|
1226
|
-
switch (_f.label) {
|
|
1227
|
-
case 0: return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
1228
|
-
case 1:
|
|
1229
|
-
userAccountPublicKey = _f.sent();
|
|
1230
|
-
remainingAccounts = this.getRemainingAccounts({
|
|
1231
|
-
writablePerpMarketIndex: marketIndex
|
|
1232
|
-
});
|
|
1233
|
-
_b = (_a = this.program.instruction).addLiquidity;
|
|
1234
|
-
_c = [amount, marketIndex];
|
|
1235
|
-
_d = {};
|
|
1236
|
-
_e = {};
|
|
1237
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
1238
|
-
case 2: return [2 /*return*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
1239
|
-
_e.user = userAccountPublicKey,
|
|
1240
|
-
_e.authority = this.wallet.publicKey,
|
|
1241
|
-
_e),
|
|
1242
|
-
_d.remainingAccounts = remainingAccounts,
|
|
1243
|
-
_d)]))];
|
|
1244
|
-
}
|
|
1245
|
-
});
|
|
1246
|
-
});
|
|
1247
|
-
};
|
|
1248
|
-
ClearingHouse.prototype.openPosition = function (direction, amount, marketIndex, limitPrice) {
|
|
1249
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1250
|
-
return __generator(this, function (_a) {
|
|
1251
|
-
switch (_a.label) {
|
|
1252
|
-
case 0: return [4 /*yield*/, this.placeAndTake({
|
|
1253
|
-
orderType: types_1.OrderType.MARKET,
|
|
1254
|
-
marketIndex: marketIndex,
|
|
1255
|
-
direction: direction,
|
|
1256
|
-
baseAssetAmount: amount,
|
|
1257
|
-
price: limitPrice
|
|
1258
|
-
})];
|
|
1259
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1260
|
-
}
|
|
1261
|
-
});
|
|
1262
|
-
});
|
|
1263
|
-
};
|
|
1264
|
-
ClearingHouse.prototype.sendSignedTx = function (tx) {
|
|
1265
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1266
|
-
var txSig;
|
|
1267
|
-
return __generator(this, function (_a) {
|
|
1268
|
-
switch (_a.label) {
|
|
1269
|
-
case 0: return [4 /*yield*/, this.txSender.send(tx, undefined, this.opts, true)];
|
|
1270
|
-
case 1:
|
|
1271
|
-
txSig = (_a.sent()).txSig;
|
|
1272
|
-
return [2 /*return*/, txSig];
|
|
1273
|
-
}
|
|
1274
|
-
});
|
|
1275
|
-
});
|
|
1276
|
-
};
|
|
1277
|
-
/**
|
|
1278
|
-
* Sends a market order and returns a signed tx which can fill the order against the vamm, which the caller can use to fill their own order if required.
|
|
1279
|
-
* @param orderParams
|
|
1280
|
-
* @param userAccountPublicKey
|
|
1281
|
-
* @param userAccount
|
|
1282
|
-
* @returns
|
|
1283
|
-
*/
|
|
1284
|
-
ClearingHouse.prototype.sendMarketOrderAndGetSignedFillTx = function (orderParams, userAccountPublicKey, userAccount) {
|
|
1285
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1286
|
-
var marketIndex, orderId, marketOrderTx, _a, fillTx, _b, currentBlockHash, _c, signedMarketOrderTx, signedFillTx, _d, txSig, slot;
|
|
1287
|
-
return __generator(this, function (_e) {
|
|
1288
|
-
switch (_e.label) {
|
|
1289
|
-
case 0:
|
|
1290
|
-
marketIndex = orderParams.marketIndex;
|
|
1291
|
-
orderId = userAccount.nextOrderId;
|
|
1292
|
-
_a = utils_1.wrapInTx;
|
|
1293
|
-
return [4 /*yield*/, this.getPlaceOrderIx(orderParams)];
|
|
1294
|
-
case 1:
|
|
1295
|
-
marketOrderTx = _a.apply(void 0, [_e.sent()]);
|
|
1296
|
-
_b = utils_1.wrapInTx;
|
|
1297
|
-
return [4 /*yield*/, this.getFillOrderIx(userAccountPublicKey, userAccount, {
|
|
1298
|
-
orderId: orderId,
|
|
1299
|
-
marketIndex: marketIndex
|
|
1300
|
-
})];
|
|
1301
|
-
case 2:
|
|
1302
|
-
fillTx = _b.apply(void 0, [_e.sent()]);
|
|
1303
|
-
return [4 /*yield*/, this.connection.getLatestBlockhash('finalized')];
|
|
1304
|
-
case 3:
|
|
1305
|
-
currentBlockHash = (_e.sent()).blockhash;
|
|
1306
|
-
marketOrderTx.recentBlockhash = currentBlockHash;
|
|
1307
|
-
fillTx.recentBlockhash = currentBlockHash;
|
|
1308
|
-
marketOrderTx.feePayer = userAccount.authority;
|
|
1309
|
-
fillTx.feePayer = userAccount.authority;
|
|
1310
|
-
return [4 /*yield*/, this.provider.wallet.signAllTransactions([marketOrderTx, fillTx])];
|
|
1311
|
-
case 4:
|
|
1312
|
-
_c = _e.sent(), signedMarketOrderTx = _c[0], signedFillTx = _c[1];
|
|
1313
|
-
return [4 /*yield*/, this.txSender.send(signedMarketOrderTx, [], this.opts, true)];
|
|
1314
|
-
case 5:
|
|
1315
|
-
_d = _e.sent(), txSig = _d.txSig, slot = _d.slot;
|
|
1316
|
-
this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
|
|
1317
|
-
return [2 /*return*/, { txSig: txSig, signedFillTx: signedFillTx }];
|
|
1318
|
-
}
|
|
1319
|
-
});
|
|
1320
|
-
});
|
|
1321
|
-
};
|
|
1322
|
-
ClearingHouse.prototype.placeOrder = function (orderParams) {
|
|
1323
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1324
|
-
var _a, txSig, slot, _b, _c, _d;
|
|
1325
|
-
return __generator(this, function (_e) {
|
|
1326
|
-
switch (_e.label) {
|
|
1327
|
-
case 0:
|
|
1328
|
-
_c = (_b = this.txSender).send;
|
|
1329
|
-
_d = utils_1.wrapInTx;
|
|
1330
|
-
return [4 /*yield*/, this.getPlaceOrderIx(orderParams)];
|
|
1331
|
-
case 1: return [4 /*yield*/, _c.apply(_b, [_d.apply(void 0, [_e.sent()]),
|
|
1332
|
-
[],
|
|
1333
|
-
this.opts])];
|
|
1334
|
-
case 2:
|
|
1335
|
-
_a = _e.sent(), txSig = _a.txSig, slot = _a.slot;
|
|
1336
|
-
this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
|
|
1337
|
-
return [2 /*return*/, txSig];
|
|
1338
|
-
}
|
|
1339
|
-
});
|
|
1340
|
-
});
|
|
1341
|
-
};
|
|
1342
|
-
ClearingHouse.prototype.getOrderParams = function (optionalOrderParams, marketType) {
|
|
1343
|
-
return Object.assign({}, types_1.DefaultOrderParams, optionalOrderParams, {
|
|
1344
|
-
marketType: marketType
|
|
1345
|
-
});
|
|
1346
|
-
};
|
|
1347
|
-
ClearingHouse.prototype.getPlaceOrderIx = function (orderParams) {
|
|
1348
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1349
|
-
var userAccountPublicKey, remainingAccounts, _a, _b, _c;
|
|
1350
|
-
var _d, _e;
|
|
1351
|
-
return __generator(this, function (_f) {
|
|
1352
|
-
switch (_f.label) {
|
|
1353
|
-
case 0:
|
|
1354
|
-
orderParams = this.getOrderParams(orderParams, types_1.MarketType.PERP);
|
|
1355
|
-
return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
1356
|
-
case 1:
|
|
1357
|
-
userAccountPublicKey = _f.sent();
|
|
1358
|
-
remainingAccounts = this.getRemainingAccounts({
|
|
1359
|
-
readablePerpMarketIndex: orderParams.marketIndex
|
|
1360
|
-
});
|
|
1361
|
-
_b = (_a = this.program.instruction).placeOrder;
|
|
1362
|
-
_c = [orderParams];
|
|
1363
|
-
_d = {};
|
|
1364
|
-
_e = {};
|
|
1365
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
1366
|
-
case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
1367
|
-
_e.user = userAccountPublicKey,
|
|
1368
|
-
_e.userStats = this.getUserStatsAccountPublicKey(),
|
|
1369
|
-
_e.authority = this.wallet.publicKey,
|
|
1370
|
-
_e),
|
|
1371
|
-
_d.remainingAccounts = remainingAccounts,
|
|
1372
|
-
_d)]))];
|
|
1373
|
-
case 3: return [2 /*return*/, _f.sent()];
|
|
1374
|
-
}
|
|
1375
|
-
});
|
|
1376
|
-
});
|
|
1377
|
-
};
|
|
1378
|
-
ClearingHouse.prototype.updateAMMs = function (marketIndexes) {
|
|
1379
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1380
|
-
var txSig, _a, _b, _c;
|
|
1381
|
-
return __generator(this, function (_d) {
|
|
1382
|
-
switch (_d.label) {
|
|
1383
|
-
case 0:
|
|
1384
|
-
_b = (_a = this.txSender).send;
|
|
1385
|
-
_c = utils_1.wrapInTx;
|
|
1386
|
-
return [4 /*yield*/, this.getUpdateAMMsIx(marketIndexes)];
|
|
1387
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
1388
|
-
[],
|
|
1389
|
-
this.opts])];
|
|
1390
|
-
case 2:
|
|
1391
|
-
txSig = (_d.sent()).txSig;
|
|
1392
|
-
return [2 /*return*/, txSig];
|
|
1393
|
-
}
|
|
1394
|
-
});
|
|
1395
|
-
});
|
|
1396
|
-
};
|
|
1397
|
-
ClearingHouse.prototype.getUpdateAMMsIx = function (marketIndexes) {
|
|
1398
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1399
|
-
var i, marketAccountInfos, oracleAccountInfos, _i, marketIndexes_1, marketIndex, market, remainingAccounts, _a, _b, _c;
|
|
1400
|
-
var _d, _e;
|
|
1401
|
-
return __generator(this, function (_f) {
|
|
1402
|
-
switch (_f.label) {
|
|
1403
|
-
case 0:
|
|
1404
|
-
for (i = marketIndexes.length; i < 5; i++) {
|
|
1405
|
-
marketIndexes.push(new anchor_1.BN(100));
|
|
1406
|
-
}
|
|
1407
|
-
marketAccountInfos = [];
|
|
1408
|
-
oracleAccountInfos = [];
|
|
1409
|
-
for (_i = 0, marketIndexes_1 = marketIndexes; _i < marketIndexes_1.length; _i++) {
|
|
1410
|
-
marketIndex = marketIndexes_1[_i];
|
|
1411
|
-
if (!marketIndex.eq(new anchor_1.BN(100))) {
|
|
1412
|
-
market = this.getPerpMarketAccount(marketIndex);
|
|
1413
|
-
marketAccountInfos.push({
|
|
1414
|
-
pubkey: market.pubkey,
|
|
1415
|
-
isWritable: true,
|
|
1416
|
-
isSigner: false
|
|
1417
|
-
});
|
|
1418
|
-
oracleAccountInfos.push({
|
|
1419
|
-
pubkey: market.amm.oracle,
|
|
1420
|
-
isWritable: false,
|
|
1421
|
-
isSigner: false
|
|
1422
|
-
});
|
|
1423
|
-
}
|
|
1424
|
-
}
|
|
1425
|
-
remainingAccounts = oracleAccountInfos.concat(marketAccountInfos);
|
|
1426
|
-
_b = (_a = this.program.instruction).updateAmms;
|
|
1427
|
-
_c = [marketIndexes];
|
|
1428
|
-
_d = {};
|
|
1429
|
-
_e = {};
|
|
1430
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
1431
|
-
case 1: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
1432
|
-
_e.authority = this.wallet.publicKey,
|
|
1433
|
-
_e),
|
|
1434
|
-
_d.remainingAccounts = remainingAccounts,
|
|
1435
|
-
_d)]))];
|
|
1436
|
-
case 2: return [2 /*return*/, _f.sent()];
|
|
1437
|
-
}
|
|
1438
|
-
});
|
|
1439
|
-
});
|
|
1440
|
-
};
|
|
1441
|
-
ClearingHouse.prototype.settleExpiredMarket = function (marketIndex) {
|
|
1442
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1443
|
-
var txSig, _a, _b, _c;
|
|
1444
|
-
return __generator(this, function (_d) {
|
|
1445
|
-
switch (_d.label) {
|
|
1446
|
-
case 0:
|
|
1447
|
-
_b = (_a = this.txSender).send;
|
|
1448
|
-
_c = utils_1.wrapInTx;
|
|
1449
|
-
return [4 /*yield*/, this.getSettleExpiredMarketIx(marketIndex)];
|
|
1450
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
1451
|
-
[],
|
|
1452
|
-
this.opts])];
|
|
1453
|
-
case 2:
|
|
1454
|
-
txSig = (_d.sent()).txSig;
|
|
1455
|
-
return [2 /*return*/, txSig];
|
|
1456
|
-
}
|
|
1457
|
-
});
|
|
1458
|
-
});
|
|
1459
|
-
};
|
|
1460
|
-
ClearingHouse.prototype.getSettleExpiredMarketIx = function (marketIndex) {
|
|
1461
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1462
|
-
var marketAccountInfos, oracleAccountInfos, spotMarketAccountInfos, market, remainingAccounts, _a, _b, _c;
|
|
1463
|
-
var _d, _e;
|
|
1464
|
-
return __generator(this, function (_f) {
|
|
1465
|
-
switch (_f.label) {
|
|
1466
|
-
case 0:
|
|
1467
|
-
marketAccountInfos = [];
|
|
1468
|
-
oracleAccountInfos = [];
|
|
1469
|
-
spotMarketAccountInfos = [];
|
|
1470
|
-
market = this.getPerpMarketAccount(marketIndex);
|
|
1471
|
-
marketAccountInfos.push({
|
|
1472
|
-
pubkey: market.pubkey,
|
|
1473
|
-
isWritable: true,
|
|
1474
|
-
isSigner: false
|
|
1475
|
-
});
|
|
1476
|
-
oracleAccountInfos.push({
|
|
1477
|
-
pubkey: market.amm.oracle,
|
|
1478
|
-
isWritable: false,
|
|
1479
|
-
isSigner: false
|
|
1480
|
-
});
|
|
1481
|
-
spotMarketAccountInfos.push({
|
|
1482
|
-
pubkey: this.getSpotMarketAccount(numericConstants_1.QUOTE_SPOT_MARKET_INDEX).pubkey,
|
|
1483
|
-
isSigner: false,
|
|
1484
|
-
isWritable: true
|
|
1485
|
-
});
|
|
1486
|
-
remainingAccounts = oracleAccountInfos
|
|
1487
|
-
.concat(spotMarketAccountInfos)
|
|
1488
|
-
.concat(marketAccountInfos);
|
|
1489
|
-
_b = (_a = this.program.instruction).settleExpiredMarket;
|
|
1490
|
-
_c = [marketIndex];
|
|
1491
|
-
_d = {};
|
|
1492
|
-
_e = {};
|
|
1493
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
1494
|
-
case 1: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
1495
|
-
_e.authority = this.wallet.publicKey,
|
|
1496
|
-
_e),
|
|
1497
|
-
_d.remainingAccounts = remainingAccounts,
|
|
1498
|
-
_d)]))];
|
|
1499
|
-
case 2: return [2 /*return*/, _f.sent()];
|
|
1500
|
-
}
|
|
1501
|
-
});
|
|
1502
|
-
});
|
|
1503
|
-
};
|
|
1504
|
-
ClearingHouse.prototype.cancelOrder = function (orderId) {
|
|
1505
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1506
|
-
var txSig, _a, _b, _c;
|
|
1507
|
-
return __generator(this, function (_d) {
|
|
1508
|
-
switch (_d.label) {
|
|
1509
|
-
case 0:
|
|
1510
|
-
_b = (_a = this.txSender).send;
|
|
1511
|
-
_c = utils_1.wrapInTx;
|
|
1512
|
-
return [4 /*yield*/, this.getCancelOrderIx(orderId)];
|
|
1513
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
1514
|
-
[],
|
|
1515
|
-
this.opts])];
|
|
1516
|
-
case 2:
|
|
1517
|
-
txSig = (_d.sent()).txSig;
|
|
1518
|
-
return [2 /*return*/, txSig];
|
|
1519
|
-
}
|
|
1520
|
-
});
|
|
1521
|
-
});
|
|
1522
|
-
};
|
|
1523
|
-
ClearingHouse.prototype.getCancelOrderIx = function (orderId) {
|
|
1524
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1525
|
-
var userAccountPublicKey, remainingAccounts, _a, _b, _c;
|
|
1526
|
-
var _d, _e;
|
|
1527
|
-
return __generator(this, function (_f) {
|
|
1528
|
-
switch (_f.label) {
|
|
1529
|
-
case 0: return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
1530
|
-
case 1:
|
|
1531
|
-
userAccountPublicKey = _f.sent();
|
|
1532
|
-
remainingAccounts = this.getRemainingAccounts({});
|
|
1533
|
-
_b = (_a = this.program.instruction).cancelOrder;
|
|
1534
|
-
_c = [orderId !== null && orderId !== void 0 ? orderId : null];
|
|
1535
|
-
_d = {};
|
|
1536
|
-
_e = {};
|
|
1537
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
1538
|
-
case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
1539
|
-
_e.user = userAccountPublicKey,
|
|
1540
|
-
_e.authority = this.wallet.publicKey,
|
|
1541
|
-
_e),
|
|
1542
|
-
_d.remainingAccounts = remainingAccounts,
|
|
1543
|
-
_d)]))];
|
|
1544
|
-
case 3: return [2 /*return*/, _f.sent()];
|
|
1545
|
-
}
|
|
1546
|
-
});
|
|
1547
|
-
});
|
|
1548
|
-
};
|
|
1549
|
-
ClearingHouse.prototype.cancelOrderByUserId = function (userOrderId) {
|
|
1550
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1551
|
-
var txSig, _a, _b, _c;
|
|
1552
|
-
return __generator(this, function (_d) {
|
|
1553
|
-
switch (_d.label) {
|
|
1554
|
-
case 0:
|
|
1555
|
-
_b = (_a = this.txSender).send;
|
|
1556
|
-
_c = utils_1.wrapInTx;
|
|
1557
|
-
return [4 /*yield*/, this.getCancelOrderByUserIdIx(userOrderId)];
|
|
1558
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
1559
|
-
[],
|
|
1560
|
-
this.opts])];
|
|
1561
|
-
case 2:
|
|
1562
|
-
txSig = (_d.sent()).txSig;
|
|
1563
|
-
return [2 /*return*/, txSig];
|
|
1564
|
-
}
|
|
1565
|
-
});
|
|
1566
|
-
});
|
|
1567
|
-
};
|
|
1568
|
-
ClearingHouse.prototype.getCancelOrderByUserIdIx = function (userOrderId) {
|
|
1569
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1570
|
-
var userAccountPublicKey, order, oracle, remainingAccounts, _a, _b, _c;
|
|
1571
|
-
var _d, _e;
|
|
1572
|
-
return __generator(this, function (_f) {
|
|
1573
|
-
switch (_f.label) {
|
|
1574
|
-
case 0: return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
1575
|
-
case 1:
|
|
1576
|
-
userAccountPublicKey = _f.sent();
|
|
1577
|
-
order = this.getOrderByUserId(userOrderId);
|
|
1578
|
-
oracle = this.getPerpMarketAccount(order.marketIndex).amm.oracle;
|
|
1579
|
-
remainingAccounts = this.getRemainingAccounts({});
|
|
1580
|
-
_b = (_a = this.program.instruction).cancelOrderByUserId;
|
|
1581
|
-
_c = [userOrderId];
|
|
1582
|
-
_d = {};
|
|
1583
|
-
_e = {};
|
|
1584
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
1585
|
-
case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
1586
|
-
_e.user = userAccountPublicKey,
|
|
1587
|
-
_e.authority = this.wallet.publicKey,
|
|
1588
|
-
_e.oracle = oracle,
|
|
1589
|
-
_e),
|
|
1590
|
-
_d.remainingAccounts = remainingAccounts,
|
|
1591
|
-
_d)]))];
|
|
1592
|
-
case 3: return [2 /*return*/, _f.sent()];
|
|
1593
|
-
}
|
|
1594
|
-
});
|
|
1595
|
-
});
|
|
1596
|
-
};
|
|
1597
|
-
ClearingHouse.prototype.fillOrder = function (userAccountPublicKey, user, order, makerInfo, referrerInfo) {
|
|
1598
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1599
|
-
var txSig, _a, _b, _c;
|
|
1600
|
-
return __generator(this, function (_d) {
|
|
1601
|
-
switch (_d.label) {
|
|
1602
|
-
case 0:
|
|
1603
|
-
_b = (_a = this.txSender).send;
|
|
1604
|
-
_c = utils_1.wrapInTx;
|
|
1605
|
-
return [4 /*yield*/, this.getFillOrderIx(userAccountPublicKey, user, order, makerInfo, referrerInfo)];
|
|
1606
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
1607
|
-
[],
|
|
1608
|
-
this.opts])];
|
|
1609
|
-
case 2:
|
|
1610
|
-
txSig = (_d.sent()).txSig;
|
|
1611
|
-
return [2 /*return*/, txSig];
|
|
1612
|
-
}
|
|
1613
|
-
});
|
|
1614
|
-
});
|
|
1615
|
-
};
|
|
1616
|
-
ClearingHouse.prototype.getFillOrderIx = function (userAccountPublicKey, userAccount, order, makerInfo, referrerInfo) {
|
|
1617
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1618
|
-
var userStatsPublicKey, fillerPublicKey, fillerStatsPublicKey, marketIndex, marketAccount, oracleAccountMap, spotMarketAccountMap, perpMarketAccountMap, _i, _a, spotPosition, spotMarketAccount, _b, _c, position, market, remainingAccounts, orderId, makerOrderId, _d, _e, _f;
|
|
1619
|
-
var _g, _h;
|
|
1620
|
-
return __generator(this, function (_j) {
|
|
1621
|
-
switch (_j.label) {
|
|
1622
|
-
case 0:
|
|
1623
|
-
userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, userAccount.authority);
|
|
1624
|
-
return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
1625
|
-
case 1:
|
|
1626
|
-
fillerPublicKey = _j.sent();
|
|
1627
|
-
fillerStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
1628
|
-
marketIndex = order
|
|
1629
|
-
? order.marketIndex
|
|
1630
|
-
: userAccount.orders.find(function (order) {
|
|
1631
|
-
return order.orderId.eq(userAccount.nextOrderId.sub(numericConstants_1.ONE));
|
|
1632
|
-
}).marketIndex;
|
|
1633
|
-
marketAccount = this.getPerpMarketAccount(marketIndex);
|
|
1634
|
-
oracleAccountMap = new Map();
|
|
1635
|
-
spotMarketAccountMap = new Map();
|
|
1636
|
-
perpMarketAccountMap = new Map();
|
|
1637
|
-
for (_i = 0, _a = userAccount.spotPositions; _i < _a.length; _i++) {
|
|
1638
|
-
spotPosition = _a[_i];
|
|
1639
|
-
if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
|
|
1640
|
-
spotMarketAccount = this.getSpotMarketAccount(spotPosition.marketIndex);
|
|
1641
|
-
spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
|
|
1642
|
-
pubkey: spotMarketAccount.pubkey,
|
|
1643
|
-
isSigner: false,
|
|
1644
|
-
isWritable: false
|
|
1645
|
-
});
|
|
1646
|
-
if (!spotMarketAccount.oracle.equals(web3_js_1.PublicKey["default"])) {
|
|
1647
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
1648
|
-
pubkey: spotMarketAccount.oracle,
|
|
1649
|
-
isSigner: false,
|
|
1650
|
-
isWritable: false
|
|
1651
|
-
});
|
|
1652
|
-
}
|
|
1653
|
-
}
|
|
1654
|
-
}
|
|
1655
|
-
for (_b = 0, _c = userAccount.perpPositions; _b < _c.length; _b++) {
|
|
1656
|
-
position = _c[_b];
|
|
1657
|
-
if (!(0, position_1.positionIsAvailable)(position) &&
|
|
1658
|
-
!position.marketIndex.eq(order.marketIndex)) {
|
|
1659
|
-
market = this.getPerpMarketAccount(position.marketIndex);
|
|
1660
|
-
perpMarketAccountMap.set(position.marketIndex.toNumber(), {
|
|
1661
|
-
pubkey: market.pubkey,
|
|
1662
|
-
isWritable: false,
|
|
1663
|
-
isSigner: false
|
|
1664
|
-
});
|
|
1665
|
-
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
1666
|
-
pubkey: market.amm.oracle,
|
|
1667
|
-
isWritable: false,
|
|
1668
|
-
isSigner: false
|
|
1669
|
-
});
|
|
1670
|
-
}
|
|
1671
|
-
}
|
|
1672
|
-
perpMarketAccountMap.set(marketIndex.toNumber(), {
|
|
1673
|
-
pubkey: marketAccount.pubkey,
|
|
1674
|
-
isWritable: true,
|
|
1675
|
-
isSigner: false
|
|
1676
|
-
});
|
|
1677
|
-
oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
|
|
1678
|
-
pubkey: marketAccount.amm.oracle,
|
|
1679
|
-
isWritable: false,
|
|
1680
|
-
isSigner: false
|
|
1681
|
-
});
|
|
1682
|
-
remainingAccounts = __spreadArray(__spreadArray(__spreadArray([], oracleAccountMap.values(), true), spotMarketAccountMap.values(), true), perpMarketAccountMap.values(), true);
|
|
1683
|
-
if (makerInfo) {
|
|
1684
|
-
remainingAccounts.push({
|
|
1685
|
-
pubkey: makerInfo.maker,
|
|
1686
|
-
isWritable: true,
|
|
1687
|
-
isSigner: false
|
|
1688
|
-
});
|
|
1689
|
-
remainingAccounts.push({
|
|
1690
|
-
pubkey: makerInfo.makerStats,
|
|
1691
|
-
isWritable: true,
|
|
1692
|
-
isSigner: false
|
|
1693
|
-
});
|
|
1694
|
-
}
|
|
1695
|
-
if (referrerInfo) {
|
|
1696
|
-
remainingAccounts.push({
|
|
1697
|
-
pubkey: referrerInfo.referrer,
|
|
1698
|
-
isWritable: true,
|
|
1699
|
-
isSigner: false
|
|
1700
|
-
});
|
|
1701
|
-
remainingAccounts.push({
|
|
1702
|
-
pubkey: referrerInfo.referrerStats,
|
|
1703
|
-
isWritable: true,
|
|
1704
|
-
isSigner: false
|
|
1705
|
-
});
|
|
1706
|
-
}
|
|
1707
|
-
orderId = order.orderId;
|
|
1708
|
-
makerOrderId = makerInfo ? makerInfo.order.orderId : null;
|
|
1709
|
-
_e = (_d = this.program.instruction).fillOrder;
|
|
1710
|
-
_f = [orderId, makerOrderId];
|
|
1711
|
-
_g = {};
|
|
1712
|
-
_h = {};
|
|
1713
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
1714
|
-
case 2: return [4 /*yield*/, _e.apply(_d, _f.concat([(_g.accounts = (_h.state = _j.sent(),
|
|
1715
|
-
_h.filler = fillerPublicKey,
|
|
1716
|
-
_h.fillerStats = fillerStatsPublicKey,
|
|
1717
|
-
_h.user = userAccountPublicKey,
|
|
1718
|
-
_h.userStats = userStatsPublicKey,
|
|
1719
|
-
_h.authority = this.wallet.publicKey,
|
|
1720
|
-
_h),
|
|
1721
|
-
_g.remainingAccounts = remainingAccounts,
|
|
1722
|
-
_g)]))];
|
|
1723
|
-
case 3: return [2 /*return*/, _j.sent()];
|
|
1724
|
-
}
|
|
1725
|
-
});
|
|
1726
|
-
});
|
|
1727
|
-
};
|
|
1728
|
-
ClearingHouse.prototype.placeSpotOrder = function (orderParams) {
|
|
1729
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1730
|
-
var txSig, _a, _b, _c;
|
|
1731
|
-
return __generator(this, function (_d) {
|
|
1732
|
-
switch (_d.label) {
|
|
1733
|
-
case 0:
|
|
1734
|
-
_b = (_a = this.txSender).send;
|
|
1735
|
-
_c = utils_1.wrapInTx;
|
|
1736
|
-
return [4 /*yield*/, this.getPlaceSpotOrderIx(orderParams)];
|
|
1737
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
1738
|
-
[],
|
|
1739
|
-
this.opts])];
|
|
1740
|
-
case 2:
|
|
1741
|
-
txSig = (_d.sent()).txSig;
|
|
1742
|
-
return [2 /*return*/, txSig];
|
|
1743
|
-
}
|
|
1744
|
-
});
|
|
1745
|
-
});
|
|
1746
|
-
};
|
|
1747
|
-
ClearingHouse.prototype.getPlaceSpotOrderIx = function (orderParams) {
|
|
1748
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1749
|
-
var userAccountPublicKey, remainingAccounts, _a, _b, _c;
|
|
1750
|
-
var _d, _e;
|
|
1751
|
-
return __generator(this, function (_f) {
|
|
1752
|
-
switch (_f.label) {
|
|
1753
|
-
case 0:
|
|
1754
|
-
orderParams = this.getOrderParams(orderParams, types_1.MarketType.SPOT);
|
|
1755
|
-
return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
1756
|
-
case 1:
|
|
1757
|
-
userAccountPublicKey = _f.sent();
|
|
1758
|
-
remainingAccounts = this.getRemainingAccounts({
|
|
1759
|
-
readableSpotMarketIndex: orderParams.marketIndex
|
|
1760
|
-
});
|
|
1761
|
-
_b = (_a = this.program.instruction).placeSpotOrder;
|
|
1762
|
-
_c = [orderParams];
|
|
1763
|
-
_d = {};
|
|
1764
|
-
_e = {};
|
|
1765
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
1766
|
-
case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
1767
|
-
_e.user = userAccountPublicKey,
|
|
1768
|
-
_e.userStats = this.getUserStatsAccountPublicKey(),
|
|
1769
|
-
_e.authority = this.wallet.publicKey,
|
|
1770
|
-
_e),
|
|
1771
|
-
_d.remainingAccounts = remainingAccounts,
|
|
1772
|
-
_d)]))];
|
|
1773
|
-
case 3: return [2 /*return*/, _f.sent()];
|
|
1774
|
-
}
|
|
1775
|
-
});
|
|
1776
|
-
});
|
|
1777
|
-
};
|
|
1778
|
-
ClearingHouse.prototype.fillSpotOrder = function (userAccountPublicKey, user, order, fulfillmentConfig, makerInfo, referrerInfo) {
|
|
1779
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1780
|
-
var txSig, _a, _b, _c;
|
|
1781
|
-
return __generator(this, function (_d) {
|
|
1782
|
-
switch (_d.label) {
|
|
1783
|
-
case 0:
|
|
1784
|
-
_b = (_a = this.txSender).send;
|
|
1785
|
-
_c = utils_1.wrapInTx;
|
|
1786
|
-
return [4 /*yield*/, this.getFillSpotOrderIx(userAccountPublicKey, user, order, fulfillmentConfig, makerInfo, referrerInfo)];
|
|
1787
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
1788
|
-
[],
|
|
1789
|
-
this.opts])];
|
|
1790
|
-
case 2:
|
|
1791
|
-
txSig = (_d.sent()).txSig;
|
|
1792
|
-
return [2 /*return*/, txSig];
|
|
1793
|
-
}
|
|
1794
|
-
});
|
|
1795
|
-
});
|
|
1796
|
-
};
|
|
1797
|
-
ClearingHouse.prototype.getFillSpotOrderIx = function (userAccountPublicKey, userAccount, order, fulfillmentConfig, makerInfo, referrerInfo) {
|
|
1798
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1799
|
-
var userStatsPublicKey, fillerPublicKey, fillerStatsPublicKey, marketIndex, oracleAccountMap, spotMarketAccountMap, perpMarketAccountMap, _i, _a, spotPosition, spotMarket, _b, _c, position, market, spotMarketAccount, quoteMarketAccount, remainingAccounts, orderId, makerOrderId, _d, _e, _f;
|
|
1800
|
-
var _g, _h;
|
|
1801
|
-
return __generator(this, function (_j) {
|
|
1802
|
-
switch (_j.label) {
|
|
1803
|
-
case 0:
|
|
1804
|
-
userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, userAccount.authority);
|
|
1805
|
-
return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
1806
|
-
case 1:
|
|
1807
|
-
fillerPublicKey = _j.sent();
|
|
1808
|
-
fillerStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
1809
|
-
marketIndex = order
|
|
1810
|
-
? order.marketIndex
|
|
1811
|
-
: userAccount.orders.find(function (order) {
|
|
1812
|
-
return order.orderId.eq(userAccount.nextOrderId.sub(numericConstants_1.ONE));
|
|
1813
|
-
}).marketIndex;
|
|
1814
|
-
oracleAccountMap = new Map();
|
|
1815
|
-
spotMarketAccountMap = new Map();
|
|
1816
|
-
perpMarketAccountMap = new Map();
|
|
1817
|
-
for (_i = 0, _a = userAccount.spotPositions; _i < _a.length; _i++) {
|
|
1818
|
-
spotPosition = _a[_i];
|
|
1819
|
-
if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
|
|
1820
|
-
spotMarket = this.getSpotMarketAccount(spotPosition.marketIndex);
|
|
1821
|
-
spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
|
|
1822
|
-
pubkey: spotMarket.pubkey,
|
|
1823
|
-
isSigner: false,
|
|
1824
|
-
isWritable: false
|
|
1825
|
-
});
|
|
1826
|
-
if (!spotMarket.oracle.equals(web3_js_1.PublicKey["default"])) {
|
|
1827
|
-
oracleAccountMap.set(spotMarket.oracle.toString(), {
|
|
1828
|
-
pubkey: spotMarket.oracle,
|
|
1829
|
-
isSigner: false,
|
|
1830
|
-
isWritable: false
|
|
1831
|
-
});
|
|
1832
|
-
}
|
|
1833
|
-
}
|
|
1834
|
-
}
|
|
1835
|
-
for (_b = 0, _c = userAccount.perpPositions; _b < _c.length; _b++) {
|
|
1836
|
-
position = _c[_b];
|
|
1837
|
-
if (!(0, position_1.positionIsAvailable)(position) &&
|
|
1838
|
-
!position.marketIndex.eq(order.marketIndex)) {
|
|
1839
|
-
market = this.getPerpMarketAccount(position.marketIndex);
|
|
1840
|
-
perpMarketAccountMap.set(position.marketIndex.toNumber(), {
|
|
1841
|
-
pubkey: market.pubkey,
|
|
1842
|
-
isWritable: false,
|
|
1843
|
-
isSigner: false
|
|
1844
|
-
});
|
|
1845
|
-
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
1846
|
-
pubkey: market.amm.oracle,
|
|
1847
|
-
isWritable: false,
|
|
1848
|
-
isSigner: false
|
|
1849
|
-
});
|
|
1850
|
-
}
|
|
1851
|
-
}
|
|
1852
|
-
spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
1853
|
-
spotMarketAccountMap.set(marketIndex.toNumber(), {
|
|
1854
|
-
pubkey: spotMarketAccount.pubkey,
|
|
1855
|
-
isWritable: true,
|
|
1856
|
-
isSigner: false
|
|
1857
|
-
});
|
|
1858
|
-
if (!spotMarketAccount.oracle.equals(web3_js_1.PublicKey["default"])) {
|
|
1859
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
1860
|
-
pubkey: spotMarketAccount.oracle,
|
|
1861
|
-
isWritable: false,
|
|
1862
|
-
isSigner: false
|
|
1863
|
-
});
|
|
1864
|
-
}
|
|
1865
|
-
quoteMarketAccount = this.getQuoteSpotMarketAccount();
|
|
1866
|
-
spotMarketAccountMap.set(quoteMarketAccount.marketIndex.toNumber(), {
|
|
1867
|
-
pubkey: quoteMarketAccount.pubkey,
|
|
1868
|
-
isWritable: true,
|
|
1869
|
-
isSigner: false
|
|
1870
|
-
});
|
|
1871
|
-
remainingAccounts = __spreadArray(__spreadArray(__spreadArray([], oracleAccountMap.values(), true), spotMarketAccountMap.values(), true), perpMarketAccountMap.values(), true);
|
|
1872
|
-
if (makerInfo) {
|
|
1873
|
-
remainingAccounts.push({
|
|
1874
|
-
pubkey: makerInfo.maker,
|
|
1875
|
-
isWritable: true,
|
|
1876
|
-
isSigner: false
|
|
1877
|
-
});
|
|
1878
|
-
remainingAccounts.push({
|
|
1879
|
-
pubkey: makerInfo.makerStats,
|
|
1880
|
-
isWritable: true,
|
|
1881
|
-
isSigner: false
|
|
1882
|
-
});
|
|
1883
|
-
}
|
|
1884
|
-
if (referrerInfo) {
|
|
1885
|
-
remainingAccounts.push({
|
|
1886
|
-
pubkey: referrerInfo.referrer,
|
|
1887
|
-
isWritable: true,
|
|
1888
|
-
isSigner: false
|
|
1889
|
-
});
|
|
1890
|
-
remainingAccounts.push({
|
|
1891
|
-
pubkey: referrerInfo.referrerStats,
|
|
1892
|
-
isWritable: true,
|
|
1893
|
-
isSigner: false
|
|
1894
|
-
});
|
|
1895
|
-
}
|
|
1896
|
-
orderId = order.orderId;
|
|
1897
|
-
makerOrderId = makerInfo ? makerInfo.order.orderId : null;
|
|
1898
|
-
if (fulfillmentConfig) {
|
|
1899
|
-
remainingAccounts.push({
|
|
1900
|
-
pubkey: fulfillmentConfig.pubkey,
|
|
1901
|
-
isWritable: false,
|
|
1902
|
-
isSigner: false
|
|
1903
|
-
});
|
|
1904
|
-
remainingAccounts.push({
|
|
1905
|
-
pubkey: fulfillmentConfig.serumProgramId,
|
|
1906
|
-
isWritable: false,
|
|
1907
|
-
isSigner: false
|
|
1908
|
-
});
|
|
1909
|
-
remainingAccounts.push({
|
|
1910
|
-
pubkey: fulfillmentConfig.serumMarket,
|
|
1911
|
-
isWritable: true,
|
|
1912
|
-
isSigner: false
|
|
1913
|
-
});
|
|
1914
|
-
remainingAccounts.push({
|
|
1915
|
-
pubkey: fulfillmentConfig.serumRequestQueue,
|
|
1916
|
-
isWritable: true,
|
|
1917
|
-
isSigner: false
|
|
1918
|
-
});
|
|
1919
|
-
remainingAccounts.push({
|
|
1920
|
-
pubkey: fulfillmentConfig.serumEventQueue,
|
|
1921
|
-
isWritable: true,
|
|
1922
|
-
isSigner: false
|
|
1923
|
-
});
|
|
1924
|
-
remainingAccounts.push({
|
|
1925
|
-
pubkey: fulfillmentConfig.serumBids,
|
|
1926
|
-
isWritable: true,
|
|
1927
|
-
isSigner: false
|
|
1928
|
-
});
|
|
1929
|
-
remainingAccounts.push({
|
|
1930
|
-
pubkey: fulfillmentConfig.serumAsks,
|
|
1931
|
-
isWritable: true,
|
|
1932
|
-
isSigner: false
|
|
1933
|
-
});
|
|
1934
|
-
remainingAccounts.push({
|
|
1935
|
-
pubkey: fulfillmentConfig.serumBaseVault,
|
|
1936
|
-
isWritable: true,
|
|
1937
|
-
isSigner: false
|
|
1938
|
-
});
|
|
1939
|
-
remainingAccounts.push({
|
|
1940
|
-
pubkey: fulfillmentConfig.serumQuoteVault,
|
|
1941
|
-
isWritable: true,
|
|
1942
|
-
isSigner: false
|
|
1943
|
-
});
|
|
1944
|
-
remainingAccounts.push({
|
|
1945
|
-
pubkey: fulfillmentConfig.serumOpenOrders,
|
|
1946
|
-
isWritable: true,
|
|
1947
|
-
isSigner: false
|
|
1948
|
-
});
|
|
1949
|
-
remainingAccounts.push({
|
|
1950
|
-
pubkey: (0, pda_1.getSerumSignerPublicKey)(fulfillmentConfig.serumProgramId, fulfillmentConfig.serumMarket, fulfillmentConfig.serumSignerNonce),
|
|
1951
|
-
isWritable: false,
|
|
1952
|
-
isSigner: false
|
|
1953
|
-
});
|
|
1954
|
-
remainingAccounts.push({
|
|
1955
|
-
pubkey: this.getSignerPublicKey(),
|
|
1956
|
-
isWritable: false,
|
|
1957
|
-
isSigner: false
|
|
1958
|
-
});
|
|
1959
|
-
remainingAccounts.push({
|
|
1960
|
-
pubkey: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1961
|
-
isWritable: false,
|
|
1962
|
-
isSigner: false
|
|
1963
|
-
});
|
|
1964
|
-
remainingAccounts.push({
|
|
1965
|
-
pubkey: spotMarketAccount.vault,
|
|
1966
|
-
isWritable: true,
|
|
1967
|
-
isSigner: false
|
|
1968
|
-
});
|
|
1969
|
-
remainingAccounts.push({
|
|
1970
|
-
pubkey: quoteMarketAccount.vault,
|
|
1971
|
-
isWritable: true,
|
|
1972
|
-
isSigner: false
|
|
1973
|
-
});
|
|
1974
|
-
}
|
|
1975
|
-
_e = (_d = this.program.instruction).fillSpotOrder;
|
|
1976
|
-
_f = [orderId,
|
|
1977
|
-
fulfillmentConfig ? fulfillmentConfig.fulfillmentType : null,
|
|
1978
|
-
makerOrderId];
|
|
1979
|
-
_g = {};
|
|
1980
|
-
_h = {};
|
|
1981
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
1982
|
-
case 2: return [4 /*yield*/, _e.apply(_d, _f.concat([(_g.accounts = (_h.state = _j.sent(),
|
|
1983
|
-
_h.filler = fillerPublicKey,
|
|
1984
|
-
_h.fillerStats = fillerStatsPublicKey,
|
|
1985
|
-
_h.user = userAccountPublicKey,
|
|
1986
|
-
_h.userStats = userStatsPublicKey,
|
|
1987
|
-
_h.authority = this.wallet.publicKey,
|
|
1988
|
-
_h),
|
|
1989
|
-
_g.remainingAccounts = remainingAccounts,
|
|
1990
|
-
_g)]))];
|
|
1991
|
-
case 3: return [2 /*return*/, _j.sent()];
|
|
1992
|
-
}
|
|
1993
|
-
});
|
|
1994
|
-
});
|
|
1995
|
-
};
|
|
1996
|
-
ClearingHouse.prototype.triggerOrder = function (userAccountPublicKey, user, order) {
|
|
1997
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1998
|
-
var txSig, _a, _b, _c;
|
|
1999
|
-
return __generator(this, function (_d) {
|
|
2000
|
-
switch (_d.label) {
|
|
2001
|
-
case 0:
|
|
2002
|
-
_b = (_a = this.txSender).send;
|
|
2003
|
-
_c = utils_1.wrapInTx;
|
|
2004
|
-
return [4 /*yield*/, this.getTriggerOrderIx(userAccountPublicKey, user, order)];
|
|
2005
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
2006
|
-
[],
|
|
2007
|
-
this.opts])];
|
|
2008
|
-
case 2:
|
|
2009
|
-
txSig = (_d.sent()).txSig;
|
|
2010
|
-
return [2 /*return*/, txSig];
|
|
2011
|
-
}
|
|
2012
|
-
});
|
|
2013
|
-
});
|
|
2014
|
-
};
|
|
2015
|
-
ClearingHouse.prototype.getTriggerOrderIx = function (userAccountPublicKey, userAccount, order) {
|
|
2016
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2017
|
-
var fillerPublicKey, marketIndex, marketAccount, oracleAccountMap, spotMarketAccountMap, perpMarketAccountMap, _i, _a, spotPosition, spotMarketAccount, _b, _c, position, market, remainingAccounts, orderId, _d, _e, _f;
|
|
2018
|
-
var _g, _h;
|
|
2019
|
-
return __generator(this, function (_j) {
|
|
2020
|
-
switch (_j.label) {
|
|
2021
|
-
case 0: return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
2022
|
-
case 1:
|
|
2023
|
-
fillerPublicKey = _j.sent();
|
|
2024
|
-
marketIndex = order.marketIndex;
|
|
2025
|
-
marketAccount = this.getPerpMarketAccount(marketIndex);
|
|
2026
|
-
oracleAccountMap = new Map();
|
|
2027
|
-
spotMarketAccountMap = new Map();
|
|
2028
|
-
perpMarketAccountMap = new Map();
|
|
2029
|
-
for (_i = 0, _a = userAccount.spotPositions; _i < _a.length; _i++) {
|
|
2030
|
-
spotPosition = _a[_i];
|
|
2031
|
-
if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
|
|
2032
|
-
spotMarketAccount = this.getSpotMarketAccount(spotPosition.marketIndex);
|
|
2033
|
-
spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
|
|
2034
|
-
pubkey: spotMarketAccount.pubkey,
|
|
2035
|
-
isSigner: false,
|
|
2036
|
-
isWritable: false
|
|
2037
|
-
});
|
|
2038
|
-
if (!spotMarketAccount.oracle.equals(web3_js_1.PublicKey["default"])) {
|
|
2039
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
2040
|
-
pubkey: spotMarketAccount.oracle,
|
|
2041
|
-
isSigner: false,
|
|
2042
|
-
isWritable: false
|
|
2043
|
-
});
|
|
2044
|
-
}
|
|
2045
|
-
}
|
|
2046
|
-
}
|
|
2047
|
-
for (_b = 0, _c = userAccount.perpPositions; _b < _c.length; _b++) {
|
|
2048
|
-
position = _c[_b];
|
|
2049
|
-
if (!(0, position_1.positionIsAvailable)(position) &&
|
|
2050
|
-
!position.marketIndex.eq(order.marketIndex)) {
|
|
2051
|
-
market = this.getPerpMarketAccount(position.marketIndex);
|
|
2052
|
-
perpMarketAccountMap.set(position.marketIndex.toNumber(), {
|
|
2053
|
-
pubkey: market.pubkey,
|
|
2054
|
-
isWritable: false,
|
|
2055
|
-
isSigner: false
|
|
2056
|
-
});
|
|
2057
|
-
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
2058
|
-
pubkey: market.amm.oracle,
|
|
2059
|
-
isWritable: false,
|
|
2060
|
-
isSigner: false
|
|
2061
|
-
});
|
|
2062
|
-
}
|
|
2063
|
-
}
|
|
2064
|
-
perpMarketAccountMap.set(marketIndex.toNumber(), {
|
|
2065
|
-
pubkey: marketAccount.pubkey,
|
|
2066
|
-
isWritable: true,
|
|
2067
|
-
isSigner: false
|
|
2068
|
-
});
|
|
2069
|
-
oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
|
|
2070
|
-
pubkey: marketAccount.amm.oracle,
|
|
2071
|
-
isWritable: false,
|
|
2072
|
-
isSigner: false
|
|
2073
|
-
});
|
|
2074
|
-
remainingAccounts = __spreadArray(__spreadArray(__spreadArray([], oracleAccountMap.values(), true), spotMarketAccountMap.values(), true), perpMarketAccountMap.values(), true);
|
|
2075
|
-
orderId = order.orderId;
|
|
2076
|
-
_e = (_d = this.program.instruction).triggerOrder;
|
|
2077
|
-
_f = [orderId];
|
|
2078
|
-
_g = {};
|
|
2079
|
-
_h = {};
|
|
2080
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
2081
|
-
case 2: return [4 /*yield*/, _e.apply(_d, _f.concat([(_g.accounts = (_h.state = _j.sent(),
|
|
2082
|
-
_h.filler = fillerPublicKey,
|
|
2083
|
-
_h.user = userAccountPublicKey,
|
|
2084
|
-
_h.authority = this.wallet.publicKey,
|
|
2085
|
-
_h),
|
|
2086
|
-
_g.remainingAccounts = remainingAccounts,
|
|
2087
|
-
_g)]))];
|
|
2088
|
-
case 3: return [2 /*return*/, _j.sent()];
|
|
2089
|
-
}
|
|
2090
|
-
});
|
|
2091
|
-
});
|
|
2092
|
-
};
|
|
2093
|
-
ClearingHouse.prototype.triggerSpotOrder = function (userAccountPublicKey, user, order) {
|
|
2094
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2095
|
-
var txSig, _a, _b, _c;
|
|
2096
|
-
return __generator(this, function (_d) {
|
|
2097
|
-
switch (_d.label) {
|
|
2098
|
-
case 0:
|
|
2099
|
-
_b = (_a = this.txSender).send;
|
|
2100
|
-
_c = utils_1.wrapInTx;
|
|
2101
|
-
return [4 /*yield*/, this.getTriggerSpotOrderIx(userAccountPublicKey, user, order)];
|
|
2102
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
2103
|
-
[],
|
|
2104
|
-
this.opts])];
|
|
2105
|
-
case 2:
|
|
2106
|
-
txSig = (_d.sent()).txSig;
|
|
2107
|
-
return [2 /*return*/, txSig];
|
|
2108
|
-
}
|
|
2109
|
-
});
|
|
2110
|
-
});
|
|
2111
|
-
};
|
|
2112
|
-
ClearingHouse.prototype.getTriggerSpotOrderIx = function (userAccountPublicKey, userAccount, order) {
|
|
2113
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2114
|
-
var fillerPublicKey, marketIndex, spotMarketAccount, oracleAccountMap, spotMarketAccountMap, perpMarketAccountMap, _i, _a, spotPosition, spotMarketAccount_2, _b, _c, position, market, quoteSpotMarket, remainingAccounts, orderId, _d, _e, _f;
|
|
2115
|
-
var _g, _h;
|
|
2116
|
-
return __generator(this, function (_j) {
|
|
2117
|
-
switch (_j.label) {
|
|
2118
|
-
case 0: return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
2119
|
-
case 1:
|
|
2120
|
-
fillerPublicKey = _j.sent();
|
|
2121
|
-
marketIndex = order.marketIndex;
|
|
2122
|
-
spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
2123
|
-
oracleAccountMap = new Map();
|
|
2124
|
-
spotMarketAccountMap = new Map();
|
|
2125
|
-
perpMarketAccountMap = new Map();
|
|
2126
|
-
for (_i = 0, _a = userAccount.spotPositions; _i < _a.length; _i++) {
|
|
2127
|
-
spotPosition = _a[_i];
|
|
2128
|
-
if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
|
|
2129
|
-
spotMarketAccount_2 = this.getSpotMarketAccount(spotPosition.marketIndex);
|
|
2130
|
-
spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
|
|
2131
|
-
pubkey: spotMarketAccount_2.pubkey,
|
|
2132
|
-
isSigner: false,
|
|
2133
|
-
isWritable: false
|
|
2134
|
-
});
|
|
2135
|
-
if (!spotMarketAccount_2.oracle.equals(web3_js_1.PublicKey["default"])) {
|
|
2136
|
-
oracleAccountMap.set(spotMarketAccount_2.oracle.toString(), {
|
|
2137
|
-
pubkey: spotMarketAccount_2.oracle,
|
|
2138
|
-
isSigner: false,
|
|
2139
|
-
isWritable: false
|
|
2140
|
-
});
|
|
2141
|
-
}
|
|
2142
|
-
}
|
|
2143
|
-
}
|
|
2144
|
-
for (_b = 0, _c = userAccount.perpPositions; _b < _c.length; _b++) {
|
|
2145
|
-
position = _c[_b];
|
|
2146
|
-
if (!(0, position_1.positionIsAvailable)(position) &&
|
|
2147
|
-
!position.marketIndex.eq(order.marketIndex)) {
|
|
2148
|
-
market = this.getPerpMarketAccount(position.marketIndex);
|
|
2149
|
-
perpMarketAccountMap.set(position.marketIndex.toNumber(), {
|
|
2150
|
-
pubkey: market.pubkey,
|
|
2151
|
-
isWritable: false,
|
|
2152
|
-
isSigner: false
|
|
2153
|
-
});
|
|
2154
|
-
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
2155
|
-
pubkey: market.amm.oracle,
|
|
2156
|
-
isWritable: false,
|
|
2157
|
-
isSigner: false
|
|
2158
|
-
});
|
|
2159
|
-
}
|
|
2160
|
-
}
|
|
2161
|
-
quoteSpotMarket = this.getQuoteSpotMarketAccount();
|
|
2162
|
-
spotMarketAccountMap.set(quoteSpotMarket.marketIndex.toNumber(), {
|
|
2163
|
-
pubkey: quoteSpotMarket.pubkey,
|
|
2164
|
-
isWritable: true,
|
|
2165
|
-
isSigner: false
|
|
2166
|
-
});
|
|
2167
|
-
spotMarketAccountMap.set(marketIndex.toNumber(), {
|
|
2168
|
-
pubkey: spotMarketAccount.pubkey,
|
|
2169
|
-
isWritable: false,
|
|
2170
|
-
isSigner: false
|
|
2171
|
-
});
|
|
2172
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
2173
|
-
pubkey: spotMarketAccount.oracle,
|
|
2174
|
-
isWritable: false,
|
|
2175
|
-
isSigner: false
|
|
2176
|
-
});
|
|
2177
|
-
remainingAccounts = __spreadArray(__spreadArray(__spreadArray([], oracleAccountMap.values(), true), spotMarketAccountMap.values(), true), perpMarketAccountMap.values(), true);
|
|
2178
|
-
orderId = order.orderId;
|
|
2179
|
-
_e = (_d = this.program.instruction).triggerSpotOrder;
|
|
2180
|
-
_f = [orderId];
|
|
2181
|
-
_g = {};
|
|
2182
|
-
_h = {};
|
|
2183
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
2184
|
-
case 2: return [4 /*yield*/, _e.apply(_d, _f.concat([(_g.accounts = (_h.state = _j.sent(),
|
|
2185
|
-
_h.filler = fillerPublicKey,
|
|
2186
|
-
_h.user = userAccountPublicKey,
|
|
2187
|
-
_h.authority = this.wallet.publicKey,
|
|
2188
|
-
_h),
|
|
2189
|
-
_g.remainingAccounts = remainingAccounts,
|
|
2190
|
-
_g)]))];
|
|
2191
|
-
case 3: return [2 /*return*/, _j.sent()];
|
|
2192
|
-
}
|
|
2193
|
-
});
|
|
2194
|
-
});
|
|
2195
|
-
};
|
|
2196
|
-
ClearingHouse.prototype.placeAndTake = function (orderParams, makerInfo, referrerInfo) {
|
|
2197
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2198
|
-
var _a, txSig, slot, _b, _c, _d;
|
|
2199
|
-
return __generator(this, function (_e) {
|
|
2200
|
-
switch (_e.label) {
|
|
2201
|
-
case 0:
|
|
2202
|
-
_c = (_b = this.txSender).send;
|
|
2203
|
-
_d = utils_1.wrapInTx;
|
|
2204
|
-
return [4 /*yield*/, this.getPlaceAndTakeIx(orderParams, makerInfo, referrerInfo)];
|
|
2205
|
-
case 1: return [4 /*yield*/, _c.apply(_b, [_d.apply(void 0, [_e.sent()]),
|
|
2206
|
-
[],
|
|
2207
|
-
this.opts])];
|
|
2208
|
-
case 2:
|
|
2209
|
-
_a = _e.sent(), txSig = _a.txSig, slot = _a.slot;
|
|
2210
|
-
this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
|
|
2211
|
-
return [2 /*return*/, txSig];
|
|
2212
|
-
}
|
|
2213
|
-
});
|
|
2214
|
-
});
|
|
2215
|
-
};
|
|
2216
|
-
ClearingHouse.prototype.getPlaceAndTakeIx = function (orderParams, makerInfo, referrerInfo) {
|
|
2217
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2218
|
-
var userStatsPublicKey, userAccountPublicKey, remainingAccounts, makerOrderId, _a, _b, _c;
|
|
2219
|
-
var _d, _e;
|
|
2220
|
-
return __generator(this, function (_f) {
|
|
2221
|
-
switch (_f.label) {
|
|
2222
|
-
case 0:
|
|
2223
|
-
orderParams = this.getOrderParams(orderParams, types_1.MarketType.PERP);
|
|
2224
|
-
return [4 /*yield*/, this.getUserStatsAccountPublicKey()];
|
|
2225
|
-
case 1:
|
|
2226
|
-
userStatsPublicKey = _f.sent();
|
|
2227
|
-
return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
2228
|
-
case 2:
|
|
2229
|
-
userAccountPublicKey = _f.sent();
|
|
2230
|
-
remainingAccounts = this.getRemainingAccounts({
|
|
2231
|
-
writablePerpMarketIndex: orderParams.marketIndex,
|
|
2232
|
-
writableSpotMarketIndex: numericConstants_1.QUOTE_SPOT_MARKET_INDEX
|
|
2233
|
-
});
|
|
2234
|
-
makerOrderId = null;
|
|
2235
|
-
if (makerInfo) {
|
|
2236
|
-
makerOrderId = makerInfo.order.orderId;
|
|
2237
|
-
remainingAccounts.push({
|
|
2238
|
-
pubkey: makerInfo.maker,
|
|
2239
|
-
isSigner: false,
|
|
2240
|
-
isWritable: true
|
|
2241
|
-
});
|
|
2242
|
-
remainingAccounts.push({
|
|
2243
|
-
pubkey: makerInfo.makerStats,
|
|
2244
|
-
isSigner: false,
|
|
2245
|
-
isWritable: true
|
|
2246
|
-
});
|
|
2247
|
-
}
|
|
2248
|
-
if (referrerInfo) {
|
|
2249
|
-
remainingAccounts.push({
|
|
2250
|
-
pubkey: referrerInfo.referrer,
|
|
2251
|
-
isWritable: true,
|
|
2252
|
-
isSigner: false
|
|
2253
|
-
});
|
|
2254
|
-
remainingAccounts.push({
|
|
2255
|
-
pubkey: referrerInfo.referrerStats,
|
|
2256
|
-
isWritable: true,
|
|
2257
|
-
isSigner: false
|
|
2258
|
-
});
|
|
2259
|
-
}
|
|
2260
|
-
_b = (_a = this.program.instruction).placeAndTake;
|
|
2261
|
-
_c = [orderParams,
|
|
2262
|
-
makerOrderId];
|
|
2263
|
-
_d = {};
|
|
2264
|
-
_e = {};
|
|
2265
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
2266
|
-
case 3: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
2267
|
-
_e.user = userAccountPublicKey,
|
|
2268
|
-
_e.userStats = userStatsPublicKey,
|
|
2269
|
-
_e.authority = this.wallet.publicKey,
|
|
2270
|
-
_e),
|
|
2271
|
-
_d.remainingAccounts = remainingAccounts,
|
|
2272
|
-
_d)]))];
|
|
2273
|
-
case 4: return [2 /*return*/, _f.sent()];
|
|
2274
|
-
}
|
|
2275
|
-
});
|
|
2276
|
-
});
|
|
2277
|
-
};
|
|
2278
|
-
ClearingHouse.prototype.placeAndMake = function (orderParams, takerInfo, referrerInfo) {
|
|
2279
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2280
|
-
var _a, txSig, slot, _b, _c, _d;
|
|
2281
|
-
return __generator(this, function (_e) {
|
|
2282
|
-
switch (_e.label) {
|
|
2283
|
-
case 0:
|
|
2284
|
-
_c = (_b = this.txSender).send;
|
|
2285
|
-
_d = utils_1.wrapInTx;
|
|
2286
|
-
return [4 /*yield*/, this.getPlaceAndMakeIx(orderParams, takerInfo, referrerInfo)];
|
|
2287
|
-
case 1: return [4 /*yield*/, _c.apply(_b, [_d.apply(void 0, [_e.sent()]),
|
|
2288
|
-
[],
|
|
2289
|
-
this.opts])];
|
|
2290
|
-
case 2:
|
|
2291
|
-
_a = _e.sent(), txSig = _a.txSig, slot = _a.slot;
|
|
2292
|
-
this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
|
|
2293
|
-
return [2 /*return*/, txSig];
|
|
2294
|
-
}
|
|
2295
|
-
});
|
|
2296
|
-
});
|
|
2297
|
-
};
|
|
2298
|
-
ClearingHouse.prototype.getPlaceAndMakeIx = function (orderParams, takerInfo, referrerInfo) {
|
|
2299
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2300
|
-
var userStatsPublicKey, userAccountPublicKey, remainingAccounts, takerOrderId, _a, _b, _c;
|
|
2301
|
-
var _d, _e;
|
|
2302
|
-
return __generator(this, function (_f) {
|
|
2303
|
-
switch (_f.label) {
|
|
2304
|
-
case 0:
|
|
2305
|
-
orderParams = this.getOrderParams(orderParams, types_1.MarketType.PERP);
|
|
2306
|
-
userStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
2307
|
-
return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
2308
|
-
case 1:
|
|
2309
|
-
userAccountPublicKey = _f.sent();
|
|
2310
|
-
remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
2311
|
-
counterPartyUserAccount: takerInfo.takerUserAccount,
|
|
2312
|
-
writablePerpMarketIndex: orderParams.marketIndex
|
|
2313
|
-
});
|
|
2314
|
-
if (referrerInfo) {
|
|
2315
|
-
remainingAccounts.push({
|
|
2316
|
-
pubkey: referrerInfo.referrer,
|
|
2317
|
-
isWritable: true,
|
|
2318
|
-
isSigner: false
|
|
2319
|
-
});
|
|
2320
|
-
remainingAccounts.push({
|
|
2321
|
-
pubkey: referrerInfo.referrerStats,
|
|
2322
|
-
isWritable: true,
|
|
2323
|
-
isSigner: false
|
|
2324
|
-
});
|
|
2325
|
-
}
|
|
2326
|
-
takerOrderId = takerInfo.order.orderId;
|
|
2327
|
-
_b = (_a = this.program.instruction).placeAndMake;
|
|
2328
|
-
_c = [orderParams,
|
|
2329
|
-
takerOrderId];
|
|
2330
|
-
_d = {};
|
|
2331
|
-
_e = {};
|
|
2332
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
2333
|
-
case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
2334
|
-
_e.user = userAccountPublicKey,
|
|
2335
|
-
_e.userStats = userStatsPublicKey,
|
|
2336
|
-
_e.taker = takerInfo.taker,
|
|
2337
|
-
_e.takerStats = takerInfo.takerStats,
|
|
2338
|
-
_e.authority = this.wallet.publicKey,
|
|
2339
|
-
_e),
|
|
2340
|
-
_d.remainingAccounts = remainingAccounts,
|
|
2341
|
-
_d)]))];
|
|
2342
|
-
case 3: return [2 /*return*/, _f.sent()];
|
|
2343
|
-
}
|
|
2344
|
-
});
|
|
2345
|
-
});
|
|
2346
|
-
};
|
|
2347
|
-
/**
|
|
2348
|
-
* Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
|
|
2349
|
-
* @param marketIndex
|
|
2350
|
-
* @returns
|
|
2351
|
-
*/
|
|
2352
|
-
ClearingHouse.prototype.closePosition = function (marketIndex, limitPrice) {
|
|
2353
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2354
|
-
var userPosition;
|
|
2355
|
-
return __generator(this, function (_a) {
|
|
2356
|
-
switch (_a.label) {
|
|
2357
|
-
case 0:
|
|
2358
|
-
userPosition = this.getUser().getUserPosition(marketIndex);
|
|
2359
|
-
if (!userPosition) {
|
|
2360
|
-
throw Error("No position in market ".concat(marketIndex.toString()));
|
|
2361
|
-
}
|
|
2362
|
-
return [4 /*yield*/, this.placeAndTake({
|
|
2363
|
-
orderType: types_1.OrderType.MARKET,
|
|
2364
|
-
marketIndex: marketIndex,
|
|
2365
|
-
direction: (0, position_1.findDirectionToClose)(userPosition),
|
|
2366
|
-
baseAssetAmount: userPosition.baseAssetAmount.abs(),
|
|
2367
|
-
reduceOnly: true,
|
|
2368
|
-
price: limitPrice
|
|
2369
|
-
})];
|
|
2370
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
2371
|
-
}
|
|
2372
|
-
});
|
|
2373
|
-
});
|
|
2374
|
-
};
|
|
2375
|
-
ClearingHouse.prototype.settlePNLs = function (users, marketIndex) {
|
|
2376
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2377
|
-
var ixs, _i, users_1, _a, settleeUserAccountPublicKey, settleeUserAccount, _b, _c, tx, txSig;
|
|
2378
|
-
var _d;
|
|
2379
|
-
return __generator(this, function (_e) {
|
|
2380
|
-
switch (_e.label) {
|
|
2381
|
-
case 0:
|
|
2382
|
-
ixs = [];
|
|
2383
|
-
_i = 0, users_1 = users;
|
|
2384
|
-
_e.label = 1;
|
|
2385
|
-
case 1:
|
|
2386
|
-
if (!(_i < users_1.length)) return [3 /*break*/, 4];
|
|
2387
|
-
_a = users_1[_i], settleeUserAccountPublicKey = _a.settleeUserAccountPublicKey, settleeUserAccount = _a.settleeUserAccount;
|
|
2388
|
-
_c = (_b = ixs).push;
|
|
2389
|
-
return [4 /*yield*/, this.settlePNLIx(settleeUserAccountPublicKey, settleeUserAccount, marketIndex)];
|
|
2390
|
-
case 2:
|
|
2391
|
-
_c.apply(_b, [_e.sent()]);
|
|
2392
|
-
_e.label = 3;
|
|
2393
|
-
case 3:
|
|
2394
|
-
_i++;
|
|
2395
|
-
return [3 /*break*/, 1];
|
|
2396
|
-
case 4:
|
|
2397
|
-
tx = (_d = new web3_js_1.Transaction()
|
|
2398
|
-
.add(web3_js_1.ComputeBudgetProgram.requestUnits({
|
|
2399
|
-
units: 1000000,
|
|
2400
|
-
additionalFee: 0
|
|
2401
|
-
})))
|
|
2402
|
-
.add.apply(_d, ixs);
|
|
2403
|
-
return [4 /*yield*/, this.txSender.send(tx, [], this.opts)];
|
|
2404
|
-
case 5:
|
|
2405
|
-
txSig = (_e.sent()).txSig;
|
|
2406
|
-
return [2 /*return*/, txSig];
|
|
2407
|
-
}
|
|
2408
|
-
});
|
|
2409
|
-
});
|
|
2410
|
-
};
|
|
2411
|
-
ClearingHouse.prototype.settlePNL = function (settleeUserAccountPublicKey, settleeUserAccount, marketIndex) {
|
|
2412
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2413
|
-
var txSig, _a, _b, _c;
|
|
2414
|
-
return __generator(this, function (_d) {
|
|
2415
|
-
switch (_d.label) {
|
|
2416
|
-
case 0:
|
|
2417
|
-
_b = (_a = this.txSender).send;
|
|
2418
|
-
_c = utils_1.wrapInTx;
|
|
2419
|
-
return [4 /*yield*/, this.settlePNLIx(settleeUserAccountPublicKey, settleeUserAccount, marketIndex)];
|
|
2420
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
2421
|
-
[],
|
|
2422
|
-
this.opts])];
|
|
2423
|
-
case 2:
|
|
2424
|
-
txSig = (_d.sent()).txSig;
|
|
2425
|
-
return [2 /*return*/, txSig];
|
|
2426
|
-
}
|
|
2427
|
-
});
|
|
2428
|
-
});
|
|
2429
|
-
};
|
|
2430
|
-
ClearingHouse.prototype.settlePNLIx = function (settleeUserAccountPublicKey, settleeUserAccount, marketIndex) {
|
|
2431
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2432
|
-
var perpMarketAccountMap, oracleAccountMap, spotMarketAccountMap, _i, _a, position, market, _b, _c, spotPosition, spotMarketAccount, marketAccount, remainingAccounts, _d, _e, _f;
|
|
2433
|
-
var _g, _h;
|
|
2434
|
-
return __generator(this, function (_j) {
|
|
2435
|
-
switch (_j.label) {
|
|
2436
|
-
case 0:
|
|
2437
|
-
perpMarketAccountMap = new Map();
|
|
2438
|
-
oracleAccountMap = new Map();
|
|
2439
|
-
spotMarketAccountMap = new Map();
|
|
2440
|
-
for (_i = 0, _a = settleeUserAccount.perpPositions; _i < _a.length; _i++) {
|
|
2441
|
-
position = _a[_i];
|
|
2442
|
-
if (!(0, position_1.positionIsAvailable)(position)) {
|
|
2443
|
-
market = this.getPerpMarketAccount(position.marketIndex);
|
|
2444
|
-
perpMarketAccountMap.set(position.marketIndex.toNumber(), {
|
|
2445
|
-
pubkey: market.pubkey,
|
|
2446
|
-
isWritable: false,
|
|
2447
|
-
isSigner: false
|
|
2448
|
-
});
|
|
2449
|
-
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
2450
|
-
pubkey: market.amm.oracle,
|
|
2451
|
-
isWritable: false,
|
|
2452
|
-
isSigner: false
|
|
2453
|
-
});
|
|
2454
|
-
}
|
|
2455
|
-
}
|
|
2456
|
-
for (_b = 0, _c = settleeUserAccount.spotPositions; _b < _c.length; _b++) {
|
|
2457
|
-
spotPosition = _c[_b];
|
|
2458
|
-
if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
|
|
2459
|
-
spotMarketAccount = this.getSpotMarketAccount(spotPosition.marketIndex);
|
|
2460
|
-
spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
|
|
2461
|
-
pubkey: spotMarketAccount.pubkey,
|
|
2462
|
-
isSigner: false,
|
|
2463
|
-
isWritable: false
|
|
2464
|
-
});
|
|
2465
|
-
if (!spotMarketAccount.marketIndex.eq(numericConstants_1.ZERO)) {
|
|
2466
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
2467
|
-
pubkey: spotMarketAccount.oracle,
|
|
2468
|
-
isSigner: false,
|
|
2469
|
-
isWritable: false
|
|
2470
|
-
});
|
|
2471
|
-
}
|
|
2472
|
-
}
|
|
2473
|
-
}
|
|
2474
|
-
marketAccount = this.getPerpMarketAccount(marketIndex.toNumber());
|
|
2475
|
-
perpMarketAccountMap.set(marketIndex.toNumber(), {
|
|
2476
|
-
pubkey: marketAccount.pubkey,
|
|
2477
|
-
isSigner: false,
|
|
2478
|
-
isWritable: true
|
|
2479
|
-
});
|
|
2480
|
-
oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
|
|
2481
|
-
pubkey: marketAccount.amm.oracle,
|
|
2482
|
-
isSigner: false,
|
|
2483
|
-
isWritable: false
|
|
2484
|
-
});
|
|
2485
|
-
spotMarketAccountMap.set(numericConstants_1.QUOTE_SPOT_MARKET_INDEX.toNumber(), {
|
|
2486
|
-
pubkey: this.getSpotMarketAccount(numericConstants_1.QUOTE_SPOT_MARKET_INDEX).pubkey,
|
|
2487
|
-
isSigner: false,
|
|
2488
|
-
isWritable: true
|
|
2489
|
-
});
|
|
2490
|
-
remainingAccounts = __spreadArray(__spreadArray(__spreadArray([], oracleAccountMap.values(), true), spotMarketAccountMap.values(), true), perpMarketAccountMap.values(), true);
|
|
2491
|
-
_e = (_d = this.program.instruction).settlePnl;
|
|
2492
|
-
_f = [marketIndex];
|
|
2493
|
-
_g = {};
|
|
2494
|
-
_h = {};
|
|
2495
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
2496
|
-
case 1: return [4 /*yield*/, _e.apply(_d, _f.concat([(_g.accounts = (_h.state = _j.sent(),
|
|
2497
|
-
_h.authority = this.wallet.publicKey,
|
|
2498
|
-
_h.user = settleeUserAccountPublicKey,
|
|
2499
|
-
_h),
|
|
2500
|
-
_g.remainingAccounts = remainingAccounts,
|
|
2501
|
-
_g)]))];
|
|
2502
|
-
case 2: return [2 /*return*/, _j.sent()];
|
|
2503
|
-
}
|
|
2504
|
-
});
|
|
2505
|
-
});
|
|
2506
|
-
};
|
|
2507
|
-
ClearingHouse.prototype.settleExpiredPosition = function (settleeUserAccountPublicKey, settleeUserAccount, marketIndex) {
|
|
2508
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2509
|
-
var txSig, _a, _b, _c;
|
|
2510
|
-
return __generator(this, function (_d) {
|
|
2511
|
-
switch (_d.label) {
|
|
2512
|
-
case 0:
|
|
2513
|
-
_b = (_a = this.txSender).send;
|
|
2514
|
-
_c = utils_1.wrapInTx;
|
|
2515
|
-
return [4 /*yield*/, this.getSettleExpiredPositionIx(settleeUserAccountPublicKey, settleeUserAccount, marketIndex)];
|
|
2516
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
2517
|
-
[],
|
|
2518
|
-
this.opts])];
|
|
2519
|
-
case 2:
|
|
2520
|
-
txSig = (_d.sent()).txSig;
|
|
2521
|
-
return [2 /*return*/, txSig];
|
|
2522
|
-
}
|
|
2523
|
-
});
|
|
2524
|
-
});
|
|
2525
|
-
};
|
|
2526
|
-
ClearingHouse.prototype.getSettleExpiredPositionIx = function (settleeUserAccountPublicKey, settleeUserAccount, marketIndex) {
|
|
2527
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2528
|
-
var marketAccountMap, oracleAccountMap, spotMarketAccountMap, _i, _a, position, market, _b, _c, userBankBalance, bankAccount, marketAccount, remainingAccounts, _d, _e, _f;
|
|
2529
|
-
var _g, _h;
|
|
2530
|
-
return __generator(this, function (_j) {
|
|
2531
|
-
switch (_j.label) {
|
|
2532
|
-
case 0:
|
|
2533
|
-
marketAccountMap = new Map();
|
|
2534
|
-
oracleAccountMap = new Map();
|
|
2535
|
-
spotMarketAccountMap = new Map();
|
|
2536
|
-
for (_i = 0, _a = settleeUserAccount.perpPositions; _i < _a.length; _i++) {
|
|
2537
|
-
position = _a[_i];
|
|
2538
|
-
if (!(0, position_1.positionIsAvailable)(position)) {
|
|
2539
|
-
market = this.getPerpMarketAccount(position.marketIndex);
|
|
2540
|
-
marketAccountMap.set(position.marketIndex.toNumber(), {
|
|
2541
|
-
pubkey: market.pubkey,
|
|
2542
|
-
isWritable: false,
|
|
2543
|
-
isSigner: false
|
|
2544
|
-
});
|
|
2545
|
-
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
2546
|
-
pubkey: market.amm.oracle,
|
|
2547
|
-
isWritable: false,
|
|
2548
|
-
isSigner: false
|
|
2549
|
-
});
|
|
2550
|
-
}
|
|
2551
|
-
}
|
|
2552
|
-
for (_b = 0, _c = settleeUserAccount.spotPositions; _b < _c.length; _b++) {
|
|
2553
|
-
userBankBalance = _c[_b];
|
|
2554
|
-
if (!userBankBalance.balance.eq(numericConstants_1.ZERO)) {
|
|
2555
|
-
bankAccount = this.getSpotMarketAccount(userBankBalance.marketIndex);
|
|
2556
|
-
spotMarketAccountMap.set(userBankBalance.marketIndex.toNumber(), {
|
|
2557
|
-
pubkey: bankAccount.pubkey,
|
|
2558
|
-
isSigner: false,
|
|
2559
|
-
isWritable: false
|
|
2560
|
-
});
|
|
2561
|
-
if (!bankAccount.marketIndex.eq(numericConstants_1.ZERO)) {
|
|
2562
|
-
oracleAccountMap.set(bankAccount.oracle.toString(), {
|
|
2563
|
-
pubkey: bankAccount.oracle,
|
|
2564
|
-
isSigner: false,
|
|
2565
|
-
isWritable: false
|
|
2566
|
-
});
|
|
2567
|
-
}
|
|
2568
|
-
}
|
|
2569
|
-
}
|
|
2570
|
-
marketAccount = this.getPerpMarketAccount(marketIndex.toNumber());
|
|
2571
|
-
marketAccountMap.set(marketIndex.toNumber(), {
|
|
2572
|
-
pubkey: marketAccount.pubkey,
|
|
2573
|
-
isSigner: false,
|
|
2574
|
-
isWritable: true
|
|
2575
|
-
});
|
|
2576
|
-
oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
|
|
2577
|
-
pubkey: marketAccount.amm.oracle,
|
|
2578
|
-
isSigner: false,
|
|
2579
|
-
isWritable: false
|
|
2580
|
-
});
|
|
2581
|
-
spotMarketAccountMap.set(numericConstants_1.QUOTE_SPOT_MARKET_INDEX.toNumber(), {
|
|
2582
|
-
pubkey: this.getSpotMarketAccount(numericConstants_1.QUOTE_SPOT_MARKET_INDEX).pubkey,
|
|
2583
|
-
isSigner: false,
|
|
2584
|
-
isWritable: true
|
|
2585
|
-
});
|
|
2586
|
-
remainingAccounts = __spreadArray(__spreadArray(__spreadArray([], oracleAccountMap.values(), true), spotMarketAccountMap.values(), true), marketAccountMap.values(), true);
|
|
2587
|
-
_e = (_d = this.program.instruction).settleExpiredPosition;
|
|
2588
|
-
_f = [marketIndex];
|
|
2589
|
-
_g = {};
|
|
2590
|
-
_h = {};
|
|
2591
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
2592
|
-
case 1: return [4 /*yield*/, _e.apply(_d, _f.concat([(_g.accounts = (_h.state = _j.sent(),
|
|
2593
|
-
_h.authority = this.wallet.publicKey,
|
|
2594
|
-
_h.user = settleeUserAccountPublicKey,
|
|
2595
|
-
_h),
|
|
2596
|
-
_g.remainingAccounts = remainingAccounts,
|
|
2597
|
-
_g)]))];
|
|
2598
|
-
case 2: return [2 /*return*/, _j.sent()];
|
|
2599
|
-
}
|
|
2600
|
-
});
|
|
2601
|
-
});
|
|
2602
|
-
};
|
|
2603
|
-
ClearingHouse.prototype.liquidatePerp = function (userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount) {
|
|
2604
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2605
|
-
var _a, txSig, slot, _b, _c, _d;
|
|
2606
|
-
return __generator(this, function (_e) {
|
|
2607
|
-
switch (_e.label) {
|
|
2608
|
-
case 0:
|
|
2609
|
-
_c = (_b = this.txSender).send;
|
|
2610
|
-
_d = utils_1.wrapInTx;
|
|
2611
|
-
return [4 /*yield*/, this.getLiquidatePerpIx(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount)];
|
|
2612
|
-
case 1: return [4 /*yield*/, _c.apply(_b, [_d.apply(void 0, [_e.sent()]),
|
|
2613
|
-
[],
|
|
2614
|
-
this.opts])];
|
|
2615
|
-
case 2:
|
|
2616
|
-
_a = _e.sent(), txSig = _a.txSig, slot = _a.slot;
|
|
2617
|
-
this.marketLastSlotCache.set(marketIndex.toNumber(), slot);
|
|
2618
|
-
return [2 /*return*/, txSig];
|
|
2619
|
-
}
|
|
2620
|
-
});
|
|
2621
|
-
});
|
|
2622
|
-
};
|
|
2623
|
-
ClearingHouse.prototype.getLiquidatePerpIx = function (userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount) {
|
|
2624
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2625
|
-
var userStatsPublicKey, liquidatorPublicKey, liquidatorStatsPublicKey, remainingAccounts, _a, _b, _c;
|
|
2626
|
-
var _d, _e;
|
|
2627
|
-
return __generator(this, function (_f) {
|
|
2628
|
-
switch (_f.label) {
|
|
2629
|
-
case 0:
|
|
2630
|
-
userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, userAccount.authority);
|
|
2631
|
-
return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
2632
|
-
case 1:
|
|
2633
|
-
liquidatorPublicKey = _f.sent();
|
|
2634
|
-
liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
2635
|
-
remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
2636
|
-
writablePerpMarketIndex: marketIndex,
|
|
2637
|
-
counterPartyUserAccount: userAccount
|
|
2638
|
-
});
|
|
2639
|
-
_b = (_a = this.program.instruction).liquidatePerp;
|
|
2640
|
-
_c = [marketIndex,
|
|
2641
|
-
maxBaseAssetAmount];
|
|
2642
|
-
_d = {};
|
|
2643
|
-
_e = {};
|
|
2644
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
2645
|
-
case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
2646
|
-
_e.authority = this.wallet.publicKey,
|
|
2647
|
-
_e.user = userAccountPublicKey,
|
|
2648
|
-
_e.userStats = userStatsPublicKey,
|
|
2649
|
-
_e.liquidator = liquidatorPublicKey,
|
|
2650
|
-
_e.liquidatorStats = liquidatorStatsPublicKey,
|
|
2651
|
-
_e),
|
|
2652
|
-
_d.remainingAccounts = remainingAccounts,
|
|
2653
|
-
_d)]))];
|
|
2654
|
-
case 3: return [2 /*return*/, _f.sent()];
|
|
2655
|
-
}
|
|
2656
|
-
});
|
|
2657
|
-
});
|
|
2658
|
-
};
|
|
2659
|
-
ClearingHouse.prototype.liquidateBorrow = function (userAccountPublicKey, userAccount, assetmarketIndex, liabilitymarketIndex, maxLiabilityTransfer) {
|
|
2660
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2661
|
-
var txSig, _a, _b, _c;
|
|
2662
|
-
return __generator(this, function (_d) {
|
|
2663
|
-
switch (_d.label) {
|
|
2664
|
-
case 0:
|
|
2665
|
-
_b = (_a = this.txSender).send;
|
|
2666
|
-
_c = utils_1.wrapInTx;
|
|
2667
|
-
return [4 /*yield*/, this.getLiquidateBorrowIx(userAccountPublicKey, userAccount, assetmarketIndex, liabilitymarketIndex, maxLiabilityTransfer)];
|
|
2668
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
2669
|
-
[],
|
|
2670
|
-
this.opts])];
|
|
2671
|
-
case 2:
|
|
2672
|
-
txSig = (_d.sent()).txSig;
|
|
2673
|
-
return [2 /*return*/, txSig];
|
|
2674
|
-
}
|
|
2675
|
-
});
|
|
2676
|
-
});
|
|
2677
|
-
};
|
|
2678
|
-
ClearingHouse.prototype.getLiquidateBorrowIx = function (userAccountPublicKey, userAccount, assetmarketIndex, liabilitymarketIndex, maxLiabilityTransfer) {
|
|
2679
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2680
|
-
var liquidatorPublicKey, remainingAccounts, _a, _b, _c;
|
|
2681
|
-
var _d, _e;
|
|
2682
|
-
return __generator(this, function (_f) {
|
|
2683
|
-
switch (_f.label) {
|
|
2684
|
-
case 0: return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
2685
|
-
case 1:
|
|
2686
|
-
liquidatorPublicKey = _f.sent();
|
|
2687
|
-
remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
2688
|
-
counterPartyUserAccount: userAccount,
|
|
2689
|
-
writableSpotMarketIndexes: [liabilitymarketIndex, assetmarketIndex]
|
|
2690
|
-
});
|
|
2691
|
-
_b = (_a = this.program.instruction).liquidateBorrow;
|
|
2692
|
-
_c = [assetmarketIndex,
|
|
2693
|
-
liabilitymarketIndex,
|
|
2694
|
-
maxLiabilityTransfer];
|
|
2695
|
-
_d = {};
|
|
2696
|
-
_e = {};
|
|
2697
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
2698
|
-
case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
2699
|
-
_e.authority = this.wallet.publicKey,
|
|
2700
|
-
_e.user = userAccountPublicKey,
|
|
2701
|
-
_e.liquidator = liquidatorPublicKey,
|
|
2702
|
-
_e),
|
|
2703
|
-
_d.remainingAccounts = remainingAccounts,
|
|
2704
|
-
_d)]))];
|
|
2705
|
-
case 3: return [2 /*return*/, _f.sent()];
|
|
2706
|
-
}
|
|
2707
|
-
});
|
|
2708
|
-
});
|
|
2709
|
-
};
|
|
2710
|
-
ClearingHouse.prototype.liquidateBorrowForPerpPnl = function (userAccountPublicKey, userAccount, perpMarketIndex, liabilitymarketIndex, maxLiabilityTransfer) {
|
|
2711
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2712
|
-
var _a, txSig, slot, _b, _c, _d;
|
|
2713
|
-
return __generator(this, function (_e) {
|
|
2714
|
-
switch (_e.label) {
|
|
2715
|
-
case 0:
|
|
2716
|
-
_c = (_b = this.txSender).send;
|
|
2717
|
-
_d = utils_1.wrapInTx;
|
|
2718
|
-
return [4 /*yield*/, this.getLiquidateBorrowForPerpPnlIx(userAccountPublicKey, userAccount, perpMarketIndex, liabilitymarketIndex, maxLiabilityTransfer)];
|
|
2719
|
-
case 1: return [4 /*yield*/, _c.apply(_b, [_d.apply(void 0, [_e.sent()]),
|
|
2720
|
-
[],
|
|
2721
|
-
this.opts])];
|
|
2722
|
-
case 2:
|
|
2723
|
-
_a = _e.sent(), txSig = _a.txSig, slot = _a.slot;
|
|
2724
|
-
this.marketLastSlotCache.set(perpMarketIndex.toNumber(), slot);
|
|
2725
|
-
return [2 /*return*/, txSig];
|
|
2726
|
-
}
|
|
2727
|
-
});
|
|
2728
|
-
});
|
|
2729
|
-
};
|
|
2730
|
-
ClearingHouse.prototype.getLiquidateBorrowForPerpPnlIx = function (userAccountPublicKey, userAccount, perpMarketIndex, liabilitymarketIndex, maxLiabilityTransfer) {
|
|
2731
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2732
|
-
var liquidatorPublicKey, remainingAccounts, _a, _b, _c;
|
|
2733
|
-
var _d, _e;
|
|
2734
|
-
return __generator(this, function (_f) {
|
|
2735
|
-
switch (_f.label) {
|
|
2736
|
-
case 0: return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
2737
|
-
case 1:
|
|
2738
|
-
liquidatorPublicKey = _f.sent();
|
|
2739
|
-
remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
2740
|
-
counterPartyUserAccount: userAccount,
|
|
2741
|
-
writablePerpMarketIndex: perpMarketIndex,
|
|
2742
|
-
writableSpotMarketIndexes: [liabilitymarketIndex]
|
|
2743
|
-
});
|
|
2744
|
-
_b = (_a = this.program.instruction).liquidateBorrowForPerpPnl;
|
|
2745
|
-
_c = [perpMarketIndex,
|
|
2746
|
-
liabilitymarketIndex,
|
|
2747
|
-
maxLiabilityTransfer];
|
|
2748
|
-
_d = {};
|
|
2749
|
-
_e = {};
|
|
2750
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
2751
|
-
case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
2752
|
-
_e.authority = this.wallet.publicKey,
|
|
2753
|
-
_e.user = userAccountPublicKey,
|
|
2754
|
-
_e.liquidator = liquidatorPublicKey,
|
|
2755
|
-
_e),
|
|
2756
|
-
_d.remainingAccounts = remainingAccounts,
|
|
2757
|
-
_d)]))];
|
|
2758
|
-
case 3: return [2 /*return*/, _f.sent()];
|
|
2759
|
-
}
|
|
2760
|
-
});
|
|
2761
|
-
});
|
|
2762
|
-
};
|
|
2763
|
-
ClearingHouse.prototype.liquidatePerpPnlForDeposit = function (userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer) {
|
|
2764
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2765
|
-
var _a, txSig, slot, _b, _c, _d;
|
|
2766
|
-
return __generator(this, function (_e) {
|
|
2767
|
-
switch (_e.label) {
|
|
2768
|
-
case 0:
|
|
2769
|
-
_c = (_b = this.txSender).send;
|
|
2770
|
-
_d = utils_1.wrapInTx;
|
|
2771
|
-
return [4 /*yield*/, this.getLiquidatePerpPnlForDepositIx(userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer)];
|
|
2772
|
-
case 1: return [4 /*yield*/, _c.apply(_b, [_d.apply(void 0, [_e.sent()]),
|
|
2773
|
-
[],
|
|
2774
|
-
this.opts])];
|
|
2775
|
-
case 2:
|
|
2776
|
-
_a = _e.sent(), txSig = _a.txSig, slot = _a.slot;
|
|
2777
|
-
this.marketLastSlotCache.set(perpMarketIndex.toNumber(), slot);
|
|
2778
|
-
return [2 /*return*/, txSig];
|
|
2779
|
-
}
|
|
2780
|
-
});
|
|
2781
|
-
});
|
|
2782
|
-
};
|
|
2783
|
-
ClearingHouse.prototype.getLiquidatePerpPnlForDepositIx = function (userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer) {
|
|
2784
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2785
|
-
var liquidatorPublicKey, remainingAccounts, _a, _b, _c;
|
|
2786
|
-
var _d, _e;
|
|
2787
|
-
return __generator(this, function (_f) {
|
|
2788
|
-
switch (_f.label) {
|
|
2789
|
-
case 0: return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
2790
|
-
case 1:
|
|
2791
|
-
liquidatorPublicKey = _f.sent();
|
|
2792
|
-
remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
2793
|
-
counterPartyUserAccount: userAccount,
|
|
2794
|
-
writablePerpMarketIndex: perpMarketIndex,
|
|
2795
|
-
writableSpotMarketIndexes: [assetMarketIndex]
|
|
2796
|
-
});
|
|
2797
|
-
_b = (_a = this.program.instruction).liquidatePerpPnlForDeposit;
|
|
2798
|
-
_c = [perpMarketIndex,
|
|
2799
|
-
assetMarketIndex,
|
|
2800
|
-
maxPnlTransfer];
|
|
2801
|
-
_d = {};
|
|
2802
|
-
_e = {};
|
|
2803
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
2804
|
-
case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
2805
|
-
_e.authority = this.wallet.publicKey,
|
|
2806
|
-
_e.user = userAccountPublicKey,
|
|
2807
|
-
_e.liquidator = liquidatorPublicKey,
|
|
2808
|
-
_e),
|
|
2809
|
-
_d.remainingAccounts = remainingAccounts,
|
|
2810
|
-
_d)]))];
|
|
2811
|
-
case 3: return [2 /*return*/, _f.sent()];
|
|
2812
|
-
}
|
|
2813
|
-
});
|
|
2814
|
-
});
|
|
2815
|
-
};
|
|
2816
|
-
ClearingHouse.prototype.resolvePerpBankruptcy = function (userAccountPublicKey, userAccount, marketIndex) {
|
|
2817
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2818
|
-
var txSig, _a, _b, _c;
|
|
2819
|
-
return __generator(this, function (_d) {
|
|
2820
|
-
switch (_d.label) {
|
|
2821
|
-
case 0:
|
|
2822
|
-
_b = (_a = this.txSender).send;
|
|
2823
|
-
_c = utils_1.wrapInTx;
|
|
2824
|
-
return [4 /*yield*/, this.getResolvePerpBankruptcyIx(userAccountPublicKey, userAccount, marketIndex)];
|
|
2825
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
2826
|
-
[],
|
|
2827
|
-
this.opts])];
|
|
2828
|
-
case 2:
|
|
2829
|
-
txSig = (_d.sent()).txSig;
|
|
2830
|
-
return [2 /*return*/, txSig];
|
|
2831
|
-
}
|
|
2832
|
-
});
|
|
2833
|
-
});
|
|
2834
|
-
};
|
|
2835
|
-
ClearingHouse.prototype.getResolvePerpBankruptcyIx = function (userAccountPublicKey, userAccount, marketIndex) {
|
|
2836
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2837
|
-
var liquidatorPublicKey, remainingAccounts, spotMarket, _a, _b, _c;
|
|
2838
|
-
var _d, _e;
|
|
2839
|
-
return __generator(this, function (_f) {
|
|
2840
|
-
switch (_f.label) {
|
|
2841
|
-
case 0: return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
2842
|
-
case 1:
|
|
2843
|
-
liquidatorPublicKey = _f.sent();
|
|
2844
|
-
remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
2845
|
-
writablePerpMarketIndex: marketIndex,
|
|
2846
|
-
writableSpotMarketIndexes: [numericConstants_1.QUOTE_SPOT_MARKET_INDEX],
|
|
2847
|
-
counterPartyUserAccount: userAccount
|
|
2848
|
-
});
|
|
2849
|
-
spotMarket = this.getSpotMarketAccount(marketIndex);
|
|
2850
|
-
_b = (_a = this.program.instruction).resolvePerpBankruptcy;
|
|
2851
|
-
_c = [numericConstants_1.QUOTE_SPOT_MARKET_INDEX,
|
|
2852
|
-
marketIndex];
|
|
2853
|
-
_d = {};
|
|
2854
|
-
_e = {};
|
|
2855
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
2856
|
-
case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
2857
|
-
_e.authority = this.wallet.publicKey,
|
|
2858
|
-
_e.user = userAccountPublicKey,
|
|
2859
|
-
_e.liquidator = liquidatorPublicKey,
|
|
2860
|
-
_e.spotMarketVault = spotMarket.vault,
|
|
2861
|
-
_e.insuranceFundVault = spotMarket.insuranceFundVault,
|
|
2862
|
-
_e.clearingHouseSigner = this.getSignerPublicKey(),
|
|
2863
|
-
_e.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
|
|
2864
|
-
_e),
|
|
2865
|
-
_d.remainingAccounts = remainingAccounts,
|
|
2866
|
-
_d)]))];
|
|
2867
|
-
case 3: return [2 /*return*/, _f.sent()];
|
|
2868
|
-
}
|
|
2869
|
-
});
|
|
2870
|
-
});
|
|
2871
|
-
};
|
|
2872
|
-
ClearingHouse.prototype.resolveBorrowBankruptcy = function (userAccountPublicKey, userAccount, marketIndex) {
|
|
2873
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2874
|
-
var txSig, _a, _b, _c;
|
|
2875
|
-
return __generator(this, function (_d) {
|
|
2876
|
-
switch (_d.label) {
|
|
2877
|
-
case 0:
|
|
2878
|
-
_b = (_a = this.txSender).send;
|
|
2879
|
-
_c = utils_1.wrapInTx;
|
|
2880
|
-
return [4 /*yield*/, this.getResolveBorrowBankruptcyIx(userAccountPublicKey, userAccount, marketIndex)];
|
|
2881
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
2882
|
-
[],
|
|
2883
|
-
this.opts])];
|
|
2884
|
-
case 2:
|
|
2885
|
-
txSig = (_d.sent()).txSig;
|
|
2886
|
-
return [2 /*return*/, txSig];
|
|
2887
|
-
}
|
|
2888
|
-
});
|
|
2889
|
-
});
|
|
2890
|
-
};
|
|
2891
|
-
ClearingHouse.prototype.getResolveBorrowBankruptcyIx = function (userAccountPublicKey, userAccount, marketIndex) {
|
|
2892
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2893
|
-
var liquidatorPublicKey, remainingAccounts, spotMarket, _a, _b, _c;
|
|
2894
|
-
var _d, _e;
|
|
2895
|
-
return __generator(this, function (_f) {
|
|
2896
|
-
switch (_f.label) {
|
|
2897
|
-
case 0: return [4 /*yield*/, this.getUserAccountPublicKey()];
|
|
2898
|
-
case 1:
|
|
2899
|
-
liquidatorPublicKey = _f.sent();
|
|
2900
|
-
remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
2901
|
-
writableSpotMarketIndexes: [marketIndex],
|
|
2902
|
-
counterPartyUserAccount: userAccount
|
|
2903
|
-
});
|
|
2904
|
-
spotMarket = this.getSpotMarketAccount(marketIndex);
|
|
2905
|
-
_b = (_a = this.program.instruction).resolveBorrowBankruptcy;
|
|
2906
|
-
_c = [marketIndex];
|
|
2907
|
-
_d = {};
|
|
2908
|
-
_e = {};
|
|
2909
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
2910
|
-
case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
2911
|
-
_e.authority = this.wallet.publicKey,
|
|
2912
|
-
_e.user = userAccountPublicKey,
|
|
2913
|
-
_e.liquidator = liquidatorPublicKey,
|
|
2914
|
-
_e.spotMarketVault = spotMarket.vault,
|
|
2915
|
-
_e.insuranceFundVault = spotMarket.insuranceFundVault,
|
|
2916
|
-
_e.clearingHouseSigner = this.getSignerPublicKey(),
|
|
2917
|
-
_e.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
|
|
2918
|
-
_e),
|
|
2919
|
-
_d.remainingAccounts = remainingAccounts,
|
|
2920
|
-
_d)]))];
|
|
2921
|
-
case 3: return [2 /*return*/, _f.sent()];
|
|
2922
|
-
}
|
|
2923
|
-
});
|
|
2924
|
-
});
|
|
2925
|
-
};
|
|
2926
|
-
ClearingHouse.prototype.getRemainingAccountsWithCounterparty = function (params) {
|
|
2927
|
-
var counterPartyUserAccount = params.counterPartyUserAccount;
|
|
2928
|
-
var oracleAccountMap = new Map();
|
|
2929
|
-
var spotMarketAccountMap = new Map();
|
|
2930
|
-
var marketAccountMap = new Map();
|
|
2931
|
-
for (var _i = 0, _a = counterPartyUserAccount.spotPositions; _i < _a.length; _i++) {
|
|
2932
|
-
var spotPosition = _a[_i];
|
|
2933
|
-
if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
|
|
2934
|
-
var spotMarket = this.getSpotMarketAccount(spotPosition.marketIndex);
|
|
2935
|
-
spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
|
|
2936
|
-
pubkey: spotMarket.pubkey,
|
|
2937
|
-
isSigner: false,
|
|
2938
|
-
isWritable: false
|
|
2939
|
-
});
|
|
2940
|
-
if (!spotMarket.oracle.equals(web3_js_1.PublicKey["default"])) {
|
|
2941
|
-
oracleAccountMap.set(spotMarket.oracle.toString(), {
|
|
2942
|
-
pubkey: spotMarket.oracle,
|
|
2943
|
-
isSigner: false,
|
|
2944
|
-
isWritable: false
|
|
2945
|
-
});
|
|
2946
|
-
}
|
|
2947
|
-
}
|
|
2948
|
-
}
|
|
2949
|
-
for (var _b = 0, _c = counterPartyUserAccount.perpPositions; _b < _c.length; _b++) {
|
|
2950
|
-
var position = _c[_b];
|
|
2951
|
-
if (!(0, position_1.positionIsAvailable)(position)) {
|
|
2952
|
-
var market = this.getPerpMarketAccount(position.marketIndex);
|
|
2953
|
-
marketAccountMap.set(position.marketIndex.toNumber(), {
|
|
2954
|
-
pubkey: market.pubkey,
|
|
2955
|
-
isWritable: false,
|
|
2956
|
-
isSigner: false
|
|
2957
|
-
});
|
|
2958
|
-
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
2959
|
-
pubkey: market.amm.oracle,
|
|
2960
|
-
isWritable: false,
|
|
2961
|
-
isSigner: false
|
|
2962
|
-
});
|
|
2963
|
-
}
|
|
2964
|
-
}
|
|
2965
|
-
var userAccountAndSlot = this.getUserAccountAndSlot();
|
|
2966
|
-
if (!userAccountAndSlot) {
|
|
2967
|
-
throw Error('No user account found. Most likely user account does not exist or failed to fetch account');
|
|
2968
|
-
}
|
|
2969
|
-
var userAccount = userAccountAndSlot.data, lastUserPositionsSlot = userAccountAndSlot.slot;
|
|
2970
|
-
for (var _d = 0, _e = this.marketLastSlotCache.entries(); _d < _e.length; _d++) {
|
|
2971
|
-
var _f = _e[_d], marketIndexNum = _f[0], slot = _f[1];
|
|
2972
|
-
// if cache has more recent slot than user positions account slot, add market to remaining accounts
|
|
2973
|
-
// otherwise remove from slot
|
|
2974
|
-
if (slot > lastUserPositionsSlot) {
|
|
2975
|
-
var marketAccount = this.getPerpMarketAccount(marketIndexNum);
|
|
2976
|
-
marketAccountMap.set(marketIndexNum, {
|
|
2977
|
-
pubkey: marketAccount.pubkey,
|
|
2978
|
-
isSigner: false,
|
|
2979
|
-
isWritable: false
|
|
2980
|
-
});
|
|
2981
|
-
oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
|
|
2982
|
-
pubkey: marketAccount.amm.oracle,
|
|
2983
|
-
isSigner: false,
|
|
2984
|
-
isWritable: false
|
|
2985
|
-
});
|
|
2986
|
-
}
|
|
2987
|
-
else {
|
|
2988
|
-
this.marketLastSlotCache["delete"](marketIndexNum);
|
|
2989
|
-
}
|
|
2990
|
-
}
|
|
2991
|
-
for (var _g = 0, _h = userAccount.spotPositions; _g < _h.length; _g++) {
|
|
2992
|
-
var spotPosition = _h[_g];
|
|
2993
|
-
if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
|
|
2994
|
-
var spotMarket = this.getSpotMarketAccount(spotPosition.marketIndex);
|
|
2995
|
-
spotMarketAccountMap.set(spotPosition.marketIndex.toNumber(), {
|
|
2996
|
-
pubkey: spotMarket.pubkey,
|
|
2997
|
-
isSigner: false,
|
|
2998
|
-
isWritable: false
|
|
2999
|
-
});
|
|
3000
|
-
if (!spotMarket.oracle.equals(web3_js_1.PublicKey["default"])) {
|
|
3001
|
-
oracleAccountMap.set(spotMarket.oracle.toString(), {
|
|
3002
|
-
pubkey: spotMarket.oracle,
|
|
3003
|
-
isSigner: false,
|
|
3004
|
-
isWritable: false
|
|
3005
|
-
});
|
|
3006
|
-
}
|
|
3007
|
-
}
|
|
3008
|
-
}
|
|
3009
|
-
for (var _j = 0, _k = userAccount.perpPositions; _j < _k.length; _j++) {
|
|
3010
|
-
var position = _k[_j];
|
|
3011
|
-
if (!(0, position_1.positionIsAvailable)(position)) {
|
|
3012
|
-
var market = this.getPerpMarketAccount(position.marketIndex);
|
|
3013
|
-
marketAccountMap.set(position.marketIndex.toNumber(), {
|
|
3014
|
-
pubkey: market.pubkey,
|
|
3015
|
-
isWritable: false,
|
|
3016
|
-
isSigner: false
|
|
3017
|
-
});
|
|
3018
|
-
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
3019
|
-
pubkey: market.amm.oracle,
|
|
3020
|
-
isWritable: false,
|
|
3021
|
-
isSigner: false
|
|
3022
|
-
});
|
|
3023
|
-
}
|
|
3024
|
-
}
|
|
3025
|
-
if (params.writablePerpMarketIndex) {
|
|
3026
|
-
var market = this.getPerpMarketAccount(params.writablePerpMarketIndex);
|
|
3027
|
-
marketAccountMap.set(market.marketIndex.toNumber(), {
|
|
3028
|
-
pubkey: market.pubkey,
|
|
3029
|
-
isSigner: false,
|
|
3030
|
-
isWritable: true
|
|
3031
|
-
});
|
|
3032
|
-
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
3033
|
-
pubkey: market.amm.oracle,
|
|
3034
|
-
isSigner: false,
|
|
3035
|
-
isWritable: false
|
|
3036
|
-
});
|
|
3037
|
-
}
|
|
3038
|
-
if (params.writableSpotMarketIndexes) {
|
|
3039
|
-
for (var _l = 0, _m = params.writableSpotMarketIndexes; _l < _m.length; _l++) {
|
|
3040
|
-
var writableSpotMarketIndex = _m[_l];
|
|
3041
|
-
var spotMarketAccount = this.getSpotMarketAccount(writableSpotMarketIndex);
|
|
3042
|
-
spotMarketAccountMap.set(spotMarketAccount.marketIndex.toNumber(), {
|
|
3043
|
-
pubkey: spotMarketAccount.pubkey,
|
|
3044
|
-
isSigner: false,
|
|
3045
|
-
isWritable: true
|
|
3046
|
-
});
|
|
3047
|
-
if (!spotMarketAccount.oracle.equals(web3_js_1.PublicKey["default"])) {
|
|
3048
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
3049
|
-
pubkey: spotMarketAccount.oracle,
|
|
3050
|
-
isSigner: false,
|
|
3051
|
-
isWritable: false
|
|
3052
|
-
});
|
|
3053
|
-
}
|
|
3054
|
-
}
|
|
3055
|
-
}
|
|
3056
|
-
return __spreadArray(__spreadArray(__spreadArray([], oracleAccountMap.values(), true), spotMarketAccountMap.values(), true), marketAccountMap.values(), true);
|
|
3057
|
-
};
|
|
3058
|
-
ClearingHouse.prototype.updateFundingRate = function (oracle, marketIndex) {
|
|
3059
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
3060
|
-
var txSig, _a, _b, _c;
|
|
3061
|
-
return __generator(this, function (_d) {
|
|
3062
|
-
switch (_d.label) {
|
|
3063
|
-
case 0:
|
|
3064
|
-
_b = (_a = this.txSender).send;
|
|
3065
|
-
_c = utils_1.wrapInTx;
|
|
3066
|
-
return [4 /*yield*/, this.getUpdateFundingRateIx(oracle, marketIndex)];
|
|
3067
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
3068
|
-
[],
|
|
3069
|
-
this.opts])];
|
|
3070
|
-
case 2:
|
|
3071
|
-
txSig = (_d.sent()).txSig;
|
|
3072
|
-
return [2 /*return*/, txSig];
|
|
3073
|
-
}
|
|
3074
|
-
});
|
|
3075
|
-
});
|
|
3076
|
-
};
|
|
3077
|
-
ClearingHouse.prototype.getUpdateFundingRateIx = function (oracle, marketIndex) {
|
|
3078
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
3079
|
-
var _a, _b, _c;
|
|
3080
|
-
var _d, _e;
|
|
3081
|
-
return __generator(this, function (_f) {
|
|
3082
|
-
switch (_f.label) {
|
|
3083
|
-
case 0:
|
|
3084
|
-
_b = (_a = this.program.instruction).updateFundingRate;
|
|
3085
|
-
_c = [marketIndex];
|
|
3086
|
-
_d = {};
|
|
3087
|
-
_e = {};
|
|
3088
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
3089
|
-
case 1:
|
|
3090
|
-
_e.state = _f.sent();
|
|
3091
|
-
return [4 /*yield*/, (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex)];
|
|
3092
|
-
case 2: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.market = _f.sent(),
|
|
3093
|
-
_e.oracle = oracle,
|
|
3094
|
-
_e),
|
|
3095
|
-
_d)]))];
|
|
3096
|
-
case 3: return [2 /*return*/, _f.sent()];
|
|
3097
|
-
}
|
|
3098
|
-
});
|
|
3099
|
-
});
|
|
3100
|
-
};
|
|
3101
|
-
ClearingHouse.prototype.settleFundingPayment = function (userAccount) {
|
|
3102
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
3103
|
-
var txSig, _a, _b, _c;
|
|
3104
|
-
return __generator(this, function (_d) {
|
|
3105
|
-
switch (_d.label) {
|
|
3106
|
-
case 0:
|
|
3107
|
-
_b = (_a = this.txSender).send;
|
|
3108
|
-
_c = utils_1.wrapInTx;
|
|
3109
|
-
return [4 /*yield*/, this.getSettleFundingPaymentIx(userAccount)];
|
|
3110
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
3111
|
-
[],
|
|
3112
|
-
this.opts])];
|
|
3113
|
-
case 2:
|
|
3114
|
-
txSig = (_d.sent()).txSig;
|
|
3115
|
-
return [2 /*return*/, txSig];
|
|
3116
|
-
}
|
|
3117
|
-
});
|
|
3118
|
-
});
|
|
3119
|
-
};
|
|
3120
|
-
ClearingHouse.prototype.getSettleFundingPaymentIx = function (userAccount) {
|
|
3121
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
3122
|
-
var user, userPositions, remainingAccounts, _i, userPositions_2, position, marketPublicKey, _a, _b;
|
|
3123
|
-
var _c, _d;
|
|
3124
|
-
return __generator(this, function (_e) {
|
|
3125
|
-
switch (_e.label) {
|
|
3126
|
-
case 0: return [4 /*yield*/, this.program.account.user.fetch(userAccount)];
|
|
3127
|
-
case 1:
|
|
3128
|
-
user = (_e.sent());
|
|
3129
|
-
userPositions = user.perpPositions;
|
|
3130
|
-
remainingAccounts = [];
|
|
3131
|
-
_i = 0, userPositions_2 = userPositions;
|
|
3132
|
-
_e.label = 2;
|
|
3133
|
-
case 2:
|
|
3134
|
-
if (!(_i < userPositions_2.length)) return [3 /*break*/, 5];
|
|
3135
|
-
position = userPositions_2[_i];
|
|
3136
|
-
if (!!(0, position_1.positionIsAvailable)(position)) return [3 /*break*/, 4];
|
|
3137
|
-
return [4 /*yield*/, (0, pda_1.getMarketPublicKey)(this.program.programId, position.marketIndex)];
|
|
3138
|
-
case 3:
|
|
3139
|
-
marketPublicKey = _e.sent();
|
|
3140
|
-
remainingAccounts.push({
|
|
3141
|
-
pubkey: marketPublicKey,
|
|
3142
|
-
isWritable: false,
|
|
3143
|
-
isSigner: false
|
|
3144
|
-
});
|
|
3145
|
-
_e.label = 4;
|
|
3146
|
-
case 4:
|
|
3147
|
-
_i++;
|
|
3148
|
-
return [3 /*break*/, 2];
|
|
3149
|
-
case 5:
|
|
3150
|
-
_b = (_a = this.program.instruction).settleFundingPayment;
|
|
3151
|
-
_c = {};
|
|
3152
|
-
_d = {};
|
|
3153
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
3154
|
-
case 6: return [4 /*yield*/, _b.apply(_a, [(_c.accounts = (_d.state = _e.sent(),
|
|
3155
|
-
_d.user = userAccount,
|
|
3156
|
-
_d),
|
|
3157
|
-
_c.remainingAccounts = remainingAccounts,
|
|
3158
|
-
_c)])];
|
|
3159
|
-
case 7: return [2 /*return*/, _e.sent()];
|
|
3160
|
-
}
|
|
3161
|
-
});
|
|
3162
|
-
});
|
|
3163
|
-
};
|
|
3164
|
-
ClearingHouse.prototype.triggerEvent = function (eventName, data) {
|
|
3165
|
-
this.eventEmitter.emit(eventName, data);
|
|
3166
|
-
};
|
|
3167
|
-
ClearingHouse.prototype.getOracleDataForMarket = function (marketIndex) {
|
|
3168
|
-
var oracleKey = this.getPerpMarketAccount(marketIndex).amm.oracle;
|
|
3169
|
-
var oracleData = this.getOraclePriceDataAndSlot(oracleKey).data;
|
|
3170
|
-
return oracleData;
|
|
3171
|
-
};
|
|
3172
|
-
ClearingHouse.prototype.initializeInsuranceFundStake = function (marketIndex) {
|
|
3173
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
3174
|
-
var txSig, _a, _b, _c;
|
|
3175
|
-
return __generator(this, function (_d) {
|
|
3176
|
-
switch (_d.label) {
|
|
3177
|
-
case 0:
|
|
3178
|
-
_b = (_a = this.txSender).send;
|
|
3179
|
-
_c = utils_1.wrapInTx;
|
|
3180
|
-
return [4 /*yield*/, this.getInitializeInsuranceFundStakeIx(marketIndex)];
|
|
3181
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
3182
|
-
[],
|
|
3183
|
-
this.opts])];
|
|
3184
|
-
case 2:
|
|
3185
|
-
txSig = (_d.sent()).txSig;
|
|
3186
|
-
return [2 /*return*/, txSig];
|
|
3187
|
-
}
|
|
3188
|
-
});
|
|
3189
|
-
});
|
|
3190
|
-
};
|
|
3191
|
-
ClearingHouse.prototype.getInitializeInsuranceFundStakeIx = function (marketIndex) {
|
|
3192
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
3193
|
-
var ifStakeAccountPublicKey, _a, _b, _c;
|
|
3194
|
-
var _d, _e;
|
|
3195
|
-
return __generator(this, function (_f) {
|
|
3196
|
-
switch (_f.label) {
|
|
3197
|
-
case 0:
|
|
3198
|
-
ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
|
|
3199
|
-
_b = (_a = this.program.instruction).initializeInsuranceFundStake;
|
|
3200
|
-
_c = [marketIndex];
|
|
3201
|
-
_d = {};
|
|
3202
|
-
_e = {
|
|
3203
|
-
insuranceFundStake: ifStakeAccountPublicKey,
|
|
3204
|
-
spotMarket: this.getSpotMarketAccount(marketIndex).pubkey,
|
|
3205
|
-
userStats: this.getUserStatsAccountPublicKey(),
|
|
3206
|
-
authority: this.wallet.publicKey,
|
|
3207
|
-
payer: this.wallet.publicKey,
|
|
3208
|
-
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
3209
|
-
systemProgram: anchor.web3.SystemProgram.programId
|
|
3210
|
-
};
|
|
3211
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
3212
|
-
case 1: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
3213
|
-
_e),
|
|
3214
|
-
_d)]))];
|
|
3215
|
-
case 2: return [2 /*return*/, _f.sent()];
|
|
3216
|
-
}
|
|
3217
|
-
});
|
|
3218
|
-
});
|
|
3219
|
-
};
|
|
3220
|
-
ClearingHouse.prototype.addInsuranceFundStake = function (marketIndex, amount, collateralAccountPublicKey) {
|
|
3221
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
3222
|
-
var spotMarket, ifStakeAccountPublicKey, remainingAccounts, _a, _b, _c;
|
|
3223
|
-
var _d, _e;
|
|
3224
|
-
return __generator(this, function (_f) {
|
|
3225
|
-
switch (_f.label) {
|
|
3226
|
-
case 0:
|
|
3227
|
-
spotMarket = this.getSpotMarketAccount(marketIndex);
|
|
3228
|
-
ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
|
|
3229
|
-
remainingAccounts = this.getRemainingAccounts({
|
|
3230
|
-
writableSpotMarketIndex: marketIndex
|
|
3231
|
-
});
|
|
3232
|
-
_b = (_a = this.program.rpc).addInsuranceFundStake;
|
|
3233
|
-
_c = [marketIndex, amount];
|
|
3234
|
-
_d = {};
|
|
3235
|
-
_e = {};
|
|
3236
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
3237
|
-
case 1: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
3238
|
-
_e.spotMarket = spotMarket.pubkey,
|
|
3239
|
-
_e.insuranceFundStake = ifStakeAccountPublicKey,
|
|
3240
|
-
_e.userStats = this.getUserStatsAccountPublicKey(),
|
|
3241
|
-
_e.authority = this.wallet.publicKey,
|
|
3242
|
-
_e.insuranceFundVault = spotMarket.insuranceFundVault,
|
|
3243
|
-
_e.userTokenAccount = collateralAccountPublicKey,
|
|
3244
|
-
_e.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
|
|
3245
|
-
_e),
|
|
3246
|
-
_d.remainingAccounts = remainingAccounts,
|
|
3247
|
-
_d)]))];
|
|
3248
|
-
case 2: return [2 /*return*/, _f.sent()];
|
|
3249
|
-
}
|
|
3250
|
-
});
|
|
3251
|
-
});
|
|
3252
|
-
};
|
|
3253
|
-
ClearingHouse.prototype.requestRemoveInsuranceFundStake = function (marketIndex, amount) {
|
|
3254
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
3255
|
-
var spotMarketAccount, ifStakeAccountPublicKey, remainingAccounts, _a, _b, _c;
|
|
3256
|
-
var _d, _e;
|
|
3257
|
-
return __generator(this, function (_f) {
|
|
3258
|
-
switch (_f.label) {
|
|
3259
|
-
case 0:
|
|
3260
|
-
spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
3261
|
-
ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
|
|
3262
|
-
remainingAccounts = this.getRemainingAccounts({
|
|
3263
|
-
writableSpotMarketIndex: marketIndex
|
|
3264
|
-
});
|
|
3265
|
-
_b = (_a = this.program.rpc).requestRemoveInsuranceFundStake;
|
|
3266
|
-
_c = [marketIndex,
|
|
3267
|
-
amount];
|
|
3268
|
-
_d = {};
|
|
3269
|
-
_e = {};
|
|
3270
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
3271
|
-
case 1: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
3272
|
-
_e.spotMarket = spotMarketAccount.pubkey,
|
|
3273
|
-
_e.insuranceFundStake = ifStakeAccountPublicKey,
|
|
3274
|
-
_e.userStats = this.getUserStatsAccountPublicKey(),
|
|
3275
|
-
_e.authority = this.wallet.publicKey,
|
|
3276
|
-
_e.insuranceFundVault = spotMarketAccount.insuranceFundVault,
|
|
3277
|
-
_e),
|
|
3278
|
-
_d.remainingAccounts = remainingAccounts,
|
|
3279
|
-
_d)]))];
|
|
3280
|
-
case 2: return [2 /*return*/, _f.sent()];
|
|
3281
|
-
}
|
|
3282
|
-
});
|
|
3283
|
-
});
|
|
3284
|
-
};
|
|
3285
|
-
ClearingHouse.prototype.cancelRequestRemoveInsuranceFundStake = function (marketIndex) {
|
|
3286
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
3287
|
-
var spotMarketAccount, ifStakeAccountPublicKey, remainingAccounts, _a, _b, _c;
|
|
3288
|
-
var _d, _e;
|
|
3289
|
-
return __generator(this, function (_f) {
|
|
3290
|
-
switch (_f.label) {
|
|
3291
|
-
case 0:
|
|
3292
|
-
spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
3293
|
-
ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
|
|
3294
|
-
remainingAccounts = this.getRemainingAccounts({
|
|
3295
|
-
writableSpotMarketIndex: marketIndex
|
|
3296
|
-
});
|
|
3297
|
-
_b = (_a = this.program.rpc).cancelRequestRemoveInsuranceFundStake;
|
|
3298
|
-
_c = [marketIndex];
|
|
3299
|
-
_d = {};
|
|
3300
|
-
_e = {};
|
|
3301
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
3302
|
-
case 1: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
3303
|
-
_e.spotMarket = spotMarketAccount.pubkey,
|
|
3304
|
-
_e.insuranceFundStake = ifStakeAccountPublicKey,
|
|
3305
|
-
_e.userStats = this.getUserStatsAccountPublicKey(),
|
|
3306
|
-
_e.authority = this.wallet.publicKey,
|
|
3307
|
-
_e.insuranceFundVault = spotMarketAccount.insuranceFundVault,
|
|
3308
|
-
_e),
|
|
3309
|
-
_d.remainingAccounts = remainingAccounts,
|
|
3310
|
-
_d)]))];
|
|
3311
|
-
case 2: return [2 /*return*/, _f.sent()];
|
|
3312
|
-
}
|
|
3313
|
-
});
|
|
3314
|
-
});
|
|
3315
|
-
};
|
|
3316
|
-
ClearingHouse.prototype.removeInsuranceFundStake = function (marketIndex, collateralAccountPublicKey) {
|
|
3317
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
3318
|
-
var spotMarketAccount, ifStakeAccountPublicKey, remainingAccounts, _a, _b, _c;
|
|
3319
|
-
var _d, _e;
|
|
3320
|
-
return __generator(this, function (_f) {
|
|
3321
|
-
switch (_f.label) {
|
|
3322
|
-
case 0:
|
|
3323
|
-
spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
3324
|
-
ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
|
|
3325
|
-
remainingAccounts = this.getRemainingAccounts({
|
|
3326
|
-
writableSpotMarketIndex: marketIndex
|
|
3327
|
-
});
|
|
3328
|
-
_b = (_a = this.program.rpc).removeInsuranceFundStake;
|
|
3329
|
-
_c = [marketIndex];
|
|
3330
|
-
_d = {};
|
|
3331
|
-
_e = {};
|
|
3332
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
3333
|
-
case 1: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
3334
|
-
_e.spotMarket = spotMarketAccount.pubkey,
|
|
3335
|
-
_e.insuranceFundStake = ifStakeAccountPublicKey,
|
|
3336
|
-
_e.userStats = this.getUserStatsAccountPublicKey(),
|
|
3337
|
-
_e.authority = this.wallet.publicKey,
|
|
3338
|
-
_e.insuranceFundVault = spotMarketAccount.insuranceFundVault,
|
|
3339
|
-
_e.clearingHouseSigner = this.getSignerPublicKey(),
|
|
3340
|
-
_e.userTokenAccount = collateralAccountPublicKey,
|
|
3341
|
-
_e.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
|
|
3342
|
-
_e),
|
|
3343
|
-
_d.remainingAccounts = remainingAccounts,
|
|
3344
|
-
_d)]))];
|
|
3345
|
-
case 2: return [2 /*return*/, _f.sent()];
|
|
3346
|
-
}
|
|
3347
|
-
});
|
|
3348
|
-
});
|
|
3349
|
-
};
|
|
3350
|
-
ClearingHouse.prototype.settleRevenueToInsuranceFund = function (marketIndex) {
|
|
3351
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
3352
|
-
var spotMarketAccount, remainingAccounts, _a, _b, _c;
|
|
3353
|
-
var _d, _e;
|
|
3354
|
-
return __generator(this, function (_f) {
|
|
3355
|
-
switch (_f.label) {
|
|
3356
|
-
case 0:
|
|
3357
|
-
spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
3358
|
-
remainingAccounts = this.getRemainingAccounts({
|
|
3359
|
-
writableSpotMarketIndex: marketIndex
|
|
3360
|
-
});
|
|
3361
|
-
_b = (_a = this.program.rpc).settleRevenueToInsuranceFund;
|
|
3362
|
-
_c = [marketIndex];
|
|
3363
|
-
_d = {};
|
|
3364
|
-
_e = {};
|
|
3365
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
3366
|
-
case 1: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
3367
|
-
_e.spotMarket = spotMarketAccount.pubkey,
|
|
3368
|
-
_e.spotMarketVault = spotMarketAccount.vault,
|
|
3369
|
-
_e.clearingHouseSigner = this.getSignerPublicKey(),
|
|
3370
|
-
_e.insuranceFundVault = spotMarketAccount.insuranceFundVault,
|
|
3371
|
-
_e.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
|
|
3372
|
-
_e),
|
|
3373
|
-
_d.remainingAccounts = remainingAccounts,
|
|
3374
|
-
_d)]))];
|
|
3375
|
-
case 2: return [2 /*return*/, _f.sent()];
|
|
3376
|
-
}
|
|
3377
|
-
});
|
|
3378
|
-
});
|
|
3379
|
-
};
|
|
3380
|
-
ClearingHouse.prototype.resolvePerpPnlDeficit = function (spotMarketIndex, perpMarketIndex) {
|
|
3381
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
3382
|
-
var txSig, _a, _b, _c;
|
|
3383
|
-
return __generator(this, function (_d) {
|
|
3384
|
-
switch (_d.label) {
|
|
3385
|
-
case 0:
|
|
3386
|
-
_b = (_a = this.txSender).send;
|
|
3387
|
-
_c = utils_1.wrapInTx;
|
|
3388
|
-
return [4 /*yield*/, this.getResolvePerpPnlDeficitIx(spotMarketIndex, perpMarketIndex)];
|
|
3389
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [_c.apply(void 0, [_d.sent()]),
|
|
3390
|
-
[],
|
|
3391
|
-
this.opts])];
|
|
3392
|
-
case 2:
|
|
3393
|
-
txSig = (_d.sent()).txSig;
|
|
3394
|
-
return [2 /*return*/, txSig];
|
|
3395
|
-
}
|
|
3396
|
-
});
|
|
3397
|
-
});
|
|
3398
|
-
};
|
|
3399
|
-
ClearingHouse.prototype.getResolvePerpPnlDeficitIx = function (spotMarketIndex, perpMarketIndex) {
|
|
3400
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
3401
|
-
var remainingAccounts, spotMarket, _a, _b, _c;
|
|
3402
|
-
var _d, _e;
|
|
3403
|
-
return __generator(this, function (_f) {
|
|
3404
|
-
switch (_f.label) {
|
|
3405
|
-
case 0:
|
|
3406
|
-
remainingAccounts = this.getRemainingAccounts({
|
|
3407
|
-
writablePerpMarketIndex: perpMarketIndex,
|
|
3408
|
-
writableSpotMarketIndex: spotMarketIndex
|
|
3409
|
-
});
|
|
3410
|
-
spotMarket = this.getSpotMarketAccount(spotMarketIndex);
|
|
3411
|
-
_b = (_a = this.program.instruction).resolvePerpPnlDeficit;
|
|
3412
|
-
_c = [spotMarketIndex,
|
|
3413
|
-
perpMarketIndex];
|
|
3414
|
-
_d = {};
|
|
3415
|
-
_e = {};
|
|
3416
|
-
return [4 /*yield*/, this.getStatePublicKey()];
|
|
3417
|
-
case 1: return [4 /*yield*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.state = _f.sent(),
|
|
3418
|
-
_e.authority = this.wallet.publicKey,
|
|
3419
|
-
_e.spotMarketVault = spotMarket.vault,
|
|
3420
|
-
_e.insuranceFundVault = spotMarket.insuranceFundVault,
|
|
3421
|
-
_e.clearingHouseSigner = this.getSignerPublicKey(),
|
|
3422
|
-
_e.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
|
|
3423
|
-
_e),
|
|
3424
|
-
_d.remainingAccounts = remainingAccounts,
|
|
3425
|
-
_d)]))];
|
|
3426
|
-
case 2: return [2 /*return*/, _f.sent()];
|
|
3427
|
-
}
|
|
3428
|
-
});
|
|
3429
|
-
});
|
|
3430
|
-
};
|
|
3431
|
-
return ClearingHouse;
|
|
3432
|
-
}());
|
|
3433
|
-
exports.ClearingHouse = ClearingHouse;
|