flash-sdk 2.42.9-alpha.0 → 2.42.11

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.
@@ -4340,7 +4340,7 @@ export declare class PerpetualsClient {
4340
4340
  closeAmount: BN;
4341
4341
  feesAmount: BN;
4342
4342
  };
4343
- getMaxWithdrawableAmountSync: (positionAccount: PositionAccount, targetPrice: OraclePrice, targetEmaPrice: OraclePrice, targetCustodyAccount: CustodyAccount, collateralPrice: OraclePrice, collateralEmaPrice: OraclePrice, collateralCustodyAccount: CustodyAccount, currentTimestamp: BN, poolConfig: PoolConfig, errorBandwidthPercentageUi?: number) => BN;
4343
+ getMaxWithdrawableAmountSync: (positionAccount: PositionAccount, targetPrice: OraclePrice, targetEmaPrice: OraclePrice, targetCustodyAccount: CustodyAccount, collateralPrice: OraclePrice, collateralEmaPrice: OraclePrice, collateralCustodyAccount: CustodyAccount, currentTimestamp: BN, poolConfig: PoolConfig, closeAmount?: BN, errorBandwidthPercentageUi?: number) => BN;
4344
4344
  getCumulativeLockFeeSync: (custodyAccount: CustodyAccount, currentTimestamp: BN) => BN;
4345
4345
  getBorrowRateSync: (custodyAccount: CustodyAccount, currentUtilization: BN) => BN;
4346
4346
  getLockFeeAndUnsettledUsdForPosition: (position: PositionAccount, collateralCustodyAccount: CustodyAccount, currentTimestamp: BN) => BN;
@@ -4600,7 +4600,7 @@ export declare class PerpetualsClient {
4600
4600
  instructions: TransactionInstruction[];
4601
4601
  additionalSigners: Signer[];
4602
4602
  }>;
