@net-protocol/bazaar 0.1.12 → 0.1.13

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.
package/dist/react.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { useState, useMemo, useEffect } from 'react';
2
2
  import { usePublicClient } from 'wagmi';
3
3
  import { useNetMessageCount, useNetMessages } from '@net-protocol/core/react';
4
- import { createPublicClient, http, defineChain, decodeAbiParameters, formatEther, keccak256, toBytes } from 'viem';
4
+ import { createPublicClient, http, defineChain, decodeAbiParameters, keccak256, toBytes, formatEther } from 'viem';
5
5
  import { readContract } from 'viem/actions';
6
6
  import { NetClient } from '@net-protocol/core';
7
7
  import { Seaport } from '@opensea/seaport-js';
@@ -851,6 +851,12 @@ function getTotalConsiderationAmount(parameters) {
851
851
  function formatPrice(priceWei) {
852
852
  return parseFloat(formatEther(priceWei));
853
853
  }
854
+ function formatPricePerToken(priceWei, tokenAmount, tokenDecimals = 18) {
855
+ if (tokenAmount === 0n) return "0";
856
+ const scaled = priceWei * 10n ** BigInt(tokenDecimals);
857
+ const result = scaled / tokenAmount;
858
+ return formatEther(result);
859
+ }
854
860
  async function bulkFetchOrderStatuses(client, chainId, orderHashes) {
855
861
  if (orderHashes.length === 0) {
856
862
  return [];
@@ -1078,7 +1084,7 @@ function sortOffersByPrice(offers) {
1078
1084
  return 0;
1079
1085
  });
1080
1086
  }
1081
- function parseErc20OfferFromMessage(message, chainId) {
1087
+ function parseErc20OfferFromMessage(message, chainId, tokenDecimals = 18) {
1082
1088
  try {
1083
1089
  const submission = decodeSeaportSubmission(message.data);
1084
1090
  const { parameters } = submission;
@@ -1108,7 +1114,7 @@ function parseErc20OfferFromMessage(message, chainId) {
1108
1114
  priceWei,
1109
1115
  pricePerTokenWei,
1110
1116
  price: formatPrice(priceWei),
1111
- pricePerToken: formatPrice(pricePerTokenWei),
1117
+ pricePerToken: formatPricePerToken(priceWei, tokenAmount, tokenDecimals),
1112
1118
  currency: getCurrencySymbol(chainId),
1113
1119
  expirationDate: Number(parameters.endTime),
1114
1120
  orderHash: "0x",
@@ -1133,7 +1139,7 @@ function sortErc20OffersByPricePerToken(offers) {
1133
1139
  return 0;
1134
1140
  });
1135
1141
  }
1136
- function parseErc20ListingFromMessage(message, chainId) {
1142
+ function parseErc20ListingFromMessage(message, chainId, tokenDecimals = 18) {
1137
1143
  try {
1138
1144
  const submission = decodeSeaportSubmission(message.data);
1139
1145
  const { parameters } = submission;
@@ -1160,7 +1166,7 @@ function parseErc20ListingFromMessage(message, chainId) {
1160
1166
  priceWei,
1161
1167
  pricePerTokenWei,
1162
1168
  price: formatPrice(priceWei),
1163
- pricePerToken: formatPrice(pricePerTokenWei),
1169
+ pricePerToken: formatPricePerToken(priceWei, tokenAmount, tokenDecimals),
1164
1170
  currency: getCurrencySymbol(chainId),
1165
1171
  expirationDate: Number(parameters.endTime),
1166
1172
  orderHash: "0x",
@@ -1252,7 +1258,7 @@ function parseSaleFromStoredData(storedData, chainId) {
1252
1258
  amount: offerItem.amount,
1253
1259
  itemType: offerItem.itemType,
1254
1260
  priceWei: totalConsideration,
1255
- price: parseFloat(formatEther(totalConsideration)),
1261
+ price: formatPrice(totalConsideration),
1256
1262
  currency: getCurrencySymbol(chainId),
1257
1263
  timestamp: Number(timestamp),
1258
1264
  orderHash: zoneParameters.orderHash
@@ -1794,6 +1800,16 @@ var CHAIN_RPC_URLS = {
1794
1800
  9745: ["https://rpc.plasma.to"],
1795
1801
  143: ["https://rpc3.monad.xyz"]
1796
1802
  };
1803
+ var ERC20_DECIMALS_ABI = [
1804
+ {
1805
+ type: "function",
1806
+ name: "decimals",
1807
+ inputs: [],
1808
+ outputs: [{ type: "uint8", name: "" }],
1809
+ stateMutability: "view"
1810
+ }
1811
+ ];
1812
+ var tokenDecimalsCache = /* @__PURE__ */ new Map();
1797
1813
  var BazaarClient = class {
1798
1814
  constructor(params) {
1799
1815
  if (!isBazaarSupportedOnChain(params.chainId)) {
@@ -1830,6 +1846,28 @@ var BazaarClient = class {
1830
1846
  overrides: params.rpcUrl ? { rpcUrls: [params.rpcUrl] } : void 0
1831
1847
  });
1832
1848
  }
1849
+ /**
1850
+ * Fetch ERC20 token decimals with caching.
1851
+ * Falls back to 18 if the on-chain call fails.
1852
+ */
1853
+ async fetchTokenDecimals(tokenAddress) {
1854
+ const cacheKey = `${this.chainId}:${tokenAddress.toLowerCase()}`;
1855
+ const cached = tokenDecimalsCache.get(cacheKey);
1856
+ if (cached !== void 0) return cached;
1857
+ try {
1858
+ const result = await readContract(this.client, {
1859
+ address: tokenAddress,
1860
+ abi: ERC20_DECIMALS_ABI,
1861
+ functionName: "decimals"
1862
+ });
1863
+ const decimals = Number(result);
1864
+ tokenDecimalsCache.set(cacheKey, decimals);
1865
+ return decimals;
1866
+ } catch {
1867
+ console.warn(`[BazaarClient] Failed to fetch decimals for ${tokenAddress}, defaulting to 18`);
1868
+ return 18;
1869
+ }
1870
+ }
1833
1871
  /**
1834
1872
  * Get valid NFT listings for a collection
1835
1873
  *
@@ -2133,9 +2171,10 @@ var BazaarClient = class {
2133
2171
  if (!weth) {
2134
2172
  return [];
2135
2173
  }
2174
+ const tokenDecimals = await this.fetchTokenDecimals(tokenAddress);
2136
2175
  let offers = [];
2137
2176
  for (const message of messages) {
2138
- const offer = parseErc20OfferFromMessage(message, this.chainId);
2177
+ const offer = parseErc20OfferFromMessage(message, this.chainId, tokenDecimals);
2139
2178
  if (!offer) continue;
2140
2179
  const order = getSeaportOrderFromMessageData(offer.messageData);
2141
2180
  const offerToken = order.parameters.offer?.[0]?.token?.toLowerCase();
@@ -2240,9 +2279,10 @@ var BazaarClient = class {
2240
2279
  async processErc20ListingsFromMessages(messages, options) {
2241
2280
  const { tokenAddress, excludeMaker } = options;
2242
2281
  const tag = `[BazaarClient.processErc20Listings chain=${this.chainId}]`;
2282
+ const tokenDecimals = await this.fetchTokenDecimals(tokenAddress);
2243
2283
  let listings = [];
2244
2284
  for (const message of messages) {
2245
- const listing = parseErc20ListingFromMessage(message, this.chainId);
2285
+ const listing = parseErc20ListingFromMessage(message, this.chainId, tokenDecimals);
2246
2286
  if (!listing) continue;
2247
2287
  if (listing.tokenAddress.toLowerCase() !== tokenAddress.toLowerCase()) {
2248
2288
  continue;