@streamflow/distributor 9.0.0-alpha.p308.8fcedb9 → 9.0.1

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.
@@ -354,19 +354,6 @@ var fetchAirdropFee = async (distributorId, cluster = common.ICluster.Mainnet, f
354
354
  const json = await res.json();
355
355
  return json;
356
356
  };
357
- function getAirdropFeeQueryOptions(params, options) {
358
- const { staleTimeMs = 6e4, gcTimeMs = 3e5, retry = 2, refetchOnWindowFocus = false } = options ?? {};
359
- const cluster = params.cluster ?? common.ICluster.Mainnet;
360
- const key = ["sf", "airdrop", "fee", cluster, params.distributorId];
361
- return {
362
- queryKey: key,
363
- queryFn: () => fetchAirdropFee(params.distributorId, cluster, params.fetchFn ?? fetch),
364
- staleTime: staleTimeMs,
365
- gcTime: gcTimeMs,
366
- retry,
367
- refetchOnWindowFocus
368
- };
369
- }
370
357
 
371
358
  // solana/fees.ts
372
359
  var MAXIMUM_FEE_FALLBACK = 9900000n;
@@ -408,14 +395,43 @@ var toLamportsSOL = (value, solDecimals = 9) => {
408
395
  const scale = 10n ** BigInt(solDecimals);
409
396
  return common.multiplyBigIntByNumber(scale, y);
410
397
  };
