@streamflow/distributor 8.9.0 → 9.0.0-alpha.p308.791571f

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.
@@ -54,6 +54,7 @@ __export(solana_exports, {
54
54
  clawback: () => clawback,
55
55
  closeClaim: () => closeClaim,
56
56
  constants: () => constants_exports,
57
+ fetchAirdropFee: () => fetchAirdropFee,
57
58
  fromCode: () => fromCode3,
58
59
  fromTxError: () => fromTxError,
59
60
  getAlignedDistributorPda: () => getAlignedDistributorPda,
@@ -366,6 +367,123 @@ var DISTRIBUTOR_PROGRAM_ID = {
366
367
  [common.ICluster.Testnet]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N",
367
368
  [common.ICluster.Local]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N"
368
369
  };
370
+ var fetchAirdropFee = async (distributorId, cluster = common.ICluster.Mainnet, fetchFn = fetch) => {
371
+ const baseUrl = cluster === common.ICluster.Mainnet ? "https://api.streamflow.finance" : "https://staging-api.streamflow.finance";
372
+ const url = `${baseUrl}/v2/api/airdrops/${encodeURIComponent(distributorId)}/fees`;
373
+ const res = await fetchFn(url, {
374
+ method: "GET",
375
+ headers: {
376
+ "Content-Type": "application/json"
377
+ }
378
+ });
379
+ if (!res.ok) {
380
+ const text = await res.text().catch(() => "");
381
+ throw new Error(`Airdrop fee API error: ${res.status} ${res.statusText}${text ? ` - ${text}` : ""}`);
382
+ }
383
+ const json = await res.json();
384
+ return json;
385
+ };
386
+
387
+ // solana/fees.ts
388
+ var MAXIMUM_FEE_FALLBACK = 9900000n;
389
+ var MINIMUM_FEE_FALLBACK = 5000000n;
390
+ var FEE_ALLOCATION_FACTOR_FALLBACK_NUMERATOR = 90n;
391
+ var FEE_ALLOCATION_FACTOR_FALLBACK_DENOMINATOR = 100n;
392
+ var LAMPORTS_PER_SOL = 1000000000n;
393
+ var lamportsToSolString = (lamports) => {
394
+ const int = lamports / LAMPORTS_PER_SOL;
395
+ const frac = lamports % LAMPORTS_PER_SOL;
396
+ if (frac === 0n) return int.toString();
397
+ const fracStr = frac.toString().padStart(9, "0").replace(/0+$/, "");
398
+ return `${int.toString()}.${fracStr}`;
399
+ };
400
+ var allocationToString = (num, den) => {
401
+ const SCALE = 1000000000n;
402
+ const scaled = num * SCALE / den;
403
+ const int = scaled / SCALE;
404
+ const frac = scaled % SCALE;
405
+ if (frac === 0n) return int.toString();
406
+ const fracStr = frac.toString().padStart(9, "0").replace(/0+$/, "");
407
+ return `${int.toString()}.${fracStr}`;
408
+ };
409
+ var defaultAPIFeesResponse = {
410
+ isCustom: false,
411
+ claimFee: void 0,
412
+ claimFeeDynamic: {
413
+ minPrice: lamportsToSolString(MINIMUM_FEE_FALLBACK),
414
+ maxPrice: lamportsToSolString(MAXIMUM_FEE_FALLBACK),
415
+ allocationFactor: allocationToString(
416
+ FEE_ALLOCATION_FACTOR_FALLBACK_NUMERATOR,
417
+ FEE_ALLOCATION_FACTOR_FALLBACK_DENOMINATOR
418
+ )
419
+ }
420
+ };
421
+ var toLamportsSOL = (value, solDecimals = 9) => {
422
+ const y = typeof value === "bigint" ? Number(value) : typeof value === "string" ? Number(value) : value;
423
+ if (!Number.isFinite(y) || y <= 0) return 0n;
424
+ const scale = 10n ** BigInt(solDecimals);
425
+ return common.multiplyBigIntByNumber(scale, y);
426
+ };
427
+ function calculateClaimableLamportsFromPrices(params) {
428
+ const { claimableAmount, tokenPriceUsd, solPriceUsd, tokenDecimals, solDecimals = 9 } = params;
429
+ if (tokenPriceUsd <= 0 || solPriceUsd <= 0) return 0n;
430
+ const scaleDigits = Math.min(18, Math.max(0, tokenDecimals));
431
+ const SCALE = 10n ** BigInt(scaleDigits);
432
+ const pScaled = common.multiplyBigIntByNumber(SCALE, tokenPriceUsd, scaleDigits);
433
+ const sScaled = common.multiplyBigIntByNumber(SCALE, solPriceUsd, scaleDigits);
434
+ if (pScaled <= 0n || sScaled <= 0n) return 0n;
435
+ const pow10 = (n) => 10n ** BigInt(n);
436
+ const numerator = claimableAmount * pow10(solDecimals) * pScaled;
437
+ const denominator = pow10(tokenDecimals) * sScaled;
438
+ return numerator / denominator;
439
+ }
440
+ function calculateDynamicFeeFromSolParams(params) {
441
+ const { minPrice, maxPrice, allocationFactor, claimableLamports, solDecimals = 9 } = params;
442
+ const minLamports = toLamportsSOL(minPrice, solDecimals);
443
+ const maxLamports = toLamportsSOL(maxPrice, solDecimals);
444
+ const factor = Math.max(0, Math.min(1, parseFloat(allocationFactor)));
445
+ const raw = common.multiplyBigIntByNumber(claimableLamports, factor);
446
+ if (raw < minLamports) return minLamports;
447
+ if (raw > maxLamports) return maxLamports;
448
+ return raw;
449
+ }
450
+ async function resolveAirdropFeeLamportsUsingApi(params) {
451
+ const { distributorAddress, mintAccount, claimableAmount, cluster } = params;
452
+ let apiFeesResponse = void 0;
453
+ try {
454
+ apiFeesResponse = await fetchAirdropFee(distributorAddress, cluster);
455
+ } catch (_) {
456
+ }
457
+ let solPrice = null;
458
+ let tokenPrice = null;
459
+ try {
460
+ [solPrice, tokenPrice] = await Promise.all([
461
+ common.fetchTokenPrice(splToken.NATIVE_MINT.toBase58(), cluster),
462
+ common.fetchTokenPrice(mintAccount.address.toBase58(), cluster)
463
+ ]);
464
+ } catch (_) {
465
+ }
466
+ const response = apiFeesResponse ?? defaultAPIFeesResponse;
467
+ if (response?.isCustom && response.claimFee !== void 0) {
468
+ return toLamportsSOL(response.claimFee);
469
+ }
470
+ const claimFeeDynamic = response?.claimFeeDynamic ?? defaultAPIFeesResponse.claimFeeDynamic;
471
+ if (!tokenPrice?.value || !solPrice?.value) {
472
+ return MINIMUM_FEE_FALLBACK;
473
+ }
474
+ const baseLamports = calculateClaimableLamportsFromPrices({
475
+ claimableAmount,
476
+ tokenPriceUsd: tokenPrice.value,
477
+ solPriceUsd: solPrice.value,
478
+ tokenDecimals: mintAccount.decimals
479
+ });
480
+ return calculateDynamicFeeFromSolParams({
481
+ minPrice: claimFeeDynamic.minPrice,
482
+ maxPrice: claimFeeDynamic.maxPrice,
483
+ allocationFactor: claimFeeDynamic.allocationFactor,
484
+ claimableLamports: baseLamports
485
+ });
486
+ }
369
487
 
370
488
  // solana/descriptor/idl/merkle_distributor.json
371
489
  var merkle_distributor_default = {
@@ -3441,6 +3559,7 @@ var BaseDistributorClient = class {
3441
3559
  sendThrottler
3442
3560
  }) {
3443
3561
  this.commitment = commitment;
3562
+ this.cluster = cluster;
3444
3563
  this.connection = new web3_js.Connection(clusterUrl, this.commitment);
3445
3564
  this.programId = programId !== "" ? new web3_js.PublicKey(programId) : new web3_js.PublicKey(DISTRIBUTOR_PROGRAM_ID[cluster]);
3446
3565
  this.sendThrottler = sendThrottler ?? solana.buildSendThrottler(sendRate);
@@ -3624,12 +3743,28 @@ var BaseDistributorClient = class {
3624
3743
  if (claimStatus || new BN5__default.default(data.amountLocked).gtn(0) && nowTs.sub(distributor.startTs).gte(distributor.unlockPeriod)) {
3625
3744
  ixs.push(claimLocked(accounts, this.programId));
3626
3745
  }
3627
- ixs.push(
3628
- this.prepareClaimFeeInstruction(
3629
- extParams.feePayer ?? extParams.invoker.publicKey,
3630
- typeof _serviceTransfer === "bigint" ? _serviceTransfer : void 0
3631
- )
3632
- );
3746
+ let feeLamports = typeof _serviceTransfer === "bigint" ? _serviceTransfer : void 0;
3747
+ if (!feeLamports) {
3748
+ try {
3749
+ const { mint: mintAccount } = await solana.getMintAndProgram(this.connection, distributor.mint);
3750
+ const claimableAmountRaw = data?.claimableAmount;
3751
+ const unlockedRaw = data?.amountUnlocked;
3752
+ const lockedRaw = data?.amountLocked;
3753
+ const unlocked = unlockedRaw != null ? new BN5__default.default(unlockedRaw) : new BN5__default.default(0);
3754
+ const locked = lockedRaw != null ? new BN5__default.default(lockedRaw) : new BN5__default.default(0);
3755
+ const computedFallback = unlocked.add(locked);
3756
+ const claimableAmount = claimableAmountRaw != null ? BigInt(claimableAmountRaw.toString()) : BigInt(computedFallback.toString());
3757
+ feeLamports = await resolveAirdropFeeLamportsUsingApi({
3758
+ distributorAddress: distributorPublicKey.toBase58(),
3759
+ mintAccount,
3760
+ claimableAmount,
3761
+ cluster: this.cluster
3762
+ });
3763
+ } catch (_) {
3764
+ feeLamports = MINIMUM_FEE_FALLBACK;
3765
+ }
3766
+ }
3767
+ ixs.push(this.prepareClaimFeeInstruction(extParams.feePayer ?? extParams.invoker.publicKey, feeLamports));
3633
3768
  return ixs;
3634
3769
  }
3635
3770
  async closeClaim(data, extParams) {