flash-sdk 1.0.127 → 1.0.129

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.
@@ -10,6 +10,29 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ var desc = Object.getOwnPropertyDescriptor(m, k);
16
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
+ desc = { enumerable: true, get: function() { return m[k]; } };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }) : (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ }));
24
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
26
+ }) : function(o, v) {
27
+ o["default"] = v;
28
+ });
29
+ var __importStar = (this && this.__importStar) || function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
+ __setModuleDefault(result, mod);
34
+ return result;
35
+ };
13
36
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
37
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
38
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -74,8 +97,11 @@ var rpc_1 = require("./utils/rpc");
74
97
  var utils_1 = require("./utils");
75
98
  var constants_1 = require("./constants");
76
99
  var bignumber_js_1 = __importDefault(require("bignumber.js"));
100
+ var backupOracle_1 = require("./backupOracle");
101
+ var bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
102
+ var nacl = __importStar(require("tweetnacl"));
77
103
  var PerpetualsClient = (function () {
78
- function PerpetualsClient(provider, programId, composabilityProgramId, opts) {
104
+ function PerpetualsClient(provider, poolConfig, composabilityProgramId, opts) {
79
105
  var _this = this;
80
106
  var _a;
81
107
  this.addressLookupTables = [];
@@ -569,6 +595,44 @@ var PerpetualsClient = (function () {
569
595
  }
570
596
  });
571
597
  }); };
