flash-sdk 1.0.16 → 1.0.18
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/PerpetualsClient.d.ts +2 -0
- package/lib/PerpetualsClient.js +259 -109
- package/lib/PoolConfig.d.ts +1 -0
- package/lib/PoolConfig.js +3 -2
- package/lib/PoolConfig.json +4 -0
- package/lib/idl/perpetuals.d.ts +165 -41
- package/lib/idl/perpetuals.js +165 -41
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/PerpetualsClient.ts +245 -57
- package/src/PoolConfig.json +4 -0
- package/src/PoolConfig.ts +3 -1
- package/src/idl/perpetuals.ts +330 -82
@@ -1957,6 +1957,8 @@ export declare class PerpetualsClient {
|
|
1957
1957
|
instructions: TransactionInstruction[];
|
1958
1958
|
additionalSigners: Signer[];
|
1959
1959
|
}>;
|
1960
|
+
openPositionOld: (payTokenSymbol: string, priceAfterSlippage: BN, collateral: BN, fee: BN, size: BN, side: Side, poolConfig: PoolConfig, createUserWSOLATA?: boolean) => Promise<TransactionInstruction[]>;
|
1961
|
+
closePositionOld: (receivingTokenSymbol: string, priceAfterSlippage: BN, side: Side, poolConfig: PoolConfig) => Promise<TransactionInstruction[]>;
|
1960
1962
|
swap: (poolReceivingTokenSymbol: string, poolDispensingTokenSymbol: string, amountIn: BN, minAmountOut: BN, poolConfig: PoolConfig, unWrapSol?: boolean) => Promise<TransactionInstruction[]>;
|
1961
1963
|
addLiquidity: (payTokenSymbol: string, tokenAmountIn: BN, minLpAmountOut: BN, poolConfig: PoolConfig) => Promise<TransactionInstruction[]>;
|
1962
1964
|
removeLiquidity: (recieveTokenSymbol: string, liquidityAmountIn: BN, minTokenAmountOut: BN, poolConfig: PoolConfig, closeLpATA?: boolean) => Promise<TransactionInstruction[]>;
|
package/lib/PerpetualsClient.js
CHANGED
@@ -116,7 +116,7 @@ var PerpetualsClient = /** @class */ (function () {
|
|
116
116
|
};
|
117
117
|
this.getPool = function (name) { return __awaiter(_this, void 0, void 0, function () {
|
118
118
|
return __generator(this, function (_a) {
|
119
|
-
console.log("pool:", this.getPoolKey(name).toBase58())
|
119
|
+
// console.log("pool:", this.getPoolKey(name).toBase58())
|
120
120
|
return [2 /*return*/, this.program.account.pool.fetch(this.getPoolKey(name))];
|
121
121
|
});
|
122
122
|
}); };
|
@@ -163,7 +163,7 @@ var PerpetualsClient = /** @class */ (function () {
|
|
163
163
|
};
|
164
164
|
this.getCustody = function (poolName, tokenMint) { return __awaiter(_this, void 0, void 0, function () {
|
165
165
|
return __generator(this, function (_a) {
|
166
|
-
console.log("custody key :", this.getCustodyKey(poolName, tokenMint).toBase58());
|
166
|
+
// console.log("custody key :", this.getCustodyKey(poolName, tokenMint).toBase58());
|
167
167
|
return [2 /*return*/, this.program.account.custody.fetch(this.getCustodyKey(poolName, tokenMint))];
|
168
168
|
});
|
169
169
|
}); };
|
@@ -369,7 +369,7 @@ var PerpetualsClient = /** @class */ (function () {
|
|
369
369
|
err_1 = _a.sent();
|
370
370
|
// @ts-ignore
|
371
371
|
if (this.printErrors) {
|
372
|
-
console.
|
372
|
+
console.error("setAdminSigners err:", err_1);
|
373
373
|
}
|
374
374
|
throw err_1;
|
375
375
|
case 4: return [2 /*return*/];
|
@@ -434,11 +434,7 @@ var PerpetualsClient = /** @class */ (function () {
|
|
434
434
|
return __generator(this, function (_a) {
|
435
435
|
switch (_a.label) {
|
436
436
|
case 0:
|
437
|
-
|
438
|
-
console.log("getCustodyTokenAccountKey", this.getCustodyTokenAccountKey(poolName, tokenMint).toBase58());
|
439
|
-
_a.label = 1;
|
440
|
-
case 1:
|
441
|
-
_a.trys.push([1, 3, , 4]);
|
437
|
+
_a.trys.push([0, 2, , 3]);
|
442
438
|
return [4 /*yield*/, this.program.methods
|
443
439
|
// @ts-ignore
|
444
440
|
.addCustody({
|
@@ -469,15 +465,15 @@ var PerpetualsClient = /** @class */ (function () {
|
|
469
465
|
console.error(err);
|
470
466
|
throw err;
|
471
467
|
})];
|
472
|
-
case
|
468
|
+
case 1:
|
473
469
|
trx_id = _a.sent();
|
474
470
|
console.log("trx_id:", "https://explorer.solana.com/tx/".concat(trx_id, "?cluster=devnet"));
|
475
|
-
return [3 /*break*/,
|
476
|
-
case
|
471
|
+
return [3 /*break*/, 3];
|
472
|
+
case 2:
|
477
473
|
error_1 = _a.sent();
|
478
|
-
console.
|
479
|
-
return [3 /*break*/,
|
480
|
-
case
|
474
|
+
console.error("cli error :", error_1);
|
475
|
+
return [3 /*break*/, 3];
|
476
|
+
case 3: return [2 /*return*/];
|
481
477
|
}
|
482
478
|
});
|
483
479
|
}); };
|
@@ -485,39 +481,36 @@ var PerpetualsClient = /** @class */ (function () {
|
|
485
481
|
var trx_id;
|
486
482
|
return __generator(this, function (_a) {
|
487
483
|
switch (_a.label) {
|
488
|
-
case 0:
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
console.error(err);
|
519
|
-
throw err;
|
520
|
-
})];
|
484
|
+
case 0: return [4 /*yield*/, this.program.methods
|
485
|
+
//@ts-ignore
|
486
|
+
.testingEditCustody({
|
487
|
+
isStable: isStable,
|
488
|
+
oracle: oracle,
|
489
|
+
pricing: pricing,
|
490
|
+
permissions: permissions,
|
491
|
+
fees: fees,
|
492
|
+
borrowRate: borrowRate,
|
493
|
+
ratios: ratios,
|
494
|
+
})
|
495
|
+
.accounts({
|
496
|
+
admin: this.admin,
|
497
|
+
multisig: this.multisig.publicKey,
|
498
|
+
transferAuthority: this.authority.publicKey,
|
499
|
+
perpetuals: this.perpetuals.publicKey,
|
500
|
+
pool: this.getPoolKey(poolName),
|
501
|
+
custody: this.getCustodyKey(poolName, tokenMint),
|
502
|
+
custodyTokenAccount: this.getCustodyTokenAccountKey(poolName, tokenMint),
|
503
|
+
custodyTokenMint: tokenMint,
|
504
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
505
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
506
|
+
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
507
|
+
})
|
508
|
+
// .signers([this.admin])
|
509
|
+
.rpc()
|
510
|
+
.catch(function (err) {
|
511
|
+
console.error(err);
|
512
|
+
throw err;
|
513
|
+
})];
|
521
514
|
case 1:
|
522
515
|
trx_id = _a.sent();
|
523
516
|
console.log("trx_id:", "https://explorer.solana.com/tx/".concat(trx_id, "?cluster=devnet"));
|
@@ -706,20 +699,12 @@ var PerpetualsClient = /** @class */ (function () {
|
|
706
699
|
});
|
707
700
|
}); };
|
708
701
|
this.getEntryPriceAndFee = function (poolName, tokenMint, collateral, size, side) { return __awaiter(_this, void 0, void 0, function () {
|
709
|
-
var _a, _b
|
710
|
-
var
|
711
|
-
return __generator(this, function (
|
712
|
-
switch (
|
702
|
+
var _a, _b;
|
703
|
+
var _c;
|
704
|
+
return __generator(this, function (_d) {
|
705
|
+
switch (_d.label) {
|
713
706
|
case 0:
|
714
|
-
|
715
|
-
console.log("poolKey: ", this.getPoolKey(poolName).toBase58());
|
716
|
-
console.log("custody key : ", this.getCustodyKey(poolName, tokenMint).toBase58());
|
717
|
-
_b = (_a = console).log;
|
718
|
-
_c = ["orcalve: "];
|
719
|
-
return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
|
720
|
-
case 1:
|
721
|
-
_b.apply(_a, _c.concat([(_g.sent()).toBase58()]));
|
722
|
-
_e = (_d = this.program.methods
|
707
|
+
_b = (_a = this.program.methods
|
723
708
|
//@ts-ignore
|
724
709
|
.getEntryPriceAndFee({
|
725
710
|
collateral: collateral,
|
@@ -727,23 +712,27 @@ var PerpetualsClient = /** @class */ (function () {
|
|
727
712
|
side: side == "long" ? { long: {} } : { short: {} },
|
728
713
|
}))
|
729
714
|
.accounts;
|
730
|
-
|
715
|
+
_c = {
|
731
716
|
// signer: this.provider.wallet.publicKey,
|
732
717
|
perpetuals: this.perpetuals.publicKey,
|
733
718
|
pool: this.getPoolKey(poolName),
|
734
719
|
custody: this.getCustodyKey(poolName, tokenMint)
|
735
720
|
};
|
736
721
|
return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
|
737
|
-
case
|
738
|
-
|
722
|
+
case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
|
723
|
+
_c)])
|
739
724
|
.view()
|
740
725
|
.catch(function (err) {
|
741
726
|
console.error(err);
|
742
727
|
throw err;
|
743
728
|
})];
|
744
|
-
case
|
729
|
+
case 2:
|
730
|
+
// console.log("perps: ", this.perpetuals.publicKey.toBase58())
|
731
|
+
// console.log("poolKey: ", this.getPoolKey(poolName).toBase58())
|
732
|
+
// console.log("custody key : ", this.getCustodyKey(poolName, tokenMint).toBase58());
|
733
|
+
// console.log("oracle: ", (await this.getCustodyOracleAccountKey(poolName, tokenMint)).toBase58())
|
745
734
|
//@ts-ignore
|
746
|
-
return [2 /*return*/,
|
735
|
+
return [2 /*return*/, _d.sent()];
|
747
736
|
}
|
748
737
|
});
|
749
738
|
}); };
|
@@ -890,7 +879,6 @@ var PerpetualsClient = /** @class */ (function () {
|
|
890
879
|
switch (_d.label) {
|
891
880
|
case 0:
|
892
881
|
pos = this.getPositionKey(wallet, poolName, tokenMint, side);
|
893
|
-
console.log("pos:", pos.toBase58());
|
894
882
|
_b = (_a = this.program.methods
|
895
883
|
.getPnl({}))
|
896
884
|
.accounts;
|
@@ -908,7 +896,9 @@ var PerpetualsClient = /** @class */ (function () {
|
|
908
896
|
console.error(err);
|
909
897
|
throw err;
|
910
898
|
})];
|
911
|
-
case 2:
|
899
|
+
case 2:
|
900
|
+
// console.log("pos:", pos.toBase58())
|
901
|
+
return [2 /*return*/, _d.sent()];
|
912
902
|
}
|
913
903
|
});
|
914
904
|
}); };
|
@@ -919,7 +909,6 @@ var PerpetualsClient = /** @class */ (function () {
|
|
919
909
|
switch (_d.label) {
|
920
910
|
case 0:
|
921
911
|
pos = this.getPositionKey(wallet, poolName, tokenMint, side);
|
922
|
-
console.log("pos:", pos.toBase58());
|
923
912
|
_b = (_a = this.program.methods
|
924
913
|
.getPnl({}))
|
925
914
|
.accounts;
|
@@ -937,7 +926,9 @@ var PerpetualsClient = /** @class */ (function () {
|
|
937
926
|
console.error(err);
|
938
927
|
throw err;
|
939
928
|
})];
|
940
|
-
case 2:
|
929
|
+
case 2:
|
930
|
+
// console.log("pos:", pos.toBase58())
|
931
|
+
return [2 /*return*/, _d.sent()];
|
941
932
|
}
|
942
933
|
});
|
943
934
|
}); };
|
@@ -1061,44 +1052,6 @@ var PerpetualsClient = /** @class */ (function () {
|
|
1061
1052
|
_b.trys.push([2, 10, , 11]);
|
1062
1053
|
if (!(payTokenSymbol == 'SOL' && createUserWSOLATA)) return [3 /*break*/, 4];
|
1063
1054
|
console.log("payTokenSymbol === sol", payTokenSymbol);
|
1064
|
-
// const wsolAssociatedTokenAccount = userCustodyTokenAccount;
|
1065
|
-
// const wsolATAExist = await checkIfAccountExists(wsolAssociatedTokenAccount, this.provider.connection)
|
1066
|
-
// if (!wsolATAExist) {
|
1067
|
-
// console.log("wsol ata does not exist");
|
1068
|
-
// instructions.push(
|
1069
|
-
// createAssociatedTokenAccountInstruction(
|
1070
|
-
// publicKey,
|
1071
|
-
// wsolAssociatedTokenAccount,
|
1072
|
-
// publicKey,
|
1073
|
-
// NATIVE_MINT
|
1074
|
-
// )
|
1075
|
-
// );
|
1076
|
-
// }
|
1077
|
-
// // get balance of WSOL associated token account
|
1078
|
-
// const wsolBalance = new BN(wsolATAExist ? (await this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)).value.amount : 0);
|
1079
|
-
// if (wsolBalance.lt(collateralWithfee)) {
|
1080
|
-
// console.log("WSOL balance insufficient \n so Convert SOL to WSOL");
|
1081
|
-
// let unWrappedSolBalance = new BN(await this.provider.connection.getBalance(publicKey));
|
1082
|
-
// const totalSolBal = unWrappedSolBalance.add(wsolBalance);
|
1083
|
-
// // const rentExemptSolNeedforATA = new BN(0.02 * LAMPORTS_PER_SOL);
|
1084
|
-
// // console.log("rentExemptSolNeedforATA:",rentExemptSolNeedforATA.toString())
|
1085
|
-
// //TODOD :: add(rentExemptSolNeedforATA)
|
1086
|
-
// if (totalSolBal.lt(collateralWithfee)) {
|
1087
|
-
// throw "Insufficient SOL Funds"
|
1088
|
-
// } else {
|
1089
|
-
// console.log("SOL balance sufficient so transfer from SOL to WSOL ATA")
|
1090
|
-
// }
|
1091
|
-
// let conversionAmt = collateralWithfee.sub(wsolBalance);
|
1092
|
-
// console.log("conversionAmt:",conversionAmt.toString())
|
1093
|
-
// instructions.push(
|
1094
|
-
// SystemProgram.transfer({
|
1095
|
-
// fromPubkey: publicKey,
|
1096
|
-
// toPubkey: wsolAssociatedTokenAccount,
|
1097
|
-
// lamports: conversionAmt.toNumber(), // IS IT SAFE TO PUT AS NUMBER
|
1098
|
-
// }),
|
1099
|
-
// createSyncNativeInstruction(wsolAssociatedTokenAccount)
|
1100
|
-
// );
|
1101
|
-
// }
|
1102
1055
|
wrappedSolAccount = new web3_js_1.Keypair();
|
1103
1056
|
return [4 /*yield*/, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
|
1104
1057
|
case 3:
|
@@ -1251,7 +1204,6 @@ var PerpetualsClient = /** @class */ (function () {
|
|
1251
1204
|
receivingTokenCustody.custodyAccount.toBuffer(),
|
1252
1205
|
(0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
|
1253
1206
|
], this.programId)[0];
|
1254
|
-
console.log("positionAccount:", positionAccount.toBase58());
|
1255
1207
|
params = {
|
1256
1208
|
price: priceAfterSlippage,
|
1257
1209
|
};
|
@@ -1289,6 +1241,204 @@ var PerpetualsClient = /** @class */ (function () {
|
|
1289
1241
|
});
|
1290
1242
|
});
|
1291
1243
|
};
|
1244
|
+
// ==== OLD
|
1245
|
+
// TODO: handle SOL wrapping to WSOL and create a ATA - DONE
|
1246
|
+
// TODO: Balance checks - DONE
|
1247
|
+
// TODO: ATA check - else create - DONE
|
1248
|
+
// TODO: for close Accounts - NOT NEEDED
|
1249
|
+
this.openPositionOld = function (payTokenSymbol, priceAfterSlippage, collateral, fee, size, side, poolConfig, createUserWSOLATA // if false will also skip balance checks
|
1250
|
+
) {
|
1251
|
+
if (createUserWSOLATA === void 0) { createUserWSOLATA = true; }
|
1252
|
+
return __awaiter(_this, void 0, void 0, function () {
|
1253
|
+
var publicKey, payTokenCustody, userCustodyTokenAccount, instructions, wsolAssociatedTokenAccount, wsolATAExist, collateralWithfee, wsolBalance, _a, _b, unWrappedSolBalance, _c, totalSolBal, conversionAmt, tokenAccountBalance, _d, positionAccount, params, instruction, error_4;
|
1254
|
+
return __generator(this, function (_e) {
|
1255
|
+
switch (_e.label) {
|
1256
|
+
case 0:
|
1257
|
+
console.log("open position :::", payTokenSymbol, poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey.toBase58());
|
1258
|
+
publicKey = this.provider.wallet.publicKey;
|
1259
|
+
payTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
|
1260
|
+
return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey, publicKey)];
|
1261
|
+
case 1:
|
1262
|
+
userCustodyTokenAccount = _e.sent();
|
1263
|
+
instructions = [];
|
1264
|
+
_e.label = 2;
|
1265
|
+
case 2:
|
1266
|
+
_e.trys.push([2, 15, , 16]);
|
1267
|
+
if (!(payTokenSymbol == 'SOL' && createUserWSOLATA)) return [3 /*break*/, 9];
|
1268
|
+
console.log("payTokenSymbol === sol", payTokenSymbol);
|
1269
|
+
wsolAssociatedTokenAccount = userCustodyTokenAccount;
|
1270
|
+
return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
|
1271
|
+
case 3:
|
1272
|
+
wsolATAExist = _e.sent();
|
1273
|
+
if (!wsolATAExist) {
|
1274
|
+
console.log("wsol ata does not exist");
|
1275
|
+
instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
|
1276
|
+
}
|
1277
|
+
collateralWithfee = collateral.add(fee);
|
1278
|
+
_a = anchor_1.BN.bind;
|
1279
|
+
if (!wsolATAExist) return [3 /*break*/, 5];
|
1280
|
+
return [4 /*yield*/, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
|
1281
|
+
case 4:
|
1282
|
+
_b = (_e.sent()).value.amount;
|
1283
|
+
return [3 /*break*/, 6];
|
1284
|
+
case 5:
|
1285
|
+
_b = 0;
|
1286
|
+
_e.label = 6;
|
1287
|
+
case 6:
|
1288
|
+
wsolBalance = new (_a.apply(anchor_1.BN, [void 0, _b]))();
|
1289
|
+
if (!wsolBalance.lt(collateralWithfee)) return [3 /*break*/, 8];
|
1290
|
+
console.log("WSOL balance insufficient \n so Convert SOL to WSOL");
|
1291
|
+
_c = anchor_1.BN.bind;
|
1292
|
+
return [4 /*yield*/, this.provider.connection.getBalance(publicKey)];
|
1293
|
+
case 7:
|
1294
|
+
unWrappedSolBalance = new (_c.apply(anchor_1.BN, [void 0, _e.sent()]))();
|
1295
|
+
totalSolBal = unWrappedSolBalance.add(wsolBalance);
|
1296
|
+
// const rentExemptSolNeedforATA = new BN(0.02 * LAMPORTS_PER_SOL);
|
1297
|
+
// console.log("rentExemptSolNeedforATA:",rentExemptSolNeedforATA.toString())
|
1298
|
+
//TODOD :: add(rentExemptSolNeedforATA)
|
1299
|
+
if (totalSolBal.lt(collateralWithfee)) {
|
1300
|
+
throw "Insufficient SOL Funds";
|
1301
|
+
}
|
1302
|
+
else {
|
1303
|
+
console.log("SOL balance sufficient so transfer from SOL to WSOL ATA");
|
1304
|
+
}
|
1305
|
+
conversionAmt = collateralWithfee.sub(wsolBalance);
|
1306
|
+
console.log("conversionAmt:", conversionAmt.toString());
|
1307
|
+
instructions.push(web3_js_1.SystemProgram.transfer({
|
1308
|
+
fromPubkey: publicKey,
|
1309
|
+
toPubkey: wsolAssociatedTokenAccount,
|
1310
|
+
lamports: conversionAmt.toNumber(), // IS IT SAFE TO PUT AS NUMBER
|
1311
|
+
}), (0, spl_token_1.createSyncNativeInstruction)(wsolAssociatedTokenAccount));
|
1312
|
+
_e.label = 8;
|
1313
|
+
case 8: return [3 /*break*/, 13];
|
1314
|
+
case 9:
|
1315
|
+
if (!(createUserWSOLATA == false)) return [3 /*break*/, 10];
|
1316
|
+
console.log("skip WSOL checks and creation ,since createUserWSOLATA == false");
|
1317
|
+
return [3 /*break*/, 13];
|
1318
|
+
case 10: return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userCustodyTokenAccount, this.provider.connection)];
|
1319
|
+
case 11:
|
1320
|
+
// for other tokens check if ATA and balance
|
1321
|
+
if (!(_e.sent())) {
|
1322
|
+
throw "Insufficient Funds , token Account doesn't exist";
|
1323
|
+
}
|
1324
|
+
_d = anchor_1.BN.bind;
|
1325
|
+
return [4 /*yield*/, this.provider.connection.getTokenAccountBalance(userCustodyTokenAccount)];
|
1326
|
+
case 12:
|
1327
|
+
tokenAccountBalance = new (_d.apply(anchor_1.BN, [void 0, (_e.sent()).value.amount]))();
|
1328
|
+
if (tokenAccountBalance.lt(collateral.add(fee))) {
|
1329
|
+
throw "Insufficient Funds";
|
1330
|
+
}
|
1331
|
+
_e.label = 13;
|
1332
|
+
case 13:
|
1333
|
+
positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
|
1334
|
+
Buffer.from("position"),
|
1335
|
+
publicKey.toBuffer(),
|
1336
|
+
poolConfig.poolAddress.toBuffer(),
|
1337
|
+
payTokenCustody.custodyAccount.toBuffer(),
|
1338
|
+
(0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
|
1339
|
+
], this.programId)[0];
|
1340
|
+
params = {
|
1341
|
+
price: priceAfterSlippage,
|
1342
|
+
collateral: collateral,
|
1343
|
+
size: size,
|
1344
|
+
side: side,
|
1345
|
+
};
|
1346
|
+
return [4 /*yield*/, this.program.methods
|
1347
|
+
.openPosition(params)
|
1348
|
+
.accounts({
|
1349
|
+
owner: publicKey,
|
1350
|
+
fundingAccount: userCustodyTokenAccount,
|
1351
|
+
transferAuthority: poolConfig.transferAuthority,
|
1352
|
+
perpetuals: poolConfig.perpetuals,
|
1353
|
+
pool: poolConfig.poolAddress,
|
1354
|
+
position: positionAccount,
|
1355
|
+
custody: payTokenCustody.custodyAccount,
|
1356
|
+
custodyOracleAccount: payTokenCustody.oracleAddress,
|
1357
|
+
custodyTokenAccount: payTokenCustody.tokenAccount,
|
1358
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
1359
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
1360
|
+
}).instruction()];
|
1361
|
+
case 14:
|
1362
|
+
instruction = _e.sent();
|
1363
|
+
instructions.push(instruction);
|
1364
|
+
return [3 /*break*/, 16];
|
1365
|
+
case 15:
|
1366
|
+
error_4 = _e.sent();
|
1367
|
+
console.log("perpClient openPosition error:", error_4);
|
1368
|
+
return [3 /*break*/, 16];
|
1369
|
+
case 16: return [2 /*return*/, instructions];
|
1370
|
+
}
|
1371
|
+
});
|
1372
|
+
});
|
1373
|
+
};
|
1374
|
+
// ==== OLD
|
1375
|
+
// TODO: handle SOL wrapping to WSOL and create a ATA - NOT NEEDED
|
1376
|
+
// TODO : Balance checks - NOT NEEDED
|
1377
|
+
// TODO: ATA check - else create - DONE
|
1378
|
+
// TODO: for close Accounts - DONE BY ANCHOR
|
1379
|
+
// TODO : if out token WSOL -> unwrap to SOL - DONE
|
1380
|
+
this.closePositionOld = function (receivingTokenSymbol, priceAfterSlippage, side, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
|
1381
|
+
var publicKey, userReceivingTokenAccount, instructions, receivingTokenCustody, positionAccount, params, instruction, closeWsolATAIns, error_5;
|
1382
|
+
return __generator(this, function (_a) {
|
1383
|
+
switch (_a.label) {
|
1384
|
+
case 0:
|
1385
|
+
console.log("close position :::", receivingTokenSymbol, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey.toBase58());
|
1386
|
+
publicKey = this.provider.wallet.publicKey;
|
1387
|
+
return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey, publicKey)];
|
1388
|
+
case 1:
|
1389
|
+
userReceivingTokenAccount = _a.sent();
|
1390
|
+
instructions = [];
|
1391
|
+
_a.label = 2;
|
1392
|
+
case 2:
|
1393
|
+
_a.trys.push([2, 5, , 6]);
|
1394
|
+
return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
|
1395
|
+
case 3:
|
1396
|
+
if (!(_a.sent())) {
|
1397
|
+
instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey));
|
1398
|
+
}
|
1399
|
+
receivingTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey); });
|
1400
|
+
positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
|
1401
|
+
Buffer.from("position"),
|
1402
|
+
publicKey.toBuffer(),
|
1403
|
+
poolConfig.poolAddress.toBuffer(),
|
1404
|
+
receivingTokenCustody.custodyAccount.toBuffer(),
|
1405
|
+
(0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
|
1406
|
+
], this.programId)[0];
|
1407
|
+
console.log("positionAccount:", positionAccount.toBase58());
|
1408
|
+
params = {
|
1409
|
+
price: priceAfterSlippage,
|
1410
|
+
};
|
1411
|
+
return [4 /*yield*/, this.program.methods
|
1412
|
+
.closePosition(params)
|
1413
|
+
.accounts({
|
1414
|
+
owner: publicKey,
|
1415
|
+
receivingAccount: userReceivingTokenAccount,
|
1416
|
+
transferAuthority: poolConfig.transferAuthority,
|
1417
|
+
perpetuals: poolConfig.perpetuals,
|
1418
|
+
pool: poolConfig.poolAddress,
|
1419
|
+
position: positionAccount,
|
1420
|
+
custody: receivingTokenCustody.custodyAccount,
|
1421
|
+
custodyOracleAccount: receivingTokenCustody.oracleAddress,
|
1422
|
+
custodyTokenAccount: receivingTokenCustody.tokenAccount,
|
1423
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
1424
|
+
}).instruction()];
|
1425
|
+
case 4:
|
1426
|
+
instruction = _a.sent();
|
1427
|
+
instructions.push(instruction);
|
1428
|
+
// SOL is only retrievable by closing the token account and choosing the desired address to send the token account's lamports.
|
1429
|
+
if (receivingTokenSymbol == 'SOL') {
|
1430
|
+
closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
|
1431
|
+
instructions.push(closeWsolATAIns);
|
1432
|
+
}
|
1433
|
+
return [3 /*break*/, 6];
|
1434
|
+
case 5:
|
1435
|
+
error_5 = _a.sent();
|
1436
|
+
console.error("perpclient closePosition error:", error_5);
|
1437
|
+
return [3 /*break*/, 6];
|
1438
|
+
case 6: return [2 /*return*/, instructions];
|
1439
|
+
}
|
1440
|
+
});
|
1441
|
+
}); };
|
1292
1442
|
// TODO: ATA check - else create - DONE
|
1293
1443
|
// TODO: handle SOL wrapping to WSOL and create a ATA - DONE
|
1294
1444
|
// TODO : Balance checks - NOT NEEDED
|
@@ -1414,7 +1564,7 @@ var PerpetualsClient = /** @class */ (function () {
|
|
1414
1564
|
return [3 /*break*/, 18];
|
1415
1565
|
case 17:
|
1416
1566
|
err_2 = _e.sent();
|
1417
|
-
console.
|
1567
|
+
console.error("perpClient Swap error:: ", err_2);
|
1418
1568
|
throw err_2;
|
1419
1569
|
case 18: return [2 /*return*/, instructions];
|
1420
1570
|
}
|
package/lib/PoolConfig.d.ts
CHANGED
package/lib/PoolConfig.js
CHANGED
@@ -79,8 +79,9 @@ var PoolConfig = /** @class */ (function () {
|
|
79
79
|
var tokens = poolConfig['tokens'].map(function (i) {
|
80
80
|
return __assign(__assign({}, i), { mintKey: new web3_js_1.PublicKey(i.mintKey) });
|
81
81
|
});
|
82
|
-
var custodies = poolConfig['custodies'].map(function (i) {
|
83
|
-
|
82
|
+
var custodies = poolConfig['custodies'].map(function (i, index) {
|
83
|
+
var _a;
|
84
|
+
return __assign(__assign({}, i), { custodyId: (_a = i === null || i === void 0 ? void 0 : i.custodyId) !== null && _a !== void 0 ? _a : index, custodyAccount: new web3_js_1.PublicKey(i.custodyAccount), tokenAccount: new web3_js_1.PublicKey(i.tokenAccount), mintKey: new web3_js_1.PublicKey(i.mintKey), oracleAddress: new web3_js_1.PublicKey(i.oracleAddress) });
|
84
85
|
});
|
85
86
|
return new PoolConfig(new web3_js_1.PublicKey(poolConfig.programId), poolConfig.cluster, poolConfig.poolName, new web3_js_1.PublicKey(poolConfig.poolAddress), new web3_js_1.PublicKey(poolConfig.lpTokenMint), poolConfig.lpDecimals, new web3_js_1.PublicKey(poolConfig.perpetuals), new web3_js_1.PublicKey(poolConfig.transferAuthority), new web3_js_1.PublicKey(poolConfig.multisig), tokens, custodies);
|
86
87
|
};
|
package/lib/PoolConfig.json
CHANGED
@@ -55,6 +55,7 @@
|
|
55
55
|
],
|
56
56
|
"custodies": [
|
57
57
|
{
|
58
|
+
"custodyId": 0,
|
58
59
|
"custodyAccount": "CkYddgaB8c5tvs9WP11B83HvMaU35ZFpTzLLGg4NviEj",
|
59
60
|
"tokenAccount": "DHy9BXo36hwKkvBJnPd11zH77BefA77mTiL7yu2LNWPx",
|
60
61
|
"symbol": "USDC",
|
@@ -64,6 +65,7 @@
|
|
64
65
|
"oracleAddress": "5SSkXsEKQepHHAewytPVwdej4epN1nxgLVM84L4KXgy7"
|
65
66
|
},
|
66
67
|
{
|
68
|
+
"custodyId": 1,
|
67
69
|
"custodyAccount": "4ueMZBmpTMPykYwTENJs7diYfgx6UBCPQnRwb2F5UFgw",
|
68
70
|
"tokenAccount": "G9bJQdZkzXZsiwouZ8MsZw6p8Pg5vtM9hbVHjfqSrJZo",
|
69
71
|
"symbol": "SOL",
|
@@ -73,6 +75,7 @@
|
|
73
75
|
"oracleAddress": "J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix"
|
74
76
|
},
|
75
77
|
{
|
78
|
+
"custodyId": 2,
|
76
79
|
"custodyAccount": "F9qzX5pHGS5SFfP5M24a8dqXUHQdraexJ3P9TNGF2WFj",
|
77
80
|
"tokenAccount": "y5vPUaQdM3fJkEyn3oA8uFMRzwJYcNJsjBZyW1yRUar",
|
78
81
|
"symbol": "BTC",
|
@@ -82,6 +85,7 @@
|
|
82
85
|
"oracleAddress": "HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J"
|
83
86
|
},
|
84
87
|
{
|
88
|
+
"custodyId": 3,
|
85
89
|
"custodyAccount": "BZggMsfpcpbB3tnhmtM2qJ1uMobKVBEmYZp2jmDrBHZD",
|
86
90
|
"tokenAccount": "HP4CcezCpCZqcoo8BsrroFEaykz2F4bz6sWBCCUJEBPy",
|
87
91
|
"symbol": "ETH",
|