flash-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/lib/CustodyAccount.d.ts +29 -0
  2. package/lib/CustodyAccount.d.ts.map +1 -0
  3. package/lib/CustodyAccount.js +64 -0
  4. package/lib/CustodyAccount.js.map +1 -0
  5. package/lib/OraclePrice.d.ts +18 -0
  6. package/lib/OraclePrice.d.ts.map +1 -0
  7. package/lib/OraclePrice.js +83 -0
  8. package/lib/OraclePrice.js.map +1 -0
  9. package/lib/PerpetualsClient.d.ts +1937 -0
  10. package/lib/PerpetualsClient.d.ts.map +1 -0
  11. package/lib/PerpetualsClient.js +1013 -0
  12. package/lib/PerpetualsClient.js.map +1 -0
  13. package/lib/PoolAccount.d.ts +34 -0
  14. package/lib/PoolAccount.d.ts.map +1 -0
  15. package/lib/PoolAccount.js +306 -0
  16. package/lib/PoolAccount.js.map +1 -0
  17. package/lib/PositionAccount.d.ts +26 -0
  18. package/lib/PositionAccount.d.ts.map +1 -0
  19. package/lib/PositionAccount.js +90 -0
  20. package/lib/PositionAccount.js.map +1 -0
  21. package/lib/client/src/CustodyAccount.d.ts +30 -0
  22. package/lib/client/src/CustodyAccount.d.ts.map +1 -0
  23. package/lib/client/src/CustodyAccount.js +64 -0
  24. package/lib/client/src/CustodyAccount.js.map +1 -0
  25. package/lib/client/src/OraclePrice.d.ts +19 -0
  26. package/lib/client/src/OraclePrice.d.ts.map +1 -0
  27. package/lib/client/src/OraclePrice.js +84 -0
  28. package/lib/client/src/OraclePrice.js.map +1 -0
  29. package/lib/client/src/PerpetualsClient.d.ts +1940 -0
  30. package/lib/client/src/PerpetualsClient.d.ts.map +1 -0
  31. package/lib/client/src/PerpetualsClient.js +1013 -0
  32. package/lib/client/src/PerpetualsClient.js.map +1 -0
  33. package/lib/client/src/PoolAccount.d.ts +35 -0
  34. package/lib/client/src/PoolAccount.d.ts.map +1 -0
  35. package/lib/client/src/PoolAccount.js +307 -0
  36. package/lib/client/src/PoolAccount.js.map +1 -0
  37. package/lib/client/src/PositionAccount.d.ts +27 -0
  38. package/lib/client/src/PositionAccount.d.ts.map +1 -0
  39. package/lib/client/src/PositionAccount.js +91 -0
  40. package/lib/client/src/PositionAccount.js.map +1 -0
  41. package/lib/client/src/constants/index.d.ts +12 -0
  42. package/lib/client/src/constants/index.d.ts.map +1 -0
  43. package/lib/client/src/constants/index.js +15 -0
  44. package/lib/client/src/constants/index.js.map +1 -0
  45. package/lib/client/src/index.d.ts +8 -0
  46. package/lib/client/src/index.d.ts.map +1 -0
  47. package/lib/client/src/index.js +24 -0
  48. package/lib/client/src/index.js.map +1 -0
  49. package/lib/client/src/target/types/perpetuals.d.ts +3539 -0
  50. package/lib/client/src/target/types/perpetuals.d.ts.map +1 -0
  51. package/lib/client/src/target/types/perpetuals.js +3541 -0
  52. package/lib/client/src/target/types/perpetuals.js.map +1 -0
  53. package/lib/client/src/types/index.d.ts +188 -0
  54. package/lib/client/src/types/index.d.ts.map +1 -0
  55. package/lib/client/src/types/index.js +57 -0
  56. package/lib/client/src/types/index.js.map +1 -0
  57. package/lib/client/src/utils/helpers.d.ts +8 -0
  58. package/lib/client/src/utils/helpers.d.ts.map +1 -0
  59. package/lib/client/src/utils/helpers.js +152 -0
  60. package/lib/client/src/utils/helpers.js.map +1 -0
  61. package/lib/constants/index.d.ts +11 -0
  62. package/lib/constants/index.d.ts.map +1 -0
  63. package/lib/constants/index.js +14 -0
  64. package/lib/constants/index.js.map +1 -0
  65. package/lib/index.d.ts +8 -0
  66. package/lib/index.d.ts.map +1 -0
  67. package/lib/index.js +24 -0
  68. package/lib/index.js.map +1 -0
  69. package/lib/target/types/perpetuals.d.ts +3538 -0
  70. package/lib/target/types/perpetuals.d.ts.map +1 -0
  71. package/lib/target/types/perpetuals.js +3540 -0
  72. package/lib/target/types/perpetuals.js.map +1 -0
  73. package/lib/tsconfig.tsbuildinfo +1 -0
  74. package/lib/types/index.d.ts +187 -0
  75. package/lib/types/index.d.ts.map +1 -0
  76. package/lib/types/index.js +56 -0
  77. package/lib/types/index.js.map +1 -0
  78. package/lib/utils/helpers.d.ts +7 -0
  79. package/lib/utils/helpers.d.ts.map +1 -0
  80. package/lib/utils/helpers.js +151 -0
  81. package/lib/utils/helpers.js.map +1 -0
  82. package/lib/utils/index.d.ts +7 -0
  83. package/lib/utils/index.js +151 -0
  84. package/package.json +35 -0
  85. package/readme.md +4 -0
  86. package/src/CustodyAccount.ts +90 -0
  87. package/src/OraclePrice.ts +108 -0
  88. package/src/PerpetualsClient.ts +983 -0
  89. package/src/PoolAccount.ts +434 -0
  90. package/src/PositionAccount.ts +57 -0
  91. package/src/constants/index.ts +17 -0
  92. package/src/index.ts +14 -0
  93. package/src/readme.md +4 -0
  94. package/src/target/types/perpetuals.js +3540 -0
  95. package/src/target/types/perpetuals.ts +7075 -0
  96. package/src/types/index.ts +260 -0
  97. package/src/utils/index.ts +169 -0
  98. package/tsconfig.json +23 -0