598
+ this.getPythnetOraclePrices = function (poolConfig, backupOracleSecretKey) { return __awaiter(_this, void 0, void 0, function () {
599
+ var pythPriceIds, currentPrices, backupOracleAccount, caches_1, permissionlessPythCache, message, signature, preInstruction;
600
+ return __generator(this, function (_a) {
601
+ switch (_a.label) {
602
+ case 0:
603
+ pythPriceIds = poolConfig.custodies.map(function (f) { return f.pythPriceId; });
604
+ return [4, backupOracle_1.pythPriceServiceConnection.getLatestPriceFeeds(pythPriceIds)];
605
+ case 1:
606
+ currentPrices = _a.sent();
607
+ backupOracleAccount = web3_js_1.Keypair.fromSecretKey(bytes_1.bs58.decode(backupOracleSecretKey));
608
+ if (pythPriceIds.length === currentPrices.length) {
609
+ caches_1 = currentPrices.map(function (price) {
610
+ var uncheckedPrice = price.getPriceUnchecked();
611
+ var uncheckedEmaPrice = price.getEmaPriceUnchecked();
612
+ return {
613
+ price: new anchor_1.BN(uncheckedPrice.price),
614
+ expo: uncheckedPrice.expo,
615
+ conf: new anchor_1.BN(uncheckedPrice.conf),
616
+ ema: new anchor_1.BN(uncheckedEmaPrice.price),
617
+ publishTime: new anchor_1.BN(uncheckedPrice.publishTime)
618
+ };
619
+ });
620
+ permissionlessPythCache = {
621
+ backupCache: caches_1
622
+ };
623
+ message = this.program._coder.types.encode('PermissionlessPythCache', permissionlessPythCache);
624
+ signature = nacl.sign.detached(message, backupOracleAccount.secretKey);
625
+ preInstruction = web3_js_1.Ed25519Program.createInstructionWithPublicKey({
626
+ publicKey: backupOracleAccount.publicKey.toBytes(),
627
+ message: message,
628
+ signature: signature,
629
+ });
630
+ return [2, preInstruction];
631
+ }
632
+ return [2];
633
+ }
634
+ });
635
+ }); };
572
636
  this.getLiquidationState = function (wallet, poolName, tokenMint, collateralMint, side) { return __awaiter(_this, void 0, void 0, function () {
573
637
  var _a, _b;
574
638
  var _c;
@@ -587,11 +651,12 @@ var PerpetualsClient = (function () {
587
651
  return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
588
652
  case 1:
589
653
  _c.custodyOracleAccount = _d.sent(),
590
- _c.custodyCustomOracleAccount = web3_js_1.SystemProgram.programId,
654
+ _c.custodyCustomOracleAccount = this.poolConfig.backupOracle,
591
655
  _c.collateralCustody = this.getCustodyKey(poolName, collateralMint),
592
- _c.collateralCustodyCustomOracleAccount = web3_js_1.SystemProgram.programId;
656
+ _c.collateralCustodyCustomOracleAccount = this.poolConfig.backupOracle;
593
657
  return [4, this.getCustodyOracleAccountKey(poolName, collateralMint)];
594
658
  case 2: return [4, _b.apply(_a, [(_c.collateralCustodyOracleAccount = _d.sent(),
659
+ _c.ixSysvar = web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
595
660
  _c)])
596
661
  .view()
597
662
  .catch(function (err) {
@@ -616,10 +681,11 @@ var PerpetualsClient = (function () {
616
681
  pool: this.getPoolKey(poolName),
617
682
  custody: marketCustodyConfig.custodyAccount,
618
683
  custodyOracleAccount: marketCustodyConfig.oracleAddress,
619
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
684
+ custodyCustomOracleAccount: this.poolConfig.backupOracle,
620
685
  collateralCustody: collateralCustodyConfig.custodyAccount,
621
686
  collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
622
- collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
687
+ collateralCustodyCustomOracleAccount: this.poolConfig.backupOracle,
688
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
623
689
  })
624
690
  .remainingAccounts([])
625
691
  .rpc()
@@ -659,11 +725,12 @@ var PerpetualsClient = (function () {
659
725
  return [4, this.getCustodyOracleAccountKey(poolName, collateralMint)];
660
726
  case 2: return [4, _b.apply(_a, [(_c.collateralCustodyOracleAccount = _d.sent(),
661
727
  _c.collateralCustodyTokenAccount = this.getCustodyTokenAccountKey(poolName, collateralMint),
662
- _c.collateralCustodyCustomOracleAccount = web3_js_1.SystemProgram.programId,
663
- _c.custodyCustomOracleAccount = web3_js_1.SystemProgram.programId,
728
+ _c.collateralCustodyCustomOracleAccount = this.poolConfig.backupOracle,
729
+ _c.custodyCustomOracleAccount = this.poolConfig.backupOracle,
664
730
  _c.eventAuthority = this.eventAuthority.publicKey,
665
731
  _c.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
666
732
  _c.program = this.program.programId,
733
+ _c.ixSysvar = web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
667
734
  _c)])
668
735
  .rpc()
669
736
  .catch(function (err) {
@@ -692,6 +759,7 @@ var PerpetualsClient = (function () {
692
759
  };
693
760
  return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
694
761
  case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
762
+ _c.ixSysvar = web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
695
763
  _c)])
696
764
  .view()
697
765
  .catch(function (err) {
@@ -722,6 +790,7 @@ var PerpetualsClient = (function () {
722
790
  case 1:
723
791
  _d = (_a = _c.apply(_b, [(_e.custodyOracleAccount = _f.sent(),
724
792
  _e.lpTokenMint = this.getPoolLpTokenKey(poolName),
793
+ _e.ixSysvar = web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
725
794
  _e)]))
726
795
  .remainingAccounts;
727
796
  return [4, this.getCustodyMetas(poolName)];
@@ -755,6 +824,7 @@ var PerpetualsClient = (function () {
755
824
  case 1:
756
825
  _d = (_a = _c.apply(_b, [(_e.custodyOracleAccount = _f.sent(),
757
826
  _e.lpTokenMint = this.getPoolLpTokenKey(poolName),
827
+ _e.ixSysvar = web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
758
828
  _e)]))
759
829
  .remainingAccounts;
760
830
  return [4, this.getCustodyMetas(poolName)];
@@ -788,6 +858,7 @@ var PerpetualsClient = (function () {
788
858
  };
789
859
  return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
790
860
  case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
861
+ _c.ixSysvar = web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
791
862
  _c)])
792
863
  .view()
793
864
  .catch(function (err) {
@@ -815,6 +886,7 @@ var PerpetualsClient = (function () {
815
886
  };
816
887
  return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
817
888
  case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
889
+ _c.ixSysvar = web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
818
890
  _c)])
819
891
  .view()
820
892
  .catch(function (err) {
@@ -848,6 +920,7 @@ var PerpetualsClient = (function () {
848
920
  };
849
921
  return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
850
922
  case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
923
+ _c.ixSysvar = web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
851
924
  _c)])
852
925
  .view()
853
926
  .catch(function (err) {
@@ -877,6 +950,7 @@ var PerpetualsClient = (function () {
877
950
  };
878
951
  return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
879
952
  case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
953
+ _c.ixSysvar = web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
880
954
  _c)])
881
955
  .view()
882
956
  .catch(function (err) {
@@ -909,6 +983,7 @@ var PerpetualsClient = (function () {
909
983
  _c.dispensingCustody = this.getCustodyKey(poolName, tokenMintOut);
910
984
  return [4, this.getCustodyOracleAccountKey(poolName, tokenMintOut)];
911
985
  case 2: return [4, _b.apply(_a, [(_c.dispensingCustodyOracleAccount = _d.sent(),
986
+ _c.ixSysvar = web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
912
987
  _c)])
913
988
  .view()
914
989
  .catch(function (err) {
@@ -929,6 +1004,7 @@ var PerpetualsClient = (function () {
929
1004
  .accounts({
930
1005
  perpetuals: this.perpetuals.publicKey,
931
1006
  pool: this.getPoolKey(poolName),
1007
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
932
1008
  }))
933
1009
  .remainingAccounts;
934
1010
  return [4, this.getCustodyMetas(poolName)];
@@ -1792,15 +1868,16 @@ var PerpetualsClient = (function () {
1792
1868
  position: positionAccount,
1793
1869
  custody: marketCustodyConfig.custodyAccount,
1794
1870
  custodyOracleAccount: marketCustodyConfig.oracleAddress,
1795
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1871
+ custodyCustomOracleAccount: this.poolConfig.backupOracle,
1796
1872
  collateralCustody: collateralCustodyConfig.custodyAccount,
1797
1873
  collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1798
- collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1874
+ collateralCustodyCustomOracleAccount: this.poolConfig.backupOracle,
1799
1875
  collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1800
1876
  eventAuthority: this.eventAuthority.publicKey,
1801
1877
  systemProgram: web3_js_1.SystemProgram.programId,
1802
1878
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1803
1879
  program: this.programId,
1880
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
1804
1881
  }).instruction()];
1805
1882
  case 8:
1806
1883
  instruction = _c.sent();
@@ -1907,7 +1984,7 @@ var PerpetualsClient = (function () {
1907
1984
  isWritable: false,
1908
1985
  });
1909
1986
  custodyCustomOracles.push({
1910
- pubkey: web3_js_1.SystemProgram.programId,
1987
+ pubkey: this.poolConfig.backupOracle,
1911
1988
  isSigner: false,
1912
1989
  isWritable: false,
1913
1990
  });
@@ -1917,7 +1994,8 @@ var PerpetualsClient = (function () {
1917
1994
  .accounts({
1918
1995
  payer: publicKey,
1919
1996
  perpetuals: poolConfig.perpetuals,
1920
- pool: poolConfig.poolAddress
1997
+ pool: poolConfig.poolAddress,
1998
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
1921
1999
  })
1922
2000
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), custodyCustomOracles, true))
1923
2001
  .instruction()];
@@ -1942,20 +2020,21 @@ var PerpetualsClient = (function () {
1942
2020
  pool: poolConfig.poolAddress,
1943
2021
  receivingCustody: inputCustodyConfig.custodyAccount,
1944
2022
  receivingCustodyOracleAccount: inputCustodyConfig.oracleAddress,
1945
- receivingCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2023
+ receivingCustodyCustomOracleAccount: this.poolConfig.backupOracle,
1946
2024
  receivingCustodyTokenAccount: inputCustodyConfig.tokenAccount,
1947
2025
  dispensingCustody: collateralCustodyConfig.custodyAccount,
1948
2026
  dispensingCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1949
- dispensingCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2027
+ dispensingCustodyCustomOracleAccount: this.poolConfig.backupOracle,
1950
2028
  dispensingCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1951
2029
  position: positionAccount,
1952
2030
  custody: marketCustodyConfig.custodyAccount,
1953
2031
  custodyOracleAccount: marketCustodyConfig.oracleAddress,
1954
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2032
+ custodyCustomOracleAccount: this.poolConfig.backupOracle,
1955
2033
  eventAuthority: this.eventAuthority.publicKey,
1956
2034
  systemProgram: web3_js_1.SystemProgram.programId,
1957
2035
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1958
2036
  perpProgram: this.programId,
2037
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
1959
2038
  })
1960
2039
  .instruction()];
1961
2040
  case 9:
@@ -2045,14 +2124,15 @@ var PerpetualsClient = (function () {
2045
2124
  position: positionAccount,
2046
2125
  custody: marketCustodyConfig.custodyAccount,
2047
2126
  custodyOracleAccount: marketCustodyConfig.oracleAddress,
2048
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2127
+ custodyCustomOracleAccount: this.poolConfig.backupOracle,
2049
2128
  collateralCustody: collateralCustodyConfig.custodyAccount,
2050
2129
  collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
2051
- collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2130
+ collateralCustodyCustomOracleAccount: this.poolConfig.backupOracle,
2052
2131
  collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
2053
2132
  eventAuthority: this.eventAuthority.publicKey,
2054
2133
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
2055
2134
  program: this.programId,
2135
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
2056
2136
  }).instruction()];
2057
2137
  case 8:
2058
2138
  instruction = _b.sent();
@@ -2150,7 +2230,7 @@ var PerpetualsClient = (function () {
2150
2230
  isWritable: false,
2151
2231
  });
2152
2232
  custodyCustomOracles.push({
2153
- pubkey: web3_js_1.SystemProgram.programId,
2233
+ pubkey: this.poolConfig.backupOracle,
2154
2234
  isSigner: false,
2155
2235
  isWritable: false,
2156
2236
  });
@@ -2160,7 +2240,8 @@ var PerpetualsClient = (function () {
2160
2240
  .accounts({
2161
2241
  payer: publicKey,
2162
2242
  perpetuals: poolConfig.perpetuals,
2163
- pool: poolConfig.poolAddress
2243
+ pool: poolConfig.poolAddress,
2244
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
2164
2245
  })
2165
2246
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), custodyCustomOracles, true))
2166
2247
  .instruction()];
@@ -2182,19 +2263,20 @@ var PerpetualsClient = (function () {
2182
2263
  pool: poolConfig.poolAddress,
2183
2264
  receivingCustody: collateralCustodyConfig.custodyAccount,
2184
2265
  receivingCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
2185
- receivingCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2266
+ receivingCustodyCustomOracleAccount: this.poolConfig.backupOracle,
2186
2267
  receivingCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
2187
2268
  dispensingCustody: outputCustodyConfig.custodyAccount,
2188
2269
  dispensingCustodyOracleAccount: outputCustodyConfig.oracleAddress,
2189
- dispensingCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2270
+ dispensingCustodyCustomOracleAccount: this.poolConfig.backupOracle,
2190
2271
  dispensingCustodyTokenAccount: outputCustodyConfig.tokenAccount,
2191
2272
  position: positionAccount,
2192
2273
  custody: marketCustodyConfig.custodyAccount,
2193
2274
  custodyOracleAccount: marketCustodyConfig.oracleAddress,
2194
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2275
+ custodyCustomOracleAccount: this.poolConfig.backupOracle,
2195
2276
  eventAuthority: this.eventAuthority.publicKey,
2196
2277
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
2197
2278
  perpProgram: this.programId,
2279
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
2198
2280
  })
2199
2281
  .instruction()];
2200
2282
  case 8:
@@ -2368,7 +2450,7 @@ var PerpetualsClient = (function () {
2368
2450
  isWritable: false,
2369
2451
  });
2370
2452
  custodyCustomOracles.push({
2371
- pubkey: web3_js_1.SystemProgram.programId,
2453
+ pubkey: this.poolConfig.backupOracle,
2372
2454
  isSigner: false,
2373
2455
  isWritable: false,
2374
2456
  });
@@ -2378,7 +2460,8 @@ var PerpetualsClient = (function () {
2378
2460
  .accounts({
2379
2461
  payer: publicKey,
2380
2462
  perpetuals: poolConfig.perpetuals,
2381
- pool: poolConfig.poolAddress
2463
+ pool: poolConfig.poolAddress,
2464
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
2382
2465
  })
2383
2466
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), custodyCustomOracles, true))
2384
2467
  .instruction()];
@@ -2401,15 +2484,16 @@ var PerpetualsClient = (function () {
2401
2484
  pool: poolConfig.poolAddress,
2402
2485
  receivingCustody: userInputCustodyConfig.custodyAccount,
2403
2486
  receivingCustodyOracleAccount: userInputCustodyConfig.oracleAddress,
2404
- receivingCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2487
+ receivingCustodyCustomOracleAccount: this.poolConfig.backupOracle,
2405
2488
  receivingCustodyTokenAccount: userInputCustodyConfig.tokenAccount,
2406
2489
  dispensingCustody: userOutputCustodyConfig.custodyAccount,
2407
2490
  dispensingCustodyOracleAccount: userOutputCustodyConfig.oracleAddress,
2408
- dispensingCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2491
+ dispensingCustodyCustomOracleAccount: this.poolConfig.backupOracle,
2409
2492
  dispensingCustodyTokenAccount: userOutputCustodyConfig.tokenAccount,
2410
2493
  eventAuthority: this.eventAuthority.publicKey,
2411
2494
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
2412
2495
  program: this.programId,
2496
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
2413
2497
  })
2414
2498
  .instruction()];
2415
2499
  case 21:
@@ -2511,14 +2595,15 @@ var PerpetualsClient = (function () {
2511
2595
  pool: poolConfig.poolAddress,
2512
2596
  custody: marketCustodyConfig.custodyAccount,
2513
2597
  custodyOracleAccount: marketCustodyConfig.oracleAddress,
2514
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2598
+ custodyCustomOracleAccount: this.poolConfig.backupOracle,
2515
2599
  collateralCustody: collateralCustodyConfig.custodyAccount,
2516
2600
  collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
2517
- collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2601
+ collateralCustodyCustomOracleAccount: this.poolConfig.backupOracle,
2518
2602
  collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
2519
2603
  eventAuthority: this.eventAuthority.publicKey,
2520
2604
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
2521
2605
  program: this.programId,
2606
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
2522
2607
  })
2523
2608
  .instruction()];
2524
2609
  case 8:
@@ -2623,7 +2708,7 @@ var PerpetualsClient = (function () {
2623
2708
  isWritable: false,
2624
2709
  });
2625
2710
  custodyCustomOracles.push({
2626
- pubkey: web3_js_1.SystemProgram.programId,
2711
+ pubkey: this.poolConfig.backupOracle,
2627
2712
  isSigner: false,
2628
2713
  isWritable: false,
2629
2714
  });
@@ -2633,7 +2718,8 @@ var PerpetualsClient = (function () {
2633
2718
  .accounts({
2634
2719
  payer: publicKey,
2635
2720
  perpetuals: poolConfig.perpetuals,
2636
- pool: poolConfig.poolAddress
2721
+ pool: poolConfig.poolAddress,
2722
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
2637
2723
  })
2638
2724
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), custodyCustomOracles, true))
2639
2725
  .instruction()];
@@ -2654,18 +2740,19 @@ var PerpetualsClient = (function () {
2654
2740
  pool: poolConfig.poolAddress,
2655
2741
  receivingCustody: inputCustodyConfig.custodyAccount,
2656
2742
  receivingCustodyOracleAccount: inputCustodyConfig.oracleAddress,
2657
- receivingCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2743
+ receivingCustodyCustomOracleAccount: this.poolConfig.backupOracle,
2658
2744
  receivingCustodyTokenAccount: inputCustodyConfig.tokenAccount,
2659
2745
  dispensingCustody: collateralCustodyConfig.custodyAccount,
2660
2746
  dispensingCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
2661
- dispensingCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2747
+ dispensingCustodyCustomOracleAccount: this.poolConfig.backupOracle,
2662
2748
  dispensingCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
2663
2749
  custody: marketCustodyConfig.custodyAccount,
2664
2750
  custodyOracleAccount: marketCustodyConfig.oracleAddress,
2665
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2751
+ custodyCustomOracleAccount: this.poolConfig.backupOracle,
2666
2752
  eventAuthority: this.eventAuthority.publicKey,
2667
2753
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
2668
2754
  perpProgram: this.programId,
2755
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
2669
2756
  })
2670
2757
  .instruction()];
2671
2758
  case 9:
@@ -2755,14 +2842,15 @@ var PerpetualsClient = (function () {
2755
2842
  position: positionPubKey,
2756
2843
  custody: marketCustodyConfig.custodyAccount,
2757
2844
  custodyOracleAccount: marketCustodyConfig.oracleAddress,
2758
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2845
+ custodyCustomOracleAccount: this.poolConfig.backupOracle,
2759
2846
  collateralCustody: collateralCustodyConfig.custodyAccount,
2760
2847
  collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
2761
- collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
2848
+ collateralCustodyCustomOracleAccount: this.poolConfig.backupOracle,
2762
2849
  collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
2763
2850
  eventAuthority: this.eventAuthority.publicKey,
2764
2851
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
2765
2852
  program: this.programId,
2853
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
2766
2854
  })
2767
2855
  .instruction()];
2768
2856
  case 8:
@@ -2785,6 +2873,141 @@ var PerpetualsClient = (function () {
2785
2873
  });
2786
2874
  });
2787
2875
  };
2876
+ this.RemoveCollateralWithSwap = function (marketSymbol, collateralSymbol, outputSymbol, minAmountOut, collateralDelta, side, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
2877
+ var publicKey, marketCustodyConfig, collateralCustodyConfig, outputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, lamports, userCollateralTokenAccount, positionAccount, custodyAccountMetas, custodyOracleAccountMetas, custodyCustomOracles, _i, _a, custody, poolAumInstruction, instruction;
2878
+ return __generator(this, function (_b) {
2879
+ switch (_b.label) {
2880
+ case 0:
2881
+ publicKey = this.provider.wallet.publicKey;
2882
+ marketCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
2883
+ collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2884
+ outputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outputSymbol).mintKey); });
2885
+ if (outputCustodyConfig.mintKey.equals(collateralCustodyConfig.mintKey)) {
2886
+ throw "Dont use swap, just call remove collateral";
2887
+ }
2888
+ preInstructions = [];
2889
+ instructions = [];
2890
+ postInstructions = [];
2891
+ additionalSigners = [];
2892
+ if (!(outputSymbol == 'SOL')) return [3, 2];
2893
+ console.log("outputSymbol === SOL", outputSymbol);
2894
+ wrappedSolAccount = new web3_js_1.Keypair();
2895
+ return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
2896
+ case 1:
2897
+ lamports = (_b.sent());
2898
+ preInstructions = [
2899
+ web3_js_1.SystemProgram.createAccount({
2900
+ fromPubkey: publicKey,
2901
+ newAccountPubkey: wrappedSolAccount.publicKey,
2902
+ lamports: lamports,
2903
+ space: 165,
2904
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
2905
+ }),
2906
+ (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
2907
+ ];
2908
+ postInstructions = [
2909
+ (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
2910
+ ];
2911
+ additionalSigners.push(wrappedSolAccount);
2912
+ return [3, 5];
2913
+ case 2: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(outputSymbol).mintKey, publicKey)];
2914
+ case 3:
2915
+ userReceivingTokenAccount = _b.sent();
2916
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
2917
+ case 4:
2918
+ if (!(_b.sent())) {
2919
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(outputSymbol).mintKey));
2920
+ }
2921
+ _b.label = 5;
2922
+ case 5:
2923
+ userCollateralTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey);
2924
+ return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
2925
+ case 6:
2926
+ if (!(_b.sent())) {
2927
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userCollateralTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
2928
+ }
2929
+ positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
2930
+ Buffer.from("position"),
2931
+ publicKey.toBuffer(),
2932
+ poolConfig.poolAddress.toBuffer(),
2933
+ marketCustodyConfig.custodyAccount.toBuffer(),
2934
+ (0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
2935
+ ], this.programId)[0];
2936
+ custodyAccountMetas = [];
2937
+ custodyOracleAccountMetas = [];
2938
+ custodyCustomOracles = [];
2939
+ for (_i = 0, _a = poolConfig.custodies; _i < _a.length; _i++) {
2940
+ custody = _a[_i];
2941
+ custodyAccountMetas.push({
2942
+ pubkey: custody.custodyAccount,
2943
+ isSigner: false,
2944
+ isWritable: false,
2945
+ });
2946
+ custodyOracleAccountMetas.push({
2947
+ pubkey: custody.oracleAddress,
2948
+ isSigner: false,
2949
+ isWritable: false,
2950
+ });
2951
+ custodyCustomOracles.push({
2952
+ pubkey: this.poolConfig.backupOracle,
2953
+ isSigner: false,
2954
+ isWritable: false,
2955
+ });
2956
+ }
2957
+ return [4, this.program.methods
2958
+ .updatePoolAum()
2959
+ .accounts({
2960
+ payer: publicKey,
2961
+ perpetuals: poolConfig.perpetuals,
2962
+ pool: poolConfig.poolAddress,
2963
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
2964
+ })
2965
+ .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), custodyCustomOracles, true))
2966
+ .instruction()];
2967
+ case 7:
2968
+ poolAumInstruction = _b.sent();
2969
+ preInstructions.push(poolAumInstruction);
2970
+ return [4, this.programPerpComposability.methods
2971
+ .removeCollateralAndSwap({
2972
+ minAmountOut: minAmountOut,
2973
+ collateralDelta: collateralDelta,
2974
+ })
2975
+ .accounts({
2976
+ delegate: publicKey,
2977
+ owner: publicKey,
2978
+ fundingAccount: userCollateralTokenAccount,
2979
+ receivingAccount: outputSymbol == 'SOL' ? wrappedSolAccount.publicKey : userReceivingTokenAccount,
2980
+ transferAuthority: poolConfig.transferAuthority,
2981
+ perpetuals: poolConfig.perpetuals,
2982
+ pool: poolConfig.poolAddress,
2983
+ receivingCustody: collateralCustodyConfig.custodyAccount,
2984
+ receivingCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
2985
+ receivingCustodyCustomOracleAccount: this.poolConfig.backupOracle,
2986
+ receivingCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
2987
+ dispensingCustody: outputCustodyConfig.custodyAccount,
2988
+ dispensingCustodyOracleAccount: outputCustodyConfig.oracleAddress,
2989
+ dispensingCustodyCustomOracleAccount: this.poolConfig.backupOracle,
2990
+ dispensingCustodyTokenAccount: outputCustodyConfig.tokenAccount,
2991
+ position: positionAccount,
2992
+ custody: marketCustodyConfig.custodyAccount,
2993
+ custodyOracleAccount: marketCustodyConfig.oracleAddress,
2994
+ custodyCustomOracleAccount: this.poolConfig.backupOracle,
2995
+ eventAuthority: this.eventAuthority.publicKey,
2996
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
2997
+ perpProgram: this.programId,
2998
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
2999
+ })
3000
+ .instruction()];
3001
+ case 8:
3002
+ instruction = _b.sent();
3003
+ instructions.push(instruction);
3004
+ return [2, {
3005
+ instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3006
+ additionalSigners: additionalSigners
3007
+ }];
3008
+ }
3009
+ });
3010
+ }); };
2788
3011
  this.increaseSize = function (marketSymbol, collateralSymbol, positionPubKey, poolConfig, price, sizeDelta) { return __awaiter(_this, void 0, void 0, function () {
2789
3012
  var publicKey, collateralCustodyConfig, marketCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, params, instruction;
2790
3013
  return __generator(this, function (_a) {
@@ -2818,12 +3041,13 @@ var PerpetualsClient = (function () {
2818
3041
  position: positionPubKey,
2819
3042
  custody: marketCustodyConfig.custodyAccount,
2820
3043
  custodyOracleAccount: marketCustodyConfig.oracleAddress,
2821
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
3044
+ custodyCustomOracleAccount: this.poolConfig.backupOracle,
2822
3045
  collateralCustody: collateralCustodyConfig.custodyAccount,
2823
3046
  collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
2824
- collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
3047
+ collateralCustodyCustomOracleAccount: this.poolConfig.backupOracle,
2825
3048
  eventAuthority: this.eventAuthority.publicKey,
2826
3049
  program: this.programId,
3050
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
2827
3051
  })
2828
3052
  .instruction()];
2829
3053
  case 1:
@@ -2869,13 +3093,14 @@ var PerpetualsClient = (function () {
2869
3093
  position: positionPubKey,
2870
3094
  custody: marketCustodyConfig.custodyAccount,
2871
3095
  custodyOracleAccount: marketCustodyConfig.oracleAddress,
2872
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
3096
+ custodyCustomOracleAccount: this.poolConfig.backupOracle,
2873
3097
  collateralCustody: collateralCustodyConfig.custodyAccount,
2874
3098
  collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
2875
- collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
3099
+ collateralCustodyCustomOracleAccount: this.poolConfig.backupOracle,
2876
3100
  collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
2877
3101
  eventAuthority: this.eventAuthority.publicKey,
2878
3102
  program: this.programId,
3103
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
2879
3104
  })
2880
3105
  .instruction()];
2881
3106
  case 1:
@@ -2929,7 +3154,7 @@ var PerpetualsClient = (function () {
2929
3154
  isWritable: false,
2930
3155
  });
2931
3156
  custodyCustomOracles.push({
2932
- pubkey: web3_js_1.SystemProgram.programId,
3157
+ pubkey: this.poolConfig.backupOracle,
2933
3158
  isSigner: false,
2934
3159
  isWritable: false,
2935
3160
  });
@@ -2988,7 +3213,8 @@ var PerpetualsClient = (function () {
2988
3213
  .accounts({
2989
3214
  payer: publicKey,
2990
3215
  perpetuals: poolConfig.perpetuals,
2991
- pool: poolConfig.poolAddress
3216
+ pool: poolConfig.poolAddress,
3217
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
2992
3218
  })
2993
3219
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), custodyCustomOracles, true))
2994
3220
  .instruction()];
@@ -3010,12 +3236,13 @@ var PerpetualsClient = (function () {
3010
3236
  pool: poolConfig.poolAddress,
3011
3237
  custody: payTokenCustodyConfig.custodyAccount,
3012
3238
  custodyOracleAccount: payTokenCustodyConfig.oracleAddress,
3013
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
3239
+ custodyCustomOracleAccount: this.poolConfig.backupOracle,
3014
3240
  custodyTokenAccount: payTokenCustodyConfig.tokenAccount,
3015
3241
  lpTokenMint: poolConfig.lpTokenMint,
3016
3242
  eventAuthority: this.eventAuthority.publicKey,
3017
3243
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
3018
3244
  program: this.programId,
3245
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
3019
3246
  })
3020
3247
  .instruction()];
