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