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

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.
@@ -338,6 +338,150 @@ var DISTRIBUTOR_PROGRAM_ID = {
338
338
  [common.ICluster.Testnet]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N",
339
339
  [common.ICluster.Local]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N"
340
340
  };
341
+ var fetchAirdropFee = async (distributorId, cluster = common.ICluster.Mainnet, fetchFn = fetch) => {
342
+ const baseUrl = cluster === common.ICluster.Mainnet ? "https://api.streamflow.finance" : "https://staging-api.streamflow.finance";
343
+ const url = `${baseUrl}/v2/api/airdrops/${encodeURIComponent(distributorId)}/fees`;
344
+ const res = await fetchFn(url, {
345
+ method: "GET",
346
+ headers: {
347
+ "Content-Type": "application/json"
348
+ }
349
+ });
350
+ if (!res.ok) {
351
+ const text = await res.text().catch(() => "");
352
+ throw new Error(`Airdrop fee API error: ${res.status} ${res.statusText}${text ? ` - ${text}` : ""}`);
353
+ }
354
+ const json = await res.json();
355
+ return json;
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
+
371
+ // solana/fees.ts
372
+ var MAXIMUM_FEE_FALLBACK = 9900000n;
373
+ var MINIMUM_FEE_FALLBACK = 5000000n;
374
+ var FEE_ALLOCATION_FACTOR_FALLBACK_NUMERATOR = 90n;
375
+ var FEE_ALLOCATION_FACTOR_FALLBACK_DENOMINATOR = 100n;
376
+ var LAMPORTS_PER_SOL = 1000000000n;
377
+ var lamportsToSolString = (lamports) => {
378
+ const int = lamports / LAMPORTS_PER_SOL;
379
+ const frac = lamports % LAMPORTS_PER_SOL;
380
+ if (frac === 0n) return int.toString();
381
+ const fracStr = frac.toString().padStart(9, "0").replace(/0+$/, "");
382
+ return `${int.toString()}.${fracStr}`;
383
+ };
384
+ var allocationToString = (num, den) => {
385
+ const SCALE = 1000000000n;
386
+ const scaled = num * SCALE / den;
387
+ const int = scaled / SCALE;
388
+ const frac = scaled % SCALE;
389
+ if (frac === 0n) return int.toString();
390
+ const fracStr = frac.toString().padStart(9, "0").replace(/0+$/, "");
391
+ return `${int.toString()}.${fracStr}`;
392
+ };
393
+ var defaultAPIFeesResponse = {
394
+ isCustom: false,
395
+ claimFee: void 0,
396
+ claimFeeDynamic: {
397
+ minPrice: lamportsToSolString(MINIMUM_FEE_FALLBACK),
398
+ maxPrice: lamportsToSolString(MAXIMUM_FEE_FALLBACK),
399
+ allocationFactor: allocationToString(
400
+ FEE_ALLOCATION_FACTOR_FALLBACK_NUMERATOR,
401
+ FEE_ALLOCATION_FACTOR_FALLBACK_DENOMINATOR
402
+ )
403
+ }
404
+ };
405
+ var toLamportsSOL = (value, solDecimals = 9) => {
406
+ const y = typeof value === "bigint" ? Number(value) : typeof value === "string" ? Number(value) : value;
407
+ if (!Number.isFinite(y) || y <= 0) return 0n;
408
+ const scale = 10n ** BigInt(solDecimals);
409
+ return common.multiplyBigIntByNumber(scale, y);
410
+ };
411
+ function calculateClaimableLamportsFromPrices(params) {
412
+ const {
413
+ claimableAmount,
414
+ tokenPriceUsd,
415
+ solPriceUsd,
416
+ tokenDecimals,
417
+ solDecimals = 9
418
+ } = params;
419
+ if (tokenPriceUsd <= 0 || solPriceUsd <= 0) return 0n;
420
+ const scaleDigits = Math.min(18, Math.max(0, tokenDecimals));
421
+ const SCALE = 10n ** BigInt(scaleDigits);
422
+ const pScaled = common.multiplyBigIntByNumber(SCALE, tokenPriceUsd, scaleDigits);
423
+ const sScaled = common.multiplyBigIntByNumber(SCALE, solPriceUsd, scaleDigits);
424
+ if (pScaled <= 0n || sScaled <= 0n) return 0n;
425
+ const pow10 = (n) => 10n ** BigInt(n);
426
+ const numerator = claimableAmount * pow10(solDecimals) * pScaled;
427
+ const denominator = pow10(tokenDecimals) * sScaled;
428
+ return numerator / denominator;
429
+ }
430
+ function calculateDynamicFeeFromSolParams(params) {
431
+ const { minPrice, maxPrice, allocationFactor, claimableLamports, solDecimals = 9 } = params;
432
+ const minLamports = toLamportsSOL(minPrice, solDecimals);
433
+ const maxLamports = toLamportsSOL(maxPrice, solDecimals);
434
+ const factor = Math.max(0, Math.min(1, parseFloat(allocationFactor)));
435
+ const raw = common.multiplyBigIntByNumber(claimableLamports, factor);
436
+ if (raw < minLamports) return minLamports;
437
+ if (raw > maxLamports) return maxLamports;
438
+ return raw;
439
+ }
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
+ async function resolveAirdropFeeLamportsUsingApi(params) {
449
+ const { distributorAddress, mintAccount, claimableAmount, cluster, fetchFn } = params;
450
+ let apiFeesResponse = void 0;
451
+ try {
452
+ apiFeesResponse = await fetchAirdropFee(distributorAddress, cluster);
453
+ } catch (_) {
454
+ }
455
+ let solPrice = null;
456
+ let tokenPrice = null;
457
+ try {
458
+ [solPrice, tokenPrice] = await Promise.all([
459
+ fetchTokenPriceUsd("So11111111111111111111111111111111111111112", cluster, fetchFn),
460
+ fetchTokenPriceUsd(mintAccount.address.toBase58(), cluster, fetchFn)
461
+ ]);
462
+ } catch (_) {
463
+ }
464
+ const response = apiFeesResponse ?? defaultAPIFeesResponse;
465
+ if (response?.isCustom && response.claimFee !== void 0) {
466
+ return toLamportsSOL(response.claimFee);
467
+ }
468
+ const claimFeeDynamic = response?.claimFeeDynamic ?? defaultAPIFeesResponse.claimFeeDynamic;
469
+ if (!tokenPrice || !solPrice) {
470
+ return MINIMUM_FEE_FALLBACK;
471
+ }
472
+ const baseLamports = calculateClaimableLamportsFromPrices({
473
+ claimableAmount,
474
+ tokenPriceUsd: tokenPrice,
475
+ solPriceUsd: solPrice,
476
+ tokenDecimals: mintAccount.decimals
477
+ });
478
+ return calculateDynamicFeeFromSolParams({
479
+ minPrice: claimFeeDynamic.minPrice,
480
+ maxPrice: claimFeeDynamic.maxPrice,
481
+ allocationFactor: claimFeeDynamic.allocationFactor,
482
+ claimableLamports: baseLamports
483
+ });
484
+ }
341
485
 
342
486
  // solana/descriptor/idl/merkle_distributor.json
343
487
  var merkle_distributor_default = {
@@ -3413,6 +3557,7 @@ var BaseDistributorClient = class {
3413
3557
  sendThrottler
3414
3558
  }) {
3415
3559
  this.commitment = commitment;
3560
+ this.cluster = cluster;
3416
3561
  this.connection = new web3_js.Connection(clusterUrl, this.commitment);
3417
3562
  this.programId = programId !== "" ? new web3_js.PublicKey(programId) : new web3_js.PublicKey(DISTRIBUTOR_PROGRAM_ID[cluster]);
3418
3563
  this.sendThrottler = sendThrottler ?? solana.buildSendThrottler(sendRate);
@@ -3596,12 +3741,21 @@ var BaseDistributorClient = class {
3596
3741
  if (claimStatus || new BN5__default.default(data.amountLocked).gtn(0) && nowTs.sub(distributor.startTs).gte(distributor.unlockPeriod)) {
3597
3742
  ixs.push(claimLocked(accounts, this.programId));
3598
3743
  }
3599
- ixs.push(
3600
- this.prepareClaimFeeInstruction(
3601
- extParams.feePayer ?? extParams.invoker.publicKey,
3602
- typeof _serviceTransfer === "bigint" ? _serviceTransfer : void 0
3603
- )
3604
- );
3744
+ let feeLamports = typeof _serviceTransfer === "bigint" ? _serviceTransfer : void 0;
3745
+ if (!feeLamports) {
3746
+ try {
3747
+ const { mint: mintAccount } = await solana.getMintAndProgram(this.connection, distributor.mint);
3748
+ feeLamports = await resolveAirdropFeeLamportsUsingApi({
3749
+ distributorAddress: distributorPublicKey.toBase58(),
3750
+ mintAccount,
3751
+ claimableAmount: BigInt(data.claimableAmount.toString()),
3752
+ cluster: this.cluster
3753
+ });
3754
+ } catch (_) {
3755
+ feeLamports = MINIMUM_FEE_FALLBACK;
3756
+ }
3757
+ }
3758
+ ixs.push(this.prepareClaimFeeInstruction(extParams.feePayer ?? extParams.invoker.publicKey, feeLamports));
3605
3759
  return ixs;
3606
3760
  }
3607
3761
  async closeClaim(data, extParams) {
@@ -5475,8 +5629,10 @@ exports.claimLocked = claimLocked;
5475
5629
  exports.clawback = clawback;
5476
5630
  exports.closeClaim = closeClaim;
5477
5631
  exports.constants = constants_exports;
5632
+ exports.fetchAirdropFee = fetchAirdropFee;
5478
5633
  exports.fromCode = fromCode3;
5479
5634
  exports.fromTxError = fromTxError;
5635
+ exports.getAirdropFeeQueryOptions = getAirdropFeeQueryOptions;
5480
5636
  exports.getAlignedDistributorPda = getAlignedDistributorPda;
5481
5637
  exports.getClaimantStatusPda = getClaimantStatusPda;
5482
5638
  exports.getDistributorPda = getDistributorPda;