@flaunch/sdk 0.9.6 → 0.9.8-beta.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.
package/dist/index.cjs.js CHANGED
@@ -5786,6 +5786,10 @@ const ZoraVerifierAddress = {
5786
5786
  [base.id]: "0x656047fd43d2c3a121f2ef859d7171d7dd59f8b9",
5787
5787
  [baseSepolia.id]: "0x05a5763e9199b88bb591c6b112d0424b2cd7a99e",
5788
5788
  };
5789
+ const SolanaVerifierAddress = {
5790
+ [base.id]: "0x0000000000000000000000000000000000000000",
5791
+ [baseSepolia.id]: "0x47226918E518f205584bd75Bf81E0b532B0B3Ea7",
5792
+ };
5789
5793
  /** ======== */
5790
5794
  /** Permissions */
5791
5795
  const ClosedPermissionsAddress = {
@@ -11893,6 +11897,7 @@ exports.Verifier = void 0;
11893
11897
  Verifier["VIRTUALS"] = "virtuals";
11894
11898
  Verifier["WHITELIST"] = "whitelist";
11895
11899
  Verifier["ZORA"] = "zora";
11900
+ Verifier["SOLANA"] = "solana";
11896
11901
  })(exports.Verifier || (exports.Verifier = {}));
11897
11902
  exports.LiquidityMode = void 0;
