@flaunch/sdk 0.9.5 → 0.9.7

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.esm.js CHANGED
@@ -13998,17 +13998,18 @@ class ReadWriteFlaunchZap extends ReadFlaunchZap {
13998
13998
  * @param params.initialMarketCapUSD - Initial market cap in USD
13999
13999
  * @param params.creator - Address of the token creator
14000
14000
  * @param params.creatorFeeAllocationPercent - Percentage of fees allocated to creator (0-100)
14001
- * @param params.creatorSplitPercent - Percentage of fees allocated to creator (0-100)
14001
+ * @param params.creatorSplitPercent - Split percentage of the fees for the creator (0-100)
14002
+ * @param params.managerOwnerSplitPercent - Split percentage of the fees for the manager owner (0-100)
14002
14003
  * @param params.splitReceivers - List of recipients and their percentage of the fees
14003
14004
  * @param params.flaunchAt - Optional timestamp when the flaunch should start
14004
14005
  * @param params.premineAmount - Optional amount of tokens to premine
14005
- * @param params.creatorSplitPercent - Split percentage of the fees for the creator (0-100)
14006
- * @param params.splitReceivers - List of recipients and their percentage of the fees
14006
+ * @param params.treasuryManagerParams - Optional treasury manager configuration
14007
14007
  * @returns Transaction response for the flaunch creation
14008
14008
  */
14009
14009
  async flaunchWithSplitManager(params) {
14010
14010
  const VALID_SHARE_TOTAL = 10000000n; // 5 decimals as BigInt
14011
14011
  let creatorShare = (BigInt(params.creatorSplitPercent) * VALID_SHARE_TOTAL) / 100n;
14012
+ const managerOwnerShare = (BigInt(params.managerOwnerSplitPercent) * VALID_SHARE_TOTAL) / 100n;
14012
14013
  const recipientShares = params.splitReceivers.map((receiver) => {
14013
14014
  return {
14014
14015
  recipient: receiver.address,
@@ -14017,7 +14018,7 @@ class ReadWriteFlaunchZap extends ReadFlaunchZap {
14017
14018
  });
14018
14019
  const totalRecipientShares = recipientShares.reduce((acc, curr) => acc + curr.share, 0n);
14019
14020
  // if there's a remainder (due to rounding errors), add it to the creator share
14020
- const remainderShares = VALID_SHARE_TOTAL - totalRecipientShares;
14021
+ const remainderShares = VALID_SHARE_TOTAL - totalRecipientShares - managerOwnerShare;
14021
14022
  creatorShare += remainderShares;
14022
14023
  const initializeData = encodeAbiParameters$1([
14023
14024
  {
@@ -14025,6 +14026,7 @@ class ReadWriteFlaunchZap extends ReadFlaunchZap {
14025
14026
  name: "params",
14026
14027
  components: [
14027
14028
  { type: "uint256", name: "creatorShare" },
14029
+ { type: "uint256", name: "ownerShare" },
14028
14030
  {
14029
14031
  type: "tuple[]",
14030
14032
  name: "recipientShares",
@@ -14038,6 +14040,7 @@ class ReadWriteFlaunchZap extends ReadFlaunchZap {
14038
14040
  ], [
14039
14041
  {
14040
14042
  creatorShare,
14043
+ ownerShare: managerOwnerShare,
14041
14044
  recipientShares,
14042
14045
  },
14043
14046
  ]);
@@ -25456,6 +25459,10 @@ function maxLiquidityForAmount0Precise(sqrtRatioAX96, sqrtRatioBX96, amount0) {
25456
25459
  if (sqrtRatioAX96 > sqrtRatioBX96) {
25457
25460
  [sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
25458
25461
  }
25462
+ // Handle edge case where sqrt ratios are equal (division by zero)
25463
+ if (sqrtRatioAX96 === sqrtRatioBX96) {
25464
+ return 0n;
25465
+ }
25459
25466
  const Q96 = 2n ** 96n;
25460
25467
  const numerator = amount0 * sqrtRatioAX96 * sqrtRatioBX96;
25461
25468
  const denominator = Q96 * (sqrtRatioBX96 - sqrtRatioAX96);
@@ -25472,6 +25479,10 @@ function maxLiquidityForAmount1(sqrtRatioAX96, sqrtRatioBX96, amount1) {
25472
25479
  if (sqrtRatioAX96 > sqrtRatioBX96) {
25473
25480
  [sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
25474
25481
  }
25482
+ // Handle edge case where sqrt ratios are equal (division by zero)
25483
+ if (sqrtRatioAX96 === sqrtRatioBX96) {
25484
+ return 0n;
25485
+ }
25475
25486
  const Q96 = 2n ** 96n;
25476
25487
  return (amount1 * Q96) / (sqrtRatioBX96 - sqrtRatioAX96);
25477
25488
  }
@@ -26383,7 +26394,7 @@ class ReadFlaunchSDK {
26383
26394
  * @returns Promise<number> - The current tick of the pool
26384
26395
  */
26385
26396
  async currentTick(coinAddress, version) {
26386
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26397
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26387
26398
  const poolId = await this.poolId(coinAddress, coinVersion);
26388
26399
  const poolState = await this.readStateView.poolSlot0({ poolId });
26389
26400
  return poolState.tick;
@@ -26395,7 +26406,7 @@ class ReadFlaunchSDK {
26395
26406
  * @returns Promise<string> - The price of the coin in ETH with 18 decimals precision
26396
26407
  */
26397
26408
  async coinPriceInETH(coinAddress, version) {
26398
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26409
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26399
26410
  const isFLETHZero = this.flETHIsCurrencyZero(coinAddress);
26400
26411
  const currentTick = await this.currentTick(coinAddress, coinVersion);
26401
26412
  const price = Math.pow(1.0001, currentTick);
@@ -26415,7 +26426,7 @@ class ReadFlaunchSDK {
26415
26426
  * @returns Promise<string> - The price of the coin in USD with 18 decimal precision
26416
26427
  */
26417
26428
  async coinPriceInUSD({ coinAddress, version, drift, }) {
26418
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26429
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26419
26430
  const ethPerCoin = await this.coinPriceInETH(coinAddress, coinVersion);
26420
26431
  const ethPrice = await this.getETHUSDCPrice(drift);
26421
26432
  return (parseFloat(ethPerCoin) * ethPrice).toFixed(18);
@@ -26463,7 +26474,7 @@ class ReadFlaunchSDK {
26463
26474
  * @returns Fair launch information from the appropriate contract version
26464
26475
  */
26465
26476
  async fairLaunchInfo(coinAddress, version) {
26466
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26477
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26467
26478
  const poolId = await this.poolId(coinAddress, coinVersion);
26468
26479
  return this.getFairLaunch(coinVersion).fairLaunchInfo({ poolId });
26469
26480
  }
@@ -26474,12 +26485,12 @@ class ReadFlaunchSDK {
26474
26485
  * @returns Promise<boolean> - True if fair launch is active, false otherwise
26475
26486
  */
26476
26487
  async isFairLaunchActive(coinAddress, version) {
26477
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26488
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26478
26489
  const poolId = await this.poolId(coinAddress, coinVersion);
26479
26490
  return this.getFairLaunch(coinVersion).isFairLaunchActive({ poolId });
26480
26491
  }
26481
26492
  async trustedPoolKeySignerStatus(coinAddress, version) {
26482
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26493
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26483
26494
  if (coinVersion === FlaunchVersion.ANY) {
26484
26495
  throw new Error("AnyPositionManager is not supported for TrustedSigner");
26485
26496
  }
@@ -26528,7 +26539,7 @@ class ReadFlaunchSDK {
26528
26539
  * @returns Promise<number> - The duration in seconds (30 minutes for V1, variable for V1.1)
26529
26540
  */
26530
26541
  async fairLaunchDuration(coinAddress, version) {
26531
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26542
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26532
26543
  const poolId = await this.poolId(coinAddress, coinVersion);
26533
26544
  return this.getFairLaunch(coinVersion).fairLaunchDuration({ poolId });
26534
26545
  }
@@ -26539,7 +26550,7 @@ class ReadFlaunchSDK {
26539
26550
  * @returns Promise<number> - The initial tick value
26540
26551
  */
26541
26552
  async initialTick(coinAddress, version) {
26542
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26553
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26543
26554
  const poolId = await this.poolId(coinAddress, coinVersion);
26544
26555
  const fairLaunchInfo = await this.getFairLaunch(coinVersion).fairLaunchInfo({ poolId });
26545
26556
  return fairLaunchInfo.initialTick;
@@ -26551,7 +26562,7 @@ class ReadFlaunchSDK {
26551
26562
  * @returns Promise<{flETHAmount: bigint, coinAmount: bigint, tickLower: number, tickUpper: number}> - Position details
26552
26563
  */
26553
26564
  async fairLaunchETHOnlyPosition(coinAddress, version) {
26554
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26565
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26555
26566
  const poolId = await this.poolId(coinAddress, coinVersion);
26556
26567
  const initialTick = await this.initialTick(coinAddress, coinVersion);
26557
26568
  const currentTick = await this.currentTick(coinAddress, coinVersion);
@@ -26599,7 +26610,7 @@ class ReadFlaunchSDK {
26599
26610
  * @returns Promise<{flETHAmount: bigint, coinAmount: bigint, tickLower: number, tickUpper: number}> - Position details
26600
26611
  */
26601
26612
  async fairLaunchCoinOnlyPosition(coinAddress, version) {
26602
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26613
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26603
26614
  const poolId = await this.poolId(coinAddress, coinVersion);
26604
26615
  const initialTick = await this.initialTick(coinAddress, coinVersion);
26605
26616
  const currentTick = await this.currentTick(coinAddress, coinVersion);
@@ -26647,7 +26658,7 @@ class ReadFlaunchSDK {
26647
26658
  * @returns Promise<{flETHAmount: bigint, coinAmount: bigint, pendingEth: bigint, tickLower: number, tickUpper: number}> - Bid wall position details
26648
26659
  */
26649
26660
  async bidWallPosition(coinAddress, version) {
26650
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26661
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26651
26662
  const poolId = await this.poolId(coinAddress, coinVersion);
26652
26663
  const isFLETHZero = this.flETHIsCurrencyZero(coinAddress);
26653
26664
  const { amount0_: amount0, amount1_: amount1, pendingEth_: pendingEth, } = await this.getBidWall(coinVersion).position({ poolId });
@@ -26763,13 +26774,8 @@ class ReadFlaunchSDK {
26763
26774
  */
26764
26775
  async poolId(coinAddress, version) {
26765
26776
  let hookAddress;
26766
- if (version) {
26767
- hookAddress = this.getPositionManagerAddress(version);
26768
- }
26769
- else {
26770
- const coinVersion = await this.getCoinVersion(coinAddress);
26771
- hookAddress = this.getPositionManagerAddress(coinVersion);
26772
- }
26777
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26778
+ hookAddress = this.getPositionManagerAddress(coinVersion);
26773
26779
  return getPoolId(orderPoolKey({
26774
26780
  currency0: FLETHAddress[this.chainId],
26775
26781
  currency1: coinAddress,
@@ -26827,7 +26833,7 @@ class ReadFlaunchSDK {
26827
26833
  * @returns Promise<bigint> - The expected amount of ETH to receive
26828
26834
  */
26829
26835
  async getSellQuoteExactInput({ coinAddress, version, amountIn, intermediatePoolKey, }) {
26830
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26836
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26831
26837
  return this.readQuoter.getSellQuoteExactInput({
26832
26838
  coinAddress,
26833
26839
  amountIn,
@@ -26846,7 +26852,7 @@ class ReadFlaunchSDK {
26846
26852
  * @returns Promise<bigint> - The expected amount of coins to receive
26847
26853
  */
26848
26854
  async getBuyQuoteExactInput({ coinAddress, version, amountIn, intermediatePoolKey, hookData, userWallet, }) {
26849
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26855
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26850
26856
  return this.readQuoter.getBuyQuoteExactInput({
26851
26857
  coinAddress,
26852
26858
  amountIn,
@@ -26867,7 +26873,7 @@ class ReadFlaunchSDK {
26867
26873
  * @returns Promise<bigint> - The required amount of ETH or inputToken to spend
26868
26874
  */
26869
26875
  async getBuyQuoteExactOutput({ coinAddress, amountOut, version, intermediatePoolKey, hookData, userWallet, }) {
26870
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26876
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26871
26877
  return this.readQuoter.getBuyQuoteExactOutput({
26872
26878
  coinAddress,
26873
26879
  coinOut: amountOut,
@@ -27054,24 +27060,9 @@ class ReadFlaunchSDK {
27054
27060
  });
27055
27061
  // If no current tick is provided from the above calculation, get it from the pool state
27056
27062
  if (!currentTick) {
27057
- let version = params.version;
27058
- // if version is not provided, check on existing managers, else default to ANY
27059
- if (!version) {
27060
- try {
27061
- version = await this.getCoinVersion(coinAddress);
27062
- }
27063
- catch {
27064
- version = FlaunchVersion.ANY;
27065
- }
27066
- }
27063
+ const version = await this.determineCoinVersion(coinAddress, params.version);
27067
27064
  const poolState = await this.readStateView.poolSlot0({
27068
- poolId: getPoolId(orderPoolKey({
27069
- currency0: coinAddress,
27070
- currency1: FLETHAddress[this.chainId],
27071
- fee: 0,
27072
- tickSpacing: TICK_SPACING,
27073
- hooks: this.getPositionManagerAddress(version),
27074
- })),
27065
+ poolId: getPoolId(this.createPoolKey(coinAddress, version)),
27075
27066
  });
27076
27067
  currentTick = poolState.tick;
27077
27068
  }
@@ -27145,24 +27136,9 @@ class ReadFlaunchSDK {
27145
27136
  });
27146
27137
  // get the current pool state for the coin
27147
27138
  if (!currentTick) {
27148
- let version = params.version;
27149
- // if version is not provided, check on existing managers, else default to ANY
27150
- if (!version) {
27151
- try {
27152
- version = await this.getCoinVersion(coinAddress);
27153
- }
27154
- catch {
27155
- version = FlaunchVersion.ANY;
27156
- }
27157
- }
27139
+ const version = await this.determineCoinVersion(coinAddress, params.version);
27158
27140
  const poolState = await this.readStateView.poolSlot0({
27159
- poolId: getPoolId(orderPoolKey({
27160
- currency0: coinAddress,
27161
- currency1: FLETHAddress[this.chainId],
27162
- fee: 0,
27163
- tickSpacing: TICK_SPACING,
27164
- hooks: this.getPositionManagerAddress(version),
27165
- })),
27141
+ poolId: getPoolId(this.createPoolKey(coinAddress, version)),
27166
27142
  });
27167
27143
  currentTick = poolState.tick;
27168
27144
  }
@@ -27317,6 +27293,39 @@ class ReadFlaunchSDK {
27317
27293
  args: { owner, operator },
27318
27294
  });
27319
27295
  }
27296
+ /**
27297
+ * Determines the version for a coin, using provided version or fetching it
27298
+ * @param coinAddress - The coin address
27299
+ * @param version - Optional version, if not provided will be fetched
27300
+ * @returns The determined version
27301
+ */
27302
+ async determineCoinVersion(coinAddress, version) {
27303
+ if (!version) {
27304
+ try {
27305
+ version = await this.getCoinVersion(coinAddress);
27306
+ }
27307
+ catch {
27308
+ version = FlaunchVersion.ANY;
27309
+ }
27310
+ }
27311
+ return version;
27312
+ }
27313
+ /**
27314
+ * Creates a pool key for the given coin and version
27315
+ * @param coinAddress - The coin address
27316
+ * @param version - The version to use for position manager
27317
+ * @returns The ordered pool key
27318
+ */
27319
+ createPoolKey(coinAddress, version) {
27320
+ const flethAddress = FLETHAddress[this.chainId];
27321
+ return orderPoolKey({
27322
+ currency0: coinAddress,
27323
+ currency1: flethAddress,
27324
+ fee: 0,
27325
+ tickSpacing: this.TICK_SPACING,
27326
+ hooks: this.getPositionManagerAddress(version),
27327
+ });
27328
+ }
27320
27329
  }
27321
27330
  class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
27322
27331
  constructor(chainId, drift = createDrift$1(), publicClient) {
@@ -27441,7 +27450,7 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
27441
27450
  * @returns Transaction response for the buy operation
27442
27451
  */
27443
27452
  async buyCoin(params, version) {
27444
- const coinVersion = version || (await this.getCoinVersion(params.coinAddress));
27453
+ const coinVersion = await this.determineCoinVersion(params.coinAddress, version);
27445
27454
  const sender = await this.drift.getSignerAddress();
27446
27455
  let amountIn;
27447
27456
  let amountOutMin;
@@ -27526,7 +27535,7 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
27526
27535
  * @returns Transaction response for the sell operation
27527
27536
  */
27528
27537
  async sellCoin(params, version) {
27529
- const coinVersion = version || (await this.getCoinVersion(params.coinAddress));
27538
+ const coinVersion = await this.determineCoinVersion(params.coinAddress, version);
27530
27539
  let amountOutMin;
27531
27540
  await this.readQuoter.contract.cache.clear();
27532
27541
  if (params.amountOutMin === undefined) {
@@ -27749,22 +27758,8 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
27749
27758
  let tickLower;
27750
27759
  let tickUpper;
27751
27760
  let currentTick;
27752
- let version = params.version;
27753
- if (!version) {
27754
- try {
27755
- version = await this.getCoinVersion(coinAddress);
27756
- }
27757
- catch {
27758
- version = FlaunchVersion.ANY;
27759
- }
27760
- }
27761
- const poolKey = orderPoolKey({
27762
- currency0: coinAddress,
27763
- currency1: flethAddress,
27764
- fee: 0,
27765
- tickSpacing: this.TICK_SPACING,
27766
- hooks: this.getPositionManagerAddress(version),
27767
- });
27761
+ const version = await this.determineCoinVersion(coinAddress, params.version);
27762
+ const poolKey = this.createPoolKey(coinAddress, version);
27768
27763
  // Check if we need to calculate values or use direct values
27769
27764
  if ("tickLower" in params) {
27770
27765
  // Use the directly provided values
@@ -27962,6 +27957,173 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
27962
27957
  // Determine amounts for each currency based on pool key ordering
27963
27958
  const amount0 = poolKey.currency0 === coinAddress ? coinAmount : flethAmount;
27964
27959
  const amount1 = poolKey.currency0 === coinAddress ? flethAmount : coinAmount;
27960
+ // Calculate and constrain liquidity using shared method
27961
+ const { finalLiquidity, finalAmount0, finalAmount1 } = this.calculateConstrainedLiquidity(currentTick, tickLower, tickUpper, amount0, amount1);
27962
+ // 6. Add liquidity
27963
+ calls.push(this.createLiquidityCall(poolKey, tickLower, tickUpper, finalLiquidity, finalAmount0, finalAmount1, userAddress));
27964
+ return calls;
27965
+ }
27966
+ /**
27967
+ * Gets the calls needed to import a memecoin to Flaunch and add liquidity to AnyPositionManager as a batch
27968
+ * @param params - Parameters for importing and adding liquidity
27969
+ * @returns Array of calls with descriptions
27970
+ */
27971
+ async getImportAndAddLiquidityCalls(params) {
27972
+ const importParams = await this.readWriteTokenImporter.getInitializeParams({
27973
+ coinAddress: params.coinAddress,
27974
+ creatorFeeAllocationPercent: params.creatorFeeAllocationPercent,
27975
+ initialMarketCapUSD: params.initialMarketCapUSD,
27976
+ verifier: params.verifier,
27977
+ });
27978
+ const addLiquidityCalls = await this.getAddLiquidityCalls({
27979
+ ...params,
27980
+ version: FlaunchVersion.ANY, // optimize to avoid fetching if not passed
27981
+ });
27982
+ return [
27983
+ {
27984
+ to: this.readWriteTokenImporter.contract.address,
27985
+ data: this.readWriteTokenImporter.contract.encodeFunctionData("initialize", importParams),
27986
+ description: "Import Memecoin to Flaunch",
27987
+ },
27988
+ ...addLiquidityCalls,
27989
+ ];
27990
+ }
27991
+ /**
27992
+ * Gets the calls needed to add single-sided liquidity in coin from current tick to infinity
27993
+ * @param params - Parameters for adding single-sided liquidity
27994
+ * @returns Array of calls with descriptions
27995
+ */
27996
+ async getSingleSidedCoinAddLiquidityCalls(params) {
27997
+ const { coinAddress, coinAmount } = params;
27998
+ const version = await this.determineCoinVersion(coinAddress, params.version);
27999
+ const poolKey = this.createPoolKey(coinAddress, version);
28000
+ // get the current tick from the pool
28001
+ const poolState = await this.readStateView.poolSlot0({
28002
+ poolId: getPoolId(poolKey),
28003
+ });
28004
+ const currentTick = poolState.tick;
28005
+ // We want to add liquidity from current price to infinity (as coin appreciates vs flETH)
28006
+ // This means providing single-sided coin liquidity that becomes active as coin price increases
28007
+ const isFLETHZero = this.flETHIsCurrencyZero(coinAddress);
28008
+ let tickLower;
28009
+ let tickUpper;
28010
+ if (isFLETHZero) {
28011
+ // flETH is currency0, coin is currency1
28012
+ // Price = coin/flETH. As coin appreciates, price and tick increase.
28013
+ // For single-sided coin position, we need the range to end at current tick
28014
+ // so as price increases beyond current, position becomes coin-only
28015
+ tickLower = TickFinder.MIN_TICK;
28016
+ tickUpper = getValidTick({
28017
+ tick: currentTick,
28018
+ tickSpacing: this.TICK_SPACING,
28019
+ roundDown: true,
28020
+ });
28021
+ }
28022
+ else {
28023
+ // coin is currency0, flETH is currency1
28024
+ // Price = flETH/coin. As coin appreciates, price decreases and tick decreases.
28025
+ // For single-sided coin position, we need the range to start at current tick
28026
+ // so as price decreases below current, position becomes coin-only
28027
+ tickLower = getValidTick({
28028
+ tick: currentTick,
28029
+ tickSpacing: this.TICK_SPACING,
28030
+ roundDown: false,
28031
+ });
28032
+ tickUpper = TickFinder.MAX_TICK;
28033
+ }
28034
+ // Fetch approvals via multicall
28035
+ const userAddress = await this.drift.getSignerAddress();
28036
+ const permit2Address = Permit2Address[this.chainId];
28037
+ const results = await this.drift.multicall({
28038
+ calls: [
28039
+ // coin -> permit2
28040
+ {
28041
+ address: coinAddress,
28042
+ abi: erc20Abi,
28043
+ fn: "allowance",
28044
+ args: {
28045
+ owner: userAddress,
28046
+ spender: permit2Address,
28047
+ },
28048
+ },
28049
+ // coin --permit2--> uni position manager
28050
+ {
28051
+ address: permit2Address,
28052
+ abi: Permit2Abi,
28053
+ fn: "allowance",
28054
+ args: {
28055
+ 0: userAddress,
28056
+ 1: coinAddress,
28057
+ 2: UniV4PositionManagerAddress[this.chainId],
28058
+ },
28059
+ },
28060
+ // coin symbol
28061
+ {
28062
+ address: coinAddress,
28063
+ abi: erc20Abi,
28064
+ fn: "symbol",
28065
+ },
28066
+ ],
28067
+ });
28068
+ const coinToPermit2 = results[0].value;
28069
+ const permit2ToUniPosManagerCoinAllowance = results[1].value;
28070
+ const coinSymbol = results[2].value;
28071
+ const needsCoinApproval = coinToPermit2 < coinAmount;
28072
+ const currentTime = Math.floor(Date.now() / 1000);
28073
+ const needsCoinPermit2Approval = permit2ToUniPosManagerCoinAllowance.amount < coinAmount ||
28074
+ permit2ToUniPosManagerCoinAllowance.expiration <= currentTime;
28075
+ const calls = [];
28076
+ // 1. Coin approval to Permit2
28077
+ if (needsCoinApproval) {
28078
+ calls.push({
28079
+ to: coinAddress,
28080
+ description: `Approve ${coinSymbol} for Permit2`,
28081
+ data: encodeFunctionData({
28082
+ abi: erc20Abi,
28083
+ functionName: "approve",
28084
+ args: [permit2Address, coinAmount],
28085
+ }),
28086
+ });
28087
+ }
28088
+ // 2. Permit2 approval for coin to uni position manager
28089
+ const expiration = Math.floor(Date.now() / 1000) + 3600; // 1 hour from now
28090
+ if (needsCoinPermit2Approval) {
28091
+ calls.push({
28092
+ to: permit2Address,
28093
+ description: `Permit2 approval for ${coinSymbol} to UniV4PositionManager`,
28094
+ data: encodeFunctionData({
28095
+ abi: Permit2Abi,
28096
+ functionName: "approve",
28097
+ args: [
28098
+ coinAddress,
28099
+ UniV4PositionManagerAddress[this.chainId],
28100
+ coinAmount,
28101
+ expiration,
28102
+ ],
28103
+ }),
28104
+ });
28105
+ }
28106
+ // === generate add liquidity call ===
28107
+ // Determine amounts for each currency based on pool key ordering
28108
+ const flethAmount = 0n;
28109
+ const amount0 = poolKey.currency0 === coinAddress ? coinAmount : flethAmount;
28110
+ const amount1 = poolKey.currency0 === coinAddress ? flethAmount : coinAmount;
28111
+ // Calculate and constrain liquidity using shared method
28112
+ const { finalLiquidity, finalAmount0, finalAmount1 } = this.calculateConstrainedLiquidity(currentTick, tickLower, tickUpper, amount0, amount1);
28113
+ // 3. Add liquidity
28114
+ calls.push(this.createLiquidityCall(poolKey, tickLower, tickUpper, finalLiquidity, finalAmount0, finalAmount1, userAddress));
28115
+ return calls;
28116
+ }
28117
+ /**
28118
+ * Calculates and constrains liquidity amounts for a position
28119
+ * @param currentTick - Current pool tick
28120
+ * @param tickLower - Lower tick of the position
28121
+ * @param tickUpper - Upper tick of the position
28122
+ * @param amount0 - Amount of currency0
28123
+ * @param amount1 - Amount of currency1
28124
+ * @returns Final liquidity and amounts
28125
+ */
28126
+ calculateConstrainedLiquidity(currentTick, tickLower, tickUpper, amount0, amount1) {
27965
28127
  // Calculate liquidity first using user's input amounts
27966
28128
  const initialLiquidity = getLiquidityFromAmounts({
27967
28129
  currentTick,
@@ -28027,7 +28189,25 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
28027
28189
  }
28028
28190
  finalAmount0 = amount0; // Use user's full amount as maximum
28029
28191
  finalAmount1 = amount1; // Use user's full amount as maximum
28030
- // Prepare mint position parameters (following swiss-knife pattern)
28192
+ return {
28193
+ finalLiquidity,
28194
+ finalAmount0,
28195
+ finalAmount1,
28196
+ };
28197
+ }
28198
+ /**
28199
+ * Creates the UniV4 Position Manager liquidity call
28200
+ * @param poolKey - The pool key
28201
+ * @param tickLower - Lower tick of the position
28202
+ * @param tickUpper - Upper tick of the position
28203
+ * @param finalLiquidity - Final liquidity amount
28204
+ * @param finalAmount0 - Final amount of currency0
28205
+ * @param finalAmount1 - Final amount of currency1
28206
+ * @param userAddress - User's address
28207
+ * @returns CallWithDescription for adding liquidity
28208
+ */
28209
+ createLiquidityCall(poolKey, tickLower, tickUpper, finalLiquidity, finalAmount0, finalAmount1, userAddress) {
28210
+ // Prepare mint position parameters
28031
28211
  const V4PMActions = {
28032
28212
  MINT_POSITION: "02",
28033
28213
  SETTLE_PAIR: "0d",
@@ -28081,8 +28261,7 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
28081
28261
  currency1: poolKey.currency1,
28082
28262
  },
28083
28263
  ]);
28084
- // 6. Add liquidity
28085
- calls.push({
28264
+ return {
28086
28265
  to: UniV4PositionManagerAddress[this.chainId],
28087
28266
  data: encodeFunctionData({
28088
28267
  abi: [
@@ -28108,33 +28287,7 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
28108
28287
  }),
28109
28288
  value: 0n,
28110
28289
  description: "Add Liquidity",
28111
- });
28112
- return calls;
28113
- }
28114
- /**
28115
- * Gets the calls needed to import a memecoin to Flaunch and add liquidity to AnyPositionManager as a batch
28116
- * @param params - Parameters for importing and adding liquidity
28117
- * @returns Array of calls with descriptions
28118
- */
28119
- async getImportAndAddLiquidityCalls(params) {
28120
- const importParams = await this.readWriteTokenImporter.getInitializeParams({
28121
- coinAddress: params.coinAddress,
28122
- creatorFeeAllocationPercent: params.creatorFeeAllocationPercent,
28123
- initialMarketCapUSD: params.initialMarketCapUSD,
28124
- verifier: params.verifier,
28125
- });
28126
- const addLiquidityCalls = await this.getAddLiquidityCalls({
28127
- ...params,
28128
- version: FlaunchVersion.ANY, // optimize to avoid fetching if not passed
28129
- });
28130
- return [
28131
- {
28132
- to: this.readWriteTokenImporter.contract.address,
28133
- data: this.readWriteTokenImporter.contract.encodeFunctionData("initialize", importParams),
28134
- description: "Import Memecoin to Flaunch",
28135
- },
28136
- ...addLiquidityCalls,
28137
- ];
28290
+ };
28138
28291
  }
28139
28292
  }
28140
28293