@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.
@@ -338,6 +338,123 @@ 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
+
358
+ // solana/fees.ts
359
+ var MAXIMUM_FEE_FALLBACK = 9900000n;
360
+ var MINIMUM_FEE_FALLBACK = 5000000n;
361
+ var FEE_ALLOCATION_FACTOR_FALLBACK_NUMERATOR = 90n;
362
+ var FEE_ALLOCATION_FACTOR_FALLBACK_DENOMINATOR = 100n;
363
+ var LAMPORTS_PER_SOL = 1000000000n;
364
+ var lamportsToSolString = (lamports) => {
365
+ const int = lamports / LAMPORTS_PER_SOL;
366
+ const frac = lamports % LAMPORTS_PER_SOL;
367
+ if (frac === 0n) return int.toString();
368
+ const fracStr = frac.toString().padStart(9, "0").replace(/0+$/, "");
369
+ return `${int.toString()}.${fracStr}`;
370
+ };
371
+ var allocationToString = (num, den) => {
372
+ const SCALE = 1000000000n;
373
+ const scaled = num * SCALE / den;
374
+ const int = scaled / SCALE;
375
+ const frac = scaled % SCALE;
376
+ if (frac === 0n) return int.toString();
377
+ const fracStr = frac.toString().padStart(9, "0").replace(/0+$/, "");
378
+ return `${int.toString()}.${fracStr}`;
379
+ };
380
+ var defaultAPIFeesResponse = {
381
+ isCustom: false,
382
+ claimFee: void 0,
383
+ claimFeeDynamic: {
384
+ minPrice: lamportsToSolString(MINIMUM_FEE_FALLBACK),
385
+ maxPrice: lamportsToSolString(MAXIMUM_FEE_FALLBACK),
386
+ allocationFactor: allocationToString(
387
+ FEE_ALLOCATION_FACTOR_FALLBACK_NUMERATOR,
388
+ FEE_ALLOCATION_FACTOR_FALLBACK_DENOMINATOR
389
+ )
390
+ }
391
+ };
392
+ var toLamportsSOL = (value, solDecimals = 9) => {
393
+ const y = typeof value === "bigint" ? Number(value) : typeof value === "string" ? Number(value) : value;
394
+ if (!Number.isFinite(y) || y <= 0) return 0n;
395
+ const scale = 10n ** BigInt(solDecimals);
396
+ return common.multiplyBigIntByNumber(scale, y);
397
+ };
398
+ function calculateClaimableLamportsFromPrices(params) {
399
+ const { claimableAmount, tokenPriceUsd, solPriceUsd, tokenDecimals, solDecimals = 9 } = params;
400
+ if (tokenPriceUsd <= 0 || solPriceUsd <= 0) return 0n;
401
+ const scaleDigits = Math.min(18, Math.max(0, tokenDecimals));
402
+ const SCALE = 10n ** BigInt(scaleDigits);
403
+ const pScaled = common.multiplyBigIntByNumber(SCALE, tokenPriceUsd, scaleDigits);
404
+ const sScaled = common.multiplyBigIntByNumber(SCALE, solPriceUsd, scaleDigits);
405
+ if (pScaled <= 0n || sScaled <= 0n) return 0n;
406
+ const pow10 = (n) => 10n ** BigInt(n);
407
+ const numerator = claimableAmount * pow10(solDecimals) * pScaled;
408
+ const denominator = pow10(tokenDecimals) * sScaled;
409
+ return numerator / denominator;
410
+ }
411
+ function calculateDynamicFeeFromSolParams(params) {
412
+ const { minPrice, maxPrice, allocationFactor, claimableLamports, solDecimals = 9 } = params;
413
+ const minLamports = toLamportsSOL(minPrice, solDecimals);
414
+ const maxLamports = toLamportsSOL(maxPrice, solDecimals);
415
+ const factor = Math.max(0, Math.min(1, parseFloat(allocationFactor)));
416
+ const raw = common.multiplyBigIntByNumber(claimableLamports, factor);
417
+ if (raw < minLamports) return minLamports;
418
+ if (raw > maxLamports) return maxLamports;
419
+ return raw;
420
+ }
421
+ async function resolveAirdropFeeLamportsUsingApi(params) {
422
+ const { distributorAddress, mintAccount, claimableAmount, cluster } = params;
423
+ let apiFeesResponse = void 0;
424
+ try {
425
+ apiFeesResponse = await fetchAirdropFee(distributorAddress, cluster);
426
+ } catch (_) {
427
+ }
428
+ let solPrice = null;
429
+ let tokenPrice = null;
430
+ try {
431
+ [solPrice, tokenPrice] = await Promise.all([
432
+ common.fetchTokenPrice(splToken.NATIVE_MINT.toBase58(), cluster),
433
+ common.fetchTokenPrice(mintAccount.address.toBase58(), cluster)
434
+ ]);
435
+ } catch (_) {
436
+ }
437
+ const response = apiFeesResponse ?? defaultAPIFeesResponse;
438
+ if (response?.isCustom && response.claimFee !== void 0) {
439
+ return toLamportsSOL(response.claimFee);
440
+ }
441
+ const claimFeeDynamic = response?.claimFeeDynamic ?? defaultAPIFeesResponse.claimFeeDynamic;
442
+ if (!tokenPrice?.value || !solPrice?.value) {
443
+ return MINIMUM_FEE_FALLBACK;
444
+ }
445
+ const baseLamports = calculateClaimableLamportsFromPrices({
446
+ claimableAmount,
447
+ tokenPriceUsd: tokenPrice.value,
448
+ solPriceUsd: solPrice.value,
449
+ tokenDecimals: mintAccount.decimals
450
+ });
451
+ return calculateDynamicFeeFromSolParams({
452
+ minPrice: claimFeeDynamic.minPrice,
453
+ maxPrice: claimFeeDynamic.maxPrice,
454
+ allocationFactor: claimFeeDynamic.allocationFactor,
455
+ claimableLamports: baseLamports
456
+ });
457
+ }
341
458
 
