@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.
@@ -46,10 +46,18 @@ var solana_exports = {};
46
46
  __export(solana_exports, {
47
47
  AnchorErrorCode: () => AnchorErrorCode,
48
48
  ContractErrorCode: () => ContractErrorCode,
49
+ FEE_ALLOCATION_FACTOR_FALLBACK_DENOMINATOR: () => FEE_ALLOCATION_FACTOR_FALLBACK_DENOMINATOR,
50
+ FEE_ALLOCATION_FACTOR_FALLBACK_NUMERATOR: () => FEE_ALLOCATION_FACTOR_FALLBACK_NUMERATOR,
51
+ MAXIMUM_FEE_FALLBACK: () => MAXIMUM_FEE_FALLBACK,
52
+ MINIMUM_FEE_FALLBACK: () => MINIMUM_FEE_FALLBACK,
49
53
  MerkleDistributor: () => MerkleDistributor,
50
54
  SolanaAlignedDistributorClient: () => SolanaAlignedDistributorClient,
51
55
  SolanaDistributorClient: () => SolanaDistributorClient,
56
+ applyFeeLogic: () => applyFeeLogic,
57
+ calculateAirdropFeeLamports: () => calculateAirdropFeeLamports,
52
58
  calculateAmountWithTransferFees: () => calculateAmountWithTransferFees,
59
+ calculateClaimableLamportsFromPrices: () => calculateClaimableLamportsFromPrices,
60
+ calculateDynamicFeeFromSolParams: () => calculateDynamicFeeFromSolParams,
53
61
  claimLocked: () => claimLocked,
54
62
  clawback: () => clawback,
55
63
  closeClaim: () => closeClaim,
@@ -57,7 +65,6 @@ __export(solana_exports, {
57
65
  fetchAirdropFee: () => fetchAirdropFee,
58
66
  fromCode: () => fromCode3,
59
67
  fromTxError: () => fromTxError,
60
- getAirdropFeeQueryOptions: () => getAirdropFeeQueryOptions,
61
68
  getAlignedDistributorPda: () => getAlignedDistributorPda,
62
69
  getClaimantStatusPda: () => getClaimantStatusPda,
63
70
  getDistributorPda: () => getDistributorPda,
@@ -66,8 +73,10 @@ __export(solana_exports, {
66
73
  isCompressedClaimStatus: () => isCompressedClaimStatus,
67
74
  newClaim: () => newClaim,
68
75
  newDistributor: () => newDistributor,
76
+ resolveAirdropFeeLamportsUsingApi: () => resolveAirdropFeeLamportsUsingApi,
69
77
  setAdmin: () => setAdmin,
70
78
  setClawbackReceiver: () => setClawbackReceiver,
79
+ toLamportsSOL: () => toLamportsSOL,
71
80
  wrappedSignAndExecuteTransaction: () => wrappedSignAndExecuteTransaction
72
81
  });
73
82
  var AnchorErrorCode = /* @__PURE__ */ ((AnchorErrorCode2) => {
@@ -384,19 +393,6 @@ var fetchAirdropFee = async (distributorId, cluster = common.ICluster.Mainnet, f
384
393
  const json = await res.json();
385
394
  return json;
386
395
  };
387
- function getAirdropFeeQueryOptions(params, options) {
388
- const { staleTimeMs = 6e4, gcTimeMs = 3e5, retry = 2, refetchOnWindowFocus = false } = options ?? {};
389
- const cluster = params.cluster ?? common.ICluster.Mainnet;
390
- const key = ["sf", "airdrop", "fee", cluster, params.distributorId];
391
- return {
392
- queryKey: key,
393
- queryFn: () => fetchAirdropFee(params.distributorId, cluster, params.fetchFn ?? fetch),
394
- staleTime: staleTimeMs,
395
- gcTime: gcTimeMs,
396
- retry,
397
- refetchOnWindowFocus
398
- };
399
- }
400
396
 
401
397
  // solana/fees.ts
402
398
  var MAXIMUM_FEE_FALLBACK = 9900000n;
@@ -438,14 +434,43 @@ var toLamportsSOL = (value, solDecimals = 9) => {
438
434
  const scale = 10n ** BigInt(solDecimals);
439
435
  return common.multiplyBigIntByNumber(scale, y);
440
436
  };
437
+ var applyFeeLogic = (baseLamports) => {
438
+ if (baseLamports <= 0n) {
439
+ return MINIMUM_FEE_FALLBACK;
440
+ }
441
+ const raw = baseLamports * FEE_ALLOCATION_FACTOR_FALLBACK_NUMERATOR / FEE_ALLOCATION_FACTOR_FALLBACK_DENOMINATOR;
442
+ if (raw < MINIMUM_FEE_FALLBACK) return MINIMUM_FEE_FALLBACK;
443
+ if (raw > MAXIMUM_FEE_FALLBACK) return MAXIMUM_FEE_FALLBACK;
444
+ return raw;
445
+ };
446
+ async function calculateAirdropFeeLamports(params) {
447
+ const { distributorAddress, client, claimableLamports } = params;
448
+ try {
449
+ if (client) {
450
+ const res = await client.getAirdropFee(distributorAddress);
451
+ const data = res?.data;
452
+ if (data?.isCustom && data.claimFee !== void 0) {
453
+ return toLamportsSOL(data.claimFee);
454
+ }
455
+ if (data?.claimFeeDynamic && claimableLamports !== void 0) {
456
+ const min = toLamportsSOL(data.claimFeeDynamic.minPrice);
457
+ const max = toLamportsSOL(data.claimFeeDynamic.maxPrice);
458
+ const factor = Math.max(0, Math.min(1, parseFloat(data.claimFeeDynamic.allocationFactor)));
459
+ const fee = common.multiplyBigIntByNumber(claimableLamports, factor);
460
+ if (fee < min) return min;
461
+ if (fee > max) return max;
462
+ return fee;
463
+ }
464
+ }
465
+ } catch (_err) {
466
+ }
467
+ if (claimableLamports !== void 0) {
468
+ return applyFeeLogic(claimableLamports);
469
+ }
470
+ return MINIMUM_FEE_FALLBACK;
471
+ }
441
472
  function calculateClaimableLamportsFromPrices(params) {
442
- const {
443
- claimableAmount,
444
- tokenPriceUsd,
445
- solPriceUsd,
446
- tokenDecimals,
447
- solDecimals = 9
448
- } = params;
473
+ const { claimableAmount, tokenPriceUsd, solPriceUsd, tokenDecimals, solDecimals = 9 } = params;
449
474
  if (tokenPriceUsd <= 0 || solPriceUsd <= 0) return 0n;
450
475
  const scaleDigits = Math.min(18, Math.max(0, tokenDecimals));
451
476
  const SCALE = 10n ** BigInt(scaleDigits);
@@ -467,16 +492,8 @@ function calculateDynamicFeeFromSolParams(params) {
467
492
  if (raw > maxLamports) return maxLamports;
468
493
  return raw;
469
494
  }
470
- async function fetchTokenPriceUsd(mintId, cluster, fetchFn = fetch) {
471
- const url = `https://token-api.streamflow.finance/price?ids=${encodeURIComponent(mintId)}&cluster=${encodeURIComponent(cluster)}`;
472
- const res = await fetchFn(url, { headers: { "Content-Type": "application/json" } });
473
- if (!res.ok) return null;
474
- const json = await res.json();
475
- const val = json?.data?.[mintId]?.value;
476
- return typeof val === "number" ? val : null;
477
- }
478
495
  async function resolveAirdropFeeLamportsUsingApi(params) {
479
- const { distributorAddress, mintAccount, claimableAmount, cluster, fetchFn } = params;
496
+ const { distributorAddress, mintAccount, claimableAmount, cluster } = params;
480
497
  let apiFeesResponse = void 0;
481
498
  try {
482
499
  apiFeesResponse = await fetchAirdropFee(distributorAddress, cluster);
@@ -486,8 +503,8 @@ async function resolveAirdropFeeLamportsUsingApi(params) {
486
503
  let tokenPrice = null;
487
504
  try {
488
505
  [solPrice, tokenPrice] = await Promise.all([
489
- fetchTokenPriceUsd("So11111111111111111111111111111111111111112", cluster, fetchFn),
490
- fetchTokenPriceUsd(mintAccount.address.toBase58(), cluster, fetchFn)
506
+ common.fetchTokenPrice(splToken.NATIVE_MINT.toBase58(), cluster),
507
+ common.fetchTokenPrice(mintAccount.address.toBase58(), cluster)
491
508
  ]);
492
509
  } catch (_) {
493
510
  }
@@ -496,13 +513,13 @@ async function resolveAirdropFeeLamportsUsingApi(params) {
496
513
  return toLamportsSOL(response.claimFee);
497
514
  }
498
515
  const claimFeeDynamic = response?.claimFeeDynamic ?? defaultAPIFeesResponse.claimFeeDynamic;
499
- if (!tokenPrice || !solPrice) {
516
+ if (!tokenPrice?.value || !solPrice?.value) {
500
517
  return MINIMUM_FEE_FALLBACK;
501
518
  }
502
519
  const baseLamports = calculateClaimableLamportsFromPrices({
503
520
  claimableAmount,
504
- tokenPriceUsd: tokenPrice,
505
- solPriceUsd: solPrice,
521
+ tokenPriceUsd: tokenPrice.value,
522
+ solPriceUsd: solPrice.value,
506
523
  tokenDecimals: mintAccount.decimals
507
524
  });
508
525
  return calculateDynamicFeeFromSolParams({
@@ -3775,10 +3792,17 @@ var BaseDistributorClient = class {
3775
3792
  if (!feeLamports) {
3776
3793
  try {
3777
3794
  const { mint: mintAccount } = await solana.getMintAndProgram(this.connection, distributor.mint);
3795
+ const claimableAmountRaw = data?.claimableAmount;
3796
+ const unlockedRaw = data?.amountUnlocked;
3797
+ const lockedRaw = data?.amountLocked;
3798
+ const unlocked = unlockedRaw != null ? new BN5__default.default(unlockedRaw) : new BN5__default.default(0);
3799
+ const locked = lockedRaw != null ? new BN5__default.default(lockedRaw) : new BN5__default.default(0);
3800
+ const computedFallback = unlocked.add(locked);
3801
+ const claimableAmount = claimableAmountRaw != null ? BigInt(claimableAmountRaw.toString()) : BigInt(computedFallback.toString());
3778
3802
  feeLamports = await resolveAirdropFeeLamportsUsingApi({
3779
3803
  distributorAddress: distributorPublicKey.toBase58(),
3780
3804
  mintAccount,
3781
- claimableAmount: BigInt(data.claimableAmount.toString()),
3805
+ claimableAmount,
3782
3806
  cluster: this.cluster
3783
3807
  });
3784
3808
  } catch (_) {