398
+ var applyFeeLogic = (baseLamports) => {
399
+ if (baseLamports <= 0n) {
400
+ return MINIMUM_FEE_FALLBACK;
401
+ }
402
+ const raw = baseLamports * FEE_ALLOCATION_FACTOR_FALLBACK_NUMERATOR / FEE_ALLOCATION_FACTOR_FALLBACK_DENOMINATOR;
403
+ if (raw < MINIMUM_FEE_FALLBACK) return MINIMUM_FEE_FALLBACK;
404
+ if (raw > MAXIMUM_FEE_FALLBACK) return MAXIMUM_FEE_FALLBACK;
405
+ return raw;
406
+ };
407
+ async function calculateAirdropFeeLamports(params) {
408
+ const { distributorAddress, client, claimableLamports } = params;
409
+ try {
410
+ if (client) {
411
+ const res = await client.getAirdropFee(distributorAddress);
412
+ const data = res?.data;
413
+ if (data?.isCustom && data.claimFee !== void 0) {
414
+ return toLamportsSOL(data.claimFee);
415
+ }
416
+ if (data?.claimFeeDynamic && claimableLamports !== void 0) {
417
+ const min = toLamportsSOL(data.claimFeeDynamic.minPrice);
418
+ const max = toLamportsSOL(data.claimFeeDynamic.maxPrice);
419
+ const factor = Math.max(0, Math.min(1, parseFloat(data.claimFeeDynamic.allocationFactor)));
420
+ const fee = common.multiplyBigIntByNumber(claimableLamports, factor);
421
+ if (fee < min) return min;
422
+ if (fee > max) return max;
423
+ return fee;
424
+ }
425
+ }
426
+ } catch (_err) {
427
+ }
428
+ if (claimableLamports !== void 0) {
429
+ return applyFeeLogic(claimableLamports);
430
+ }
431
+ return MINIMUM_FEE_FALLBACK;
432
+ }
411
433
  function calculateClaimableLamportsFromPrices(params) {
412
- const {
413
- claimableAmount,
414
- tokenPriceUsd,
415
- solPriceUsd,
416
- tokenDecimals,
417
- solDecimals = 9
418
- } = params;
434
+ const { claimableAmount, tokenPriceUsd, solPriceUsd, tokenDecimals, solDecimals = 9 } = params;
419
435
  if (tokenPriceUsd <= 0 || solPriceUsd <= 0) return 0n;
420
436
  const scaleDigits = Math.min(18, Math.max(0, tokenDecimals));
421
437
  const SCALE = 10n ** BigInt(scaleDigits);
@@ -437,16 +453,8 @@ function calculateDynamicFeeFromSolParams(params) {
437
453
  if (raw > maxLamports) return maxLamports;
438
454
  return raw;
439
455
  }
440
- async function fetchTokenPriceUsd(mintId, cluster, fetchFn = fetch) {
441
- const url = `https://token-api.streamflow.finance/price?ids=${encodeURIComponent(mintId)}&cluster=${encodeURIComponent(cluster)}`;
442
- const res = await fetchFn(url, { headers: { "Content-Type": "application/json" } });
443
- if (!res.ok) return null;
444
- const json = await res.json();
445
- const val = json?.data?.[mintId]?.value;
446
- return typeof val === "number" ? val : null;
447
- }
448
456
  async function resolveAirdropFeeLamportsUsingApi(params) {
449
- const { distributorAddress, mintAccount, claimableAmount, cluster, fetchFn } = params;
457
+ const { distributorAddress, mintAccount, claimableAmount, cluster } = params;
450
458
  let apiFeesResponse = void 0;
451
459
  try {
452
460
  apiFeesResponse = await fetchAirdropFee(distributorAddress, cluster);
@@ -456,8 +464,8 @@ async function resolveAirdropFeeLamportsUsingApi(params) {
456
464
  let tokenPrice = null;
457
465
  try {
458
466
  [solPrice, tokenPrice] = await Promise.all([
459
- fetchTokenPriceUsd("So11111111111111111111111111111111111111112", cluster, fetchFn),
460
- fetchTokenPriceUsd(mintAccount.address.toBase58(), cluster, fetchFn)
467
+ common.fetchTokenPrice(splToken.NATIVE_MINT.toBase58(), cluster),
468
+ common.fetchTokenPrice(mintAccount.address.toBase58(), cluster)
461
469
  ]);
462
470
  } catch (_) {
463
471
  }
@@ -466,13 +474,13 @@ async function resolveAirdropFeeLamportsUsingApi(params) {
466
474
  return toLamportsSOL(response.claimFee);
467
475
  }
468
476
  const claimFeeDynamic = response?.claimFeeDynamic ?? defaultAPIFeesResponse.claimFeeDynamic;
469
- if (!tokenPrice || !solPrice) {
477
+ if (!tokenPrice?.value || !solPrice?.value) {
470
478
  return MINIMUM_FEE_FALLBACK;
471
479
  }
472
480
  const baseLamports = calculateClaimableLamportsFromPrices({
473
481
  claimableAmount,
474
- tokenPriceUsd: tokenPrice,
475
- solPriceUsd: solPrice,
482
+ tokenPriceUsd: tokenPrice.value,
483
+ solPriceUsd: solPrice.value,
476
484
  tokenDecimals: mintAccount.decimals
477
485
  });
478
486
  return calculateDynamicFeeFromSolParams({
@@ -3745,10 +3753,17 @@ var BaseDistributorClient = class {
3745
3753
  if (!feeLamports) {
3746
3754
  try {
3747
3755
  const { mint: mintAccount } = await solana.getMintAndProgram(this.connection, distributor.mint);
3756
+ const claimableAmountRaw = data?.claimableAmount;
3757
+ const unlockedRaw = data?.amountUnlocked;
3758
+ const lockedRaw = data?.amountLocked;
3759
+ const unlocked = unlockedRaw != null ? new BN5__default.default(unlockedRaw) : new BN5__default.default(0);
3760
+ const locked = lockedRaw != null ? new BN5__default.default(lockedRaw) : new BN5__default.default(0);
3761
+ const computedFallback = unlocked.add(locked);
3762
+ const claimableAmount = claimableAmountRaw != null ? BigInt(claimableAmountRaw.toString()) : BigInt(computedFallback.toString());
3748
3763
  feeLamports = await resolveAirdropFeeLamportsUsingApi({
3749
3764
  distributorAddress: distributorPublicKey.toBase58(),
3750
3765
  mintAccount,
3751
- claimableAmount: BigInt(data.claimableAmount.toString()),
3766
+ claimableAmount,
3752
3767
  cluster: this.cluster
3753
3768
  });
3754
3769
  } catch (_) {
@@ -5621,10 +5636,18 @@ var SolanaAlignedDistributorClient = class extends BaseDistributorClient {
5621
5636
 
5622
5637
  exports.AnchorErrorCode = AnchorErrorCode;
5623
5638
  exports.ContractErrorCode = ContractErrorCode;
5639
+ exports.FEE_ALLOCATION_FACTOR_FALLBACK_DENOMINATOR = FEE_ALLOCATION_FACTOR_FALLBACK_DENOMINATOR;
5640
+ exports.FEE_ALLOCATION_FACTOR_FALLBACK_NUMERATOR = FEE_ALLOCATION_FACTOR_FALLBACK_NUMERATOR;
5641
+ exports.MAXIMUM_FEE_FALLBACK = MAXIMUM_FEE_FALLBACK;
5642
+ exports.MINIMUM_FEE_FALLBACK = MINIMUM_FEE_FALLBACK;
5624
5643
  exports.MerkleDistributor = MerkleDistributor;
5625
5644
  exports.SolanaAlignedDistributorClient = SolanaAlignedDistributorClient;
5626
5645
  exports.SolanaDistributorClient = SolanaDistributorClient;
5646
+ exports.applyFeeLogic = applyFeeLogic;
5647
+ exports.calculateAirdropFeeLamports = calculateAirdropFeeLamports;
5627
5648
  exports.calculateAmountWithTransferFees = calculateAmountWithTransferFees;
5649
+ exports.calculateClaimableLamportsFromPrices = calculateClaimableLamportsFromPrices;
5650
+ exports.calculateDynamicFeeFromSolParams = calculateDynamicFeeFromSolParams;
5628
5651
  exports.claimLocked = claimLocked;
5629
5652
  exports.clawback = clawback;
5630
5653
  exports.closeClaim = closeClaim;
@@ -5632,7 +5655,6 @@ exports.constants = constants_exports;
5632
5655
  exports.fetchAirdropFee = fetchAirdropFee;
5633
5656
  exports.fromCode = fromCode3;
5634
5657
  exports.fromTxError = fromTxError;
5635
- exports.getAirdropFeeQueryOptions = getAirdropFeeQueryOptions;
5636
5658
  exports.getAlignedDistributorPda = getAlignedDistributorPda;
5637
5659
  exports.getClaimantStatusPda = getClaimantStatusPda;
5638
5660
  exports.getDistributorPda = getDistributorPda;
@@ -5641,8 +5663,10 @@ exports.getTestOraclePda = getTestOraclePda;
5641
5663
  exports.isCompressedClaimStatus = isCompressedClaimStatus;
5642
5664
  exports.newClaim = newClaim;
5643
5665
  exports.newDistributor = newDistributor;
5666
+ exports.resolveAirdropFeeLamportsUsingApi = resolveAirdropFeeLamportsUsingApi;
5644
5667
  exports.setAdmin = setAdmin;
5645
5668
  exports.setClawbackReceiver = setClawbackReceiver;
5669
+ exports.toLamportsSOL = toLamportsSOL;
5646
5670
  exports.wrappedSignAndExecuteTransaction = wrappedSignAndExecuteTransaction;
5647
5671
  //# sourceMappingURL=index.cjs.map
5648
5672
  //# sourceMappingURL=index.cjs.map