@strobelabs/perpcity-sdk 0.4.2 → 0.4.3

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.mjs CHANGED
@@ -3476,7 +3476,7 @@ function getPerpTickSpacing(perpData) {
3476
3476
  }
3477
3477
 
3478
3478
  // src/functions/perp-manager.ts
3479
- import { decodeEventLog as decodeEventLog2 } from "viem";
3479
+ import { decodeEventLog as decodeEventLog2, erc20Abi as erc20Abi3 } from "viem";
3480
3480
  async function createPerp(context, params) {
3481
3481
  return withErrorHandling(async () => {
3482
3482
  const sqrtPriceX96 = priceToSqrtPriceX96(params.startingPrice);
@@ -3544,7 +3544,17 @@ async function openTakerPosition(context, perpId, params) {
3544
3544
  const notional = marginScaled * BigInt(NUMBER_1E6) / BigInt(marginRatio);
3545
3545
  const totalFeeRate = creatorFee + insuranceFee + lpFee + protocolFeeRate;
3546
3546
  const totalFees = BigInt(Math.ceil(Number(notional) * totalFeeRate));
3547
- await approveUsdc(context, marginScaled + totalFees);
3547
+ const requiredAmount = marginScaled + totalFees;
3548
+ const currentAllowance = await context.publicClient.readContract({
3549
+ address: context.deployments().usdc,
3550
+ abi: erc20Abi3,
3551
+ functionName: "allowance",
3552
+ args: [context.walletClient.account.address, context.deployments().perpManager],
3553
+ blockTag: "latest"
3554
+ });
3555
+ if (currentAllowance < requiredAmount) {
3556
+ await approveUsdc(context, requiredAmount);
3557
+ }
3548
3558
  const unspecifiedAmountLimit = typeof params.unspecifiedAmountLimit === "bigint" ? params.unspecifiedAmountLimit : scale6Decimals(params.unspecifiedAmountLimit);
3549
3559
  const contractParams = {
3550
3560
  holder: context.walletClient.account.address,
@@ -3597,7 +3607,16 @@ async function openMakerPosition(context, perpId, params) {
3597
3607
  const marginScaled = scale6Decimals(params.margin);
3598
3608
  const maxAmt0InScaled = typeof params.maxAmt0In === "bigint" ? params.maxAmt0In : scale6Decimals(params.maxAmt0In);
3599
3609
  const maxAmt1InScaled = typeof params.maxAmt1In === "bigint" ? params.maxAmt1In : scale6Decimals(params.maxAmt1In);
3600
- await approveUsdc(context, marginScaled);
3610
+ const currentAllowance = await context.publicClient.readContract({
3611
+ address: context.deployments().usdc,
3612
+ abi: erc20Abi3,
3613
+ functionName: "allowance",
3614
+ args: [context.walletClient.account.address, context.deployments().perpManager],
3615
+ blockTag: "latest"
3616
+ });
3617
+ if (currentAllowance < marginScaled) {
3618
+ await approveUsdc(context, marginScaled);
3619
+ }
3601
3620
  const perpData = await context.getPerpData(perpId);
3602
3621
  const tickLower = priceToTick(params.priceLower, true);
3603
3622
  const tickUpper = priceToTick(params.priceUpper, false);