All files utils.ts

19.49% Statements 31/159
80% Branches 4/5
12.5% Functions 1/8
19.49% Lines 31/159

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 1601x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                   1x                                   1x 1x                 1x                                                   1x                                                                               1x 1x 14x 8x 8x 14x 6x 6x       1x 1x                                        
import { SUPPORTS_INTERFACE_ABI } from './abi'
import {
  GET_DUTCH_AUCTION_PRICE,
  GET_FIXED_PRICE,
  GET_FIXED_PRICE_1155,
  GET_SALE_TERMS_1155,
  REFERRAL_ADDRESS,
} from './constants'
import { type SaleTerms } from './types'
import {
  type FilterOperator,
  type MintActionParams,
  chainIdToViemChain,
} from '@rabbitholegg/questdk-plugin-utils'
import { type Address, type PublicClient, createPublicClient, http } from 'viem'
 
export async function getFixedPriceData(
  client: PublicClient,
  address: Address,
  contractAddress: Address,
) {
  const result = await client.readContract({
    address,
    abi: [GET_FIXED_PRICE],
    functionName: 'getFixedPriceSaleV2',
    args: [contractAddress],
  })
  return {
    seller: result[0] as Address,
    actionFee: result[1] as bigint,
    projectFee: result[7] as bigint,
  }
}
 
export async function getDutchAuctionData(
  client: PublicClient,
  address: Address,
  contractAddress: Address,
) {
  const result = await client.readContract({
    address,
    abi: [GET_DUTCH_AUCTION_PRICE],
    functionName: 'getDutchAuctionV2',
    args: [contractAddress],
  })
  return {
    seller: result[0] as Address,
    actionFee: result[8] as bigint,
    projectFee: result[9] as bigint,
  }
}
 
export function calculateFees(
  pricingInfo: { actionFee: bigint; projectFee: bigint },
  quantity: bigint,
) {
  return {
    actionFee: pricingInfo.actionFee * quantity,
    projectFee: pricingInfo.projectFee * quantity,
  }
}
 
export async function getSaleTermsId(
  mint: MintActionParams,
  multiTokenAddress: Address,
) {
  const { chainId, contractAddress, tokenId } = mint
  if (tokenId == null) return null

  try {
    const client = createPublicClient({
      chain: chainIdToViemChain(chainId),
      transport: http(),
    }) as PublicClient

    const result = (await client.readContract({
      address: multiTokenAddress,
      abi: [GET_SALE_TERMS_1155],
      functionName: 'getSaleTermsForToken',
      args: [contractAddress, tokenId],
    })) as bigint

    return result
  } catch {
    return null
  }
}
 
export async function getFixedPriceSaleTerms(
  client: PublicClient,
  salesTermId: bigint,
  multiTokenAddress: Address,
): Promise<SaleTerms> {
  return (await client.readContract({
    address: multiTokenAddress,
    abi: [GET_FIXED_PRICE_1155],
    functionName: 'getFixedPriceSale',
    args: [salesTermId, REFERRAL_ADDRESS],
  })) as SaleTerms
}
export async function getContractType(
  client: PublicClient,
  contractAddress: Address,
): Promise<'1155' | '721' | null> {
  const abi = SUPPORTS_INTERFACE_ABI
  const interfaceIds = {
    '1155': '0xd9b67a26', // ERC-1155
    '721': '0x80ac58cd', // ERC-721
  }

  for (const [type, interfaceId] of Object.entries(interfaceIds)) {
    try {
      const supportsInterface = (await client.readContract({
        address: contractAddress,
        abi,
        functionName: 'supportsInterface',
        args: [interfaceId],
      })) as boolean

      if (supportsInterface) {
        return type as '1155' | '721'
      }
      // eslint-disable-next-line no-empty
    } catch {}
  }
  return null
}
 
export function formatAmount(amount: FilterOperator | undefined) {
  if (amount === undefined) {
    return undefined
  }
  if (amount && ['string', 'number', 'bigint'].includes(typeof amount)) {
    return { $gte: amount }
  }

  return amount
}
 
export function getMintAmount(amount: FilterOperator | undefined) {
  // If the amount is a primitive, pass that value through
  if (['number', 'bigint'].includes(typeof amount)) {
    return BigInt(amount as number | bigint)
  }
  if (typeof amount === 'string' && !isNaN(Number(amount))) {
    return BigInt(amount)
  }

  // For $gte, the minimum amount required to pass is the value of $gte
  if (typeof amount === 'object' && '$gte' in amount && amount.$gte) {
    return BigInt(amount.$gte)
  }
  // For $gt, the minimum amount required to pass is the value of $gt + 1
  if (typeof amount === 'object' && '$gt' in amount && amount.$gt) {
    return BigInt(amount.$gt) + 1n
  }
  // For all other conditions, the minimum amount required to pass is 1
  return 1n
}