3021
3248
  case 12:
@@ -3074,7 +3301,7 @@ var PerpetualsClient = (function () {
3074
3301
  isWritable: false,
3075
3302
  });
3076
3303
  custodyCustomOracles.push({
3077
- pubkey: web3_js_1.SystemProgram.programId,
3304
+ pubkey: this.poolConfig.backupOracle,
3078
3305
  isSigner: false,
3079
3306
  isWritable: false,
3080
3307
  });
@@ -3119,7 +3346,8 @@ var PerpetualsClient = (function () {
3119
3346
  .accounts({
3120
3347
  payer: publicKey,
3121
3348
  perpetuals: poolConfig.perpetuals,
3122
- pool: poolConfig.poolAddress
3349
+ pool: poolConfig.poolAddress,
3350
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
3123
3351
  })
3124
3352
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), custodyCustomOracles, true))
3125
3353
  .instruction()];
@@ -3142,12 +3370,13 @@ var PerpetualsClient = (function () {
3142
3370
  pool: poolConfig.poolAddress,
3143
3371
  custody: recieveTokenCustodyConfig.custodyAccount,
3144
3372
  custodyOracleAccount: recieveTokenCustodyConfig.oracleAddress,
3145
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
3373
+ custodyCustomOracleAccount: this.poolConfig.backupOracle,
3146
3374
  custodyTokenAccount: recieveTokenCustodyConfig.tokenAccount,
3147
3375
  lpTokenMint: poolConfig.lpTokenMint,
3148
3376
  eventAuthority: this.eventAuthority.publicKey,
3149
3377
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
3150
3378
  program: this.programId,
3379
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
3151
3380
  })
3152
3381
  .instruction()];
3153
3382
  case 10:
@@ -3176,9 +3405,10 @@ var PerpetualsClient = (function () {
3176
3405
  };
3177
3406
  this.provider = provider;
3178
3407
  (0, anchor_1.setProvider)(provider);
3179
- this.program = new anchor_1.Program(perpetuals_1.IDL, programId);
3408
+ this.program = new anchor_1.Program(perpetuals_1.IDL, poolConfig.programId);
3180
3409
  this.programPerpComposability = new anchor_1.Program(perp_composability_1.IDL, composabilityProgramId);
3181
- this.programId = programId;
3410
+ this.poolConfig = poolConfig;
3411
+ this.programId = poolConfig.programId;
3182
3412
  this.composabilityProgramId = composabilityProgramId;
3183
3413
  this.admin = this.provider.wallet.publicKey;
3184
3414
  this.multisig = this.findProgramAddress("multisig");