flash-sdk 1.0.44 → 1.0.46
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/dist/PerpetualsClient.d.ts +21 -6
- package/dist/PerpetualsClient.js +575 -379
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
package/dist/PerpetualsClient.js
CHANGED
@@ -1024,31 +1024,38 @@ var PerpetualsClient = (function () {
|
|
1024
1024
|
this.getAumSdk = function (poolAccount, token_prices, token_ema_prices, custodies, aum_calc_mode, currentTime) {
|
1025
1025
|
return poolAccount.getAssetsUnderManagementUsd(token_prices, token_ema_prices, custodies, aum_calc_mode, currentTime);
|
1026
1026
|
};
|
1027
|
-
this.openPosition = function (marketSymbol, collateralSymbol, priceAfterSlippage, collateralWithfee,
|
1027
|
+
this.openPosition = function (marketSymbol, collateralSymbol, priceAfterSlippage, collateralWithfee, size, side, poolConfig, skipBalanceChecks) {
|
1028
1028
|
if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
|
1029
1029
|
return __awaiter(_this, void 0, void 0, function () {
|
1030
|
-
var publicKey, marketCustodyConfig, collateralCustodyConfig, userCollateralTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports,
|
1031
|
-
return __generator(this, function (
|
1032
|
-
switch (
|
1030
|
+
var publicKey, marketCustodyConfig, collateralCustodyConfig, userCollateralTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, params, instruction;
|
1031
|
+
return __generator(this, function (_c) {
|
1032
|
+
switch (_c.label) {
|
1033
1033
|
case 0:
|
1034
1034
|
publicKey = this.provider.wallet.publicKey;
|
1035
1035
|
marketCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
|
1036
1036
|
collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
|
1037
1037
|
return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey)];
|
1038
1038
|
case 1:
|
1039
|
-
userCollateralTokenAccount =
|
1039
|
+
userCollateralTokenAccount = _c.sent();
|
1040
1040
|
preInstructions = [];
|
1041
1041
|
instructions = [];
|
1042
1042
|
postInstructions = [];
|
1043
1043
|
additionalSigners = [];
|
1044
|
-
if (!(collateralSymbol == 'SOL')) return [3,
|
1044
|
+
if (!(collateralSymbol == 'SOL')) return [3, 4];
|
1045
1045
|
console.log("collateralSymbol === SOL", collateralSymbol);
|
1046
1046
|
wrappedSolAccount = new web3_js_1.Keypair();
|
1047
1047
|
return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
|
1048
1048
|
case 2:
|
1049
|
-
accCreationLamports = (
|
1049
|
+
accCreationLamports = (_c.sent());
|
1050
1050
|
console.log("accCreationLamports:", accCreationLamports);
|
1051
1051
|
lamports = collateralWithfee.add(new anchor_1.BN(accCreationLamports));
|
1052
|
+
_a = anchor_1.BN.bind;
|
1053
|
+
return [4, this.provider.connection.getBalance(publicKey)];
|
1054
|
+
case 3:
|
1055
|
+
unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _c.sent()]))();
|
1056
|
+
if (unWrappedSolBalance.lt(lamports)) {
|
1057
|
+
throw "Insufficient SOL Funds";
|
1058
|
+
}
|
1052
1059
|
preInstructions = [
|
1053
1060
|
web3_js_1.SystemProgram.createAccount({
|
1054
1061
|
fromPubkey: publicKey,
|
@@ -1063,22 +1070,22 @@ var PerpetualsClient = (function () {
|
|
1063
1070
|
(0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
|
1064
1071
|
];
|
1065
1072
|
additionalSigners.push(wrappedSolAccount);
|
1066
|
-
return [3,
|
1067
|
-
case
|
1068
|
-
case
|
1069
|
-
if (!(
|
1073
|
+
return [3, 7];
|
1074
|
+
case 4: return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
|
1075
|
+
case 5:
|
1076
|
+
if (!(_c.sent())) {
|
1070
1077
|
throw "Insufficient Funds , token Account doesn't exist";
|
1071
1078
|
}
|
1072
|
-
if (!!skipBalanceChecks) return [3,
|
1073
|
-
|
1079
|
+
if (!!skipBalanceChecks) return [3, 7];
|
1080
|
+
_b = anchor_1.BN.bind;
|
1074
1081
|
return [4, this.provider.connection.getTokenAccountBalance(userCollateralTokenAccount)];
|
1075
|
-
case
|
1076
|
-
tokenAccountBalance = new (
|
1082
|
+
case 6:
|
1083
|
+
tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_c.sent()).value.amount]))();
|
1077
1084
|
if (tokenAccountBalance.lt(collateralWithfee)) {
|
1078
1085
|
throw "Insufficient Funds need more ".concat(collateralWithfee.sub(tokenAccountBalance), " tokens");
|
1079
1086
|
}
|
1080
|
-
|
1081
|
-
case
|
1087
|
+
_c.label = 7;
|
1088
|
+
case 7:
|
1082
1089
|
positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
|
1083
1090
|
Buffer.from("position"),
|
1084
1091
|
publicKey.toBuffer(),
|
@@ -1103,19 +1110,19 @@ var PerpetualsClient = (function () {
|
|
1103
1110
|
position: positionAccount,
|
1104
1111
|
custody: marketCustodyConfig.custodyAccount,
|
1105
1112
|
custodyOracleAccount: marketCustodyConfig.oracleAddress,
|
1113
|
+
custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1106
1114
|
collateralCustody: collateralCustodyConfig.custodyAccount,
|
1107
1115
|
collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
|
1116
|
+
collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1108
1117
|
collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
|
1109
1118
|
eventAuthority: this.eventAuthority.publicKey,
|
1110
1119
|
systemProgram: web3_js_1.SystemProgram.programId,
|
1111
1120
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
1112
|
-
collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1113
|
-
custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1114
1121
|
program: this.programId,
|
1115
1122
|
payer: publicKey,
|
1116
1123
|
}).instruction()];
|
1117
|
-
case
|
1118
|
-
instruction =
|
1124
|
+
case 8:
|
1125
|
+
instruction = _c.sent();
|
1119
1126
|
instructions.push(instruction);
|
1120
1127
|
return [2, {
|
1121
1128
|
instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
|
@@ -1144,7 +1151,6 @@ var PerpetualsClient = (function () {
|
|
1144
1151
|
_b.trys.push([1, 9, , 10]);
|
1145
1152
|
if (!(collateralSymbol == 'SOL')) return [3, 3];
|
1146
1153
|
wrappedSolAccount = new web3_js_1.Keypair();
|
1147
|
-
userReceivingTokenAccount = wrappedSolAccount.publicKey;
|
1148
1154
|
return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
|
1149
1155
|
case 2:
|
1150
1156
|
lamports = (_b.sent());
|
@@ -1194,20 +1200,20 @@ var PerpetualsClient = (function () {
|
|
1194
1200
|
.closePosition(params)
|
1195
1201
|
.accounts({
|
1196
1202
|
owner: publicKey,
|
1197
|
-
receivingAccount: userReceivingTokenAccount,
|
1203
|
+
receivingAccount: collateralSymbol == 'SOL' ? wrappedSolAccount.publicKey : userReceivingTokenAccount,
|
1198
1204
|
transferAuthority: poolConfig.transferAuthority,
|
1199
1205
|
perpetuals: poolConfig.perpetuals,
|
1200
1206
|
pool: poolConfig.poolAddress,
|
1201
1207
|
position: positionAccount,
|
1202
1208
|
custody: marketCustodyConfig.custodyAccount,
|
1203
1209
|
custodyOracleAccount: marketCustodyConfig.oracleAddress,
|
1210
|
+
custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1204
1211
|
collateralCustody: collateralCustodyConfig.custodyAccount,
|
1205
1212
|
collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
|
1213
|
+
collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1206
1214
|
collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
|
1207
1215
|
eventAuthority: this.eventAuthority.publicKey,
|
1208
1216
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
1209
|
-
collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1210
|
-
custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1211
1217
|
program: this.programId,
|
1212
1218
|
payer: publicKey,
|
1213
1219
|
}).instruction()];
|
@@ -1231,93 +1237,169 @@ var PerpetualsClient = (function () {
|
|
1231
1237
|
});
|
1232
1238
|
});
|
1233
1239
|
};
|
1234
|
-
this.swap = function (
|
1240
|
+
this.swap = function (userInputTokenSymbol, userOutputTokenSymbol, amountIn, minAmountOut, poolConfig, createUserATA, unWrapSol, skipBalanceChecks) {
|
1241
|
+
if (createUserATA === void 0) { createUserATA = true; }
|
1235
1242
|
if (unWrapSol === void 0) { unWrapSol = false; }
|
1243
|
+
if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
|
1236
1244
|
return __awaiter(_this, void 0, void 0, function () {
|
1237
|
-
var
|
1238
|
-
return __generator(this, function (
|
1239
|
-
switch (
|
1245
|
+
var userInputCustodyConfig, userOutputCustodyConfig, publicKey, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userOutputTokenAccount, userInputTokenAccount, wsolAssociatedTokenAccount, wsolATAExist, unWrappedSolBalance, _a, wsolAssociatedTokenAccount, closeWsolATAIns, accCreationLamports, lamports, unWrappedSolBalance, _b, tokenAccountBalance, _c, lamports, _d, custodyAccountMetas, custodyOracleAccountMetas, custodyCustomOracles, _i, _e, custody, params, inx, closeWsolATAIns, err_2;
|
1246
|
+
return __generator(this, function (_f) {
|
1247
|
+
switch (_f.label) {
|
1240
1248
|
case 0:
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
if (!poolReceivingCustodyConfig) {
|
1245
|
-
throw "receivingTokenCustody not found";
|
1249
|
+
userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.toBase58() === poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey.toBase58(); });
|
1250
|
+
if (!userInputCustodyConfig) {
|
1251
|
+
throw "userInputCustodyConfig not found";
|
1246
1252
|
}
|
1247
|
-
|
1248
|
-
if (!
|
1249
|
-
throw "
|
1253
|
+
userOutputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.toBase58() === poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey.toBase58(); });
|
1254
|
+
if (!userOutputCustodyConfig) {
|
1255
|
+
throw "userOutputCustodyConfig not found";
|
1250
1256
|
}
|
1251
1257
|
publicKey = this.provider.wallet.publicKey;
|
1258
|
+
preInstructions = [];
|
1252
1259
|
instructions = [];
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolDispensingCustodyConfig.mintKey, publicKey)];
|
1257
|
-
case 2:
|
1258
|
-
userReceivingTokenAccount = _e.sent();
|
1259
|
-
return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
|
1260
|
-
case 3:
|
1261
|
-
if (!(_e.sent())) {
|
1262
|
-
instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolDispensingCustodyConfig.mintKey));
|
1263
|
-
}
|
1264
|
-
return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolReceivingCustodyConfig.mintKey, publicKey)];
|
1265
|
-
case 4:
|
1266
|
-
userDispensingCustodyTokenAccount = _e.sent();
|
1267
|
-
if (!(userDispensingTokenSymbol == 'SOL')) return [3, 12];
|
1268
|
-
console.log("userDispensingTokenSymbol === sol", userDispensingTokenSymbol);
|
1260
|
+
postInstructions = [];
|
1261
|
+
additionalSigners = [];
|
1262
|
+
if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 4];
|
1269
1263
|
return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
|
1270
|
-
case
|
1271
|
-
wsolAssociatedTokenAccount =
|
1264
|
+
case 1:
|
1265
|
+
wsolAssociatedTokenAccount = _f.sent();
|
1272
1266
|
return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
|
1273
|
-
case
|
1274
|
-
wsolATAExist =
|
1267
|
+
case 2:
|
1268
|
+
wsolATAExist = _f.sent();
|
1275
1269
|
if (!wsolATAExist) {
|
1276
|
-
console.log("wsol ata does not exist");
|
1277
1270
|
instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
|
1278
1271
|
}
|
1279
1272
|
_a = anchor_1.BN.bind;
|
1280
|
-
if (!wsolATAExist) return [3, 8];
|
1281
|
-
return [4, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
|
1282
|
-
case 7:
|
1283
|
-
_b = (_e.sent()).value.amount;
|
1284
|
-
return [3, 9];
|
1285
|
-
case 8:
|
1286
|
-
_b = 0;
|
1287
|
-
_e.label = 9;
|
1288
|
-
case 9:
|
1289
|
-
wsolBalance = new (_a.apply(anchor_1.BN, [void 0, _b]))();
|
1290
|
-
if (!wsolBalance.lt(amountIn)) return [3, 11];
|
1291
|
-
_c = anchor_1.BN.bind;
|
1292
1273
|
return [4, this.provider.connection.getBalance(publicKey)];
|
1293
|
-
case
|
1294
|
-
unWrappedSolBalance = new (
|
1295
|
-
|
1296
|
-
if (totalSolBal.lt(amountIn)) {
|
1274
|
+
case 3:
|
1275
|
+
unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _f.sent()]))();
|
1276
|
+
if (unWrappedSolBalance.lt(amountIn)) {
|
1297
1277
|
throw "Insufficient SOL Funds";
|
1298
1278
|
}
|
1299
|
-
conversionAmt = amountIn.sub(wsolBalance);
|
1300
1279
|
instructions.push(web3_js_1.SystemProgram.transfer({
|
1301
1280
|
fromPubkey: publicKey,
|
1302
1281
|
toPubkey: wsolAssociatedTokenAccount,
|
1303
|
-
lamports:
|
1282
|
+
lamports: amountIn.toNumber(),
|
1304
1283
|
}), (0, spl_token_1.createSyncNativeInstruction)(wsolAssociatedTokenAccount));
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1284
|
+
return [2, {
|
1285
|
+
instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
|
1286
|
+
additionalSigners: additionalSigners
|
1287
|
+
}];
|
1288
|
+
case 4:
|
1289
|
+
if (!(userInputTokenSymbol == 'WSOL' && userOutputTokenSymbol == 'SOL')) return [3, 6];
|
1290
|
+
console.log("WSOL=> SOL : NOTE : ONLY WAY IS TO CLOSE THE WSOL ATA and GET ALL SOL ");
|
1291
|
+
return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
|
1292
|
+
case 5:
|
1293
|
+
wsolAssociatedTokenAccount = _f.sent();
|
1294
|
+
closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(wsolAssociatedTokenAccount, publicKey, publicKey);
|
1295
|
+
instructions.push(closeWsolATAIns);
|
1296
|
+
return [2, {
|
1297
|
+
instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
|
1298
|
+
additionalSigners: additionalSigners
|
1299
|
+
}];
|
1300
|
+
case 6:
|
1301
|
+
_f.trys.push([6, 20, , 21]);
|
1302
|
+
if (!(userInputTokenSymbol == 'SOL')) return [3, 9];
|
1303
|
+
console.log("userInputTokenSymbol === sol", userInputTokenSymbol);
|
1304
|
+
wrappedSolAccount = new web3_js_1.Keypair();
|
1305
|
+
return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
|
1306
|
+
case 7:
|
1307
|
+
accCreationLamports = (_f.sent());
|
1308
|
+
console.log("accCreationLamports:", accCreationLamports);
|
1309
|
+
lamports = amountIn.add(new anchor_1.BN(accCreationLamports));
|
1310
|
+
_b = anchor_1.BN.bind;
|
1311
|
+
return [4, this.provider.connection.getBalance(publicKey)];
|
1312
|
+
case 8:
|
1313
|
+
unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _f.sent()]))();
|
1314
|
+
if (unWrappedSolBalance.lt(amountIn)) {
|
1315
|
+
throw "Insufficient SOL Funds";
|
1316
|
+
}
|
1317
|
+
preInstructions = [
|
1318
|
+
web3_js_1.SystemProgram.createAccount({
|
1319
|
+
fromPubkey: publicKey,
|
1320
|
+
newAccountPubkey: wrappedSolAccount.publicKey,
|
1321
|
+
lamports: lamports.toNumber(),
|
1322
|
+
space: 165,
|
1323
|
+
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
1324
|
+
}),
|
1325
|
+
(0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
|
1326
|
+
];
|
1327
|
+
postInstructions = [
|
1328
|
+
(0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
|
1329
|
+
];
|
1330
|
+
additionalSigners.push(wrappedSolAccount);
|
1331
|
+
return [3, 12];
|
1332
|
+
case 9: return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
|
1333
|
+
case 10:
|
1334
|
+
if (!(_f.sent())) {
|
1310
1335
|
throw "Insufficient Funds , Token Account doesn't exist";
|
1311
1336
|
}
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1337
|
+
if (!!skipBalanceChecks) return [3, 12];
|
1338
|
+
_c = anchor_1.BN.bind;
|
1339
|
+
return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
|
1340
|
+
case 11:
|
1341
|
+
tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_f.sent()).value.amount]))();
|
1316
1342
|
if (tokenAccountBalance.lt(amountIn)) {
|
1317
|
-
throw "Insufficient Funds";
|
1343
|
+
throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
|
1318
1344
|
}
|
1319
|
-
|
1345
|
+
_f.label = 12;
|
1346
|
+
case 12:
|
1347
|
+
if (!(userOutputTokenSymbol == 'SOL')) return [3, 14];
|
1348
|
+
wrappedSolAccount = new web3_js_1.Keypair();
|
1349
|
+
return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
|
1350
|
+
case 13:
|
1351
|
+
lamports = (_f.sent());
|
1352
|
+
preInstructions = [
|
1353
|
+
web3_js_1.SystemProgram.createAccount({
|
1354
|
+
fromPubkey: publicKey,
|
1355
|
+
newAccountPubkey: wrappedSolAccount.publicKey,
|
1356
|
+
lamports: lamports,
|
1357
|
+
space: 165,
|
1358
|
+
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
1359
|
+
}),
|
1360
|
+
(0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
|
1361
|
+
];
|
1362
|
+
postInstructions = [
|
1363
|
+
(0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
|
1364
|
+
];
|
1365
|
+
additionalSigners.push(wrappedSolAccount);
|
1366
|
+
return [3, 18];
|
1367
|
+
case 14: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey, publicKey)];
|
1320
1368
|
case 15:
|
1369
|
+
userOutputTokenAccount = _f.sent();
|
1370
|
+
_d = createUserATA;
|
1371
|
+
if (!_d) return [3, 17];
|
1372
|
+
return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
|
1373
|
+
case 16:
|
1374
|
+
_d = !(_f.sent());
|
1375
|
+
_f.label = 17;
|
1376
|
+
case 17:
|
1377
|
+
if (_d) {
|
1378
|
+
preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey));
|
1379
|
+
}
|
1380
|
+
_f.label = 18;
|
1381
|
+
case 18:
|
1382
|
+
custodyAccountMetas = [];
|
1383
|
+
custodyOracleAccountMetas = [];
|
1384
|
+
custodyCustomOracles = [];
|
1385
|
+
for (_i = 0, _e = poolConfig.custodies; _i < _e.length; _i++) {
|
1386
|
+
custody = _e[_i];
|
1387
|
+
custodyAccountMetas.push({
|
1388
|
+
pubkey: custody.custodyAccount,
|
1389
|
+
isSigner: false,
|
1390
|
+
isWritable: false,
|
1391
|
+
});
|
1392
|
+
custodyOracleAccountMetas.push({
|
1393
|
+
pubkey: custody.oracleAddress,
|
1394
|
+
isSigner: false,
|
1395
|
+
isWritable: false,
|
1396
|
+
});
|
1397
|
+
custodyCustomOracles.push({
|
1398
|
+
pubkey: web3_js_1.SystemProgram.programId,
|
1399
|
+
isSigner: false,
|
1400
|
+
isWritable: false,
|
1401
|
+
});
|
1402
|
+
}
|
1321
1403
|
params = {
|
1322
1404
|
amountIn: amountIn,
|
1323
1405
|
minAmountOut: minAmountOut,
|
@@ -1326,141 +1408,207 @@ var PerpetualsClient = (function () {
|
|
1326
1408
|
.swap(params)
|
1327
1409
|
.accounts({
|
1328
1410
|
owner: publicKey,
|
1329
|
-
fundingAccount:
|
1330
|
-
receivingAccount:
|
1411
|
+
fundingAccount: userInputTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userInputTokenAccount,
|
1412
|
+
receivingAccount: userOutputTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userOutputTokenAccount,
|
1331
1413
|
transferAuthority: poolConfig.transferAuthority,
|
1332
1414
|
perpetuals: poolConfig.perpetuals,
|
1333
1415
|
pool: poolConfig.poolAddress,
|
1334
|
-
receivingCustody:
|
1335
|
-
receivingCustodyOracleAccount:
|
1336
|
-
|
1337
|
-
|
1338
|
-
|
1339
|
-
|
1416
|
+
receivingCustody: userInputCustodyConfig.custodyAccount,
|
1417
|
+
receivingCustodyOracleAccount: userInputCustodyConfig.oracleAddress,
|
1418
|
+
receivingCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1419
|
+
receivingCustodyTokenAccount: userInputCustodyConfig.tokenAccount,
|
1420
|
+
dispensingCustody: userOutputCustodyConfig.custodyAccount,
|
1421
|
+
dispensingCustodyOracleAccount: userOutputCustodyConfig.oracleAddress,
|
1422
|
+
dispensingCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1423
|
+
dispensingCustodyTokenAccount: userOutputCustodyConfig.tokenAccount,
|
1340
1424
|
eventAuthority: this.eventAuthority.publicKey,
|
1341
1425
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
1426
|
+
program: this.programId,
|
1342
1427
|
})
|
1428
|
+
.remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), custodyCustomOracles, true))
|
1343
1429
|
.instruction()];
|
1344
|
-
case
|
1345
|
-
inx =
|
1430
|
+
case 19:
|
1431
|
+
inx = _f.sent();
|
1346
1432
|
instructions.push(inx);
|
1347
|
-
if (
|
1348
|
-
closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(
|
1433
|
+
if (userOutputTokenSymbol == 'SOL' && unWrapSol) {
|
1434
|
+
closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userOutputTokenAccount, publicKey, publicKey);
|
1349
1435
|
instructions.push(closeWsolATAIns);
|
1350
1436
|
}
|
1351
|
-
return [3,
|
1352
|
-
case
|
1353
|
-
err_2 =
|
1437
|
+
return [3, 21];
|
1438
|
+
case 20:
|
1439
|
+
err_2 = _f.sent();
|
1354
1440
|
console.error("perpClient Swap error:: ", err_2);
|
1355
1441
|
throw err_2;
|
1356
|
-
case
|
1442
|
+
case 21: return [2, {
|
1443
|
+
instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
|
1444
|
+
additionalSigners: additionalSigners
|
1445
|
+
}];
|
1357
1446
|
}
|
1358
1447
|
});
|
1359
1448
|
});
|
1360
1449
|
};
|
1361
|
-
this.addCollateral = function (
|
1362
|
-
|
1363
|
-
return
|
1364
|
-
|
1365
|
-
|
1366
|
-
|
1367
|
-
|
1368
|
-
|
1369
|
-
|
1370
|
-
|
1371
|
-
|
1372
|
-
|
1373
|
-
|
1374
|
-
|
1375
|
-
|
1376
|
-
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
|
1381
|
-
|
1382
|
-
|
1383
|
-
|
1384
|
-
|
1385
|
-
|
1386
|
-
|
1387
|
-
|
1388
|
-
|
1389
|
-
|
1390
|
-
|
1391
|
-
|
1392
|
-
|
1393
|
-
|
1394
|
-
|
1395
|
-
|
1396
|
-
|
1397
|
-
|
1398
|
-
|
1399
|
-
|
1400
|
-
|
1401
|
-
|
1402
|
-
|
1403
|
-
|
1404
|
-
|
1405
|
-
|
1406
|
-
|
1407
|
-
|
1408
|
-
|
1409
|
-
|
1410
|
-
|
1411
|
-
|
1412
|
-
|
1413
|
-
|
1414
|
-
|
1415
|
-
|
1416
|
-
|
1417
|
-
|
1418
|
-
|
1419
|
-
|
1420
|
-
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1424
|
-
|
1425
|
-
|
1426
|
-
|
1427
|
-
|
1428
|
-
|
1429
|
-
|
1430
|
-
|
1431
|
-
|
1432
|
-
|
1433
|
-
|
1434
|
-
|
1435
|
-
|
1436
|
-
|
1450
|
+
this.addCollateral = function (collateralWithFee, marketSymbol, collateralSymbol, positionPubKey, poolConfig, skipBalanceChecks) {
|
1451
|
+
if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
|
1452
|
+
return __awaiter(_this, void 0, void 0, function () {
|
1453
|
+
var publicKey, collateralCustodyConfig, marketCustodyConfig, userPayingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, solBal, tokenAccountBalance, _b, instruction;
|
1454
|
+
return __generator(this, function (_c) {
|
1455
|
+
switch (_c.label) {
|
1456
|
+
case 0:
|
1457
|
+
publicKey = this.provider.wallet.publicKey;
|
1458
|
+
collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
|
1459
|
+
marketCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
|
1460
|
+
if (!collateralCustodyConfig || !marketCustodyConfig) {
|
1461
|
+
throw "payTokenCustody not found";
|
1462
|
+
}
|
1463
|
+
userPayingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(collateralCustodyConfig.mintKey, publicKey);
|
1464
|
+
preInstructions = [];
|
1465
|
+
instructions = [];
|
1466
|
+
postInstructions = [];
|
1467
|
+
additionalSigners = [];
|
1468
|
+
if (!(collateralSymbol == 'SOL')) return [3, 4];
|
1469
|
+
console.log("collateralSymbol === SOL", collateralSymbol);
|
1470
|
+
wrappedSolAccount = new web3_js_1.Keypair();
|
1471
|
+
return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
|
1472
|
+
case 1:
|
1473
|
+
accCreationLamports = (_c.sent());
|
1474
|
+
console.log("accCreationLamports:", accCreationLamports);
|
1475
|
+
lamports = collateralWithFee.add(new anchor_1.BN(accCreationLamports));
|
1476
|
+
console.log("lamports:", lamports.toNumber());
|
1477
|
+
_a = anchor_1.BN.bind;
|
1478
|
+
return [4, this.provider.connection.getBalance(publicKey)];
|
1479
|
+
case 2:
|
1480
|
+
unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _c.sent()]))();
|
1481
|
+
if (unWrappedSolBalance.lt(lamports)) {
|
1482
|
+
throw "Insufficient SOL Funds";
|
1483
|
+
}
|
1484
|
+
return [4, this.provider.connection.getBalance(publicKey)];
|
1485
|
+
case 3:
|
1486
|
+
solBal = _c.sent();
|
1487
|
+
console.log("solBal>>:", solBal);
|
1488
|
+
preInstructions = [
|
1489
|
+
web3_js_1.SystemProgram.createAccount({
|
1490
|
+
fromPubkey: publicKey,
|
1491
|
+
newAccountPubkey: wrappedSolAccount.publicKey,
|
1492
|
+
lamports: lamports.toNumber(),
|
1493
|
+
space: 165,
|
1494
|
+
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
1495
|
+
}),
|
1496
|
+
(0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
|
1497
|
+
];
|
1498
|
+
postInstructions = [
|
1499
|
+
(0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
|
1500
|
+
];
|
1501
|
+
additionalSigners.push(wrappedSolAccount);
|
1502
|
+
return [3, 7];
|
1503
|
+
case 4: return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
|
1504
|
+
case 5:
|
1505
|
+
if (!(_c.sent())) {
|
1506
|
+
throw "Insufficient Funds , token Account doesn't exist";
|
1507
|
+
}
|
1508
|
+
if (!!skipBalanceChecks) return [3, 7];
|
1509
|
+
_b = anchor_1.BN.bind;
|
1510
|
+
return [4, this.provider.connection.getTokenAccountBalance(userPayingTokenAccount)];
|
1511
|
+
case 6:
|
1512
|
+
tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_c.sent()).value.amount]))();
|
1513
|
+
if (tokenAccountBalance.lt(collateralWithFee)) {
|
1514
|
+
throw "Insufficient Funds need more ".concat(collateralWithFee.sub(tokenAccountBalance), " tokens");
|
1515
|
+
}
|
1516
|
+
_c.label = 7;
|
1517
|
+
case 7: return [4, this.program.methods.addCollateral({
|
1518
|
+
collateralDelta: collateralWithFee
|
1519
|
+
}).accounts({
|
1520
|
+
owner: publicKey,
|
1521
|
+
position: positionPubKey,
|
1522
|
+
fundingAccount: collateralSymbol == 'SOL' ? wrappedSolAccount.publicKey : userPayingTokenAccount,
|
1523
|
+
transferAuthority: poolConfig.transferAuthority,
|
1524
|
+
perpetuals: poolConfig.perpetuals,
|
1525
|
+
pool: poolConfig.poolAddress,
|
1526
|
+
custody: marketCustodyConfig.custodyAccount,
|
1527
|
+
custodyOracleAccount: marketCustodyConfig.oracleAddress,
|
1528
|
+
custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1529
|
+
collateralCustody: collateralCustodyConfig.custodyAccount,
|
1530
|
+
collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
|
1531
|
+
collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1532
|
+
collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
|
1533
|
+
eventAuthority: this.eventAuthority.publicKey,
|
1534
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
1535
|
+
program: this.programId,
|
1536
|
+
})
|
1537
|
+
.instruction()];
|
1538
|
+
case 8:
|
1539
|
+
instruction = _c.sent();
|
1540
|
+
instructions.push(instruction);
|
1541
|
+
return [2, {
|
1542
|
+
instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
|
1543
|
+
additionalSigners: additionalSigners
|
1544
|
+
}];
|
1545
|
+
}
|
1546
|
+
});
|
1437
1547
|
});
|
1438
|
-
}
|
1439
|
-
this.removeCollateral = function (
|
1440
|
-
|
1441
|
-
|
1442
|
-
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1446
|
-
|
1447
|
-
|
1448
|
-
|
1449
|
-
|
1450
|
-
|
1451
|
-
|
1452
|
-
|
1453
|
-
|
1454
|
-
|
1455
|
-
|
1456
|
-
|
1457
|
-
|
1458
|
-
|
1459
|
-
|
1460
|
-
|
1461
|
-
|
1548
|
+
};
|
1549
|
+
this.removeCollateral = function (collateralWithFee, marketSymbol, collateralSymbol, positionPubKey, poolConfig, createUserATA, closeUsersWSOLATA) {
|
1550
|
+
if (createUserATA === void 0) { createUserATA = true; }
|
1551
|
+
if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
|
1552
|
+
return __awaiter(_this, void 0, void 0, function () {
|
1553
|
+
var publicKey, collateralCustodyConfig, marketCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, instruction, closeWsolATAIns, error_3;
|
1554
|
+
return __generator(this, function (_b) {
|
1555
|
+
switch (_b.label) {
|
1556
|
+
case 0:
|
1557
|
+
publicKey = this.provider.wallet.publicKey;
|
1558
|
+
collateralCustodyConfig = poolConfig.custodies.find(function (i) {
|
1559
|
+
return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
|
1560
|
+
});
|
1561
|
+
marketCustodyConfig = poolConfig.custodies.find(function (i) {
|
1562
|
+
return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey);
|
1563
|
+
});
|
1564
|
+
if (!collateralCustodyConfig || !marketCustodyConfig) {
|
1565
|
+
throw "collateralCustodyConfig/marketCustodyConfig not found";
|
1566
|
+
}
|
1567
|
+
preInstructions = [];
|
1568
|
+
instructions = [];
|
1569
|
+
postInstructions = [];
|
1570
|
+
additionalSigners = [];
|
1571
|
+
_b.label = 1;
|
1572
|
+
case 1:
|
1573
|
+
_b.trys.push([1, 9, , 10]);
|
1574
|
+
if (!(collateralSymbol == 'SOL')) return [3, 3];
|
1575
|
+
wrappedSolAccount = new web3_js_1.Keypair();
|
1576
|
+
userReceivingTokenAccount = wrappedSolAccount.publicKey;
|
1577
|
+
return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
|
1578
|
+
case 2:
|
1579
|
+
lamports = (_b.sent());
|
1580
|
+
preInstructions = [
|
1581
|
+
web3_js_1.SystemProgram.createAccount({
|
1582
|
+
fromPubkey: publicKey,
|
1583
|
+
newAccountPubkey: wrappedSolAccount.publicKey,
|
1584
|
+
lamports: lamports,
|
1585
|
+
space: 165,
|
1586
|
+
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
1587
|
+
}),
|
1588
|
+
(0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
|
1589
|
+
];
|
1590
|
+
postInstructions = [
|
1591
|
+
(0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
|
1592
|
+
];
|
1593
|
+
additionalSigners.push(wrappedSolAccount);
|
1594
|
+
return [3, 7];
|
1595
|
+
case 3: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey)];
|
1596
|
+
case 4:
|
1597
|
+
userReceivingTokenAccount = _b.sent();
|
1598
|
+
_a = createUserATA;
|
1599
|
+
if (!_a) return [3, 6];
|
1600
|
+
return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
|
1601
|
+
case 5:
|
1602
|
+
_a = !(_b.sent());
|
1603
|
+
_b.label = 6;
|
1604
|
+
case 6:
|
1605
|
+
if (_a) {
|
1606
|
+
preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
|
1607
|
+
}
|
1608
|
+
_b.label = 7;
|
1609
|
+
case 7: return [4, this.program.methods
|
1462
1610
|
.removeCollateral({
|
1463
|
-
collateralDelta:
|
1611
|
+
collateralDelta: collateralWithFee,
|
1464
1612
|
})
|
1465
1613
|
.accounts({
|
1466
1614
|
owner: publicKey,
|
@@ -1471,185 +1619,194 @@ var PerpetualsClient = (function () {
|
|
1471
1619
|
position: positionPubKey,
|
1472
1620
|
custody: marketCustodyConfig.custodyAccount,
|
1473
1621
|
custodyOracleAccount: marketCustodyConfig.oracleAddress,
|
1622
|
+
custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1474
1623
|
collateralCustody: collateralCustodyConfig.custodyAccount,
|
1475
1624
|
collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
|
1625
|
+
collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1476
1626
|
collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
|
1477
1627
|
eventAuthority: this.eventAuthority.publicKey,
|
1478
1628
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
1629
|
+
program: this.programId,
|
1479
1630
|
})
|
1480
1631
|
.instruction()];
|
1481
|
-
|
1482
|
-
|
1483
|
-
|
1484
|
-
|
1485
|
-
|
1486
|
-
|
1487
|
-
|
1488
|
-
|
1489
|
-
|
1632
|
+
case 8:
|
1633
|
+
instruction = _b.sent();
|
1634
|
+
instructions.push(instruction);
|
1635
|
+
if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
|
1636
|
+
closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
|
1637
|
+
postInstructions.push(closeWsolATAIns);
|
1638
|
+
}
|
1639
|
+
return [3, 10];
|
1640
|
+
case 9:
|
1641
|
+
error_3 = _b.sent();
|
1642
|
+
console.error("perpclient removeCollateral error:", error_3);
|
1643
|
+
return [3, 10];
|
1644
|
+
case 10: return [2, {
|
1645
|
+
instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
|
1646
|
+
additionalSigners: additionalSigners
|
1647
|
+
}];
|
1648
|
+
}
|
1649
|
+
});
|
1490
1650
|
});
|
1491
|
-
}
|
1492
|
-
this.addLiquidity = function (payTokenSymbol, tokenAmountIn, minLpAmountOut, poolConfig
|
1493
|
-
|
1494
|
-
return
|
1495
|
-
|
1496
|
-
|
1497
|
-
|
1498
|
-
|
1499
|
-
|
1500
|
-
|
1501
|
-
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1505
|
-
|
1506
|
-
|
1507
|
-
|
1508
|
-
|
1509
|
-
|
1510
|
-
|
1511
|
-
|
1512
|
-
|
1513
|
-
|
1514
|
-
|
1515
|
-
|
1516
|
-
|
1517
|
-
custodyAccountMetas
|
1518
|
-
|
1519
|
-
|
1520
|
-
|
1521
|
-
|
1522
|
-
|
1523
|
-
|
1524
|
-
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
|
1529
|
-
|
1530
|
-
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1534
|
-
|
1535
|
-
|
1536
|
-
|
1537
|
-
|
1538
|
-
|
1539
|
-
|
1540
|
-
|
1541
|
-
|
1542
|
-
|
1543
|
-
|
1544
|
-
|
1545
|
-
|
1546
|
-
|
1547
|
-
|
1548
|
-
|
1549
|
-
|
1550
|
-
|
1551
|
-
|
1552
|
-
|
1553
|
-
|
1554
|
-
|
1555
|
-
|
1556
|
-
|
1557
|
-
|
1558
|
-
|
1559
|
-
|
1560
|
-
|
1561
|
-
|
1562
|
-
|
1563
|
-
|
1564
|
-
|
1565
|
-
|
1566
|
-
|
1567
|
-
|
1568
|
-
|
1569
|
-
|
1570
|
-
|
1571
|
-
|
1572
|
-
|
1573
|
-
|
1574
|
-
|
1575
|
-
|
1576
|
-
|
1577
|
-
|
1578
|
-
|
1579
|
-
|
1580
|
-
|
1581
|
-
|
1582
|
-
|
1583
|
-
|
1584
|
-
|
1585
|
-
|
1586
|
-
|
1587
|
-
|
1588
|
-
|
1589
|
-
|
1590
|
-
|
1591
|
-
|
1592
|
-
|
1593
|
-
|
1594
|
-
|
1595
|
-
|
1596
|
-
|
1597
|
-
|
1598
|
-
|
1599
|
-
|
1600
|
-
|
1601
|
-
|
1602
|
-
|
1603
|
-
|
1604
|
-
|
1605
|
-
|
1606
|
-
|
1607
|
-
|
1608
|
-
|
1609
|
-
|
1610
|
-
|
1611
|
-
|
1612
|
-
.
|
1613
|
-
|
1614
|
-
|
1615
|
-
|
1616
|
-
|
1617
|
-
|
1618
|
-
|
1619
|
-
|
1620
|
-
|
1621
|
-
|
1622
|
-
|
1623
|
-
}
|
1651
|
+
};
|
1652
|
+
this.addLiquidity = function (payTokenSymbol, tokenAmountIn, minLpAmountOut, poolConfig, skipBalanceChecks) {
|
1653
|
+
if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
|
1654
|
+
return __awaiter(_this, void 0, void 0, function () {
|
1655
|
+
var publicKey, payTokenCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userPayingTokenAccount, lpTokenAccount, custodyAccountMetas, custodyOracleAccountMetas, custodyCustomOracles, _i, _a, custody, accCreationLamports, lamports, unWrappedSolBalance, _b, tokenAccountBalance, _c, instruction, err_3;
|
1656
|
+
return __generator(this, function (_d) {
|
1657
|
+
switch (_d.label) {
|
1658
|
+
case 0:
|
1659
|
+
publicKey = this.provider.wallet.publicKey;
|
1660
|
+
payTokenCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
|
1661
|
+
if (!payTokenCustodyConfig) {
|
1662
|
+
throw "payTokenCustodyConfig not found";
|
1663
|
+
}
|
1664
|
+
preInstructions = [];
|
1665
|
+
instructions = [];
|
1666
|
+
postInstructions = [];
|
1667
|
+
additionalSigners = [];
|
1668
|
+
_d.label = 1;
|
1669
|
+
case 1:
|
1670
|
+
_d.trys.push([1, 12, , 13]);
|
1671
|
+
return [4, (0, spl_token_1.getAssociatedTokenAddress)(payTokenCustodyConfig.mintKey, publicKey)];
|
1672
|
+
case 2:
|
1673
|
+
userPayingTokenAccount = _d.sent();
|
1674
|
+
return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.lpTokenMint, publicKey)];
|
1675
|
+
case 3:
|
1676
|
+
lpTokenAccount = _d.sent();
|
1677
|
+
custodyAccountMetas = [];
|
1678
|
+
custodyOracleAccountMetas = [];
|
1679
|
+
custodyCustomOracles = [];
|
1680
|
+
for (_i = 0, _a = poolConfig.custodies; _i < _a.length; _i++) {
|
1681
|
+
custody = _a[_i];
|
1682
|
+
custodyAccountMetas.push({
|
1683
|
+
pubkey: custody.custodyAccount,
|
1684
|
+
isSigner: false,
|
1685
|
+
isWritable: false,
|
1686
|
+
});
|
1687
|
+
custodyOracleAccountMetas.push({
|
1688
|
+
pubkey: custody.oracleAddress,
|
1689
|
+
isSigner: false,
|
1690
|
+
isWritable: false,
|
1691
|
+
});
|
1692
|
+
custodyCustomOracles.push({
|
1693
|
+
pubkey: web3_js_1.SystemProgram.programId,
|
1694
|
+
isSigner: false,
|
1695
|
+
isWritable: false,
|
1696
|
+
});
|
1697
|
+
}
|
1698
|
+
return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
|
1699
|
+
case 4:
|
1700
|
+
if (!(_d.sent())) {
|
1701
|
+
instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.lpTokenMint));
|
1702
|
+
}
|
1703
|
+
if (!(payTokenSymbol == 'SOL')) return [3, 7];
|
1704
|
+
console.log("payTokenSymbol === SOL", payTokenSymbol);
|
1705
|
+
wrappedSolAccount = new web3_js_1.Keypair();
|
1706
|
+
return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
|
1707
|
+
case 5:
|
1708
|
+
accCreationLamports = (_d.sent());
|
1709
|
+
console.log("accCreationLamports:", accCreationLamports);
|
1710
|
+
lamports = tokenAmountIn.add(new anchor_1.BN(accCreationLamports));
|
1711
|
+
_b = anchor_1.BN.bind;
|
1712
|
+
return [4, this.provider.connection.getBalance(publicKey)];
|
1713
|
+
case 6:
|
1714
|
+
unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _d.sent()]))();
|
1715
|
+
if (unWrappedSolBalance.lt(lamports)) {
|
1716
|
+
throw "Insufficient SOL Funds";
|
1717
|
+
}
|
1718
|
+
preInstructions = [
|
1719
|
+
web3_js_1.SystemProgram.createAccount({
|
1720
|
+
fromPubkey: publicKey,
|
1721
|
+
newAccountPubkey: wrappedSolAccount.publicKey,
|
1722
|
+
lamports: lamports.toNumber(),
|
1723
|
+
space: 165,
|
1724
|
+
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
1725
|
+
}),
|
1726
|
+
(0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
|
1727
|
+
];
|
1728
|
+
postInstructions = [
|
1729
|
+
(0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
|
1730
|
+
];
|
1731
|
+
additionalSigners.push(wrappedSolAccount);
|
1732
|
+
return [3, 10];
|
1733
|
+
case 7: return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
|
1734
|
+
case 8:
|
1735
|
+
if (!(_d.sent())) {
|
1736
|
+
throw "Insufficient Funds , token Account doesn't exist";
|
1737
|
+
}
|
1738
|
+
if (!!skipBalanceChecks) return [3, 10];
|
1739
|
+
_c = anchor_1.BN.bind;
|
1740
|
+
return [4, this.provider.connection.getTokenAccountBalance(userPayingTokenAccount)];
|
1741
|
+
case 9:
|
1742
|
+
tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_d.sent()).value.amount]))();
|
1743
|
+
if (tokenAccountBalance.lt(tokenAmountIn)) {
|
1744
|
+
throw "Insufficient Funds need more ".concat(tokenAmountIn.sub(tokenAccountBalance), " tokens");
|
1745
|
+
}
|
1746
|
+
_d.label = 10;
|
1747
|
+
case 10: return [4, this.program.methods
|
1748
|
+
.addLiquidity({
|
1749
|
+
amountIn: tokenAmountIn,
|
1750
|
+
minLpAmountOut: minLpAmountOut
|
1751
|
+
})
|
1752
|
+
.accounts({
|
1753
|
+
owner: publicKey,
|
1754
|
+
fundingAccount: payTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userPayingTokenAccount,
|
1755
|
+
lpTokenAccount: lpTokenAccount,
|
1756
|
+
transferAuthority: poolConfig.transferAuthority,
|
1757
|
+
perpetuals: poolConfig.perpetuals,
|
1758
|
+
pool: poolConfig.poolAddress,
|
1759
|
+
custody: payTokenCustodyConfig.custodyAccount,
|
1760
|
+
custodyOracleAccount: payTokenCustodyConfig.oracleAddress,
|
1761
|
+
custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1762
|
+
custodyTokenAccount: payTokenCustodyConfig.tokenAccount,
|
1763
|
+
lpTokenMint: poolConfig.lpTokenMint,
|
1764
|
+
eventAuthority: this.eventAuthority.publicKey,
|
1765
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
1766
|
+
program: this.programId,
|
1767
|
+
})
|
1768
|
+
.remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), custodyCustomOracles, true))
|
1769
|
+
.instruction()];
|
1770
|
+
case 11:
|
1771
|
+
instruction = _d.sent();
|
1772
|
+
instructions.push(instruction);
|
1773
|
+
return [3, 13];
|
1774
|
+
case 12:
|
1775
|
+
err_3 = _d.sent();
|
1776
|
+
console.error("perpClient addLiquidity error:: ", err_3);
|
1777
|
+
throw err_3;
|
1778
|
+
case 13: return [2, {
|
1779
|
+
instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
|
1780
|
+
additionalSigners: additionalSigners
|
1781
|
+
}];
|
1782
|
+
}
|
1783
|
+
});
|
1624
1784
|
});
|
1625
|
-
}
|
1626
|
-
this.removeLiquidity = function (recieveTokenSymbol, liquidityAmountIn, minTokenAmountOut, poolConfig, closeLpATA) {
|
1785
|
+
};
|
1786
|
+
this.removeLiquidity = function (recieveTokenSymbol, liquidityAmountIn, minTokenAmountOut, poolConfig, closeLpATA, createUserATA, closeUsersWSOLATA) {
|
1627
1787
|
if (closeLpATA === void 0) { closeLpATA = false; }
|
1788
|
+
if (createUserATA === void 0) { createUserATA = true; }
|
1789
|
+
if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
|
1628
1790
|
return __awaiter(_this, void 0, void 0, function () {
|
1629
|
-
var
|
1630
|
-
return __generator(this, function (
|
1631
|
-
switch (
|
1791
|
+
var recieveTokenCustodyConfig, publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lpTokenAccount, custodyAccountMetas, custodyOracleAccountMetas, custodyCustomOracles, _i, _a, custody, lamports, _b, removeLiquidityTx, closeInx, closeWsolATAIns, err_4;
|
1792
|
+
return __generator(this, function (_c) {
|
1793
|
+
switch (_c.label) {
|
1632
1794
|
case 0:
|
1633
|
-
|
1634
|
-
if (!
|
1795
|
+
recieveTokenCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(recieveTokenSymbol).mintKey); });
|
1796
|
+
if (!recieveTokenCustodyConfig) {
|
1635
1797
|
throw "recieveTokenCustody not found";
|
1636
1798
|
}
|
1637
1799
|
publicKey = this.provider.wallet.publicKey;
|
1800
|
+
preInstructions = [];
|
1638
1801
|
instructions = [];
|
1639
|
-
|
1802
|
+
postInstructions = [];
|
1803
|
+
additionalSigners = [];
|
1804
|
+
_c.label = 1;
|
1640
1805
|
case 1:
|
1641
|
-
|
1642
|
-
return [4, (0, spl_token_1.getAssociatedTokenAddress)(recieveTokenCustody.mintKey, publicKey)];
|
1643
|
-
case 2:
|
1644
|
-
userRecievingTokenAccount = _b.sent();
|
1645
|
-
return [4, (0, utils_1.checkIfAccountExists)(userRecievingTokenAccount, this.provider.connection)];
|
1646
|
-
case 3:
|
1647
|
-
if (!(_b.sent())) {
|
1648
|
-
instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userRecievingTokenAccount, publicKey, recieveTokenCustody.mintKey));
|
1649
|
-
}
|
1806
|
+
_c.trys.push([1, 10, , 11]);
|
1650
1807
|
return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.lpTokenMint, publicKey)];
|
1651
|
-
case
|
1652
|
-
lpTokenAccount =
|
1808
|
+
case 2:
|
1809
|
+
lpTokenAccount = _c.sent();
|
1653
1810
|
custodyAccountMetas = [];
|
1654
1811
|
custodyOracleAccountMetas = [];
|
1655
1812
|
custodyCustomOracles = [];
|
@@ -1671,6 +1828,42 @@ var PerpetualsClient = (function () {
|
|
1671
1828
|
isWritable: false,
|
1672
1829
|
});
|
1673
1830
|
}
|
1831
|
+
if (!(recieveTokenSymbol == 'SOL')) return [3, 4];
|
1832
|
+
wrappedSolAccount = new web3_js_1.Keypair();
|
1833
|
+
userReceivingTokenAccount = wrappedSolAccount.publicKey;
|
1834
|
+
return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
|
1835
|
+
case 3:
|
1836
|
+
lamports = (_c.sent());
|
1837
|
+
preInstructions = [
|
1838
|
+
web3_js_1.SystemProgram.createAccount({
|
1839
|
+
fromPubkey: publicKey,
|
1840
|
+
newAccountPubkey: wrappedSolAccount.publicKey,
|
1841
|
+
lamports: lamports,
|
1842
|
+
space: 165,
|
1843
|
+
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
1844
|
+
}),
|
1845
|
+
(0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
|
1846
|
+
];
|
1847
|
+
postInstructions = [
|
1848
|
+
(0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
|
1849
|
+
];
|
1850
|
+
additionalSigners.push(wrappedSolAccount);
|
1851
|
+
return [3, 8];
|
1852
|
+
case 4: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(recieveTokenSymbol).mintKey, publicKey)];
|
1853
|
+
case 5:
|
1854
|
+
userReceivingTokenAccount = _c.sent();
|
1855
|
+
_b = createUserATA;
|
1856
|
+
if (!_b) return [3, 7];
|
1857
|
+
return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
|
1858
|
+
case 6:
|
1859
|
+
_b = !(_c.sent());
|
1860
|
+
_c.label = 7;
|
1861
|
+
case 7:
|
1862
|
+
if (_b) {
|
1863
|
+
preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(recieveTokenSymbol).mintKey));
|
1864
|
+
}
|
1865
|
+
_c.label = 8;
|
1866
|
+
case 8:
|
1674
1867
|
console.log("liquidityAmountIn", liquidityAmountIn.toString());
|
1675
1868
|
return [4, this.program.methods
|
1676
1869
|
.removeLiquidity({
|
@@ -1679,39 +1872,42 @@ var PerpetualsClient = (function () {
|
|
1679
1872
|
})
|
1680
1873
|
.accounts({
|
1681
1874
|
owner: publicKey,
|
1682
|
-
receivingAccount:
|
1875
|
+
receivingAccount: recieveTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userReceivingTokenAccount,
|
1683
1876
|
lpTokenAccount: lpTokenAccount,
|
1684
1877
|
transferAuthority: poolConfig.transferAuthority,
|
1685
1878
|
perpetuals: poolConfig.perpetuals,
|
1686
1879
|
pool: poolConfig.poolAddress,
|
1687
|
-
custody:
|
1688
|
-
custodyOracleAccount:
|
1689
|
-
|
1880
|
+
custody: recieveTokenCustodyConfig.custodyAccount,
|
1881
|
+
custodyOracleAccount: recieveTokenCustodyConfig.oracleAddress,
|
1882
|
+
custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1883
|
+
custodyTokenAccount: recieveTokenCustodyConfig.tokenAccount,
|
1690
1884
|
lpTokenMint: poolConfig.lpTokenMint,
|
1691
1885
|
eventAuthority: this.eventAuthority.publicKey,
|
1692
1886
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
1693
|
-
custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
|
1694
1887
|
program: this.programId,
|
1695
1888
|
})
|
1696
1889
|
.remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), custodyCustomOracles, true))
|
1697
1890
|
.instruction()];
|
1698
|
-
case
|
1699
|
-
removeLiquidityTx =
|
1891
|
+
case 9:
|
1892
|
+
removeLiquidityTx = _c.sent();
|
1700
1893
|
instructions.push(removeLiquidityTx);
|
1701
1894
|
if (closeLpATA) {
|
1702
1895
|
closeInx = (0, spl_token_1.createCloseAccountInstruction)(lpTokenAccount, publicKey, publicKey);
|
1703
1896
|
instructions.push(closeInx);
|
1704
1897
|
}
|
1705
|
-
if (recieveTokenSymbol == '
|
1706
|
-
closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(
|
1707
|
-
|
1898
|
+
if (recieveTokenSymbol == 'WSOL' && closeUsersWSOLATA) {
|
1899
|
+
closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
|
1900
|
+
postInstructions.push(closeWsolATAIns);
|
1708
1901
|
}
|
1709
|
-
return [3,
|
1710
|
-
case
|
1711
|
-
err_4 =
|
1902
|
+
return [3, 11];
|
1903
|
+
case 10:
|
1904
|
+
err_4 = _c.sent();
|
1712
1905
|
console.log("perpClient removeLiquidity error:: ", err_4);
|
1713
1906
|
throw err_4;
|
1714
|
-
case
|
1907
|
+
case 11: return [2, {
|
1908
|
+
instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
|
1909
|
+
additionalSigners: additionalSigners
|
1910
|
+
}];
|
1715
1911
|
}
|
1716
1912
|
});
|
1717
1913
|
});
|