4603
- editTriggerOrder: (targetSymbol: string, collateralSymbol: string, receiveSymbol: string, side: Side, orderId: number, triggerPrice: ContractOraclePrice, deltaSizeAmount: BN, isStopLoss: boolean, poolConfig: PoolConfig) => Promise<{
4603
+ editTriggerOrder: (targetSymbol: string, collateralSymbol: string, side: Side, orderId: number, triggerPrice: ContractOraclePrice, deltaSizeAmount: BN, isStopLoss: boolean, receiveCustodyId: number, poolConfig: PoolConfig) => Promise<{
4604
4604
  instructions: TransactionInstruction[];
4605
4605
  additionalSigners: Signer[];
4606
4606
  }>;
@@ -1280,6 +1280,9 @@ var PerpetualsClient = (function () {
1280
1280
  var liabilityUsd = newPnl.lossUsd.add(totalFeesUsd);
1281
1281
  var assetsUsd = newPnl.profitUsd.add(currentCollateralUsd);
1282
1282
  var closeAmount, feesAmount;
1283
+ if (debugLogs) {
1284
+ console.log("assetsUsd.sub(liabilityUsd):", collateralCustodyAccount.decimals, assetsUsd.toString(), liabilityUsd.toString(), assetsUsd.sub(liabilityUsd).toString());
1285
+ }
1283
1286
  if (assetsUsd.gte(liabilityUsd)) {
1284
1287
  closeAmount = collateralMinMaxPrice.max.getTokenAmount(assetsUsd.sub(liabilityUsd), collateralCustodyAccount.decimals);
1285
1288
  feesAmount = collateralMinMaxPrice.min.getTokenAmount(totalFeesUsd, collateralCustodyAccount.decimals);
@@ -1293,7 +1296,8 @@ var PerpetualsClient = (function () {
1293
1296
  newPosition.sizeUsd = positionAccount.sizeUsd.sub(positionDelta.sizeUsd);
1294
1297
  newPosition.lockedUsd = positionAccount.lockedUsd.sub(positionDelta.lockedUsd);
1295
1298
  newPosition.lockedAmount = positionAccount.lockedAmount.sub(positionDelta.lockedAmount);
1296
- newPosition.collateralAmount = positionAccount.collateralAmount.sub(closeAmount);
1299
+ newPosition.collateralAmount = positionAccount.collateralAmount.sub(positionDelta.collateralAmount);
1300
+ newPosition.unsettledFeesUsd = positionAccount.unsettledFeesUsd.sub(positionDelta.unsettledFeesUsd);
1297
1301
  newPosition.collateralUsd = collateralMinMaxPrice.min.getAssetAmountUsd(newPosition.collateralAmount, collateralCustodyAccount.decimals);
1298
1302
  var feeUsdWithDiscount = constants_1.BN_ZERO;
1299
1303
  var feeUsd = sizeDeltaUsd.mul(targetCustodyAccount.fees.closePosition).div(new anchor_1.BN(constants_1.RATE_POWER));
@@ -1312,7 +1316,7 @@ var PerpetualsClient = (function () {
1312
1316
  var pnlUsdWithFee = pnlUsd.sub(lockAndUnsettledFeeUsd_1).sub(exitFee.exitFeeUsd);
1313
1317
  var collateralAmountReceived = closeAmount;
1314
1318
  var collateralAmountRecievedUsd = collateralMinMaxPrice.min.getAssetAmountUsd(collateralAmountReceived, collateralCustodyAccount.decimals);
1315
- var maxWithdrawableAmount = _this.getMaxWithdrawableAmountSync(newPosition, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, poolConfig);
1319
+ var maxWithdrawableAmount = _this.getMaxWithdrawableAmountSync(newPosition, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, poolConfig, closeAmount);
1316
1320
  if (debugLogs) {
1317
1321
  console.log("maxWithdrawableAmount ", maxWithdrawableAmount.toString(), keepLevSame);
1318
1322
  console.log("collateralAmountReceived ", collateralAmountReceived.toString(), keepLevSame);
@@ -1346,7 +1350,7 @@ var PerpetualsClient = (function () {
1346
1350
  };
1347
1351
  }
1348
1352
  else {
1349
- var collateralAmountReceived = closeAmount.sub(positionDelta.collateralAmount);
1353
+ var collateralAmountReceived = closeAmount;
1350
1354
  if (collateralAmountReceived.lte(constants_1.BN_ZERO)) {
1351
1355
  collateralAmountReceived = constants_1.BN_ZERO;
1352
1356
  }
@@ -1366,7 +1370,7 @@ var PerpetualsClient = (function () {
1366
1370
  newLev: newLev,
1367
1371
  liquidationPrice: finalLiquidationPrice,
1368
1372
  collateralAmountRecieved: collateralAmountReceived,
1369
- newCollateralAmount: newPosition.collateralAmount.sub(collateralAmountReceived),
1373
+ newCollateralAmount: newPosition.collateralAmount,
1370
1374
  newPnl: finalPnlUsd
1371
1375
  };
1372
1376
  }
@@ -1391,20 +1395,16 @@ var PerpetualsClient = (function () {
1391
1395
  }
1392
1396
  return { closeAmount: closeAmount, feesAmount: feesAmount };
1393
1397
  };
1394
- this.getMaxWithdrawableAmountSync = function (positionAccount, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, poolConfig, errorBandwidthPercentageUi) {
1398
+ this.getMaxWithdrawableAmountSync = function (positionAccount, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, poolConfig, closeAmount, errorBandwidthPercentageUi) {
1399
+ if (closeAmount === void 0) { closeAmount = constants_1.BN_ZERO; }
1395
1400
  if (errorBandwidthPercentageUi === void 0) { errorBandwidthPercentageUi = 5; }
1396
1401
  if (errorBandwidthPercentageUi > 100 || errorBandwidthPercentageUi < 0) {
1397
1402
  throw new Error("errorBandwidthPercentageUi cannot be >100 or <0");
1398
1403
  }
1399
1404
  var MAX_INIT_LEVERAGE = targetCustodyAccount.pricing.maxInitialLeverage.mul(new anchor_1.BN(100 - errorBandwidthPercentageUi)).div(new anchor_1.BN(100));
1400
- var maxRemoveableCollateralUsdAfterMinRequired = positionAccount.collateralUsd.sub(targetCustodyAccount.pricing.minCollateralUsd.mul(new anchor_1.BN(100 + errorBandwidthPercentageUi)).div(new anchor_1.BN(100)));
1401
- if (maxRemoveableCollateralUsdAfterMinRequired.isNeg()) {
1402
- console.log("THIS cannot happen but still");
1403
- return constants_1.BN_ZERO;
1404
- }
1405
1405
  var profitLoss = _this.getPnlSync(positionAccount, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, targetCustodyAccount.pricing.delaySeconds, poolConfig);
1406
1406
  var _a = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount), collateralMinPrice = _a.min, collateralMaxPrice = _a.max;
1407
- var currentCollateralUsd = collateralMinPrice.getAssetAmountUsd(positionAccount.collateralAmount, collateralCustodyAccount.decimals);
1407
+ var currentCollateralUsd = collateralMinPrice.getAssetAmountUsd(positionAccount.collateralAmount.add(closeAmount), collateralCustodyAccount.decimals);
1408
1408
  var availableInitMarginUsd = constants_1.BN_ZERO;
1409
1409
  if (profitLoss.lossUsd.lt(currentCollateralUsd)) {
1410
1410
  availableInitMarginUsd = currentCollateralUsd.sub(profitLoss.lossUsd);
@@ -1418,8 +1418,16 @@ var PerpetualsClient = (function () {
1418
1418
  return constants_1.BN_ZERO;
1419
1419
  }
1420
1420
  var maxWithdrawableAmount;
1421
- if (maxRemoveableCollateralUsdAfterMinRequired.lt(maxRemovableCollateralUsd)) {
1422
- maxWithdrawableAmount = collateralMaxPrice.getTokenAmount(maxRemoveableCollateralUsdAfterMinRequired, collateralCustodyAccount.decimals);
1421
+ var remainingCollateralUsd = availableInitMarginUsd.sub(maxRemovableCollateralUsd);
1422
+ if (remainingCollateralUsd < targetCustodyAccount.pricing.minCollateralUsd) {
1423
+ var diff = targetCustodyAccount.pricing.minCollateralUsd.sub(remainingCollateralUsd);
1424
+ var updatedMaxRemovableCollateralUsd = maxRemovableCollateralUsd.sub(diff);
1425
+ if (updatedMaxRemovableCollateralUsd.isNeg()) {
1426
+ return constants_1.BN_ZERO;
1427
+ }
1428
+ else {
1429
+ maxWithdrawableAmount = collateralMaxPrice.getTokenAmount(updatedMaxRemovableCollateralUsd, collateralCustodyAccount.decimals);
1430
+ }
1423
1431
  }
1424
1432
  else {
1425
1433
  maxWithdrawableAmount = collateralMaxPrice.getTokenAmount(maxRemovableCollateralUsd, collateralCustodyAccount.decimals);
@@ -6436,15 +6444,14 @@ var PerpetualsClient = (function () {
6436
6444
  }
6437
6445
  });
6438
6446
  }); };
6439
- this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6440
- var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, editTriggerOrder, err_45;
6447
+ this.editTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, receiveCustodyId, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6448
+ var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, editTriggerOrder, err_45;
6441
6449
  return __generator(this, function (_a) {
6442
6450
  switch (_a.label) {
6443
6451
  case 0:
6444
6452
  publicKey = this.provider.wallet.publicKey;
6445
6453
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6446
6454
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6447
- receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
6448
6455
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
6449
6456
  preInstructions = [];
6450
6457
  instructions = [];
@@ -6460,7 +6467,8 @@ var PerpetualsClient = (function () {
6460
6467
  orderId: orderId,
6461
6468
  triggerPrice: triggerPrice,
6462
6469
  deltaSizeAmount: deltaSizeAmount,
6463
- isStopLoss: isStopLoss
6470
+ isStopLoss: isStopLoss,
6471
+ receiveCustodyId: receiveCustodyId
6464
6472
  })
6465
6473
  .accounts({
6466
6474
  owner: publicKey,
@@ -6473,7 +6481,6 @@ var PerpetualsClient = (function () {
6473
6481
  targetOracleAccount: this.useExtOracleAccount ? targetCustodyConfig.extOracleAccount : targetCustodyConfig.intOracleAccount,
6474
6482
  collateralCustody: collateralCustodyConfig.custodyAccount,
6475
6483
  collateralOracleAccount: this.useExtOracleAccount ? collateralCustodyConfig.extOracleAccount : collateralCustodyConfig.intOracleAccount,
6476
- receiveCustody: receivingCustodyConfig.custodyAccount,
6477
6484
  eventAuthority: this.eventAuthority.publicKey,
6478
6485
  program: this.programId,
6479
6486
  ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
@@ -4233,99 +4233,6 @@ export type Perpetuals = {
4233
4233
  }
4234
4234
  ];
4235
4235
  },
4236
- {
4237
- "name": "decreaseAndRemoveCollateral";
4238
- "accounts": [
4239
- {
4240
- "name": "owner";
4241
- "isMut": false;
4242
- "isSigner": true;
4243
- },
4244
- {
4245
- "name": "receivingAccount";
4246
- "isMut": true;
4247
- "isSigner": false;
4248
- },
4249
- {
4250
- "name": "transferAuthority";
4251
- "isMut": false;
4252
- "isSigner": false;
4253
- },
4254
- {
4255
- "name": "perpetuals";
4256
- "isMut": false;
4257
- "isSigner": false;
4258
- },
4259
- {
4260
- "name": "pool";
4261
- "isMut": false;
4262
- "isSigner": false;
4263
- },
4264
- {
4265
- "name": "position";
4266
- "isMut": true;
4267
- "isSigner": false;
4268
- },
4269
- {
4270
- "name": "market";
4271
- "isMut": true;
4272
- "isSigner": false;
4273
- },
4274
- {
4275
- "name": "targetCustody";
4276
- "isMut": false;
4277
- "isSigner": false;
4278
- },
4279
- {
4280
- "name": "targetOracleAccount";
4281
- "isMut": false;
4282
- "isSigner": false;
4283
- },
4284
- {
4285
- "name": "collateralCustody";
4286
- "isMut": true;
4287
- "isSigner": false;
4288
- },
4289
- {
4290
- "name": "collateralOracleAccount";
4291
- "isMut": false;
4292
- "isSigner": false;
4293
- },
4294
- {
4295
- "name": "collateralCustodyTokenAccount";
4296
- "isMut": true;
4297
- "isSigner": false;
4298
- },
4299
- {
4300
- "name": "tokenProgram";
4301
- "isMut": false;
4302
- "isSigner": false;
4303
- },
4304
- {
4305
- "name": "eventAuthority";
4306
- "isMut": false;
4307
- "isSigner": false;
4308
- },
4309
- {
4310
- "name": "program";
4311
- "isMut": false;
4312
- "isSigner": false;
4313
- },
4314
- {
4315
- "name": "ixSysvar";
4316
- "isMut": false;
4317
- "isSigner": false;
4318
- }
4319
- ];
4320
- "args": [
4321
- {
4322
- "name": "params";
4323
- "type": {
4324
- "defined": "DecreaseAndRemoveCollateralParams";
4325
- };
4326
- }
4327
- ];
4328
- },
4329
4236
  {
4330
4237
  "name": "decreaseSize";
4331
4238
  "accounts": [
@@ -5153,11 +5060,6 @@ export type Perpetuals = {
5153
5060
  "isMut": false;
5154
5061
  "isSigner": false;
5155
5062
  },
5156
- {
5157
- "name": "receiveCustody";
5158
- "isMut": false;
5159
- "isSigner": false;
5160
- },
5161
5063
  {
5162
5064
  "name": "eventAuthority";
5163
5065
  "isMut": false;
@@ -8244,30 +8146,6 @@ export type Perpetuals = {
8244
8146
  ];
8245
8147
  };
8246
8148
  },
8247
- {
8248
- "name": "DecreaseAndRemoveCollateralParams";
8249
- "type": {
8250
- "kind": "struct";
8251
- "fields": [
8252
- {
8253
- "name": "priceWithSlippage";
8254
- "type": {
8255
- "defined": "OraclePrice";
8256
- };
8257
- },
8258
- {
8259
- "name": "sizeDelta";
8260
- "type": "u64";
8261
- },
8262
- {
8263
- "name": "privilege";
8264
- "type": {
8265
- "defined": "Privilege";
8266
- };
8267
- }
8268
- ];
8269
- };
8270
- },
8271
8149
  {
8272
8150
  "name": "DecreaseSizeParams";
8273
8151
  "type": {
@@ -8392,6 +8270,10 @@ export type Perpetuals = {
8392
8270
  {
8393
8271
  "name": "isStopLoss";
8394
8272
  "type": "bool";
8273
+ },
8274
+ {
8275
+ "name": "receiveCustodyId";
8276
+ "type": "u8";
8395
8277
  }
8396
8278
  ];
8397
8279
  };
@@ -11083,9 +10965,6 @@ export type Perpetuals = {
11083
10965
  },
11084
10966
  {
11085
10967
  "name": "CollectStakeReward";
11086
- },
11087
- {
11088
- "name": "DecreaseAndRemoveCollateral";
11089
10968
  }
11090
10969
  ];
11091
10970
  };
@@ -12007,136 +11886,6 @@ export type Perpetuals = {
12007
11886
  }
12008
11887
  ];
12009
11888
  },
12010
- {
12011
- "name": "DecreaseAndRemoveCollateralLog";
12012
- "fields": [
12013
- {
12014
- "name": "owner";
12015
- "type": "publicKey";
12016
- "index": false;
12017
- },
12018
- {
12019
- "name": "market";
12020
- "type": "publicKey";
12021
- "index": false;
12022
- },
12023
- {
12024
- "name": "entryPrice";
12025
- "type": "u64";
12026
- "index": false;
12027
- },
12028
- {
12029
- "name": "entryPriceExponent";
12030
- "type": "i32";
12031
- "index": false;
12032
- },
12033
- {
12034
- "name": "duration";
12035
- "type": "i64";
12036
- "index": false;
12037
- },
12038
- {
12039
- "name": "exitPrice";
12040
- "type": "u64";
12041
- "index": false;
12042
- },
12043
- {
12044
- "name": "exitPriceExponent";
12045
- "type": "i32";
12046
- "index": false;
12047
- },
12048
- {
12049
- "name": "deltaSizeAmount";
12050
- "type": "u64";
12051
- "index": false;
12052
- },
12053
- {
12054
- "name": "deltaSizeUsd";
12055
- "type": "u64";
12056
- "index": false;
12057
- },
12058
- {
12059
- "name": "collateralPrice";
12060
- "type": "u64";
12061
- "index": false;
12062
- },
12063
- {
12064
- "name": "collateralPriceExponent";
12065
- "type": "i32";
12066
- "index": false;
12067
- },
12068
- {
12069
- "name": "deltaCollateralAmount";
12070
- "type": "u64";
12071
- "index": false;
12072
- },
12073
- {
12074
- "name": "finalSizeAmount";
12075
- "type": "u64";
12076
- "index": false;
12077
- },
12078
- {
12079
- "name": "finalSizeUsd";
12080
- "type": "u64";
12081
- "index": false;
12082
- },
12083
- {
12084
- "name": "finalCollateralAmount";
12085
- "type": "u64";
12086
- "index": false;
12087
- },
12088
- {
12089
- "name": "finalCollateralUsd";
12090
- "type": "u64";
12091
- "index": false;
12092
- },
12093
- {
12094
- "name": "profitUsd";
12095
- "type": "u64";
12096
- "index": false;
12097
- },
12098
- {
12099
- "name": "lossUsd";
12100
- "type": "u64";
12101
- "index": false;
12102
- },
12103
- {
12104
- "name": "feeAmount";
12105
- "type": "u64";
12106
- "index": false;
12107
- },
12108
- {
12109
- "name": "feeRebateAmount";
12110
- "type": "u64";
12111
- "index": false;
12112
- },
12113
- {
12114
- "name": "exitFeeAmount";
12115
- "type": "u64";
12116
- "index": false;
12117
- },
12118
- {
12119
- "name": "oracleAccountTime";
12120
- "type": "i64";
12121
- "index": false;
12122
- },
12123
- {
12124
- "name": "oracleAccountType";
12125
- "type": "u8";
12126
- "index": false;
12127
- },
12128
- {
12129
- "name": "oracleAccountPrice";
12130
- "type": "u64";
12131
- "index": false;
12132
- },
12133
- {
12134
- "name": "oracleAccountPriceExponent";
12135
- "type": "i32";
12136
- "index": false;
12137
- }
12138
- ];
12139
- },
12140
11889
  {
12141
11890
  "name": "DecreaseSizeLog";
12142
11891
  "fields": [