@flaunch/sdk 0.9.7 → 0.9.8

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
@@ -26939,7 +26939,8 @@ class ReadFlaunchSDK {
26939
26939
  memecoin.totalSupply(),
26940
26940
  memecoin.decimals(),
26941
26941
  ]);
26942
- return { totalSupply, decimals };
26942
+ const formattedTotalSupplyInDecimals = parseFloat(viem.formatUnits(totalSupply, decimals));
26943
+ return { totalSupply, decimals, formattedTotalSupplyInDecimals };
26943
26944
  }
26944
26945
  /**
26945
26946
  * Gets market context information needed for tick calculations
@@ -26961,8 +26962,8 @@ class ReadFlaunchSDK {
26961
26962
  /**
26962
26963
  * Converts market cap in USD to token price in ETH
26963
26964
  */
26964
- marketCapToTokenPriceEth(marketCapUsd, totalSupplyDecimal, ethUsdPrice) {
26965
- const tokenPriceUsd = marketCapUsd / totalSupplyDecimal;
26965
+ marketCapToTokenPriceEth(marketCapUsd, formattedTotalSupplyInDecimals, ethUsdPrice) {
26966
+ const tokenPriceUsd = marketCapUsd / formattedTotalSupplyInDecimals;
26966
26967
  return tokenPriceUsd / ethUsdPrice;
26967
26968
  }
26968
26969
  /**
@@ -26980,24 +26981,22 @@ class ReadFlaunchSDK {
26980
26981
  /**
26981
26982
  * Calculates current tick from market cap if provided
26982
26983
  */
26983
- calculateCurrentTickFromMarketCap(currentMarketCap, totalSupplyDecimal, marketContext) {
26984
+ calculateCurrentTickFromMarketCap(currentMarketCap, formattedTotalSupplyInDecimals, marketContext) {
26984
26985
  if (!currentMarketCap) {
26985
26986
  return undefined;
26986
26987
  }
26987
26988
  const currentMarketCapNum = parseFloat(currentMarketCap);
26988
- const currentTokenPriceEth = this.marketCapToTokenPriceEth(currentMarketCapNum, totalSupplyDecimal, marketContext.ethUsdPrice);
26989
+ const currentTokenPriceEth = this.marketCapToTokenPriceEth(currentMarketCapNum, formattedTotalSupplyInDecimals, marketContext.ethUsdPrice);
26989
26990
  return this.convertPriceToTick(currentTokenPriceEth, marketContext.isFlethZero, marketContext.decimals0, marketContext.decimals1);
26990
26991
  }
26991
26992
  async calculateAddLiquidityTicks({ coinAddress, liquidityMode, minMarketCap, maxMarketCap, currentMarketCap, }) {
26992
26993
  // Get coin information
26993
- const { totalSupply: coinTotalSupply, decimals: coinDecimals } = await this.getCoinInfo(coinAddress);
26994
- // Convert total supply to decimal format
26995
- const totalSupplyDecimal = parseFloat(viem.formatEther(coinTotalSupply));
26994
+ const { totalSupply: coinTotalSupply, decimals: coinDecimals, formattedTotalSupplyInDecimals, } = await this.getCoinInfo(coinAddress);
26996
26995
  if (liquidityMode === exports.LiquidityMode.FULL_RANGE) {
26997
26996
  let currentTick;
26998
26997
  if (currentMarketCap) {
26999
26998
  const marketContext = await this.getMarketContext(coinAddress, coinDecimals);
27000
- currentTick = this.calculateCurrentTickFromMarketCap(currentMarketCap, totalSupplyDecimal, marketContext);
26999
+ currentTick = this.calculateCurrentTickFromMarketCap(currentMarketCap, formattedTotalSupplyInDecimals, marketContext);
27001
27000
  }
27002
27001
  return {
27003
27002
  tickLower: getNearestUsableTick({
@@ -27024,13 +27023,13 @@ class ReadFlaunchSDK {
27024
27023
  throw new Error("[ReadFlaunchSDK.addLiquidityCalculateTicks]: Invalid market cap range");
27025
27024
  }
27026
27025
  // Convert market caps to token prices in ETH
27027
- const minTokenPriceEth = this.marketCapToTokenPriceEth(minMarketCapNum, totalSupplyDecimal, marketContext.ethUsdPrice);
27028
- const maxTokenPriceEth = this.marketCapToTokenPriceEth(maxMarketCapNum, totalSupplyDecimal, marketContext.ethUsdPrice);
27026
+ const minTokenPriceEth = this.marketCapToTokenPriceEth(minMarketCapNum, formattedTotalSupplyInDecimals, marketContext.ethUsdPrice);
27027
+ const maxTokenPriceEth = this.marketCapToTokenPriceEth(maxMarketCapNum, formattedTotalSupplyInDecimals, marketContext.ethUsdPrice);
27029
27028
  // Convert to ticks
27030
27029
  const minTick = this.convertPriceToTick(minTokenPriceEth, marketContext.isFlethZero, marketContext.decimals0, marketContext.decimals1);
27031
27030
  const maxTick = this.convertPriceToTick(maxTokenPriceEth, marketContext.isFlethZero, marketContext.decimals0, marketContext.decimals1);
27032
27031
  // Calculate current tick if provided
27033
- const currentTick = this.calculateCurrentTickFromMarketCap(currentMarketCap, totalSupplyDecimal, marketContext);
27032
+ const currentTick = this.calculateCurrentTickFromMarketCap(currentMarketCap, formattedTotalSupplyInDecimals, marketContext);
27034
27033
  return {
27035
27034
  tickLower: Math.min(minTick, maxTick),
27036
27035
  tickUpper: Math.max(minTick, maxTick),
@@ -27051,12 +27050,11 @@ class ReadFlaunchSDK {
27051
27050
  currentMarketCap = params.currentMarketCap;
27052
27051
  }
27053
27052
  else {
27054
- const { totalSupply, decimals } = await this.getCoinInfo(coinAddress);
27055
- const formattedTotalSupply = parseFloat(viem.formatUnits(totalSupply, decimals));
27056
- minMarketCap = (parseFloat(params.minPriceUSD) * formattedTotalSupply).toString();
27057
- maxMarketCap = (parseFloat(params.maxPriceUSD) * formattedTotalSupply).toString();
27053
+ const { formattedTotalSupplyInDecimals } = await this.getCoinInfo(coinAddress);
27054
+ minMarketCap = (parseFloat(params.minPriceUSD) * formattedTotalSupplyInDecimals).toString();
27055
+ maxMarketCap = (parseFloat(params.maxPriceUSD) * formattedTotalSupplyInDecimals).toString();
27058
27056
  if (params.currentPriceUSD) {
27059
- currentMarketCap = (params.currentPriceUSD * formattedTotalSupply).toString();
27057
+ currentMarketCap = (params.currentPriceUSD * formattedTotalSupplyInDecimals).toString();
27060
27058
  }
27061
27059
  }
27062
27060
  let { tickLower, tickUpper, currentTick } = await this.calculateAddLiquidityTicks({
@@ -27127,12 +27125,11 @@ class ReadFlaunchSDK {
27127
27125
  currentMarketCap = params.currentMarketCap;
27128
27126
  }
27129
27127
  else {
27130
- const { totalSupply, decimals } = await this.getCoinInfo(coinAddress);
27131
- const formattedTotalSupply = parseFloat(viem.formatUnits(totalSupply, decimals));
27132
- minMarketCap = (parseFloat(params.minPriceUSD) * formattedTotalSupply).toString();
27133
- maxMarketCap = (parseFloat(params.maxPriceUSD) * formattedTotalSupply).toString();
27128
+ const { formattedTotalSupplyInDecimals } = await this.getCoinInfo(coinAddress);
27129
+ minMarketCap = (parseFloat(params.minPriceUSD) * formattedTotalSupplyInDecimals).toString();
27130
+ maxMarketCap = (parseFloat(params.maxPriceUSD) * formattedTotalSupplyInDecimals).toString();
27134
27131
  if (params.currentPriceUSD) {
27135
- currentMarketCap = (params.currentPriceUSD * formattedTotalSupply).toString();
27132
+ currentMarketCap = (params.currentPriceUSD * formattedTotalSupplyInDecimals).toString();
27136
27133
  }
27137
27134
  }
27138
27135
  let { tickLower, tickUpper, currentTick } = await this.calculateAddLiquidityTicks({
@@ -27796,12 +27793,12 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
27796
27793
  initialMarketCapUSD = params.initialMarketCapUSD;
27797
27794
  }
27798
27795
  else {
27799
- const { totalSupply, decimals } = await this.getCoinInfo(coinAddress);
27800
- const formattedTotalSupply = parseFloat(viem.formatUnits(totalSupply, decimals));
27801
- minMarketCap = (parseFloat(params.minPriceUSD) * formattedTotalSupply).toString();
27802
- maxMarketCap = (parseFloat(params.maxPriceUSD) * formattedTotalSupply).toString();
27796
+ const { formattedTotalSupplyInDecimals } = await this.getCoinInfo(coinAddress);
27797
+ minMarketCap = (parseFloat(params.minPriceUSD) * formattedTotalSupplyInDecimals).toString();
27798
+ maxMarketCap = (parseFloat(params.maxPriceUSD) * formattedTotalSupplyInDecimals).toString();
27803
27799
  if (params.initialPriceUSD) {
27804
- initialMarketCapUSD = params.initialPriceUSD * formattedTotalSupply;
27800
+ initialMarketCapUSD =
27801
+ params.initialPriceUSD * formattedTotalSupplyInDecimals;
27805
27802
  }
27806
27803
  }
27807
27804
  const calculated = await this.calculateAddLiquidityAmounts({
@@ -27971,18 +27968,27 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
27971
27968
  calls.push(this.createLiquidityCall(poolKey, tickLower, tickUpper, finalLiquidity, finalAmount0, finalAmount1, userAddress));
27972
27969
  return calls;
27973
27970
  }
27974
- /**
27975
- * Gets the calls needed to import a memecoin to Flaunch and add liquidity to AnyPositionManager as a batch
27976
- * @param params - Parameters for importing and adding liquidity
27977
- * @returns Array of calls with descriptions
27978
- */
27971
+ // Implementation with union type for internal use
27979
27972
  async getImportAndAddLiquidityCalls(params) {
27980
- const importParams = await this.readWriteTokenImporter.getInitializeParams({
27981
- coinAddress: params.coinAddress,
27982
- creatorFeeAllocationPercent: params.creatorFeeAllocationPercent,
27983
- initialMarketCapUSD: params.initialMarketCapUSD,
27984
- verifier: params.verifier,
27985
- });
27973
+ let importParams;
27974
+ if ("initialMarketCapUSD" in params) {
27975
+ const paramsWithMarketCap = params;
27976
+ importParams = await this.readWriteTokenImporter.getInitializeParams({
27977
+ coinAddress: paramsWithMarketCap.coinAddress,
27978
+ creatorFeeAllocationPercent: paramsWithMarketCap.creatorFeeAllocationPercent,
27979
+ initialMarketCapUSD: paramsWithMarketCap.initialMarketCapUSD,
27980
+ verifier: paramsWithMarketCap.verifier,
27981
+ });
27982
+ }
27983
+ else {
27984
+ const paramsWithPrice = params;
27985
+ importParams = await this.readWriteTokenImporter.getInitializeParams({
27986
+ coinAddress: paramsWithPrice.coinAddress,
27987
+ creatorFeeAllocationPercent: paramsWithPrice.creatorFeeAllocationPercent,
27988
+ initialPriceUSD: paramsWithPrice.initialPriceUSD,
27989
+ verifier: paramsWithPrice.verifier,
27990
+ });
27991
+ }
27986
27992
  const addLiquidityCalls = await this.getAddLiquidityCalls({
27987
27993
  ...params,
27988
27994
  version: exports.FlaunchVersion.ANY, // optimize to avoid fetching if not passed
@@ -28005,11 +28011,38 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
28005
28011
  const { coinAddress, coinAmount } = params;
28006
28012
  const version = await this.determineCoinVersion(coinAddress, params.version);
28007
28013
  const poolKey = this.createPoolKey(coinAddress, version);
28008
- // get the current tick from the pool
28009
- const poolState = await this.readStateView.poolSlot0({
28010
- poolId: getPoolId(poolKey),
28011
- });
28012
- const currentTick = poolState.tick;
28014
+ let currentTick;
28015
+ // if initial marketcap or price is provided, it means that the pool is not initialized yet
28016
+ // so determining the currentTick
28017
+ if (("initialMarketCapUSD" in params && params.initialMarketCapUSD) ||
28018
+ ("initialPriceUSD" in params && params.initialPriceUSD)) {
28019
+ const { decimals: coinDecimals, formattedTotalSupplyInDecimals } = await this.getCoinInfo(coinAddress);
28020
+ // Determine market cap based on provided parameter
28021
+ let initialMarketCapUSD;
28022
+ if ("initialMarketCapUSD" in params && params.initialMarketCapUSD) {
28023
+ initialMarketCapUSD = params.initialMarketCapUSD;
28024
+ }
28025
+ else if ("initialPriceUSD" in params && params.initialPriceUSD) {
28026
+ initialMarketCapUSD =
28027
+ params.initialPriceUSD * formattedTotalSupplyInDecimals;
28028
+ }
28029
+ else {
28030
+ throw new Error("Either initialMarketCapUSD or initialPriceUSD must be provided");
28031
+ }
28032
+ const marketContext = await this.getMarketContext(coinAddress, coinDecimals);
28033
+ const calculatedTick = this.calculateCurrentTickFromMarketCap(initialMarketCapUSD.toString(), formattedTotalSupplyInDecimals, marketContext);
28034
+ if (calculatedTick === undefined) {
28035
+ throw new Error("Failed to calculate current tick from market cap");
28036
+ }
28037
+ currentTick = calculatedTick;
28038
+ }
28039
+ else {
28040
+ // the pool is already initialized, get the current tick from the pool
28041
+ const poolState = await this.readStateView.poolSlot0({
28042
+ poolId: getPoolId(poolKey),
28043
+ });
28044
+ currentTick = poolState.tick;
28045
+ }
28013
28046
  // We want to add liquidity from current price to infinity (as coin appreciates vs flETH)
28014
28047
  // This means providing single-sided coin liquidity that becomes active as coin price increases
28015
28048
  const isFLETHZero = this.flETHIsCurrencyZero(coinAddress);
@@ -28122,6 +28155,43 @@ class ReadWriteFlaunchSDK extends ReadFlaunchSDK {
28122
28155
  calls.push(this.createLiquidityCall(poolKey, tickLower, tickUpper, finalLiquidity, finalAmount0, finalAmount1, userAddress));
28123
28156
  return calls;
28124
28157
  }
28158
+ // Implementation with union type for internal use
28159
+ async getImportAndSingleSidedCoinAddLiquidityCalls(params) {
28160
+ let importParams;
28161
+ if ("initialMarketCapUSD" in params) {
28162
+ const paramsWithMarketCap = params;
28163
+ importParams = await this.readWriteTokenImporter.getInitializeParams({
28164
+ coinAddress: paramsWithMarketCap.coinAddress,
28165
+ creatorFeeAllocationPercent: paramsWithMarketCap.creatorFeeAllocationPercent,
28166
+ initialMarketCapUSD: paramsWithMarketCap.initialMarketCapUSD,
28167
+ verifier: paramsWithMarketCap.verifier,
28168
+ });
28169
+ }
28170
+ else {
28171
+ const paramsWithPrice = params;
28172
+ importParams = await this.readWriteTokenImporter.getInitializeParams({
28173
+ coinAddress: paramsWithPrice.coinAddress,
28174
+ creatorFeeAllocationPercent: paramsWithPrice.creatorFeeAllocationPercent,
28175
+ initialPriceUSD: paramsWithPrice.initialPriceUSD,
28176
+ verifier: paramsWithPrice.verifier,
28177
+ });
28178
+ }
28179
+ const addLiquidityCalls = await this.getSingleSidedCoinAddLiquidityCalls({
28180
+ ...params,
28181
+ version: exports.FlaunchVersion.ANY, // optimize to avoid fetching if not passed
28182
+ });
28183
+ return [
28184
+ {
28185
+ to: this.readWriteTokenImporter.contract.address,
28186
+ data: this.readWriteTokenImporter.contract.encodeFunctionData("initialize", importParams),
28187
+ description: "Import Memecoin to Flaunch",
28188
+ },
28189
+ ...addLiquidityCalls,
28190
+ ];
28191
+ }
28192
+ /**
28193
+ * === Private helper functions ===
28194
+ */
28125
28195
  /**
28126
28196
  * Calculates and constrains liquidity amounts for a position
28127
28197
  * @param currentTick - Current pool tick