@strobelabs/perpcity-sdk 0.4.1 → 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);
@@ -3532,8 +3532,29 @@ async function openTakerPosition(context, perpId, params) {
3532
3532
  throw new Error("Leverage must be greater than 0");
3533
3533
  }
3534
3534
  const marginScaled = scale6Decimals(params.margin);
3535
- await approveUsdc(context, marginScaled);
3536
3535
  const marginRatio = Math.floor(NUMBER_1E6 / params.leverage);
3536
+ const perpData = await context.getPerpData(perpId);
3537
+ const { creatorFee, insuranceFee, lpFee } = perpData.fees;
3538
+ const protocolFeeRaw = await context.publicClient.readContract({
3539
+ address: context.deployments().perpManager,
3540
+ abi: PERP_MANAGER_ABI,
3541
+ functionName: "protocolFee"
3542
+ });
3543
+ const protocolFeeRate = Number(protocolFeeRaw) / NUMBER_1E6;
3544
+ const notional = marginScaled * BigInt(NUMBER_1E6) / BigInt(marginRatio);
3545
+ const totalFeeRate = creatorFee + insuranceFee + lpFee + protocolFeeRate;
3546
+ const totalFees = BigInt(Math.ceil(Number(notional) * totalFeeRate));
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
+ }
3537
3558
  const unspecifiedAmountLimit = typeof params.unspecifiedAmountLimit === "bigint" ? params.unspecifiedAmountLimit : scale6Decimals(params.unspecifiedAmountLimit);
3538
3559
  const contractParams = {
3539
3560
  holder: context.walletClient.account.address,
@@ -3586,7 +3607,16 @@ async function openMakerPosition(context, perpId, params) {
3586
3607
  const marginScaled = scale6Decimals(params.margin);
3587
3608
  const maxAmt0InScaled = typeof params.maxAmt0In === "bigint" ? params.maxAmt0In : scale6Decimals(params.maxAmt0In);
3588
3609
  const maxAmt1InScaled = typeof params.maxAmt1In === "bigint" ? params.maxAmt1In : scale6Decimals(params.maxAmt1In);
3589
- 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
+ }
3590
3620
  const perpData = await context.getPerpData(perpId);
3591
3621
  const tickLower = priceToTick(params.priceLower, true);
3592
3622
  const tickUpper = priceToTick(params.priceUpper, false);