342
459
  // solana/descriptor/idl/merkle_distributor.json
343
460
  var merkle_distributor_default = {
@@ -3413,6 +3530,7 @@ var BaseDistributorClient = class {
3413
3530
  sendThrottler
3414
3531
  }) {
3415
3532
  this.commitment = commitment;
3533
+ this.cluster = cluster;
3416
3534
  this.connection = new web3_js.Connection(clusterUrl, this.commitment);
3417
3535
  this.programId = programId !== "" ? new web3_js.PublicKey(programId) : new web3_js.PublicKey(DISTRIBUTOR_PROGRAM_ID[cluster]);
3418
3536
  this.sendThrottler = sendThrottler ?? solana.buildSendThrottler(sendRate);
@@ -3596,12 +3714,28 @@ var BaseDistributorClient = class {
3596
3714
  if (claimStatus || new BN5__default.default(data.amountLocked).gtn(0) && nowTs.sub(distributor.startTs).gte(distributor.unlockPeriod)) {
3597
3715
  ixs.push(claimLocked(accounts, this.programId));
3598
3716
  }
3599
- ixs.push(
3600
- this.prepareClaimFeeInstruction(
3601
- extParams.feePayer ?? extParams.invoker.publicKey,
3602
- typeof _serviceTransfer === "bigint" ? _serviceTransfer : void 0
3603
- )
3604
- );
3717
+ let feeLamports = typeof _serviceTransfer === "bigint" ? _serviceTransfer : void 0;
3718
+ if (!feeLamports) {
3719
+ try {
3720
+ const { mint: mintAccount } = await solana.getMintAndProgram(this.connection, distributor.mint);
3721
+ const claimableAmountRaw = data?.claimableAmount;
3722
+ const unlockedRaw = data?.amountUnlocked;
3723
+ const lockedRaw = data?.amountLocked;
3724
+ const unlocked = unlockedRaw != null ? new BN5__default.default(unlockedRaw) : new BN5__default.default(0);
3725
+ const locked = lockedRaw != null ? new BN5__default.default(lockedRaw) : new BN5__default.default(0);
3726
+ const computedFallback = unlocked.add(locked);
3727
+ const claimableAmount = claimableAmountRaw != null ? BigInt(claimableAmountRaw.toString()) : BigInt(computedFallback.toString());
3728
+ feeLamports = await resolveAirdropFeeLamportsUsingApi({
3729
+ distributorAddress: distributorPublicKey.toBase58(),
3730
+ mintAccount,
3731
+ claimableAmount,
3732
+ cluster: this.cluster
3733
+ });
3734
+ } catch (_) {
3735
+ feeLamports = MINIMUM_FEE_FALLBACK;
3736
+ }
3737
+ }
3738
+ ixs.push(this.prepareClaimFeeInstruction(extParams.feePayer ?? extParams.invoker.publicKey, feeLamports));
3605
3739
  return ixs;
3606
3740
  }
3607
3741
  async closeClaim(data, extParams) {
@@ -5475,6 +5609,7 @@ exports.claimLocked = claimLocked;
5475
5609
  exports.clawback = clawback;
5476
5610
  exports.closeClaim = closeClaim;
5477
5611
  exports.constants = constants_exports;
5612
+ exports.fetchAirdropFee = fetchAirdropFee;
5478
5613
  exports.fromCode = fromCode3;
5479
5614
  exports.fromTxError = fromTxError;
5480
5615
  exports.getAlignedDistributorPda = getAlignedDistributorPda;