11898
11903
  (function (LiquidityMode) {
@@ -20363,6 +20368,8 @@ class ReadTokenImporter {
20363
20368
  return WhitelistVerifierAddress[this.chainId];
20364
20369
  case exports.Verifier.ZORA:
20365
20370
  return ZoraVerifierAddress[this.chainId];
20371
+ case exports.Verifier.SOLANA:
20372
+ return SolanaVerifierAddress[this.chainId];
20366
20373
  default:
20367
20374
  throw new Error(`Unknown verifier: ${verifier}`);
20368
20375
  }
@@ -25467,6 +25474,10 @@ function maxLiquidityForAmount0Precise(sqrtRatioAX96, sqrtRatioBX96, amount0) {
25467
25474
  if (sqrtRatioAX96 > sqrtRatioBX96) {
25468
25475
  [sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
25469
25476
  }
25477
+ // Handle edge case where sqrt ratios are equal (division by zero)
25478
+ if (sqrtRatioAX96 === sqrtRatioBX96) {
25479
+ return 0n;
25480
+ }
25470
25481
  const Q96 = 2n ** 96n;
25471
25482
  const numerator = amount0 * sqrtRatioAX96 * sqrtRatioBX96;
25472
25483
  const denominator = Q96 * (sqrtRatioBX96 - sqrtRatioAX96);
@@ -25483,6 +25494,10 @@ function maxLiquidityForAmount1(sqrtRatioAX96, sqrtRatioBX96, amount1) {
25483
25494
  if (sqrtRatioAX96 > sqrtRatioBX96) {
25484
25495
  [sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
25485
25496
  }
25497
+ // Handle edge case where sqrt ratios are equal (division by zero)
25498
+ if (sqrtRatioAX96 === sqrtRatioBX96) {
25499
+ return 0n;
25500
+ }
25486
25501
  const Q96 = 2n ** 96n;
25487
25502
  return (amount1 * Q96) / (sqrtRatioBX96 - sqrtRatioAX96);
25488
25503
  }
@@ -26394,7 +26409,7 @@ class ReadFlaunchSDK {
26394
26409
  * @returns Promise<number> - The current tick of the pool
26395
26410
  */
26396
26411
  async currentTick(coinAddress, version) {
26397
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26412
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26398
26413
  const poolId = await this.poolId(coinAddress, coinVersion);
26399
26414
  const poolState = await this.readStateView.poolSlot0({ poolId });
26400
26415
  return poolState.tick;
@@ -26406,7 +26421,7 @@ class ReadFlaunchSDK {
26406
26421
  * @returns Promise<string> - The price of the coin in ETH with 18 decimals precision
26407
26422
  */
26408
26423
  async coinPriceInETH(coinAddress, version) {
26409
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26424
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26410
26425
  const isFLETHZero = this.flETHIsCurrencyZero(coinAddress);
26411
26426
  const currentTick = await this.currentTick(coinAddress, coinVersion);
26412
26427
  const price = Math.pow(1.0001, currentTick);
@@ -26426,7 +26441,7 @@ class ReadFlaunchSDK {
26426
26441
  * @returns Promise<string> - The price of the coin in USD with 18 decimal precision
26427
26442
  */
26428
26443
  async coinPriceInUSD({ coinAddress, version, drift, }) {
26429
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26444
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26430
26445
  const ethPerCoin = await this.coinPriceInETH(coinAddress, coinVersion);
26431
26446
  const ethPrice = await this.getETHUSDCPrice(drift);
26432
26447
  return (parseFloat(ethPerCoin) * ethPrice).toFixed(18);
@@ -26474,7 +26489,7 @@ class ReadFlaunchSDK {
26474
26489
  * @returns Fair launch information from the appropriate contract version
26475
26490
  */
26476
26491
  async fairLaunchInfo(coinAddress, version) {
26477
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26492
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26478
26493
  const poolId = await this.poolId(coinAddress, coinVersion);
26479
26494
  return this.getFairLaunch(coinVersion).fairLaunchInfo({ poolId });
26480
26495
  }
@@ -26485,12 +26500,12 @@ class ReadFlaunchSDK {
26485
26500
  * @returns Promise<boolean> - True if fair launch is active, false otherwise
26486
26501
  */
26487
26502
  async isFairLaunchActive(coinAddress, version) {
26488
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26503
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26489
26504
  const poolId = await this.poolId(coinAddress, coinVersion);
26490
26505
  return this.getFairLaunch(coinVersion).isFairLaunchActive({ poolId });
26491
26506
  }
26492
26507
  async trustedPoolKeySignerStatus(coinAddress, version) {
26493
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26508
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26494
26509
  if (coinVersion === exports.FlaunchVersion.ANY) {
26495
26510
  throw new Error("AnyPositionManager is not supported for TrustedSigner");
26496
26511
  }
@@ -26539,7 +26554,7 @@ class ReadFlaunchSDK {
26539
26554
  * @returns Promise<number> - The duration in seconds (30 minutes for V1, variable for V1.1)
26540
26555
  */
26541
26556
  async fairLaunchDuration(coinAddress, version) {
26542
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26557
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26543
26558
  const poolId = await this.poolId(coinAddress, coinVersion);
26544
26559
  return this.getFairLaunch(coinVersion).fairLaunchDuration({ poolId });
26545
26560
  }
@@ -26550,7 +26565,7 @@ class ReadFlaunchSDK {
26550
26565
  * @returns Promise<number> - The initial tick value
26551
26566
  */
26552
26567
  async initialTick(coinAddress, version) {
26553
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26568
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26554
26569
  const poolId = await this.poolId(coinAddress, coinVersion);
26555
26570
  const fairLaunchInfo = await this.getFairLaunch(coinVersion).fairLaunchInfo({ poolId });
26556
26571
  return fairLaunchInfo.initialTick;
@@ -26562,7 +26577,7 @@ class ReadFlaunchSDK {
26562
26577
  * @returns Promise<{flETHAmount: bigint, coinAmount: bigint, tickLower: number, tickUpper: number}> - Position details
26563
26578
  */
26564
26579
  async fairLaunchETHOnlyPosition(coinAddress, version) {
26565
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26580
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26566
26581
  const poolId = await this.poolId(coinAddress, coinVersion);
26567
26582
  const initialTick = await this.initialTick(coinAddress, coinVersion);
26568
26583
  const currentTick = await this.currentTick(coinAddress, coinVersion);
@@ -26610,7 +26625,7 @@ class ReadFlaunchSDK {
26610
26625
  * @returns Promise<{flETHAmount: bigint, coinAmount: bigint, tickLower: number, tickUpper: number}> - Position details
26611
26626
  */
26612
26627
  async fairLaunchCoinOnlyPosition(coinAddress, version) {
26613
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26628
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26614
26629
  const poolId = await this.poolId(coinAddress, coinVersion);
26615
26630
  const initialTick = await this.initialTick(coinAddress, coinVersion);
26616
26631
  const currentTick = await this.currentTick(coinAddress, coinVersion);
@@ -26658,7 +26673,7 @@ class ReadFlaunchSDK {
26658
26673
  * @returns Promise<{flETHAmount: bigint, coinAmount: bigint, pendingEth: bigint, tickLower: number, tickUpper: number}> - Bid wall position details
26659
26674
  */
26660
26675
  async bidWallPosition(coinAddress, version) {
26661
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26676
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26662
26677
  const poolId = await this.poolId(coinAddress, coinVersion);
26663
26678
  const isFLETHZero = this.flETHIsCurrencyZero(coinAddress);
26664
26679
  const { amount0_: amount0, amount1_: amount1, pendingEth_: pendingEth, } = await this.getBidWall(coinVersion).position({ poolId });
@@ -26774,13 +26789,8 @@ class ReadFlaunchSDK {
26774
26789
  */
26775
26790
  async poolId(coinAddress, version) {
26776
26791
  let hookAddress;
26777
- if (version) {
26778
- hookAddress = this.getPositionManagerAddress(version);
26779
- }
26780
- else {
26781
- const coinVersion = await this.getCoinVersion(coinAddress);
26782
- hookAddress = this.getPositionManagerAddress(coinVersion);
26783
- }
26792
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26793
+ hookAddress = this.getPositionManagerAddress(coinVersion);
26784
26794
  return getPoolId(orderPoolKey({
26785
26795
  currency0: FLETHAddress[this.chainId],
26786
26796
  currency1: coinAddress,
@@ -26838,7 +26848,7 @@ class ReadFlaunchSDK {
26838
26848
  * @returns Promise<bigint> - The expected amount of ETH to receive
26839
26849
  */
26840
26850
  async getSellQuoteExactInput({ coinAddress, version, amountIn, intermediatePoolKey, }) {
26841
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26851
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26842
26852
  return this.readQuoter.getSellQuoteExactInput({
26843
26853
  coinAddress,
26844
26854
  amountIn,
@@ -26857,7 +26867,7 @@ class ReadFlaunchSDK {
26857
26867
  * @returns Promise<bigint> - The expected amount of coins to receive
26858
26868
  */
26859
26869
  async getBuyQuoteExactInput({ coinAddress, version, amountIn, intermediatePoolKey, hookData, userWallet, }) {
26860
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26870
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26861
26871
  return this.readQuoter.getBuyQuoteExactInput({
26862
26872
  coinAddress,
26863
26873
  amountIn,
@@ -26878,7 +26888,7 @@ class ReadFlaunchSDK {
26878
26888
  * @returns Promise<bigint> - The required amount of ETH or inputToken to spend
26879
26889
  */
26880
26890
  async getBuyQuoteExactOutput({ coinAddress, amountOut, version, intermediatePoolKey, hookData, userWallet, }) {
26881
- const coinVersion = version || (await this.getCoinVersion(coinAddress));
26891
+ const coinVersion = await this.determineCoinVersion(coinAddress, version);
26882
26892
  return this.readQuoter.getBuyQuoteExactOutput({
26883
26893
  coinAddress,
26884
26894
  coinOut: amountOut,
@@ -26936,7 +26946,8 @@ class ReadFlaunchSDK {
26936
26946
  memecoin.totalSupply(),
26937
26947
  memecoin.decimals(),
26938
26948
  ]);
26939
- return { totalSupply, decimals };
26949
+ const formattedTotalSupplyInDecimals = parseFloat(viem.formatUnits(totalSupply, decimals));
26950
+ return { totalSupply, decimals, formattedTotalSupplyInDecimals };
26940
26951
  }
26941
26952
  /**
26942
26953
  * Gets market context information needed for tick calculations
@@ -26958,8 +26969,8 @@ class ReadFlaunchSDK {
26958
26969
  /**
26959
26970
  * Converts market cap in USD to token price in ETH
26960
26971
  */
26961
- marketCapToTokenPriceEth(marketCapUsd, totalSupplyDecimal, ethUsdPrice) {
26962
- const tokenPriceUsd = marketCapUsd / totalSupplyDecimal;
26972
+ marketCapToTokenPriceEth(marketCapUsd, formattedTotalSupplyInDecimals, ethUsdPrice) {
26973
+ const tokenPriceUsd = marketCapUsd / formattedTotalSupplyInDecimals;
26963
26974
  return tokenPriceUsd / ethUsdPrice;
26964
26975
  }
26965
26976
  /**
@@ -26977,24 +26988,22 @@ class ReadFlaunchSDK {
26977
26988
  /**
26978
26989
  * Calculates current tick from market cap if provided
26979
26990
  */
26980
- calculateCurrentTickFromMarketCap(currentMarketCap, totalSupplyDecimal, marketContext) {
26991
+ calculateCurrentTickFromMarketCap(currentMarketCap, formattedTotalSupplyInDecimals, marketContext) {
26981
26992
  if (!currentMarketCap) {
26982
26993
  return undefined;
26983
26994
  }
26984
26995
  const currentMarketCapNum = parseFloat(currentMarketCap);
26985
- const currentTokenPriceEth = this.marketCapToTokenPriceEth(currentMarketCapNum, totalSupplyDecimal, marketContext.ethUsdPrice);
26996
+ const currentTokenPriceEth = this.marketCapToTokenPriceEth(currentMarketCapNum, formattedTotalSupplyInDecimals, marketContext.ethUsdPrice);
26986
26997
  return this.convertPriceToTick(currentTokenPriceEth, marketContext.isFlethZero, marketContext.decimals0, marketContext.decimals1);
26987
26998
  }
26988
26999
  async calculateAddLiquidityTicks({ coinAddress, liquidityMode, minMarketCap, maxMarketCap, currentMarketCap, }) {
26989
27000
  // Get coin information
26990
- const { totalSupply: coinTotalSupply, decimals: coinDecimals } = await this.getCoinInfo(coinAddress);
26991
- // Convert total supply to decimal format
26992
- const totalSupplyDecimal = parseFloat(viem.formatEther(coinTotalSupply));
27001
+ const { totalSupply: coinTotalSupply, decimals: coinDecimals, formattedTotalSupplyInDecimals, } = await this.getCoinInfo(coinAddress);
26993
27002
  if (liquidityMode === exports.LiquidityMode.FULL_RANGE) {
26994
27003
  let currentTick;
26995
27004
  if (currentMarketCap) {
26996
27005
  const marketContext = await this.getMarketContext(coinAddress, coinDecimals);
26997
- currentTick = this.calculateCurrentTickFromMarketCap(currentMarketCap, totalSupplyDecimal, marketContext);
27006
+ currentTick = this.calculateCurrentTickFromMarketCap(currentMarketCap, formattedTotalSupplyInDecimals, marketContext);
26998
27007
  }
26999
27008
  return {
27000
27009
  tickLower: getNearestUsableTick({
@@ -27021,13 +27030,13 @@ class ReadFlaunchSDK {
27021
27030
  throw new Error("[ReadFlaunchSDK.addLiquidityCalculateTicks]: Invalid market cap range");
27022
27031
  }
27023
27032
  // Convert market caps to token prices in ETH
27024
- const minTokenPriceEth = this.marketCapToTokenPriceEth(minMarketCapNum, totalSupplyDecimal, marketContext.ethUsdPrice);
27025
- const maxTokenPriceEth = this.marketCapToTokenPriceEth(maxMarketCapNum, totalSupplyDecimal, marketContext.ethUsdPrice);
27033
+ const minTokenPriceEth = this.marketCapToTokenPriceEth(minMarketCapNum, formattedTotalSupplyInDecimals, marketContext.ethUsdPrice);
27034
+ const maxTokenPriceEth = this.marketCapToTokenPriceEth(maxMarketCapNum, formattedTotalSupplyInDecimals, marketContext.ethUsdPrice);
27026
27035
  // Convert to ticks
27027
27036
  const minTick = this.convertPriceToTick(minTokenPriceEth, marketContext.isFlethZero, marketContext.decimals0, marketContext.decimals1);
27028
27037
  const maxTick = this.convertPriceToTick(maxTokenPriceEth, marketContext.isFlethZero, marketContext.decimals0, marketContext.decimals1);
27029
27038
  // Calculate current tick if provided
27030
- const currentTick = this.calculateCurrentTickFromMarketCap(currentMarketCap, totalSupplyDecimal, marketContext);
27039
+ const currentTick = this.calculateCurrentTickFromMarketCap(currentMarketCap, formattedTotalSupplyInDecimals, marketContext);
27031
27040
  return {
27032
27041
  tickLower: Math.min(minTick, maxTick),
27033
27042
  tickUpper: Math.max(minTick, maxTick),
@@ -27048,12 +27057,11 @@ class ReadFlaunchSDK {
27048
27057
  currentMarketCap = params.currentMarketCap;
27049
27058
  }
27050
27059
  else {
27051
- const { totalSupply, decimals } = await this.getCoinInfo(coinAddress);
27052
- const formattedTotalSupply = parseFloat(viem.formatUnits(totalSupply, decimals));
27053
- minMarketCap = (parseFloat(params.minPriceUSD) * formattedTotalSupply).toString();
27054
- maxMarketCap = (parseFloat(params.maxPriceUSD) * formattedTotalSupply).toString();
27060
+ const { formattedTotalSupplyInDecimals } = await this.getCoinInfo(coinAddress);
27061
+ minMarketCap = (parseFloat(params.minPriceUSD) * formattedTotalSupplyInDecimals).toString();
27062
+ maxMarketCap = (parseFloat(params.maxPriceUSD) * formattedTotalSupplyInDecimals).toString();
27055
27063
  if (params.currentPriceUSD) {
27056
- currentMarketCap = (params.currentPriceUSD * formattedTotalSupply).toString();
27064
+ currentMarketCap = (params.currentPriceUSD * formattedTotalSupplyInDecimals).toString();
27057
27065
  }
27058
27066
  }
27059
27067
  let { tickLower, tickUpper, currentTick } = await this.calculateAddLiquidityTicks({
@@ -27065,24 +27073,9 @@ class ReadFlaunchSDK {
27065
27073
  });
27066
27074
  // If no current tick is provided from the above calculation, get it from the pool state
27067
27075
  if (!currentTick) {
27068
- let version = params.version;
27069
- // if version is not provided, check on existing managers, else default to ANY
27070
- if (!version) {
27071
- try {
27072
- version = await this.getCoinVersion(coinAddress);
27073
- }
27074
- catch {
27075
- version = exports.FlaunchVersion.ANY;
27076
- }
27077
- }
27076
+ const version = await this.determineCoinVersion(coinAddress, params.version);
27078
27077
  const poolState = await this.readStateView.poolSlot0({
27079
- poolId: getPoolId(orderPoolKey({
27080
- currency0: coinAddress,
27081
- currency1: FLETHAddress[this.chainId],
27082
- fee: 0,
27083
- tickSpacing: TICK_SPACING,
27084
- hooks: this.getPositionManagerAddress(version),
27085
- })),
27078
+ poolId: getPoolId(this.createPoolKey(coinAddress, version)),
27086
27079
  });
27087
27080
  currentTick = poolState.tick;
27088
27081
  }
@@ -27139,12 +27132,11 @@ class ReadFlaunchSDK {
27139
27132
  currentMarketCap = params.currentMarketCap;
27140
27133
  }
27141
27134
  else {
27142
- const { totalSupply, decimals } = await this.getCoinInfo(coinAddress);
27143
- const formattedTotalSupply = parseFloat(viem.formatUnits(totalSupply, decimals));
27144
- minMarketCap = (parseFloat(params.minPriceUSD) * formattedTotalSupply).toString();
27145
- maxMarketCap = (parseFloat(params.maxPriceUSD) * formattedTotalSupply).toString();
27135
+ const { formattedTotalSupplyInDecimals } = await this.getCoinInfo(coinAddress);
27136
+ minMarketCap = (parseFloat(params.minPriceUSD) * formattedTotalSupplyInDecimals).toString();
27137
+ maxMarketCap = (parseFloat(params.maxPriceUSD) * formattedTotalSupplyInDecimals).toString();
27146
27138
  if (params.currentPriceUSD) {
27147
- currentMarketCap = (params.currentPriceUSD * formattedTotalSupply).toString();
27139
+ currentMarketCap = (params.currentPriceUSD * formattedTotalSupplyInDecimals).toString();
27148
27140
  }
27149
27141
  }
27150
27142
  let { tickLower, tickUpper, currentTick } = await this.calculateAddLiquidityTicks({
@@ -27156,24 +27148,9 @@ class ReadFlaunchSDK {
27156
27148
  });
27157
27149
  // get the current pool state for the coin
27158
27150
  if (!currentTick) {
27159
- let version = params.version;
27160
- // if version is not provided, check on existing managers, else default to ANY
27161
- if (!version) {
27162
- try {
27163
- version = await this.getCoinVersion(coinAddress);
27164
- }
27165
- catch {
27166
- version = exports.FlaunchVersion.ANY;
27167
- }
27168
- }
27151
+ const version = await this.determineCoinVersion(coinAddress, params.version);
27169
27152
  const poolState = await this.readStateView.poolSlot0({
27170
- poolId: getPoolId(orderPoolKey({
27171
- currency0: coinAddress,
27172
- currency1: FLETHAddress[this.chainId],
27173
- fee: 0,
27174
- tickSpacing: TICK_SPACING,
27175
- hooks: this.getPositionManagerAddress(version),
27176
- })),
27153
+ poolId: getPoolId(this.createPoolKey(coinAddress, version)),
27177
27154
  });
27178
27155
  currentTick = poolState.tick;
27179
27156
  }
@@ -27328,6 +27305,39 @@ class ReadFlaunchSDK {
27328
27305
  args: { owner, operator },
27329
27306
  });
27330
27307
  }
27308
+ /**
27309
+ * Determines the version for a coin, using provided version or fetching it
27310
+ * @param coinAddress - The coin address
27311
+ * @param version - Optional version, if not provided will be fetched
27312
+ * @returns The determined version
27313
+ */
27314
+ async determineCoinVersion(coinAddress, version) {
27315
+ if (!version) {
27316
+ try {
27317
+ version = await this.getCoinVersion(coinAddress);
27318
+ }
27319
+ catch {
27320
+ version = exports.FlaunchVersion.ANY;
27321
+ }
27322
+ }
27323
+ return version;
27324
+ }
27325
+ /**
27326
+ * Creates a pool key for the given coin and version
27327
+ * @param coinAddress - The coin address
27328
+ * @param version - The version to use for position manager
27329
+ * @returns The ordered pool key
27330
+ */
27331
+ createPoolKey(coinAddress, version) {
27332
+ const flethAddress = FLETHAddress[this.chainId];
27333
+ return orderPoolKey({
27334
+ currency0: coinAddress,
27335
+ currency1: flethAddress,
27336
+ fee: 0,
27337
+ tickSpacing: this.TICK_SPACING,
27338
+ hooks: this.getPositionManagerAddress(version),
27339
+ });
27340
+ }
27331
27341
  }
27332
27342
  class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
27333
27343
  constructor(chainId, drift$1 = drift.createDrift(), publicClient) {
@@ -27452,7 +27462,7 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
27452
27462
  * @returns Transaction response for the buy operation
27453
27463
  */
27454
27464
  async buyCoin(params, version) {
27455
- const coinVersion = version || (await this.getCoinVersion(params.coinAddress));
27465
+ const coinVersion = await this.determineCoinVersion(params.coinAddress, version);
27456
27466
  const sender = await this.drift.getSignerAddress();
27457
27467
  let amountIn;
27458
27468
  let amountOutMin;
@@ -27537,7 +27547,7 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
27537
27547
  * @returns Transaction response for the sell operation
27538
27548
  */
27539
27549
  async sellCoin(params, version) {
27540
- const coinVersion = version || (await this.getCoinVersion(params.coinAddress));
27550
+ const coinVersion = await this.determineCoinVersion(params.coinAddress, version);
27541
27551
  let amountOutMin;
27542
27552
  await this.readQuoter.contract.cache.clear();
27543
27553
  if (params.amountOutMin === undefined) {
@@ -27760,22 +27770,8 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
27760
27770
  let tickLower;
27761
27771
  let tickUpper;
27762
27772
  let currentTick;
27763
- let version = params.version;
27764
- if (!version) {
27765
- try {
27766
- version = await this.getCoinVersion(coinAddress);
27767
- }
27768
- catch {
27769
- version = exports.FlaunchVersion.ANY;
27770
- }
27771
- }
27772
- const poolKey = orderPoolKey({
27773
- currency0: coinAddress,
27774
- currency1: flethAddress,
27775
- fee: 0,
27776
- tickSpacing: this.TICK_SPACING,
27777
- hooks: this.getPositionManagerAddress(version),
27778
- });
27773
+ const version = await this.determineCoinVersion(coinAddress, params.version);
27774
+ const poolKey = this.createPoolKey(coinAddress, version);
27779
27775
  // Check if we need to calculate values or use direct values
27780
27776
  if ("tickLower" in params) {
27781
27777
  // Use the directly provided values
@@ -27804,12 +27800,12 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
27804
27800
  initialMarketCapUSD = params.initialMarketCapUSD;
27805
27801
  }
27806
27802
  else {
27807
- const { totalSupply, decimals } = await this.getCoinInfo(coinAddress);
27808
- const formattedTotalSupply = parseFloat(viem.formatUnits(totalSupply, decimals));
27809
- minMarketCap = (parseFloat(params.minPriceUSD) * formattedTotalSupply).toString();
27810
- maxMarketCap = (parseFloat(params.maxPriceUSD) * formattedTotalSupply).toString();
27803
+ const { formattedTotalSupplyInDecimals } = await this.getCoinInfo(coinAddress);
27804
+ minMarketCap = (parseFloat(params.minPriceUSD) * formattedTotalSupplyInDecimals).toString();
27805
+ maxMarketCap = (parseFloat(params.maxPriceUSD) * formattedTotalSupplyInDecimals).toString();
27811
27806
  if (params.initialPriceUSD) {
27812
- initialMarketCapUSD = params.initialPriceUSD * formattedTotalSupply;
27807
+ initialMarketCapUSD =
27808
+ params.initialPriceUSD * formattedTotalSupplyInDecimals;
27813
27809
  }
27814
27810
  }
27815
27811
  const calculated = await this.calculateAddLiquidityAmounts({
@@ -27973,6 +27969,246 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
27973
27969
  // Determine amounts for each currency based on pool key ordering
27974
27970
  const amount0 = poolKey.currency0 === coinAddress ? coinAmount : flethAmount;
27975
27971
  const amount1 = poolKey.currency0 === coinAddress ? flethAmount : coinAmount;
27972
+ // Calculate and constrain liquidity using shared method
27973
+ const { finalLiquidity, finalAmount0, finalAmount1 } = this.calculateConstrainedLiquidity(currentTick, tickLower, tickUpper, amount0, amount1);
27974
+ // 6. Add liquidity
27975
+ calls.push(this.createLiquidityCall(poolKey, tickLower, tickUpper, finalLiquidity, finalAmount0, finalAmount1, userAddress));
27976
+ return calls;
27977
+ }
27978
+ // Implementation with union type for internal use
27979
+ async getImportAndAddLiquidityCalls(params) {
27980
+ let importParams;
27981
+ if ("initialMarketCapUSD" in params) {
27982
+ const paramsWithMarketCap = params;
27983
+ importParams = await this.readWriteTokenImporter.getInitializeParams({
27984
+ coinAddress: paramsWithMarketCap.coinAddress,
27985
+ creatorFeeAllocationPercent: paramsWithMarketCap.creatorFeeAllocationPercent,
27986
+ initialMarketCapUSD: paramsWithMarketCap.initialMarketCapUSD,
27987
+ verifier: paramsWithMarketCap.verifier,
27988
+ });
27989
+ }
27990
+ else {
27991
+ const paramsWithPrice = params;
27992
+ importParams = await this.readWriteTokenImporter.getInitializeParams({
27993
+ coinAddress: paramsWithPrice.coinAddress,
27994
+ creatorFeeAllocationPercent: paramsWithPrice.creatorFeeAllocationPercent,
27995
+ initialPriceUSD: paramsWithPrice.initialPriceUSD,
27996
+ verifier: paramsWithPrice.verifier,
27997
+ });
27998
+ }
27999
+ const addLiquidityCalls = await this.getAddLiquidityCalls({
28000
+ ...params,
28001
+ version: exports.FlaunchVersion.ANY, // optimize to avoid fetching if not passed
28002
+ });
28003
+ return [
28004
+ {
28005
+ to: this.readWriteTokenImporter.contract.address,
28006
+ data: this.readWriteTokenImporter.contract.encodeFunctionData("initialize", importParams),
28007
+ description: "Import Memecoin to Flaunch",
28008
+ },
28009
+ ...addLiquidityCalls,
28010
+ ];
28011
+ }
28012
+ /**
28013
+ * Gets the calls needed to add single-sided liquidity in coin from current tick to infinity
28014
+ * @param params - Parameters for adding single-sided liquidity
28015
+ * @returns Array of calls with descriptions
28016
+ */
28017
+ async getSingleSidedCoinAddLiquidityCalls(params) {
28018
+ const { coinAddress, coinAmount } = params;
28019
+ const version = await this.determineCoinVersion(coinAddress, params.version);
28020
+ const poolKey = this.createPoolKey(coinAddress, version);
28021
+ let currentTick;
28022
+ // if initial marketcap or price is provided, it means that the pool is not initialized yet
28023
+ // so determining the currentTick
28024
+ if (("initialMarketCapUSD" in params && params.initialMarketCapUSD) ||
28025
+ ("initialPriceUSD" in params && params.initialPriceUSD)) {
28026
+ const { decimals: coinDecimals, formattedTotalSupplyInDecimals } = await this.getCoinInfo(coinAddress);
28027
+ // Determine market cap based on provided parameter
28028
+ let initialMarketCapUSD;
28029
+ if ("initialMarketCapUSD" in params && params.initialMarketCapUSD) {
28030
+ initialMarketCapUSD = params.initialMarketCapUSD;
28031
+ }
28032
+ else if ("initialPriceUSD" in params && params.initialPriceUSD) {
28033
+ initialMarketCapUSD =
28034
+ params.initialPriceUSD * formattedTotalSupplyInDecimals;
28035
+ }
28036
+ else {
28037
+ throw new Error("Either initialMarketCapUSD or initialPriceUSD must be provided");
28038
+ }
28039
+ const marketContext = await this.getMarketContext(coinAddress, coinDecimals);
28040
+ const calculatedTick = this.calculateCurrentTickFromMarketCap(initialMarketCapUSD.toString(), formattedTotalSupplyInDecimals, marketContext);
28041
+ if (calculatedTick === undefined) {
28042
+ throw new Error("Failed to calculate current tick from market cap");
28043
+ }
28044
+ currentTick = calculatedTick;
28045
+ }
28046
+ else {
28047
+ // the pool is already initialized, get the current tick from the pool
28048
+ const poolState = await this.readStateView.poolSlot0({
28049
+ poolId: getPoolId(poolKey),
28050
+ });
28051
+ currentTick = poolState.tick;
28052
+ }
28053
+ // We want to add liquidity from current price to infinity (as coin appreciates vs flETH)
28054
+ // This means providing single-sided coin liquidity that becomes active as coin price increases
28055
+ const isFLETHZero = this.flETHIsCurrencyZero(coinAddress);
28056
+ let tickLower;
28057
+ let tickUpper;
28058
+ if (isFLETHZero) {
28059
+ // flETH is currency0, coin is currency1
28060
+ // Price = coin/flETH. As coin appreciates, price and tick increase.
28061
+ // For single-sided coin position, we need the range to end at current tick
28062
+ // so as price increases beyond current, position becomes coin-only
28063
+ tickLower = TickFinder.MIN_TICK;
28064
+ tickUpper = getValidTick({
28065
+ tick: currentTick,
28066
+ tickSpacing: this.TICK_SPACING,
28067
+ roundDown: true,
28068
+ });
28069
+ }
28070
+ else {
28071
+ // coin is currency0, flETH is currency1
28072
+ // Price = flETH/coin. As coin appreciates, price decreases and tick decreases.
28073
+ // For single-sided coin position, we need the range to start at current tick
28074
+ // so as price decreases below current, position becomes coin-only
28075
+ tickLower = getValidTick({
28076
+ tick: currentTick,
28077
+ tickSpacing: this.TICK_SPACING,
28078
+ roundDown: false,
28079
+ });
28080
+ tickUpper = TickFinder.MAX_TICK;
28081
+ }
28082
+ // Fetch approvals via multicall
28083
+ const userAddress = await this.drift.getSignerAddress();
28084
+ const permit2Address = Permit2Address[this.chainId];
28085
+ const results = await this.drift.multicall({
28086
+ calls: [
28087
+ // coin -> permit2
28088
+ {
28089
+ address: coinAddress,
28090
+ abi: viem.erc20Abi,
28091
+ fn: "allowance",
28092
+ args: {
28093
+ owner: userAddress,
28094
+ spender: permit2Address,
28095
+ },
28096
+ },
28097
+ // coin --permit2--> uni position manager
28098
+ {
28099
+ address: permit2Address,
28100
+ abi: Permit2Abi,
28101
+ fn: "allowance",
28102
+ args: {
28103
+ 0: userAddress,
28104
+ 1: coinAddress,
28105
+ 2: UniV4PositionManagerAddress[this.chainId],
28106
+ },
28107
+ },
28108
+ // coin symbol
28109
+ {
28110
+ address: coinAddress,
28111
+ abi: viem.erc20Abi,
28112
+ fn: "symbol",
28113
+ },
28114
+ ],
28115
+ });
28116
+ const coinToPermit2 = results[0].value;
28117
+ const permit2ToUniPosManagerCoinAllowance = results[1].value;
28118
+ const coinSymbol = results[2].value;
28119
+ const needsCoinApproval = coinToPermit2 < coinAmount;
28120
+ const currentTime = Math.floor(Date.now() / 1000);
28121
+ const needsCoinPermit2Approval = permit2ToUniPosManagerCoinAllowance.amount < coinAmount ||
28122
+ permit2ToUniPosManagerCoinAllowance.expiration <= currentTime;
28123
+ const calls = [];
28124
+ // 1. Coin approval to Permit2
28125
+ if (needsCoinApproval) {
28126
+ calls.push({
28127
+ to: coinAddress,
28128
+ description: `Approve ${coinSymbol} for Permit2`,
28129
+ data: viem.encodeFunctionData({
28130
+ abi: viem.erc20Abi,
28131
+ functionName: "approve",
28132
+ args: [permit2Address, coinAmount],
28133
+ }),
28134
+ });
28135
+ }
28136
+ // 2. Permit2 approval for coin to uni position manager
28137
+ const expiration = Math.floor(Date.now() / 1000) + 3600; // 1 hour from now
28138
+ if (needsCoinPermit2Approval) {
28139
+ calls.push({
28140
+ to: permit2Address,
28141
+ description: `Permit2 approval for ${coinSymbol} to UniV4PositionManager`,
28142
+ data: viem.encodeFunctionData({
28143
+ abi: Permit2Abi,
28144
+ functionName: "approve",
28145
+ args: [
28146
+ coinAddress,
28147
+ UniV4PositionManagerAddress[this.chainId],
28148
+ coinAmount,
28149
+ expiration,
28150
+ ],
28151
+ }),
28152
+ });
28153
+ }
28154
+ // === generate add liquidity call ===
28155
+ // Determine amounts for each currency based on pool key ordering
28156
+ const flethAmount = 0n;
28157
+ const amount0 = poolKey.currency0 === coinAddress ? coinAmount : flethAmount;
28158
+ const amount1 = poolKey.currency0 === coinAddress ? flethAmount : coinAmount;
28159
+ // Calculate and constrain liquidity using shared method
28160
+ const { finalLiquidity, finalAmount0, finalAmount1 } = this.calculateConstrainedLiquidity(currentTick, tickLower, tickUpper, amount0, amount1);
28161
+ // 3. Add liquidity
28162
+ calls.push(this.createLiquidityCall(poolKey, tickLower, tickUpper, finalLiquidity, finalAmount0, finalAmount1, userAddress));
28163
+ return calls;
28164
+ }
28165
+ // Implementation with union type for internal use
28166
+ async getImportAndSingleSidedCoinAddLiquidityCalls(params) {
28167
+ let importParams;
28168
+ if ("initialMarketCapUSD" in params) {
28169
+ const paramsWithMarketCap = params;
28170
+ importParams = await this.readWriteTokenImporter.getInitializeParams({
28171
+ coinAddress: paramsWithMarketCap.coinAddress,
28172
+ creatorFeeAllocationPercent: paramsWithMarketCap.creatorFeeAllocationPercent,
28173
+ initialMarketCapUSD: paramsWithMarketCap.initialMarketCapUSD,
28174
+ verifier: paramsWithMarketCap.verifier,
28175
+ });
28176
+ }
28177
+ else {
28178
+ const paramsWithPrice = params;
28179
+ importParams = await this.readWriteTokenImporter.getInitializeParams({
28180
+ coinAddress: paramsWithPrice.coinAddress,
28181
+ creatorFeeAllocationPercent: paramsWithPrice.creatorFeeAllocationPercent,
28182
+ initialPriceUSD: paramsWithPrice.initialPriceUSD,
28183
+ verifier: paramsWithPrice.verifier,
28184
+ });
28185
+ }
28186
+ const addLiquidityCalls = await this.getSingleSidedCoinAddLiquidityCalls({
28187
+ ...params,
28188
+ version: exports.FlaunchVersion.ANY, // optimize to avoid fetching if not passed
28189
+ });
28190
+ return [
28191
+ {
28192
+ to: this.readWriteTokenImporter.contract.address,
28193
+ data: this.readWriteTokenImporter.contract.encodeFunctionData("initialize", importParams),
28194
+ description: "Import Memecoin to Flaunch",
28195
+ },
28196
+ ...addLiquidityCalls,
28197
+ ];
28198
+ }
28199
+ /**
28200
+ * === Private helper functions ===
28201
+ */
28202
+ /**
28203
+ * Calculates and constrains liquidity amounts for a position
28204
+ * @param currentTick - Current pool tick
28205
+ * @param tickLower - Lower tick of the position
28206
+ * @param tickUpper - Upper tick of the position
28207
+ * @param amount0 - Amount of currency0
28208
+ * @param amount1 - Amount of currency1
28209
+ * @returns Final liquidity and amounts
28210
+ */
28211
+ calculateConstrainedLiquidity(currentTick, tickLower, tickUpper, amount0, amount1) {
27976
28212
  // Calculate liquidity first using user's input amounts
27977
28213
  const initialLiquidity = getLiquidityFromAmounts({
27978
28214
  currentTick,
@@ -28038,7 +28274,25 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
28038
28274
  }
28039
28275
  finalAmount0 = amount0; // Use user's full amount as maximum
28040
28276
  finalAmount1 = amount1; // Use user's full amount as maximum
28041
- // Prepare mint position parameters (following swiss-knife pattern)
28277
+ return {
28278
+ finalLiquidity,
28279
+ finalAmount0,
28280
+ finalAmount1,
28281
+ };
28282
+ }
28283
+ /**
28284
+ * Creates the UniV4 Position Manager liquidity call
28285
+ * @param poolKey - The pool key
28286
+ * @param tickLower - Lower tick of the position
28287
+ * @param tickUpper - Upper tick of the position
28288
+ * @param finalLiquidity - Final liquidity amount
28289
+ * @param finalAmount0 - Final amount of currency0
28290
+ * @param finalAmount1 - Final amount of currency1
28291
+ * @param userAddress - User's address
28292
+ * @returns CallWithDescription for adding liquidity
28293
+ */
28294
+ createLiquidityCall(poolKey, tickLower, tickUpper, finalLiquidity, finalAmount0, finalAmount1, userAddress) {
28295
+ // Prepare mint position parameters
28042
28296
  const V4PMActions = {
28043
28297
  MINT_POSITION: "02",
28044
28298
  SETTLE_PAIR: "0d",
@@ -28092,8 +28346,7 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
28092
28346
  currency1: poolKey.currency1,
28093
28347
  },
28094
28348
  ]);
28095
- // 6. Add liquidity
28096
- calls.push({
28349
+ return {
28097
28350
  to: UniV4PositionManagerAddress[this.chainId],
28098
28351
  data: viem.encodeFunctionData({
28099
28352
  abi: [
@@ -28119,33 +28372,7 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
28119
28372
  }),
28120
28373
  value: 0n,
28121
28374
  description: "Add Liquidity",
28122
- });
28123
- return calls;
28124
- }
28125
- /**
28126
- * Gets the calls needed to import a memecoin to Flaunch and add liquidity to AnyPositionManager as a batch
28127
- * @param params - Parameters for importing and adding liquidity
28128
- * @returns Array of calls with descriptions
28129
- */
28130
- async getImportAndAddLiquidityCalls(params) {
28131
- const importParams = await this.readWriteTokenImporter.getInitializeParams({
28132
- coinAddress: params.coinAddress,
28133
- creatorFeeAllocationPercent: params.creatorFeeAllocationPercent,
28134
- initialMarketCapUSD: params.initialMarketCapUSD,
28135
- verifier: params.verifier,
28136
- });
28137
- const addLiquidityCalls = await this.getAddLiquidityCalls({
28138
- ...params,
28139
- version: exports.FlaunchVersion.ANY, // optimize to avoid fetching if not passed
28140
- });
28141
- return [
28142
- {
28143
- to: this.readWriteTokenImporter.contract.address,
28144
- data: this.readWriteTokenImporter.contract.encodeFunctionData("initialize", importParams),
28145
- description: "Import Memecoin to Flaunch",
28146
- },
28147
- ...addLiquidityCalls,
28148
- ];
28375
+ };
28149
28376
  }
28150
28377
  }
28151
28378
 
@@ -31842,6 +32069,7 @@ exports.ReferralEscrowAbi = ReferralEscrowAbi;
31842
32069
  exports.ReferralEscrowAddress = ReferralEscrowAddress;
31843
32070
  exports.RevenueManagerAbi = RevenueManagerAbi;
31844
32071
  exports.RevenueManagerAddress = RevenueManagerAddress;
32072
+ exports.SolanaVerifierAddress = SolanaVerifierAddress;
31845
32073
  exports.StakingManagerAddress = StakingManagerAddress;
31846
32074
  exports.StateViewAbi = StateViewAbi;
31847
32075
  exports.StateViewAddress = StateViewAddress;