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