@parqxchange/sdk 0.1.0

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.
Files changed (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +122 -0
  3. package/dist/accounts/marketOracle.d.ts +21 -0
  4. package/dist/accounts/marketOracle.js +114 -0
  5. package/dist/accounts/resolve.d.ts +75 -0
  6. package/dist/accounts/resolve.js +116 -0
  7. package/dist/actions/cranks.d.ts +8 -0
  8. package/dist/actions/cranks.js +10 -0
  9. package/dist/actions/liquidity.d.ts +23 -0
  10. package/dist/actions/liquidity.js +21 -0
  11. package/dist/actions/trading.d.ts +39 -0
  12. package/dist/actions/trading.js +95 -0
  13. package/dist/auth/tradingKey.d.ts +27 -0
  14. package/dist/auth/tradingKey.js +71 -0
  15. package/dist/decode/index.d.ts +39 -0
  16. package/dist/decode/index.js +445 -0
  17. package/dist/events/decoder.d.ts +60 -0
  18. package/dist/events/decoder.js +369 -0
  19. package/dist/events/emitter.d.ts +49 -0
  20. package/dist/events/emitter.js +153 -0
  21. package/dist/index.d.ts +25 -0
  22. package/dist/index.js +93 -0
  23. package/dist/indexer/client.d.ts +13 -0
  24. package/dist/indexer/client.js +36 -0
  25. package/dist/indexer/types.d.ts +9 -0
  26. package/dist/indexer/types.js +2 -0
  27. package/dist/minimal.d.ts +3 -0
  28. package/dist/minimal.js +13 -0
  29. package/dist/node.d.ts +1 -0
  30. package/dist/node.js +5 -0
  31. package/dist/programs/feeDistributor.d.ts +42 -0
  32. package/dist/programs/feeDistributor.js +42 -0
  33. package/dist/programs/oracle.d.ts +31 -0
  34. package/dist/programs/oracle.js +49 -0
  35. package/dist/programs/perp.d.ts +283 -0
  36. package/dist/programs/perp.js +346 -0
  37. package/dist/programs/pool.d.ts +113 -0
  38. package/dist/programs/pool.js +126 -0
  39. package/dist/programs/priceFeed.d.ts +13 -0
  40. package/dist/programs/priceFeed.js +21 -0
  41. package/dist/programs/staking.d.ts +76 -0
  42. package/dist/programs/staking.js +135 -0
  43. package/dist/types/index.d.ts +337 -0
  44. package/dist/types/index.js +9 -0
  45. package/dist/utils/computeBudget.d.ts +5 -0
  46. package/dist/utils/computeBudget.js +14 -0
  47. package/dist/utils/errors.d.ts +56 -0
  48. package/dist/utils/errors.js +138 -0
  49. package/dist/utils/pda.d.ts +28 -0
  50. package/dist/utils/pda.js +131 -0
  51. package/dist/utils/priceMath.d.ts +5 -0
  52. package/dist/utils/priceMath.js +46 -0
  53. package/dist/utils/tokenProgram.d.ts +2 -0
  54. package/dist/utils/tokenProgram.js +16 -0
  55. package/dist/utils/transaction.d.ts +2 -0
  56. package/dist/utils/transaction.js +12 -0
  57. package/dist/utils/version.d.ts +11 -0
  58. package/dist/utils/version.js +29 -0
  59. package/idl/fee_distributor.json +779 -0
  60. package/idl/oracle_adapter.json +767 -0
  61. package/idl/perp_engine.json +3908 -0
  62. package/idl/pool_program.json +2956 -0
  63. package/idl/price_feed.json +478 -0
  64. package/idl/staking.json +1336 -0
  65. package/package.json +98 -0
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ParquetError = void 0;
4
+ exports.errorMessage = errorMessage;
5
+ exports.decodeError = decodeError;
6
+ exports.isParquetError = isParquetError;
7
+ var ParquetError;
8
+ (function (ParquetError) {
9
+ ParquetError[ParquetError["PriceStale"] = 7001] = "PriceStale";
10
+ ParquetError[ParquetError["PriceUncertain"] = 7002] = "PriceUncertain";
11
+ ParquetError[ParquetError["OracleNoReturnData"] = 7003] = "OracleNoReturnData";
12
+ ParquetError[ParquetError["OracleReturnMismatch"] = 7004] = "OracleReturnMismatch";
13
+ ParquetError[ParquetError["InsufficientPool"] = 8001] = "InsufficientPool";
14
+ ParquetError[ParquetError["LeverageExceeded"] = 8002] = "LeverageExceeded";
15
+ ParquetError[ParquetError["BelowMinSize"] = 8003] = "BelowMinSize";
16
+ ParquetError[ParquetError["WrongPool"] = 8004] = "WrongPool";
17
+ ParquetError[ParquetError["OiCapExceeded"] = 8005] = "OiCapExceeded";
18
+ ParquetError[ParquetError["BelowMinCollateral"] = 8006] = "BelowMinCollateral";
19
+ ParquetError[ParquetError["InvalidConfig"] = 8007] = "InvalidConfig";
20
+ ParquetError[ParquetError["QueueCollateralExceedsClaim"] = 8008] = "QueueCollateralExceedsClaim";
21
+ ParquetError[ParquetError["WalletAndQueueZero"] = 8009] = "WalletAndQueueZero";
22
+ ParquetError[ParquetError["MissingQueueEntryAccount"] = 8010] = "MissingQueueEntryAccount";
23
+ ParquetError[ParquetError["LeverageExceedsTierCap"] = 8011] = "LeverageExceedsTierCap";
24
+ ParquetError[ParquetError["MalformedQueueEntry"] = 8012] = "MalformedQueueEntry";
25
+ ParquetError[ParquetError["QueueOwnerMismatch"] = 8013] = "QueueOwnerMismatch";
26
+ ParquetError[ParquetError["QueueMarketMismatch"] = 8014] = "QueueMarketMismatch";
27
+ ParquetError[ParquetError["InitialMarginUnmet"] = 8015] = "InitialMarginUnmet";
28
+ ParquetError[ParquetError["HealthFactorTooLow"] = 9001] = "HealthFactorTooLow";
29
+ ParquetError[ParquetError["PositionHealthy"] = 9002] = "PositionHealthy";
30
+ ParquetError[ParquetError["TradingKeyExpired"] = 10001] = "TradingKeyExpired";
31
+ ParquetError[ParquetError["Unauthorized"] = 10002] = "Unauthorized";
32
+ ParquetError[ParquetError["PositionNotFound"] = 10003] = "PositionNotFound";
33
+ ParquetError[ParquetError["MarketPaused"] = 10004] = "MarketPaused";
34
+ ParquetError[ParquetError["ProtocolHalted"] = 10005] = "ProtocolHalted";
35
+ ParquetError[ParquetError["InvalidHaltMode"] = 10006] = "InvalidHaltMode";
36
+ ParquetError[ParquetError["MathOverflow"] = 11001] = "MathOverflow";
37
+ ParquetError[ParquetError["SlippageExceeded"] = 12001] = "SlippageExceeded";
38
+ ParquetError[ParquetError["OrderNotTriggered"] = 12002] = "OrderNotTriggered";
39
+ ParquetError[ParquetError["WrongOrderType"] = 12003] = "WrongOrderType";
40
+ ParquetError[ParquetError["ReferralCodeTaken"] = 13001] = "ReferralCodeTaken";
41
+ ParquetError[ParquetError["ReferralCodeNotFound"] = 13002] = "ReferralCodeNotFound";
42
+ ParquetError[ParquetError["TraderAlreadyReferred"] = 13003] = "TraderAlreadyReferred";
43
+ ParquetError[ParquetError["InvalidTier"] = 13004] = "InvalidTier";
44
+ ParquetError[ParquetError["FundingCadenceTooFast"] = 14001] = "FundingCadenceTooFast";
45
+ ParquetError[ParquetError["InvalidOptimalUsage"] = 15001] = "InvalidOptimalUsage";
46
+ ParquetError[ParquetError["MustBePaused"] = 15002] = "MustBePaused";
47
+ ParquetError[ParquetError["MissingFeeSettings"] = 15003] = "MissingFeeSettings";
48
+ ParquetError[ParquetError["MissingAffiliateReward"] = 15004] = "MissingAffiliateReward";
49
+ ParquetError[ParquetError["MissingAffiliateRewardBump"] = 15005] = "MissingAffiliateRewardBump";
50
+ ParquetError[ParquetError["MissingFeeDistributorAccounts"] = 15006] = "MissingFeeDistributorAccounts";
51
+ ParquetError[ParquetError["QueueEntryNotPending"] = 6012] = "QueueEntryNotPending";
52
+ ParquetError[ParquetError["QueueNonEmpty"] = 6013] = "QueueNonEmpty";
53
+ ParquetError[ParquetError["PhantomEmpty"] = 6014] = "PhantomEmpty";
54
+ ParquetError[ParquetError["InsufficientClaim"] = 6015] = "InsufficientClaim";
55
+ ParquetError[ParquetError["PhantomDrainInsufficientFunds"] = 6016] = "PhantomDrainInsufficientFunds";
56
+ ParquetError[ParquetError["MissingAccount"] = 6017] = "MissingAccount";
57
+ ParquetError[ParquetError["WrongEntryIndex"] = 6018] = "WrongEntryIndex";
58
+ ParquetError[ParquetError["WrongMarketId"] = 6019] = "WrongMarketId";
59
+ ParquetError[ParquetError["OwnerMismatch"] = 6020] = "OwnerMismatch";
60
+ })(ParquetError || (exports.ParquetError = ParquetError = {}));
61
+ const ERROR_MESSAGES = {
62
+ [ParquetError.PriceStale]: "Price feed is stale",
63
+ [ParquetError.PriceUncertain]: "Price confidence too wide",
64
+ [ParquetError.OracleNoReturnData]: "Oracle returned no data",
65
+ [ParquetError.OracleReturnMismatch]: "Oracle return data mismatch",
66
+ [ParquetError.InsufficientPool]: "Insufficient pool liquidity",
67
+ [ParquetError.LeverageExceeded]: "Leverage exceeds max",
68
+ [ParquetError.BelowMinSize]: "Size below minimum",
69
+ [ParquetError.WrongPool]: "Wrong pool account",
70
+ [ParquetError.OiCapExceeded]: "Open interest cap exceeded",
71
+ [ParquetError.BelowMinCollateral]: "Collateral below minimum ($10)",
72
+ [ParquetError.InvalidConfig]: "Invalid market config — inconsistent or default-pubkey field",
73
+ [ParquetError.QueueCollateralExceedsClaim]: "Position's collateral_from_queue exceeds available user claim",
74
+ [ParquetError.WalletAndQueueZero]: "Both wallet_collateral and from_queue_amount are zero",
75
+ [ParquetError.MissingQueueEntryAccount]: "Liquidation flow: required queue entry account missing",
76
+ [ParquetError.LeverageExceedsTierCap]: "Requested leverage exceeds the current-session tier cap for this wallet's notional",
77
+ [ParquetError.MalformedQueueEntry]: "Liquidation clawback: queue entry/claims account malformed or too short",
78
+ [ParquetError.QueueOwnerMismatch]: "Liquidation clawback: queue entry/claims owner != liquidated position owner",
79
+ [ParquetError.QueueMarketMismatch]: "Liquidation clawback: queue entry/claims belongs to a different market",
80
+ [ParquetError.InitialMarginUnmet]: "Initial margin requirement not met (collateral below notional * initial_margin_bps)",
81
+ [ParquetError.HealthFactorTooLow]: "Health factor too low",
82
+ [ParquetError.PositionHealthy]: "Position health >= 1000, not liquidatable",
83
+ [ParquetError.TradingKeyExpired]: "Trading key expired",
84
+ [ParquetError.Unauthorized]: "Unauthorized",
85
+ [ParquetError.PositionNotFound]: "Position not found",
86
+ [ParquetError.MarketPaused]: "Market is paused",
87
+ [ParquetError.ProtocolHalted]: "Protocol trading is halted",
88
+ [ParquetError.InvalidHaltMode]: "Invalid halt mode — must be 0 (None), 1 (ReduceOnly), or 2 (Full)",
89
+ [ParquetError.MathOverflow]: "Math overflow",
90
+ [ParquetError.SlippageExceeded]: "Slippage tolerance exceeded",
91
+ [ParquetError.OrderNotTriggered]: "Order not triggered",
92
+ [ParquetError.WrongOrderType]: "Wrong order type",
93
+ [ParquetError.ReferralCodeTaken]: "Referral code already taken",
94
+ [ParquetError.ReferralCodeNotFound]: "Referral code not found",
95
+ [ParquetError.TraderAlreadyReferred]: "Trader already has a referral",
96
+ [ParquetError.InvalidTier]: "Invalid tier — must be 0–3",
97
+ [ParquetError.FundingCadenceTooFast]: "update_funding_rate called within 5s of last update",
98
+ [ParquetError.InvalidOptimalUsage]: "Invalid optimal usage factor — must be < PRECISION",
99
+ [ParquetError.MustBePaused]: "Market must be paused for migration",
100
+ [ParquetError.MissingFeeSettings]: "Missing fee_settings remaining account",
101
+ [ParquetError.MissingAffiliateReward]: "Missing affiliate_reward remaining account",
102
+ [ParquetError.MissingAffiliateRewardBump]: "Missing affiliate_reward_bump in args",
103
+ [ParquetError.MissingFeeDistributorAccounts]: "Missing fee-distributor remaining accounts (need 4: program, fee_pool, referral_reserve, token_program)",
104
+ [ParquetError.QueueEntryNotPending]: "Queue entry is not in Pending status",
105
+ [ParquetError.QueueNonEmpty]: "Queue is non-empty; phantom drain requires queue_total_owed == 0",
106
+ [ParquetError.PhantomEmpty]: "Phantom credit is zero",
107
+ [ParquetError.InsufficientClaim]: "Insufficient claim for the requested from_queue_amount",
108
+ [ParquetError.PhantomDrainInsufficientFunds]: "Pool insufficient funds to satisfy phantom drain",
109
+ [ParquetError.MissingAccount]: "Required account missing from remaining_accounts",
110
+ [ParquetError.WrongEntryIndex]: "Queue entry idx does not match queue_head_idx",
111
+ [ParquetError.WrongMarketId]: "Queue entry market_id mismatch",
112
+ [ParquetError.OwnerMismatch]: "UserQueueClaims owner mismatch with queue entry owner",
113
+ };
114
+ function errorMessage(code) {
115
+ return ERROR_MESSAGES[code] ?? `Unknown error: ${code}`;
116
+ }
117
+ const KNOWN_ERROR_CODES = new Set(Object.values(ParquetError).filter((v) => typeof v === "number"));
118
+ function decodeError(err) {
119
+ if (typeof err !== "object" || err === null)
120
+ return null;
121
+ const code = err
122
+ ?.error?.errorCode?.number;
123
+ if (code !== undefined && KNOWN_ERROR_CODES.has(code))
124
+ return code;
125
+ const message = err?.message;
126
+ if (typeof message === "string") {
127
+ const match = message.match(/custom program error: 0x([0-9a-fA-F]+)/);
128
+ if (match) {
129
+ const parsed = parseInt(match[1], 16);
130
+ if (KNOWN_ERROR_CODES.has(parsed))
131
+ return parsed;
132
+ }
133
+ }
134
+ return null;
135
+ }
136
+ function isParquetError(err, code) {
137
+ return decodeError(err) === code;
138
+ }
@@ -0,0 +1,28 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ export declare function poolStatePDA(marketId: Uint8Array, programId: PublicKey): [PublicKey, number];
3
+ export declare function vaultAuthorityPDA(marketId: Uint8Array, programId: PublicKey): [PublicKey, number];
4
+ export declare function usdcVaultPDA(marketId: Uint8Array, programId: PublicKey): [PublicKey, number];
5
+ export declare function lpMintPDA(marketId: Uint8Array, programId: PublicKey): [PublicKey, number];
6
+ export declare function marketStatePDA(marketId: Uint8Array, programId: PublicKey): [PublicKey, number];
7
+ export declare function positionPDA(owner: PublicKey, marketId: Uint8Array, nonce: bigint, programId: PublicKey): [PublicKey, number];
8
+ export declare function tradingKeyPDA(wallet: PublicKey, programId: PublicKey): [PublicKey, number];
9
+ export declare function marketOraclePDA(marketId: Uint8Array, programId: PublicKey): [PublicKey, number];
10
+ export declare function engineAuthPDA(marketId: Uint8Array, programId: PublicKey): [PublicKey, number];
11
+ export declare function protocolConfigPDA(programId: PublicKey): [PublicKey, number];
12
+ export declare function priceFeedPDA(marketId: Uint8Array, programId: PublicKey): [PublicKey, number];
13
+ export declare function marketIdFromString(s: string): Uint8Array;
14
+ export declare function referralConfigPDA(programId: PublicKey): [PublicKey, number];
15
+ export declare function referralCodePDA(code: Uint8Array, programId: PublicKey): [PublicKey, number];
16
+ export declare function traderReferralPDA(trader: PublicKey, programId: PublicKey): [PublicKey, number];
17
+ export declare function affiliateRewardPDA(affiliate: PublicKey, marketId: Uint8Array, programId: PublicKey): [PublicKey, number];
18
+ export declare function orderNoncePDA(owner: PublicKey, programId: PublicKey): [PublicKey, number];
19
+ export declare function orderPDA(owner: PublicKey, marketId: Uint8Array, nonce: bigint, programId: PublicKey): [PublicKey, number];
20
+ export declare function payoutQueueEntryPDA(marketId: Uint8Array, idx: bigint, programId: PublicKey): [PublicKey, number];
21
+ export declare function userQueueClaimsPDA(marketId: Uint8Array, owner: PublicKey, programId: PublicKey): [PublicKey, number];
22
+ export declare function insuranceFundPda(programId: PublicKey): [PublicKey, number];
23
+ export declare function insuranceVaultPda(programId: PublicKey): [PublicKey, number];
24
+ export declare function insuranceVaultAuthorityPda(programId: PublicKey): [PublicKey, number];
25
+ export declare function feeSettingsPDA(marketId: Uint8Array, programId: PublicKey): [PublicKey, number];
26
+ export declare function feePoolPDA(feeDistributorProgramId: PublicKey): [PublicKey, number];
27
+ export declare function stakingPoolPDA(tokenMint: PublicKey, programId: PublicKey): [PublicKey, number];
28
+ export declare function stakePositionPDA(pool: PublicKey, owner: PublicKey, programId: PublicKey): [PublicKey, number];
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.poolStatePDA = poolStatePDA;
4
+ exports.vaultAuthorityPDA = vaultAuthorityPDA;
5
+ exports.usdcVaultPDA = usdcVaultPDA;
6
+ exports.lpMintPDA = lpMintPDA;
7
+ exports.marketStatePDA = marketStatePDA;
8
+ exports.positionPDA = positionPDA;
9
+ exports.tradingKeyPDA = tradingKeyPDA;
10
+ exports.marketOraclePDA = marketOraclePDA;
11
+ exports.engineAuthPDA = engineAuthPDA;
12
+ exports.protocolConfigPDA = protocolConfigPDA;
13
+ exports.priceFeedPDA = priceFeedPDA;
14
+ exports.marketIdFromString = marketIdFromString;
15
+ exports.referralConfigPDA = referralConfigPDA;
16
+ exports.referralCodePDA = referralCodePDA;
17
+ exports.traderReferralPDA = traderReferralPDA;
18
+ exports.affiliateRewardPDA = affiliateRewardPDA;
19
+ exports.orderNoncePDA = orderNoncePDA;
20
+ exports.orderPDA = orderPDA;
21
+ exports.payoutQueueEntryPDA = payoutQueueEntryPDA;
22
+ exports.userQueueClaimsPDA = userQueueClaimsPDA;
23
+ exports.insuranceFundPda = insuranceFundPda;
24
+ exports.insuranceVaultPda = insuranceVaultPda;
25
+ exports.insuranceVaultAuthorityPda = insuranceVaultAuthorityPda;
26
+ exports.feeSettingsPDA = feeSettingsPDA;
27
+ exports.feePoolPDA = feePoolPDA;
28
+ exports.stakingPoolPDA = stakingPoolPDA;
29
+ exports.stakePositionPDA = stakePositionPDA;
30
+ const web3_js_1 = require("@solana/web3.js");
31
+ function poolStatePDA(marketId, programId) {
32
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("pool"), marketId instanceof Buffer ? marketId : Buffer.from(marketId)], programId);
33
+ }
34
+ function vaultAuthorityPDA(marketId, programId) {
35
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("vault_authority"), marketId instanceof Buffer ? marketId : Buffer.from(marketId)], programId);
36
+ }
37
+ function usdcVaultPDA(marketId, programId) {
38
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("vault"), marketId instanceof Buffer ? marketId : Buffer.from(marketId)], programId);
39
+ }
40
+ function lpMintPDA(marketId, programId) {
41
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("lp_mint"), marketId instanceof Buffer ? marketId : Buffer.from(marketId)], programId);
42
+ }
43
+ function marketStatePDA(marketId, programId) {
44
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("market"), marketId instanceof Buffer ? marketId : Buffer.from(marketId)], programId);
45
+ }
46
+ function positionPDA(owner, marketId, nonce, programId) {
47
+ const nonceBuf = Buffer.alloc(8);
48
+ nonceBuf.writeBigUInt64LE(nonce);
49
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("position"), owner.toBuffer(), marketId instanceof Buffer ? marketId : Buffer.from(marketId), nonceBuf], programId);
50
+ }
51
+ function tradingKeyPDA(wallet, programId) {
52
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("trading_key"), wallet.toBuffer()], programId);
53
+ }
54
+ function marketOraclePDA(marketId, programId) {
55
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("oracle"), marketId instanceof Buffer ? marketId : Buffer.from(marketId)], programId);
56
+ }
57
+ function engineAuthPDA(marketId, programId) {
58
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("engine_auth"), marketId instanceof Buffer ? marketId : Buffer.from(marketId)], programId);
59
+ }
60
+ function protocolConfigPDA(programId) {
61
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("protocol_config")], programId);
62
+ }
63
+ function priceFeedPDA(marketId, programId) {
64
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("feed"), marketId instanceof Buffer ? marketId : Buffer.from(marketId)], programId);
65
+ }
66
+ function marketIdFromString(s) {
67
+ const encoded = Buffer.from(s, "utf8");
68
+ if (encoded.length > 32) {
69
+ throw new Error(`marketIdFromString: identifier exceeds 32 bytes (${encoded.length})`);
70
+ }
71
+ const buf = new Uint8Array(32);
72
+ buf.set(encoded);
73
+ return buf;
74
+ }
75
+ function referralConfigPDA(programId) {
76
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("referral_config")], programId);
77
+ }
78
+ function referralCodePDA(code, programId) {
79
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("referral_code"), code instanceof Buffer ? code : Buffer.from(code)], programId);
80
+ }
81
+ function traderReferralPDA(trader, programId) {
82
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("trader_referral"), trader.toBuffer()], programId);
83
+ }
84
+ function affiliateRewardPDA(affiliate, marketId, programId) {
85
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("affiliate_reward"), affiliate.toBuffer(), marketId instanceof Buffer ? marketId : Buffer.from(marketId)], programId);
86
+ }
87
+ function orderNoncePDA(owner, programId) {
88
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("order_nonce"), owner.toBuffer()], programId);
89
+ }
90
+ function orderPDA(owner, marketId, nonce, programId) {
91
+ const nonceBuf = Buffer.alloc(8);
92
+ nonceBuf.writeBigUInt64LE(nonce);
93
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("order"), owner.toBuffer(), marketId instanceof Buffer ? marketId : Buffer.from(marketId), nonceBuf], programId);
94
+ }
95
+ function payoutQueueEntryPDA(marketId, idx, programId) {
96
+ const idxLe = Buffer.alloc(8);
97
+ idxLe.writeBigUInt64LE(idx);
98
+ return web3_js_1.PublicKey.findProgramAddressSync([
99
+ Buffer.from("payout_queue_entry"),
100
+ marketId instanceof Buffer ? marketId : Buffer.from(marketId),
101
+ idxLe,
102
+ ], programId);
103
+ }
104
+ function userQueueClaimsPDA(marketId, owner, programId) {
105
+ return web3_js_1.PublicKey.findProgramAddressSync([
106
+ Buffer.from("user_queue_claims"),
107
+ marketId instanceof Buffer ? marketId : Buffer.from(marketId),
108
+ owner.toBuffer(),
109
+ ], programId);
110
+ }
111
+ function insuranceFundPda(programId) {
112
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("insurance_fund")], programId);
113
+ }
114
+ function insuranceVaultPda(programId) {
115
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("insurance_vault")], programId);
116
+ }
117
+ function insuranceVaultAuthorityPda(programId) {
118
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("insurance_vault_authority")], programId);
119
+ }
120
+ function feeSettingsPDA(marketId, programId) {
121
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("fee_settings"), marketId instanceof Buffer ? marketId : Buffer.from(marketId)], programId);
122
+ }
123
+ function feePoolPDA(feeDistributorProgramId) {
124
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("fee_pool")], feeDistributorProgramId);
125
+ }
126
+ function stakingPoolPDA(tokenMint, programId) {
127
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("staking_pool"), tokenMint.toBuffer()], programId);
128
+ }
129
+ function stakePositionPDA(pool, owner, programId) {
130
+ return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), pool.toBuffer(), owner.toBuffer()], programId);
131
+ }
@@ -0,0 +1,5 @@
1
+ export declare function calcPnl(side: "long" | "short", entry: bigint, exit: bigint, size: bigint): bigint;
2
+ export declare function calcFee(size: bigint, feeBps: number, discountPct: number): bigint;
3
+ export declare function formatUsdcAmount(amount: bigint): string;
4
+ export declare function formatPrice(price: bigint): string;
5
+ export declare function calcHealth(collateral: bigint, size: bigint, mmrBps: number): bigint;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.calcPnl = calcPnl;
4
+ exports.calcFee = calcFee;
5
+ exports.formatUsdcAmount = formatUsdcAmount;
6
+ exports.formatPrice = formatPrice;
7
+ exports.calcHealth = calcHealth;
8
+ function calcPnl(side, entry, exit, size) {
9
+ if (entry === 0n) {
10
+ throw new Error("calcPnl: entry price must be non-zero");
11
+ }
12
+ const diff = side === "long" ? exit - entry : entry - exit;
13
+ const num = diff * size;
14
+ return diff < 0n ? (num - (entry - 1n)) / entry : num / entry;
15
+ }
16
+ function calcFee(size, feeBps, discountPct) {
17
+ if (!Number.isInteger(feeBps) || feeBps < 0 || feeBps > 65_535) {
18
+ throw new Error("calcFee: feeBps must be an integer in u16 range (0–65535)");
19
+ }
20
+ if (!Number.isInteger(discountPct) || discountPct < 0 || discountPct > 100) {
21
+ throw new Error("calcFee: discountPct must be an integer in 0–100");
22
+ }
23
+ const numerator = size * BigInt(feeBps) * BigInt(Math.max(0, 100 - discountPct));
24
+ return numerator / 10000000n;
25
+ }
26
+ function formatUsdcAmount(amount) {
27
+ const abs = amount < 0n ? -amount : amount;
28
+ const sign = amount < 0n ? "-" : "";
29
+ const whole = abs / 1000000n;
30
+ const frac = (abs % 1000000n).toString().padStart(6, "0");
31
+ return `${sign}${whole}.${frac}`;
32
+ }
33
+ function formatPrice(price) {
34
+ const whole = price / 1000000000n;
35
+ const frac = (price % 1000000000n).toString().padStart(9, "0");
36
+ return `${whole}.${frac}`;
37
+ }
38
+ function calcHealth(collateral, size, mmrBps) {
39
+ if (mmrBps <= 0) {
40
+ throw new Error("calcHealth: mmrBps must be positive");
41
+ }
42
+ const threshold = (size * BigInt(mmrBps)) / 10000n;
43
+ if (threshold === 0n)
44
+ return (1n << 64n) - 1n;
45
+ return (collateral * 1000n) / threshold;
46
+ }
@@ -0,0 +1,2 @@
1
+ import { Connection, PublicKey } from "@solana/web3.js";
2
+ export declare function probeTokenProgram(connection: Connection, mint: PublicKey): Promise<PublicKey>;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.probeTokenProgram = probeTokenProgram;
4
+ const spl_token_1 = require("@solana/spl-token");
5
+ async function probeTokenProgram(connection, mint) {
6
+ const info = await connection.getAccountInfo(mint);
7
+ if (!info) {
8
+ throw new Error(`probeTokenProgram: mint ${mint.toBase58()} not found`);
9
+ }
10
+ if (info.owner.equals(spl_token_1.TOKEN_2022_PROGRAM_ID))
11
+ return spl_token_1.TOKEN_2022_PROGRAM_ID;
12
+ if (info.owner.equals(spl_token_1.TOKEN_PROGRAM_ID))
13
+ return spl_token_1.TOKEN_PROGRAM_ID;
14
+ throw new Error(`probeTokenProgram: mint ${mint.toBase58()} is owned by ${info.owner.toBase58()}, ` +
15
+ `not a recognized token program`);
16
+ }
@@ -0,0 +1,2 @@
1
+ import { TransactionInstruction, VersionedTransaction, PublicKey, AddressLookupTableAccount } from "@solana/web3.js";
2
+ export declare function buildVersionedTransaction(instructions: TransactionInstruction[], payer: PublicKey, blockhash: string, lookupTables?: AddressLookupTableAccount[]): VersionedTransaction;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildVersionedTransaction = buildVersionedTransaction;
4
+ const web3_js_1 = require("@solana/web3.js");
5
+ function buildVersionedTransaction(instructions, payer, blockhash, lookupTables) {
6
+ const message = new web3_js_1.TransactionMessage({
7
+ payerKey: payer,
8
+ recentBlockhash: blockhash,
9
+ instructions,
10
+ }).compileToV0Message(lookupTables);
11
+ return new web3_js_1.VersionedTransaction(message);
12
+ }
@@ -0,0 +1,11 @@
1
+ import { Connection, PublicKey } from "@solana/web3.js";
2
+ export type MarketStateDataLayout = "unknown" | "legacy_tiny" | "v1_460" | "v2_476" | "v2_adl_surface" | "v3_511";
3
+ export declare function detectProgramVersion(connection: Connection, marketStatePda: PublicKey): Promise<MarketStateDataLayout>;
4
+ export declare const PROGRAM_IDS_V4: {
5
+ readonly POOL_PROGRAM: "Acme8JzWrvVqGJz7nTKVsLYisN6MtP83nrs4fVAeXJsN";
6
+ readonly PERP_ENGINE: "6QrsMTMEu9rsLpyxQgRdvQsWoPgHGY9npNNiwTtXsbdc";
7
+ readonly ORACLE_ADAPTER: "6fsnWa9tcKcPiuQgdUbTMsiwUr43MNoxa1FECPFrvSpd";
8
+ readonly PRICE_FEED: "Dgorf5LPiMttdTxWcrsiA2j94kWKw55gJLRJm8P4E1Hn";
9
+ readonly FEE_DISTRIBUTOR: "CHTpVtZQboxjM6N9xk1RR29jZyWqFNV77cmQNJ18RNNL";
10
+ readonly STAKING_PROGRAM: "35HddZHf84u6DeyLoZL3Z3a8pZ59594xu1aizj7VrAGR";
11
+ };
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PROGRAM_IDS_V4 = void 0;
4
+ exports.detectProgramVersion = detectProgramVersion;
5
+ async function detectProgramVersion(connection, marketStatePda) {
6
+ const info = await connection.getAccountInfo(marketStatePda);
7
+ if (!info)
8
+ return "unknown";
9
+ const len = info.data.length;
10
+ if (len <= 256)
11
+ return "legacy_tiny";
12
+ if (len === 511)
13
+ return "v3_511";
14
+ if (len === 476)
15
+ return "v2_476";
16
+ if (len === 460)
17
+ return "v1_460";
18
+ if (len >= 484 && len <= 510)
19
+ return "v2_adl_surface";
20
+ return "unknown";
21
+ }
22
+ exports.PROGRAM_IDS_V4 = {
23
+ POOL_PROGRAM: "Acme8JzWrvVqGJz7nTKVsLYisN6MtP83nrs4fVAeXJsN",
24
+ PERP_ENGINE: "6QrsMTMEu9rsLpyxQgRdvQsWoPgHGY9npNNiwTtXsbdc",
25
+ ORACLE_ADAPTER: "6fsnWa9tcKcPiuQgdUbTMsiwUr43MNoxa1FECPFrvSpd",
26
+ PRICE_FEED: "Dgorf5LPiMttdTxWcrsiA2j94kWKw55gJLRJm8P4E1Hn",
27
+ FEE_DISTRIBUTOR: "CHTpVtZQboxjM6N9xk1RR29jZyWqFNV77cmQNJ18RNNL",
28
+ STAKING_PROGRAM: "35HddZHf84u6DeyLoZL3Z3a8pZ59594xu1aizj7VrAGR",
29
+ };