All files utils.ts

15.87% Statements 20/126
100% Branches 0/0
0% Functions 0/6
15.87% Lines 20/126

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 1271x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                   1x                                   1x 1x                 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 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
}