@@ -0,0 +1,1013 @@
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 (g && (g = 0, op[0] && (_ = 0)), _) 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
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.PerpetualsClient = void 0;
40
+ var anchor_1 = require("@coral-xyz/anchor");
41
+ var web3_js_1 = require("@solana/web3.js");
42
+ var spl_token_1 = require("@solana/spl-token");
43
+ var js_sha256_1 = require("js-sha256");
44
+ var bs58_1 = require("bs58");
45
+ var fs_1 = require("fs");
46
+ var PositionAccount_1 = require("./PositionAccount");
47
+ // import { Perpetuals } from "../../target/types/perpetuals"
48
+ var PerpetualsClient = /** @class */ (function () {
49
+ function PerpetualsClient(clusterUrl, adminKey) {
50
+ var _this = this;
51
+ this.findProgramAddress = function (label, extraSeeds) {
52
+ if (extraSeeds === void 0) { extraSeeds = null; }
53
+ var seeds = [Buffer.from(anchor_1.utils.bytes.utf8.encode(label))];
54
+ if (extraSeeds) {
55
+ for (var _i = 0, extraSeeds_1 = extraSeeds; _i < extraSeeds_1.length; _i++) {
56
+ var extraSeed = extraSeeds_1[_i];
57
+ if (typeof extraSeed === "string") {
58
+ seeds.push(Buffer.from(anchor_1.utils.bytes.utf8.encode(extraSeed)));
59
+ }
60
+ else if (Array.isArray(extraSeed)) {
61
+ seeds.push(Buffer.from(extraSeed));
62
+ }
63
+ else {
64
+ seeds.push(extraSeed.toBuffer());
65
+ }
66
+ }
67
+ }
68
+ var res = web3_js_1.PublicKey.findProgramAddressSync(seeds, _this.program.programId);
69
+ return { publicKey: res[0], bump: res[1] };
70
+ };
71
+ this.adjustTokenRatios = function (ratios) {
72
+ if (ratios.length == 0) {
73
+ return ratios;
74
+ }
75
+ var target = Math.floor(10000 / ratios.length);
76
+ for (var _i = 0, ratios_1 = ratios; _i < ratios_1.length; _i++) {
77
+ var ratio = ratios_1[_i];
78
+ ratio.target = new anchor_1.BN(target);
79
+ }
80
+ if (10000 % ratios.length !== 0) {
81
+ ratios[ratios.length - 1].target = new anchor_1.BN(target + (10000 % ratios.length));
82
+ }
83
+ return ratios;
84
+ };
85
+ this.getPerpetuals = function () { return __awaiter(_this, void 0, void 0, function () {
86
+ return __generator(this, function (_a) {
87
+ return [2 /*return*/, this.program.account.perpetuals.fetch(this.perpetuals.publicKey)];
88
+ });
89
+ }); };
90
+ this.getPoolKey = function (name) {
91
+ return _this.findProgramAddress("pool", name).publicKey;
92
+ };
93
+ this.getPool = function (name) { return __awaiter(_this, void 0, void 0, function () {
94
+ return __generator(this, function (_a) {
95
+ console.log("pool:", this.getPoolKey(name).toBase58());
96
+ return [2 /*return*/, this.program.account.pool.fetch(this.getPoolKey(name))];
97
+ });
98
+ }); };
99
+ this.getPools = function () { return __awaiter(_this, void 0, void 0, function () {
100
+ var perpetuals;
101
+ return __generator(this, function (_a) {
102
+ switch (_a.label) {
103
+ case 0: return [4 /*yield*/, this.getPerpetuals()];
104
+ case 1:
105
+ perpetuals = _a.sent();
106
+ return [2 /*return*/, this.program.account.pool.fetchMultiple(perpetuals.pools)];
107
+ }
108
+ });
109
+ }); };
110
+ this.getPoolLpTokenKey = function (name) {
111
+ return _this.findProgramAddress("lp_token_mint", [_this.getPoolKey(name)])
112
+ .publicKey;
113
+ };
114
+ this.getCustodyKey = function (poolName, tokenMint) {
115
+ return _this.findProgramAddress("custody", [
116
+ _this.getPoolKey(poolName),
117
+ tokenMint,
118
+ ]).publicKey;
119
+ };
120
+ this.getCustodyTokenAccountKey = function (poolName, tokenMint) {
121
+ return _this.findProgramAddress("custody_token_account", [
122
+ _this.getPoolKey(poolName),
123
+ tokenMint,
124
+ ]).publicKey;
125
+ };
126
+ this.getCustodyOracleAccountKey = function (poolName, tokenMint) { return __awaiter(_this, void 0, void 0, function () {
127
+ return __generator(this, function (_a) {
128
+ switch (_a.label) {
129
+ case 0: return [4 /*yield*/, this.getCustody(poolName, tokenMint)];
130
+ case 1: return [2 /*return*/, (_a.sent()).oracle.oracleAccount];
131
+ }
132
+ });
133
+ }); };
134
+ this.getCustodyTestOracleAccountKey = function (poolName, tokenMint) {
135
+ return _this.findProgramAddress("oracle_account", [
136
+ _this.getPoolKey(poolName),
137
+ tokenMint,
138
+ ]).publicKey;
139
+ };
140
+ this.getCustody = function (poolName, tokenMint) { return __awaiter(_this, void 0, void 0, function () {
141
+ return __generator(this, function (_a) {
142
+ console.log("custody key :", this.getCustodyKey(poolName, tokenMint).toBase58());
143
+ return [2 /*return*/, this.program.account.custody.fetch(this.getCustodyKey(poolName, tokenMint))];
144
+ });
145
+ }); };
146
+ this.getCustodies = function (poolName) { return __awaiter(_this, void 0, void 0, function () {
147
+ var pool;
148
+ return __generator(this, function (_a) {
149
+ switch (_a.label) {
150
+ case 0: return [4 /*yield*/, this.getPool(poolName)];
151
+ case 1:
152
+ pool = _a.sent();
153
+ return [2 /*return*/, this.program.account.custody.fetchMultiple(pool.custodies)];
154
+ }
155
+ });
156
+ }); };
157
+ this.getCustodyMetas = function (poolName) { return __awaiter(_this, void 0, void 0, function () {
158
+ var pool, custodies, custodyMetas, _i, _a, custody, _b, custodies_1, custody;
159
+ return __generator(this, function (_c) {
160
+ switch (_c.label) {
161
+ case 0: return [4 /*yield*/, this.getPool(poolName)];
162
+ case 1:
163
+ pool = _c.sent();
164
+ return [4 /*yield*/, this.program.account.custody.fetchMultiple(pool.custodies)];
165
+ case 2:
166
+ custodies = _c.sent();
167
+ custodyMetas = [];
168
+ for (_i = 0, _a = pool.custodies; _i < _a.length; _i++) {
169
+ custody = _a[_i];
170
+ custodyMetas.push({
171
+ isSigner: false,
172
+ isWritable: false,
173
+ pubkey: custody,
174
+ });
175
+ }
176
+ for (_b = 0, custodies_1 = custodies; _b < custodies_1.length; _b++) {
177
+ custody = custodies_1[_b];
178
+ custodyMetas.push({
179
+ isSigner: false,
180
+ isWritable: false,
181
+ pubkey: custody === null || custody === void 0 ? void 0 : custody.oracle.oracleAccount,
182
+ });
183
+ }
184
+ return [2 /*return*/, custodyMetas];
185
+ }
186
+ });
187
+ }); };
188
+ this.getMultisig = function () { return __awaiter(_this, void 0, void 0, function () {
189
+ return __generator(this, function (_a) {
190
+ return [2 /*return*/, this.program.account.multisig.fetch(this.multisig.publicKey)];
191
+ });
192
+ }); };
193
+ this.getPositionKey = function (wallet, poolName, tokenMint, side) {
194
+ var pool = _this.getPoolKey(poolName);
195
+ var custody = _this.getCustodyKey(poolName, tokenMint);
196
+ return _this.findProgramAddress("position", [
197
+ wallet,
198
+ pool,
199
+ custody,
200
+ side === "long" ? [1] : [0],
201
+ ]).publicKey;
202
+ };
203
+ this.getPosition = function (postionKey) { return __awaiter(_this, void 0, void 0, function () {
204
+ return __generator(this, function (_a) {
205
+ return [2 /*return*/, this.program.account.position.fetch(postionKey)];
206
+ });
207
+ }); };
208
+ this.getUserPosition = function (wallet, poolName, tokenMint, side) { return __awaiter(_this, void 0, void 0, function () {
209
+ return __generator(this, function (_a) {
210
+ return [2 /*return*/, this.program.account.position.fetch(this.getPositionKey(wallet, poolName, tokenMint, side))];
211
+ });
212
+ }); };
213
+ this.getUserPositions = function (wallet) { return __awaiter(_this, void 0, void 0, function () {
214
+ var data, positions;
215
+ var _this = this;
216
+ return __generator(this, function (_a) {
217
+ switch (_a.label) {
218
+ case 0:
219
+ data = (0, bs58_1.encode)(Buffer.concat([
220
+ this.getAccountDiscriminator("Position"),
221
+ wallet.toBuffer(),
222
+ ]));
223
+ return [4 /*yield*/, this.provider.connection.getProgramAccounts(this.program.programId, {
224
+ filters: [{ dataSize: 200 }, { memcmp: { bytes: data, offset: 0 } }],
225
+ })];
226
+ case 1:
227
+ positions = _a.sent();
228
+ return [2 /*return*/, Promise.all(positions.map(function (position) {
229
+ return _this.program.account.position.fetch(position.pubkey);
230
+ }))];
231
+ }
232
+ });
233
+ }); };
234
+ this.getPoolTokenPositions = function (poolName, tokenMint) { return __awaiter(_this, void 0, void 0, function () {
235
+ var poolKey, custodyKey, data, positions;
236
+ var _this = this;
237
+ return __generator(this, function (_a) {
238
+ switch (_a.label) {
239
+ case 0:
240
+ poolKey = this.getPoolKey(poolName);
241
+ custodyKey = this.getCustodyKey(poolName, tokenMint);
242
+ data = (0, bs58_1.encode)(Buffer.concat([poolKey.toBuffer(), custodyKey.toBuffer()]));
243
+ return [4 /*yield*/, this.provider.connection.getProgramAccounts(this.program.programId, {
244
+ filters: [{ dataSize: 200 }, { memcmp: { bytes: data, offset: 40 } }],
245
+ })];
246
+ case 1:
247
+ positions = _a.sent();
248
+ return [2 /*return*/, Promise.all(positions.map(function (position) {
249
+ return _this.program.account.position.fetch(position.pubkey);
250
+ }))];
251
+ }
252
+ });
253
+ }); };
254
+ this.getAllPositions = function () { return __awaiter(_this, void 0, void 0, function () {
255
+ return __generator(this, function (_a) {
256
+ return [2 /*return*/, this.program.account.position.all()];
257
+ });
258
+ }); };
259
+ this.getAccountDiscriminator = function (name) {
260
+ return Buffer.from(js_sha256_1.sha256.digest("account:".concat(name))).slice(0, 8);
261
+ };
262
+ this.log = function () {
263
+ var message = [];
264
+ for (var _i = 0; _i < arguments.length; _i++) {
265
+ message[_i] = arguments[_i];
266
+ }
267
+ var date = new Date();
268
+ var date_str = date.toDateString();
269
+ var time = date.toLocaleTimeString();
270
+ console.log("[".concat(date_str, " ").concat(time, "] ").concat(message));
271
+ };
272
+ this.prettyPrint = function (object) {
273
+ console.log(JSON.stringify(object, null, 2));
274
+ };
275
+ ///////
276
+ // instructions
277
+ this.init = function (admins, config) { return __awaiter(_this, void 0, void 0, function () {
278
+ var perpetualsProgramData, adminMetas, _i, admins_1, admin;
279
+ return __generator(this, function (_a) {
280
+ switch (_a.label) {
281
+ case 0:
282
+ perpetualsProgramData = web3_js_1.PublicKey.findProgramAddressSync([this.program.programId.toBuffer()], new web3_js_1.PublicKey("BPFLoaderUpgradeab1e11111111111111111111111"))[0];
283
+ adminMetas = [];
284
+ for (_i = 0, admins_1 = admins; _i < admins_1.length; _i++) {
285
+ admin = admins_1[_i];
286
+ adminMetas.push({
287
+ isSigner: false,
288
+ isWritable: false,
289
+ pubkey: admin,
290
+ });
291
+ }
292
+ return [4 /*yield*/, this.program.methods
293
+ .init(config)
294
+ .accounts({
295
+ upgradeAuthority: this.provider.wallet.publicKey,
296
+ multisig: this.multisig.publicKey,
297
+ transferAuthority: this.authority.publicKey,
298
+ perpetuals: this.perpetuals.publicKey,
299
+ perpetualsProgram: this.program.programId,
300
+ perpetualsProgramData: perpetualsProgramData,
301
+ systemProgram: web3_js_1.SystemProgram.programId,
302
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
303
+ })
304
+ .remainingAccounts(adminMetas)
305
+ .rpc()
306
+ .catch(function (err) {
307
+ console.error(err);
308
+ throw err;
309
+ })];
310
+ case 1:
311
+ _a.sent();
312
+ return [2 /*return*/];
313
+ }
314
+ });
315
+ }); };
316
+ this.setAdminSigners = function (admins, minSignatures) { return __awaiter(_this, void 0, void 0, function () {
317
+ var adminMetas, _i, admins_2, admin, err_1;
318
+ return __generator(this, function (_a) {
319
+ switch (_a.label) {
320
+ case 0:
321
+ adminMetas = [];
322
+ for (_i = 0, admins_2 = admins; _i < admins_2.length; _i++) {
323
+ admin = admins_2[_i];
324
+ adminMetas.push({
325
+ isSigner: false,
326
+ isWritable: false,
327
+ pubkey: admin,
328
+ });
329
+ }
330
+ _a.label = 1;
331
+ case 1:
332
+ _a.trys.push([1, 3, , 4]);
333
+ return [4 /*yield*/, this.program.methods
334
+ .setAdminSigners({
335
+ minSignatures: minSignatures,
336
+ })
337
+ .accounts({
338
+ admin: this.admin.publicKey,
339
+ multisig: this.multisig.publicKey,
340
+ })
341
+ .remainingAccounts(adminMetas)
342
+ .signers([this.admin])
343
+ .rpc()];
344
+ case 2:
345
+ _a.sent();
346
+ return [3 /*break*/, 4];
347
+ case 3:
348
+ err_1 = _a.sent();
349
+ // @ts-ignore
350
+ if (this.printErrors) {
351
+ console.log(err_1);
352
+ }
353
+ throw err_1;
354
+ case 4: return [2 /*return*/];
355
+ }
356
+ });
357
+ }); };
358
+ this.addPool = function (name) { return __awaiter(_this, void 0, void 0, function () {
359
+ return __generator(this, function (_a) {
360
+ switch (_a.label) {
361
+ case 0: return [4 /*yield*/, this.program.methods
362
+ .addPool({ name: name })
363
+ .accounts({
364
+ admin: this.admin.publicKey,
365
+ multisig: this.multisig.publicKey,
366
+ transferAuthority: this.authority.publicKey,
367
+ perpetuals: this.perpetuals.publicKey,
368
+ pool: this.getPoolKey(name),
369
+ lpTokenMint: this.getPoolLpTokenKey(name),
370
+ systemProgram: web3_js_1.SystemProgram.programId,
371
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
372
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
373
+ })
374
+ .signers([this.admin])
375
+ .rpc()
376
+ .catch(function (err) {
377
+ console.error(err);
378
+ throw err;
379
+ })];
380
+ case 1:
381
+ _a.sent();
382
+ return [2 /*return*/];
383
+ }
384
+ });
385
+ }); };
386
+ this.removePool = function (name) { return __awaiter(_this, void 0, void 0, function () {
387
+ return __generator(this, function (_a) {
388
+ switch (_a.label) {
389
+ case 0: return [4 /*yield*/, this.program.methods
390
+ .removePool({})
391
+ .accounts({
392
+ admin: this.admin.publicKey,
393
+ multisig: this.multisig.publicKey,
394
+ transferAuthority: this.authority.publicKey,
395
+ perpetuals: this.perpetuals.publicKey,
396
+ pool: this.getPoolKey(name),
397
+ systemProgram: web3_js_1.SystemProgram.programId,
398
+ })
399
+ .signers([this.admin])
400
+ .rpc()
401
+ .catch(function (err) {
402
+ console.error(err);
403
+ throw err;
404
+ })];
405
+ case 1:
406
+ _a.sent();
407
+ return [2 /*return*/];
408
+ }
409
+ });
410
+ }); };
411
+ this.addCustody = function (poolName, tokenMint, isStable, oracle, pricing, permissions, fees, borrowRate, ratios) { return __awaiter(_this, void 0, void 0, function () {
412
+ var trx_id, error_1;
413
+ return __generator(this, function (_a) {
414
+ switch (_a.label) {
415
+ case 0:
416
+ console.log("CustodyKey", this.getCustodyKey(poolName, tokenMint).toBase58());
417
+ console.log("getCustodyTokenAccountKey", this.getCustodyTokenAccountKey(poolName, tokenMint).toBase58());
418
+ _a.label = 1;
419
+ case 1:
420
+ _a.trys.push([1, 3, , 4]);
421
+ return [4 /*yield*/, this.program.methods
422
+ // @ts-ignore
423
+ .addCustody({
424
+ isStable: isStable,
425
+ oracle: oracle,
426
+ pricing: pricing,
427
+ permissions: permissions,
428
+ fees: fees,
429
+ borrowRate: borrowRate,
430
+ ratios: ratios,
431
+ })
432
+ .accounts({
433
+ admin: this.admin.publicKey,
434
+ multisig: this.multisig.publicKey,
435
+ transferAuthority: this.authority.publicKey,
436
+ perpetuals: this.perpetuals.publicKey,
437
+ pool: this.getPoolKey(poolName),
438
+ custody: this.getCustodyKey(poolName, tokenMint),
439
+ custodyTokenAccount: this.getCustodyTokenAccountKey(poolName, tokenMint),
440
+ custodyTokenMint: tokenMint,
441
+ systemProgram: web3_js_1.SystemProgram.programId,
442
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
443
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
444
+ })
445
+ .signers([this.admin])
446
+ .rpc()
447
+ .catch(function (err) {
448
+ console.error(err);
449
+ throw err;
450
+ })];
451
+ case 2:
452
+ trx_id = _a.sent();
453
+ console.log("trx_id:", "https://explorer.solana.com/tx/".concat(trx_id, "?cluster=devnet"));
454
+ return [3 /*break*/, 4];
455
+ case 3:
456
+ error_1 = _a.sent();
457
+ console.log("cli error :", error_1);
458
+ return [3 /*break*/, 4];
459
+ case 4: return [2 /*return*/];
460
+ }
461
+ });
462
+ }); };
463
+ this.editCustody = function (poolName, tokenMint, isStable, oracle, pricing, permissions, fees, borrowRate, ratios) { return __awaiter(_this, void 0, void 0, function () {
464
+ var trx_id;
465
+ return __generator(this, function (_a) {
466
+ switch (_a.label) {
467
+ case 0:
468
+ console.log("CustodyKey", this.getCustodyKey(poolName, tokenMint).toBase58());
469
+ console.log("getCustodyTokenAccountKey", this.getCustodyTokenAccountKey(poolName, tokenMint).toBase58());
470
+ return [4 /*yield*/, this.program.methods
471
+ //@ts-ignore
472
+ .testingEditCustody({
473
+ isStable: isStable,
474
+ oracle: oracle,
475
+ pricing: pricing,
476
+ permissions: permissions,
477
+ fees: fees,
478
+ borrowRate: borrowRate,
479
+ ratios: ratios,
480
+ })
481
+ .accounts({
482
+ admin: this.admin.publicKey,
483
+ multisig: this.multisig.publicKey,
484
+ transferAuthority: this.authority.publicKey,
485
+ perpetuals: this.perpetuals.publicKey,
486
+ pool: this.getPoolKey(poolName),
487
+ custody: this.getCustodyKey(poolName, tokenMint),
488
+ custodyTokenAccount: this.getCustodyTokenAccountKey(poolName, tokenMint),
489
+ custodyTokenMint: tokenMint,
490
+ systemProgram: web3_js_1.SystemProgram.programId,
491
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
492
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
493
+ })
494
+ .signers([this.admin])
495
+ .rpc()
496
+ .catch(function (err) {
497
+ console.error(err);
498
+ throw err;
499
+ })];
500
+ case 1:
501
+ trx_id = _a.sent();
502
+ console.log("trx_id:", "https://explorer.solana.com/tx/".concat(trx_id, "?cluster=devnet"));
503
+ return [2 /*return*/];
504
+ }
505
+ });
506
+ }); };
507
+ this.removeCustody = function (poolName, tokenMint, ratios) { return __awaiter(_this, void 0, void 0, function () {
508
+ return __generator(this, function (_a) {
509
+ switch (_a.label) {
510
+ case 0: return [4 /*yield*/, this.program.methods
511
+ .removeCustody({ ratios: ratios })
512
+ .accounts({
513
+ admin: this.admin.publicKey,
514
+ multisig: this.multisig.publicKey,
515
+ transferAuthority: this.authority.publicKey,
516
+ perpetuals: this.perpetuals.publicKey,
517
+ pool: this.getPoolKey(poolName),
518
+ custody: this.getCustodyKey(poolName, tokenMint),
519
+ custodyTokenAccount: this.getCustodyTokenAccountKey(poolName, tokenMint),
520
+ systemProgram: web3_js_1.SystemProgram.programId,
521
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
522
+ })
523
+ .signers([this.admin])
524
+ .rpc()
525
+ .catch(function (err) {
526
+ console.error(err);
527
+ throw err;
528
+ })];
529
+ case 1:
530
+ _a.sent();
531
+ return [2 /*return*/];
532
+ }
533
+ });
534
+ }); };
535
+ this.upgradeCustody = function (poolName, tokenMint) { return __awaiter(_this, void 0, void 0, function () {
536
+ return __generator(this, function (_a) {
537
+ switch (_a.label) {
538
+ case 0: return [4 /*yield*/, this.program.methods
539
+ .upgradeCustody({})
540
+ .accounts({
541
+ admin: this.admin.publicKey,
542
+ multisig: this.multisig.publicKey,
543
+ pool: this.getPoolKey(poolName),
544
+ custody: this.getCustodyKey(poolName, tokenMint),
545
+ systemProgram: web3_js_1.SystemProgram.programId,
546
+ })
547
+ .signers([this.admin])
548
+ .rpc()
549
+ .catch(function (err) {
550
+ console.error(err);
551
+ throw err;
552
+ })];
553
+ case 1:
554
+ _a.sent();
555
+ return [2 /*return*/];
556
+ }
557
+ });
558
+ }); };
559
+ this.liquidate = function (wallet, poolName, tokenMint, side, receivingAccount, rewardsReceivingAccount) { return __awaiter(_this, void 0, void 0, function () {
560
+ var _a, _b;
561
+ var _c;
562
+ return __generator(this, function (_d) {
563
+ switch (_d.label) {
564
+ case 0:
565
+ _b = (_a = this.program.methods
566
+ .liquidate({}))
567
+ .accounts;
568
+ _c = {
569
+ signer: this.provider.wallet.publicKey,
570
+ receivingAccount: receivingAccount,
571
+ rewardsReceivingAccount: rewardsReceivingAccount,
572
+ transferAuthority: this.authority.publicKey,
573
+ perpetuals: this.perpetuals.publicKey,
574
+ pool: this.getPoolKey(poolName),
575
+ position: this.getPositionKey(wallet, poolName, tokenMint, side),
576
+ custody: this.getCustodyKey(poolName, tokenMint)
577
+ };
578
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
579
+ case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
580
+ _c.custodyTokenAccount = this.getCustodyTokenAccountKey(poolName, tokenMint),
581
+ _c.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
582
+ _c)])
583
+ .rpc()
584
+ .catch(function (err) {
585
+ console.error(err);
586
+ throw err;
587
+ })];
588
+ case 2: return [2 /*return*/, _d.sent()];
589
+ }
590
+ });
591
+ }); };
592
+ this.getOraclePrice = function (poolName, tokenMint, ema) { return __awaiter(_this, void 0, void 0, function () {
593
+ var _a, _b;
594
+ var _c;
595
+ return __generator(this, function (_d) {
596
+ switch (_d.label) {
597
+ case 0:
598
+ _b = (_a = this.program.methods
599
+ .getOraclePrice({
600
+ ema: ema,
601
+ }))
602
+ .accounts;
603
+ _c = {
604
+ // signer: this.provider.wallet.publicKey,
605
+ perpetuals: this.perpetuals.publicKey,
606
+ pool: this.getPoolKey(poolName),
607
+ custody: this.getCustodyKey(poolName, tokenMint)
608
+ };
609
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
610
+ case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
611
+ _c)])
612
+ .view()
613
+ .catch(function (err) {
614
+ console.error(err);
615
+ throw err;
616
+ })];
617
+ case 2: return [2 /*return*/, _d.sent()];
618
+ }
619
+ });
620
+ }); };
621
+ this.getAddLiquidityAmountAndFee = function (poolName, tokenMint, amount) { return __awaiter(_this, void 0, void 0, function () {
622
+ var _a, _b, _c, _d;
623
+ var _e;
624
+ return __generator(this, function (_f) {
625
+ switch (_f.label) {
626
+ case 0:
627
+ _c = (_b = this.program.methods
628
+ .getAddLiquidityAmountAndFee({
629
+ amountIn: amount,
630
+ }))
631
+ .accounts;
632
+ _e = {
633
+ perpetuals: this.perpetuals.publicKey,
634
+ pool: this.getPoolKey(poolName),
635
+ custody: this.getCustodyKey(poolName, tokenMint)
636
+ };
637
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
638
+ case 1:
639
+ _d = (_a = _c.apply(_b, [(_e.custodyOracleAccount = _f.sent(),
640
+ _e.lpTokenMint = this.getPoolLpTokenKey(poolName),
641
+ _e)]))
642
+ .remainingAccounts;
643
+ return [4 /*yield*/, this.getCustodyMetas(poolName)];
644
+ case 2: return [4 /*yield*/, _d.apply(_a, [_f.sent()])
645
+ .view()
646
+ .catch(function (err) {
647
+ console.error(err);
648
+ throw err;
649
+ })];
650
+ case 3: return [2 /*return*/, _f.sent()];
651
+ }
652
+ });
653
+ }); };
654
+ this.getRemoveLiquidityAmountAndFee = function (poolName, tokenMint, lpAmount) { return __awaiter(_this, void 0, void 0, function () {
655
+ var _a, _b, _c, _d;
656
+ var _e;
657
+ return __generator(this, function (_f) {
658
+ switch (_f.label) {
659
+ case 0:
660
+ _c = (_b = this.program.methods
661
+ .getRemoveLiquidityAmountAndFee({
662
+ lpAmountIn: lpAmount,
663
+ }))
664
+ .accounts;
665
+ _e = {
666
+ perpetuals: this.perpetuals.publicKey,
667
+ pool: this.getPoolKey(poolName),
668
+ custody: this.getCustodyKey(poolName, tokenMint)
669
+ };
670
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
671
+ case 1:
672
+ _d = (_a = _c.apply(_b, [(_e.custodyOracleAccount = _f.sent(),
673
+ _e.lpTokenMint = this.getPoolLpTokenKey(poolName),
674
+ _e)]))
675
+ .remainingAccounts;
676
+ return [4 /*yield*/, this.getCustodyMetas(poolName)];
677
+ case 2: return [4 /*yield*/, _d.apply(_a, [_f.sent()])
678
+ .view()
679
+ .catch(function (err) {
680
+ console.error(err);
681
+ throw err;
682
+ })];
683
+ case 3: return [2 /*return*/, _f.sent()];
684
+ }
685
+ });
686
+ }); };
687
+ this.getEntryPriceAndFee = function (poolName, tokenMint, collateral, size, side) { return __awaiter(_this, void 0, void 0, function () {
688
+ var _a, _b, _c, _d, _e;
689
+ var _f;
690
+ return __generator(this, function (_g) {
691
+ switch (_g.label) {
692
+ case 0:
693
+ console.log("perps: ", this.perpetuals.publicKey.toBase58());
694
+ console.log("poolKey: ", this.getPoolKey(poolName).toBase58());
695
+ console.log("custody key : ", this.getCustodyKey(poolName, tokenMint).toBase58());
696
+ _b = (_a = console).log;
697
+ _c = ["orcalve: "];
698
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
699
+ case 1:
700
+ _b.apply(_a, _c.concat([(_g.sent()).toBase58()]));
701
+ _e = (_d = this.program.methods
702
+ //@ts-ignore
703
+ .getEntryPriceAndFee({
704
+ collateral: collateral,
705
+ size: size,
706
+ side: side == "long" ? { long: {} } : { short: {} },
707
+ }))
708
+ .accounts;
709
+ _f = {
710
+ // signer: this.provider.wallet.publicKey,
711
+ perpetuals: this.perpetuals.publicKey,
712
+ pool: this.getPoolKey(poolName),
713
+ custody: this.getCustodyKey(poolName, tokenMint)
714
+ };
715
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
716
+ case 2: return [4 /*yield*/, _e.apply(_d, [(_f.custodyOracleAccount = _g.sent(),
717
+ _f)])
718
+ .view()
719
+ .catch(function (err) {
720
+ console.error(err);
721
+ throw err;
722
+ })];
723
+ case 3:
724
+ //@ts-ignore
725
+ return [2 /*return*/, _g.sent()];
726
+ }
727
+ });
728
+ }); };
729
+ this.getExitPriceAndFee = function (wallet, poolName, tokenMint, side) { return __awaiter(_this, void 0, void 0, function () {
730
+ var _a, _b;
731
+ var _c;
732
+ return __generator(this, function (_d) {
733
+ switch (_d.label) {
734
+ case 0:
735
+ _b = (_a = this.program.methods
736
+ .getExitPriceAndFee({}))
737
+ .accounts;
738
+ _c = {
739
+ perpetuals: this.perpetuals.publicKey,
740
+ pool: this.getPoolKey(poolName),
741
+ position: this.getPositionKey(wallet, poolName, tokenMint, side),
742
+ custody: this.getCustodyKey(poolName, tokenMint)
743
+ };
744
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
745
+ case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
746
+ _c)])
747
+ .view()
748
+ .catch(function (err) {
749
+ console.error(err);
750
+ throw err;
751
+ })];
752
+ case 2: return [2 /*return*/, _d.sent()];
753
+ }
754
+ });
755
+ }); };
756
+ this.getLiquidationPrice = function (wallet, poolName, tokenMint, side, addCollateral, removeCollateral) { return __awaiter(_this, void 0, void 0, function () {
757
+ var _a, _b;
758
+ var _c;
759
+ return __generator(this, function (_d) {
760
+ switch (_d.label) {
761
+ case 0:
762
+ _b = (_a = this.program.methods
763
+ .getLiquidationPrice({
764
+ addCollateral: addCollateral,
765
+ removeCollateral: removeCollateral,
766
+ }))
767
+ .accounts;
768
+ _c = {
769
+ perpetuals: this.perpetuals.publicKey,
770
+ pool: this.getPoolKey(poolName),
771
+ position: this.getPositionKey(wallet, poolName, tokenMint, side),
772
+ custody: this.getCustodyKey(poolName, tokenMint)
773
+ };
774
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
775
+ case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
776
+ _c)])
777
+ .view()
778
+ .catch(function (err) {
779
+ console.error(err);
780
+ throw err;
781
+ })];
782
+ case 2: return [2 /*return*/, _d.sent()];
783
+ }
784
+ });
785
+ }); };
786
+ this.getLiquidationPriceTrx = function (wallet, poolName, tokenMint, side, addCollateral, removeCollateral) { return __awaiter(_this, void 0, void 0, function () {
787
+ var _a, _b;
788
+ var _c;
789
+ return __generator(this, function (_d) {
790
+ switch (_d.label) {
791
+ case 0:
792
+ _b = (_a = this.program.methods
793
+ .getLiquidationPrice({
794
+ addCollateral: addCollateral,
795
+ removeCollateral: removeCollateral,
796
+ }))
797
+ .accounts;
798
+ _c = {
799
+ perpetuals: this.perpetuals.publicKey,
800
+ pool: this.getPoolKey(poolName),
801
+ position: this.getPositionKey(wallet, poolName, tokenMint, side),
802
+ custody: this.getCustodyKey(poolName, tokenMint)
803
+ };
804
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
805
+ case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
806
+ _c)])
807
+ .rpc()
808
+ .catch(function (err) {
809
+ console.error(err);
810
+ throw err;
811
+ })];
812
+ case 2: return [2 /*return*/, _d.sent()];
813
+ }
814
+ });
815
+ }); };
816
+ this.getLiquidationPrice2 = function (wallet, poolName, tokenMint, side, addCollateral, removeCollateral) { return __awaiter(_this, void 0, void 0, function () {
817
+ var _a, _b;
818
+ var _c;
819
+ return __generator(this, function (_d) {
820
+ switch (_d.label) {
821
+ case 0:
822
+ _b = (_a = this.program.methods
823
+ .getLiquidationPrice({
824
+ addCollateral: addCollateral,
825
+ removeCollateral: removeCollateral,
826
+ }))
827
+ .accounts;
828
+ _c = {
829
+ perpetuals: this.perpetuals.publicKey,
830
+ pool: this.getPoolKey(poolName),
831
+ position: this.getPositionKey(wallet, poolName, tokenMint, side),
832
+ custody: this.getCustodyKey(poolName, tokenMint)
833
+ };
834
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
835
+ case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
836
+ _c)])
837
+ .view()
838
+ .catch(function (err) {
839
+ console.error(err);
840
+ throw err;
841
+ })];
842
+ case 2: return [2 /*return*/, _d.sent()];
843
+ }
844
+ });
845
+ }); };
846
+ this.getLiquidationState = function (wallet, poolName, tokenMint, side) { return __awaiter(_this, void 0, void 0, function () {
847
+ var _a, _b;
848
+ var _c;
849
+ return __generator(this, function (_d) {
850
+ switch (_d.label) {
851
+ case 0:
852
+ _b = (_a = this.program.methods
853
+ .getLiquidationState({}))
854
+ .accounts;
855
+ _c = {
856
+ perpetuals: this.perpetuals.publicKey,
857
+ pool: this.getPoolKey(poolName),
858
+ position: this.getPositionKey(wallet, poolName, tokenMint, side),
859
+ custody: this.getCustodyKey(poolName, tokenMint)
860
+ };
861
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
862
+ case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
863
+ _c)])
864
+ .view()
865
+ .catch(function (err) {
866
+ console.error(err);
867
+ throw err;
868
+ })];
869
+ case 2: return [2 /*return*/, _d.sent()];
870
+ }
871
+ });
872
+ }); };
873
+ this.getPnl = function (wallet, poolName, tokenMint, side) { return __awaiter(_this, void 0, void 0, function () {
874
+ var pos, _a, _b;
875
+ var _c;
876
+ return __generator(this, function (_d) {
877
+ switch (_d.label) {
878
+ case 0:
879
+ pos = this.getPositionKey(wallet, poolName, tokenMint, side);
880
+ console.log("pos:", pos.toBase58());
881
+ _b = (_a = this.program.methods
882
+ .getPnl({}))
883
+ .accounts;
884
+ _c = {
885
+ perpetuals: this.perpetuals.publicKey,
886
+ pool: this.getPoolKey(poolName),
887
+ position: pos,
888
+ custody: this.getCustodyKey(poolName, tokenMint)
889
+ };
890
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
891
+ case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
892
+ _c)])
893
+ .view()
894
+ .catch(function (err) {
895
+ console.error(err);
896
+ throw err;
897
+ })];
898
+ case 2: return [2 /*return*/, _d.sent()];
899
+ }
900
+ });
901
+ }); };
902
+ this.getPnlTrx = function (wallet, poolName, tokenMint, side) { return __awaiter(_this, void 0, void 0, function () {
903
+ var pos, _a, _b;
904
+ var _c;
905
+ return __generator(this, function (_d) {
906
+ switch (_d.label) {
907
+ case 0:
908
+ pos = this.getPositionKey(wallet, poolName, tokenMint, side);
909
+ console.log("pos:", pos.toBase58());
910
+ _b = (_a = this.program.methods
911
+ .getPnl({}))
912
+ .accounts;
913
+ _c = {
914
+ perpetuals: this.perpetuals.publicKey,
915
+ pool: this.getPoolKey(poolName),
916
+ position: pos,
917
+ custody: this.getCustodyKey(poolName, tokenMint)
918
+ };
919
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
920
+ case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
921
+ _c)])
922
+ .rpc()
923
+ .catch(function (err) {
924
+ console.error(err);
925
+ throw err;
926
+ })];
927
+ case 2: return [2 /*return*/, _d.sent()];
928
+ }
929
+ });
930
+ }); };
931
+ this.getPnl2 = function (postionKey, postionData, tokenPrice, tokenEmaPrice, custodyAccount, poolAccount, currentTime) {
932
+ var positionAccount = PositionAccount_1.PositionAccount.from(postionKey, postionData);
933
+ // console.log("positionAccount:",positionAccount);
934
+ // console.log("side :", postionData.side, (isVariant(postionData.side, 'long')))
935
+ var _a = poolAccount.getPnlUsd(positionAccount, tokenPrice, tokenEmaPrice, custodyAccount, currentTime, false), profit = _a.profit, loss = _a.loss, exitFee = _a.exitFee;
936
+ return {
937
+ profit: profit,
938
+ loss: loss
939
+ };
940
+ };
941
+ this.getSwapAmountAndFees = function (poolName, tokenMintIn, tokenMintOut, amountIn) { return __awaiter(_this, void 0, void 0, function () {
942
+ var _a, _b;
943
+ var _c;
944
+ return __generator(this, function (_d) {
945
+ switch (_d.label) {
946
+ case 0:
947
+ _b = (_a = this.program.methods
948
+ .getSwapAmountAndFees({
949
+ amountIn: amountIn,
950
+ }))
951
+ .accounts;
952
+ _c = {
953
+ perpetuals: this.perpetuals.publicKey,
954
+ pool: this.getPoolKey(poolName),
955
+ receivingCustody: this.getCustodyKey(poolName, tokenMintIn)
956
+ };
957
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMintIn)];
958
+ case 1:
959
+ _c.receivingCustodyOracleAccount = _d.sent(),
960
+ _c.dispensingCustody = this.getCustodyKey(poolName, tokenMintOut);
961
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMintOut)];
962
+ case 2: return [4 /*yield*/, _b.apply(_a, [(_c.dispensingCustodyOracleAccount = _d.sent(),
963
+ _c)])
964
+ .view()
965
+ .catch(function (err) {
966
+ console.error(err);
967
+ throw err;
968
+ })];
969
+ case 3: return [2 /*return*/, _d.sent()];
970
+ }
971
+ });
972
+ }); };
973
+ this.getAum = function (poolName) { return __awaiter(_this, void 0, void 0, function () {
974
+ var _a, _b;
975
+ return __generator(this, function (_c) {
976
+ switch (_c.label) {
977
+ case 0:
978
+ _b = (_a = this.program.methods
979
+ .getAssetsUnderManagement({})
980
+ .accounts({
981
+ perpetuals: this.perpetuals.publicKey,
982
+ pool: this.getPoolKey(poolName),
983
+ }))
984
+ .remainingAccounts;
985
+ return [4 /*yield*/, this.getCustodyMetas(poolName)];
986
+ case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent()])
987
+ .view()
988
+ .catch(function (err) {
989
+ console.error(err);
990
+ throw err;
991
+ })];
992
+ case 2: return [2 /*return*/, _c.sent()];
993
+ }
994
+ });
995
+ }); };
996
+ this.provider = anchor_1.AnchorProvider.local(clusterUrl, {
997
+ commitment: "confirmed",
998
+ preflightCommitment: "confirmed",
999
+ skipPreflight: true
1000
+ });
1001
+ (0, anchor_1.setProvider)(this.provider);
1002
+ this.program = anchor_1.workspace.Perpetuals;
1003
+ this.admin = web3_js_1.Keypair.fromSecretKey(new Uint8Array(JSON.parse((0, fs_1.readFileSync)(adminKey).toString())));
1004
+ this.multisig = this.findProgramAddress("multisig");
1005
+ this.authority = this.findProgramAddress("transfer_authority");
1006
+ this.perpetuals = this.findProgramAddress("perpetuals");
1007
+ anchor_1.BN.prototype.toJSON = function () {
1008
+ return this.toString(10);
1009
+ };
1010
+ }
1011
+ return PerpetualsClient;
1012
+ }());
1013
+ exports.PerpetualsClient = PerpetualsClient;