@strobelabs/perpcity-sdk 0.3.10 → 0.3.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.
package/dist/index.js CHANGED
@@ -2414,7 +2414,7 @@ var import_viem = require("viem");
2414
2414
  var DEFAULT_CONFIRMATIONS = 2;
2415
2415
  async function approveUsdc(context, amount, confirmations = DEFAULT_CONFIRMATIONS) {
2416
2416
  const deployments = context.deployments();
2417
- const { request } = await context.walletClient.simulateContract({
2417
+ const { request } = await context.publicClient.simulateContract({
2418
2418
  address: deployments.usdc,
2419
2419
  abi: import_viem.erc20Abi,
2420
2420
  functionName: "approve",
@@ -2422,7 +2422,7 @@ async function approveUsdc(context, amount, confirmations = DEFAULT_CONFIRMATION
2422
2422
  account: context.walletClient.account
2423
2423
  });
2424
2424
  const hash = await context.walletClient.writeContract(request);
2425
- await context.walletClient.waitForTransactionReceipt({
2425
+ await context.publicClient.waitForTransactionReceipt({
2426
2426
  confirmations,
2427
2427
  hash
2428
2428
  });
@@ -3013,28 +3013,28 @@ async function estimateLiquidity(_context, tickLower, tickUpper, usdScaled) {
3013
3013
  }
3014
3014
  function getSqrtRatioAtTick(tick) {
3015
3015
  const absTick = Math.abs(tick);
3016
- let ratio = 0xfffcb933bd6fad37aa2d162d1a594001n;
3017
- if (absTick & 1) ratio = ratio * 0xfff97272373d413259a46990580e213an >> 128n;
3018
- if (absTick & 2) ratio = ratio * 0xfff2e50f5f656932ef12357cf3c7fdccn >> 128n;
3019
- if (absTick & 4) ratio = ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0n >> 128n;
3020
- if (absTick & 8) ratio = ratio * 0xffcb9843d60f6159c9db58835c926644n >> 128n;
3021
- if (absTick & 16) ratio = ratio * 0xff973b41fa98c081472e6896dfb254c0n >> 128n;
3022
- if (absTick & 32) ratio = ratio * 0xff2ea16466c96a3843ec78b326b52861n >> 128n;
3023
- if (absTick & 64) ratio = ratio * 0xfe5dee046a99a2a811c461f1969c3053n >> 128n;
3024
- if (absTick & 128) ratio = ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4n >> 128n;
3025
- if (absTick & 256) ratio = ratio * 0xf987a7253ac413176f2b074cf7815e54n >> 128n;
3026
- if (absTick & 512) ratio = ratio * 0xf3392b0822b70005940c7a398e4b70f3n >> 128n;
3027
- if (absTick & 1024) ratio = ratio * 0xe7159475a2c29b7443b29c7fa6e889d9n >> 128n;
3028
- if (absTick & 2048) ratio = ratio * 0xd097f3bdfd2022b8845ad8f792aa5825n >> 128n;
3029
- if (absTick & 4096) ratio = ratio * 0xa9f746462d870fdf8a65dc1f90e061e5n >> 128n;
3030
- if (absTick & 8192) ratio = ratio * 0x70d869a156d2a1b890bb3df62baf32f7n >> 128n;
3031
- if (absTick & 16384) ratio = ratio * 0x31be135f97d08fd981231505542fcfa6n >> 128n;
3032
- if (absTick & 32768) ratio = ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9n >> 128n;
3033
- if (absTick & 65536) ratio = ratio * 0x5d6af8dedb81196699c329225ee604n >> 128n;
3034
- if (absTick & 131072) ratio = ratio * 0x2216e584f5fa1ea926041bedfe98n >> 128n;
3035
- if (absTick & 262144) ratio = ratio * 0x48a170391f7dc42444e8fa2n >> 128n;
3036
- if (tick > 0) ratio = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn / ratio;
3037
- return ratio % (1n << 32n) > 0n ? (ratio >> 32n) + 1n : ratio >> 32n;
3016
+ let ratio = absTick & 1 ? 0xfffcb933bd6fad37aa2d162d1a594001n : 0x100000000000000000000000000000000n;
3017
+ if (absTick & 2) ratio = ratio * 0xfff97272373d413259a46990580e213an >> 128n;
3018
+ if (absTick & 4) ratio = ratio * 0xfff2e50f5f656932ef12357cf3c7fdccn >> 128n;
3019
+ if (absTick & 8) ratio = ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0n >> 128n;
3020
+ if (absTick & 16) ratio = ratio * 0xffcb9843d60f6159c9db58835c926644n >> 128n;
3021
+ if (absTick & 32) ratio = ratio * 0xff973b41fa98c081472e6896dfb254c0n >> 128n;
3022
+ if (absTick & 64) ratio = ratio * 0xff2ea16466c96a3843ec78b326b52861n >> 128n;
3023
+ if (absTick & 128) ratio = ratio * 0xfe5dee046a99a2a811c461f1969c3053n >> 128n;
3024
+ if (absTick & 256) ratio = ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4n >> 128n;
3025
+ if (absTick & 512) ratio = ratio * 0xf987a7253ac413176f2b074cf7815e54n >> 128n;
3026
+ if (absTick & 1024) ratio = ratio * 0xf3392b0822b70005940c7a398e4b70f3n >> 128n;
3027
+ if (absTick & 2048) ratio = ratio * 0xe7159475a2c29b7443b29c7fa6e889d9n >> 128n;
3028
+ if (absTick & 4096) ratio = ratio * 0xd097f3bdfd2022b8845ad8f792aa5825n >> 128n;
3029
+ if (absTick & 8192) ratio = ratio * 0xa9f746462d870fdf8a65dc1f90e061e5n >> 128n;
3030
+ if (absTick & 16384) ratio = ratio * 0x70d869a156d2a1b890bb3df62baf32f7n >> 128n;
3031
+ if (absTick & 32768) ratio = ratio * 0x31be135f97d08fd981231505542fcfa6n >> 128n;
3032
+ if (absTick & 65536) ratio = ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9n >> 128n;
3033
+ if (absTick & 131072) ratio = ratio * 0x5d6af8dedb81196699c329225ee604n >> 128n;
3034
+ if (absTick & 262144) ratio = ratio * 0x2216e584f5fa1ea926041bedfe98n >> 128n;
3035
+ if (absTick & 524288) ratio = ratio * 0x48a170391f7dc42444e8fa2n >> 128n;
3036
+ if (tick > 0) ratio = (1n << 256n) / ratio;
3037
+ return ratio >> 32n;
3038
3038
  }
3039
3039
  function calculateLiquidityForTargetRatio(marginScaled, tickLower, tickUpper, currentSqrtPriceX96, targetMarginRatio) {
3040
3040
  if (tickLower >= tickUpper) {
@@ -3092,9 +3092,32 @@ function getRpcUrl(config = {}) {
3092
3092
  var PerpCityContext = class {
3093
3093
  constructor(config) {
3094
3094
  this.configCache = new import_ttlcache.default({ ttl: 5 * 60 * 1e3 });
3095
- this.walletClient = config.walletClient.extend(import_viem3.publicActions);
3095
+ this.walletClient = config.walletClient;
3096
+ if (!config.walletClient.chain?.id) {
3097
+ throw new Error(
3098
+ "PerpCityContext: walletClient.chain must be defined with a numeric id. Ensure your walletClient was created with a chain parameter."
3099
+ );
3100
+ }
3101
+ this.publicClient = (0, import_viem3.createPublicClient)({
3102
+ chain: config.walletClient.chain,
3103
+ transport: (0, import_viem3.http)(config.rpcUrl, { batch: true })
3104
+ });
3096
3105
  this._deployments = config.deployments;
3097
3106
  }
3107
+ /**
3108
+ * Validates that the RPC endpoint matches the expected chain.
3109
+ * Call this after construction to verify configuration.
3110
+ * @throws Error if RPC chain ID doesn't match walletClient chain ID
3111
+ */
3112
+ async validateChainId() {
3113
+ const rpcChainId = await this.publicClient.getChainId();
3114
+ const expectedChainId = this.walletClient.chain.id;
3115
+ if (rpcChainId !== expectedChainId) {
3116
+ throw new Error(
3117
+ `PerpCityContext: RPC chain mismatch. RPC returned chain ID ${rpcChainId}, but walletClient expects chain ID ${expectedChainId}. Ensure rpcUrl corresponds to the correct network.`
3118
+ );
3119
+ }
3120
+ }
3098
3121
  deployments() {
3099
3122
  return this._deployments;
3100
3123
  }
@@ -3123,7 +3146,7 @@ var PerpCityContext = class {
3123
3146
  if (cached) {
3124
3147
  return cached;
3125
3148
  }
3126
- const result = await this.walletClient.readContract({
3149
+ const result = await this.publicClient.readContract({
3127
3150
  address: this.deployments().perpManager,
3128
3151
  abi: PERP_MANAGER_ABI,
3129
3152
  functionName: "cfgs",
@@ -3162,7 +3185,7 @@ var PerpCityContext = class {
3162
3185
  const sqrtPrice = Math.sqrt(markPrice);
3163
3186
  sqrtPriceX96 = BigInt(Math.floor(sqrtPrice * 2 ** 96));
3164
3187
  } else {
3165
- sqrtPriceX96 = await this.walletClient.readContract({
3188
+ sqrtPriceX96 = await this.publicClient.readContract({
3166
3189
  address: this.deployments().perpManager,
3167
3190
  abi: PERP_MANAGER_ABI,
3168
3191
  functionName: "timeWeightedAvgSqrtPriceX96",
@@ -3178,37 +3201,37 @@ var PerpCityContext = class {
3178
3201
  lpFee,
3179
3202
  liquidationFee
3180
3203
  ] = await Promise.all([
3181
- this.walletClient.readContract({
3204
+ this.publicClient.readContract({
3182
3205
  address: cfg.marginRatios,
3183
3206
  abi: MARGIN_RATIOS_ABI,
3184
3207
  functionName: "MIN_TAKER_RATIO"
3185
3208
  }),
3186
- this.walletClient.readContract({
3209
+ this.publicClient.readContract({
3187
3210
  address: cfg.marginRatios,
3188
3211
  abi: MARGIN_RATIOS_ABI,
3189
3212
  functionName: "MAX_TAKER_RATIO"
3190
3213
  }),
3191
- this.walletClient.readContract({
3214
+ this.publicClient.readContract({
3192
3215
  address: cfg.marginRatios,
3193
3216
  abi: MARGIN_RATIOS_ABI,
3194
3217
  functionName: "LIQUIDATION_TAKER_RATIO"
3195
3218
  }),
3196
- this.walletClient.readContract({
3219
+ this.publicClient.readContract({
3197
3220
  address: cfg.fees,
3198
3221
  abi: FEES_ABI,
3199
3222
  functionName: "CREATOR_FEE"
3200
3223
  }),
3201
- this.walletClient.readContract({
3224
+ this.publicClient.readContract({
3202
3225
  address: cfg.fees,
3203
3226
  abi: FEES_ABI,
3204
3227
  functionName: "INSURANCE_FEE"
3205
3228
  }),
3206
- this.walletClient.readContract({
3229
+ this.publicClient.readContract({
3207
3230
  address: cfg.fees,
3208
3231
  abi: FEES_ABI,
3209
3232
  functionName: "LP_FEE"
3210
3233
  }),
3211
- this.walletClient.readContract({
3234
+ this.publicClient.readContract({
3212
3235
  address: cfg.fees,
3213
3236
  abi: FEES_ABI,
3214
3237
  functionName: "LIQUIDATION_FEE"
@@ -3243,7 +3266,7 @@ var PerpCityContext = class {
3243
3266
  return this.fetchPerpData(perpId);
3244
3267
  }
3245
3268
  async fetchUserData(userAddress, positions) {
3246
- const usdcBalance = await this.walletClient.readContract({
3269
+ const usdcBalance = await this.publicClient.readContract({
3247
3270
  address: this.deployments().usdc,
3248
3271
  abi: import_viem3.erc20Abi,
3249
3272
  functionName: "balanceOf",
@@ -3269,7 +3292,7 @@ var PerpCityContext = class {
3269
3292
  }
3270
3293
  async fetchPositionLiveDetailsFromContract(_perpId, positionId) {
3271
3294
  return withErrorHandling(async () => {
3272
- const result = await this.walletClient.readContract({
3295
+ const result = await this.publicClient.readContract({
3273
3296
  address: this.deployments().perpManager,
3274
3297
  abi: PERP_MANAGER_ABI,
3275
3298
  functionName: "quoteClosePosition",
@@ -3321,7 +3344,7 @@ var PerpCityContext = class {
3321
3344
  */
3322
3345
  async getPositionRawData(positionId) {
3323
3346
  return withErrorHandling(async () => {
3324
- const result = await this.walletClient.readContract({
3347
+ const result = await this.publicClient.readContract({
3325
3348
  address: this.deployments().perpManager,
3326
3349
  abi: PERP_MANAGER_ABI,
3327
3350
  functionName: "positions",
@@ -3369,7 +3392,7 @@ var OpenPosition = class _OpenPosition {
3369
3392
  minAmt1Out: scale6Decimals(params.minAmt1Out),
3370
3393
  maxAmt1In: scale6Decimals(params.maxAmt1In)
3371
3394
  };
3372
- const { request } = await this.context.walletClient.extend(import_viem4.publicActions).simulateContract({
3395
+ const { request } = await this.context.publicClient.simulateContract({
3373
3396
  address: this.context.deployments().perpManager,
3374
3397
  abi: PERP_MANAGER_ABI,
3375
3398
  functionName: "closePosition",
@@ -3379,8 +3402,7 @@ var OpenPosition = class _OpenPosition {
3379
3402
  // Provide explicit gas limit to avoid estimation issues
3380
3403
  });
3381
3404
  const txHash = await this.context.walletClient.writeContract(request);
3382
- const publicClient = this.context.walletClient.extend(import_viem4.publicActions);
3383
- const receipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
3405
+ const receipt = await this.context.publicClient.waitForTransactionReceipt({ hash: txHash });
3384
3406
  if (receipt.status === "reverted") {
3385
3407
  throw new Error(`Transaction reverted. Hash: ${txHash}`);
3386
3408
  }
@@ -3437,7 +3459,7 @@ var OpenPosition = class _OpenPosition {
3437
3459
  }
3438
3460
  async liveDetails() {
3439
3461
  return withErrorHandling(async () => {
3440
- const result = await this.context.walletClient.readContract({
3462
+ const result = await this.context.publicClient.readContract({
3441
3463
  address: this.context.deployments().perpManager,
3442
3464
  abi: PERP_MANAGER_ABI,
3443
3465
  functionName: "quoteClosePosition",
@@ -3497,7 +3519,7 @@ async function createPerp(context, params) {
3497
3519
  sqrtPriceImpactLimit,
3498
3520
  startingSqrtPriceX96: sqrtPriceX96
3499
3521
  };
3500
- const { request } = await context.walletClient.simulateContract({
3522
+ const { request } = await context.publicClient.simulateContract({
3501
3523
  address: context.deployments().perpManager,
3502
3524
  abi: PERP_MANAGER_ABI,
3503
3525
  functionName: "createPerp",
@@ -3505,8 +3527,7 @@ async function createPerp(context, params) {
3505
3527
  account: context.walletClient.account
3506
3528
  });
3507
3529
  const txHash = await context.walletClient.writeContract(request);
3508
- const publicClient = context.walletClient.extend(import_viem5.publicActions);
3509
- const receipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
3530
+ const receipt = await context.publicClient.waitForTransactionReceipt({ hash: txHash });
3510
3531
  if (receipt.status === "reverted") {
3511
3532
  throw new Error(`Transaction reverted. Hash: ${txHash}`);
3512
3533
  }
@@ -3544,7 +3565,7 @@ async function openTakerPosition(context, perpId, params) {
3544
3565
  levX96,
3545
3566
  unspecifiedAmountLimit
3546
3567
  };
3547
- const { request } = await context.walletClient.simulateContract({
3568
+ const { request } = await context.publicClient.simulateContract({
3548
3569
  address: context.deployments().perpManager,
3549
3570
  abi: PERP_MANAGER_ABI,
3550
3571
  functionName: "openTakerPos",
@@ -3552,8 +3573,7 @@ async function openTakerPosition(context, perpId, params) {
3552
3573
  account: context.walletClient.account
3553
3574
  });
3554
3575
  const txHash = await context.walletClient.writeContract(request);
3555
- const publicClient = context.walletClient.extend(import_viem5.publicActions);
3556
- const receipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
3576
+ const receipt = await context.publicClient.waitForTransactionReceipt({ hash: txHash });
3557
3577
  if (receipt.status === "reverted") {
3558
3578
  throw new Error(`Transaction reverted. Hash: ${txHash}`);
3559
3579
  }
@@ -3605,7 +3625,7 @@ async function openMakerPosition(context, perpId, params) {
3605
3625
  maxAmt0In: maxAmt0InScaled,
3606
3626
  maxAmt1In: maxAmt1InScaled
3607
3627
  };
3608
- const { request } = await context.walletClient.simulateContract({
3628
+ const { request } = await context.publicClient.simulateContract({
3609
3629
  address: context.deployments().perpManager,
3610
3630
  abi: PERP_MANAGER_ABI,
3611
3631
  functionName: "openMakerPos",
@@ -3613,8 +3633,7 @@ async function openMakerPosition(context, perpId, params) {
3613
3633
  account: context.walletClient.account
3614
3634
  });
3615
3635
  const txHash = await context.walletClient.writeContract(request);
3616
- const publicClient = context.walletClient.extend(import_viem5.publicActions);
3617
- const receipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
3636
+ const receipt = await context.publicClient.waitForTransactionReceipt({ hash: txHash });
3618
3637
  if (receipt.status === "reverted") {
3619
3638
  throw new Error(`Transaction reverted. Hash: ${txHash}`);
3620
3639
  }
@@ -3677,7 +3696,7 @@ async function closePosition(context, perpId, positionId, params) {
3677
3696
  minAmt1Out: scale6Decimals(params.minAmt1Out),
3678
3697
  maxAmt1In: scale6Decimals(params.maxAmt1In)
3679
3698
  };
3680
- const { request } = await context.walletClient.extend(import_viem6.publicActions).simulateContract({
3699
+ const { request } = await context.publicClient.simulateContract({
3681
3700
  address: context.deployments().perpManager,
3682
3701
  abi: PERP_MANAGER_ABI,
3683
3702
  functionName: "closePosition",
@@ -3687,8 +3706,7 @@ async function closePosition(context, perpId, positionId, params) {
3687
3706
  // Provide explicit gas limit to avoid estimation issues
3688
3707
  });
3689
3708
  const txHash = await context.walletClient.writeContract(request);
3690
- const publicClient = context.walletClient.extend(import_viem6.publicActions);
3691
- const receipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
3709
+ const receipt = await context.publicClient.waitForTransactionReceipt({ hash: txHash });
3692
3710
  if (receipt.status === "reverted") {
3693
3711
  throw new Error(`Transaction reverted. Hash: ${txHash}`);
3694
3712
  }
@@ -3725,7 +3743,7 @@ async function closePosition(context, perpId, positionId, params) {
3725
3743
  }
3726
3744
  async function getPositionLiveDetailsFromContract(context, _perpId, positionId) {
3727
3745
  return withErrorHandling(async () => {
3728
- const result = await context.walletClient.readContract({
3746
+ const result = await context.publicClient.readContract({
3729
3747
  address: context.deployments().perpManager,
3730
3748
  abi: PERP_MANAGER_ABI,
3731
3749
  functionName: "quoteClosePosition",