UNPKG

@cross-nft-marketplace/auction-house-nft-hooks

Version:

Generic react hooks for fetching cross-nft-marketplace auctions, nfts, and data on arbitary 721s. Powers nft-components.

258 lines (257 loc) 12.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addAuctionInformation = exports.transformCurrencyForKey = exports.auctionDataToPricing = exports.transformCurrencyEth = exports.NULL_ETH_CURRENCY_ID = void 0; const tslib_1 = require("tslib"); const big_js_1 = tslib_1.__importDefault(require("big.js")); const zora_graph_types_1 = require("../graph-queries/zora-graph-types"); const RequestError_1 = require("./RequestError"); const AuctionInfoTypes_1 = require("./AuctionInfoTypes"); const AuctionState_1 = require("./AuctionState"); exports.NULL_ETH_CURRENCY_ID = '0x0000000000000000000000000000000000000000'; function transformCurrencyEth(currency) { let updatedCurrency = { ...currency }; if (currency.id === exports.NULL_ETH_CURRENCY_ID) { updatedCurrency.decimals = 18; updatedCurrency.name = 'Ethereum'; updatedCurrency.symbol = 'ETH'; } if (!updatedCurrency.decimals) { // Assume default 18 decimals updatedCurrency.decimals = 18; } return updatedCurrency; } exports.transformCurrencyEth = transformCurrencyEth; function auctionDataToPricing(auctionData) { var _a, _b; if (!auctionData) { return null; } return { ...auctionData, reserveAndBuyNowCurrency: transformCurrencyEth(auctionData.reserveAndBuyNowCurrency), auctionCurrencies: (_a = auctionData.auctionCurrencies) === null || _a === void 0 ? void 0 : _a.map(transformCurrencyEth), currentBid: auctionData.currentBid ? { ...auctionData.currentBid, currency: transformCurrencyEth(auctionData.currentBid.currency) } : undefined, previousBids: (_b = auctionData.previousBids) === null || _b === void 0 ? void 0 : _b.map((bid) => { return { ...bid, currency: transformCurrencyEth(bid.currency) }; }) }; } exports.auctionDataToPricing = auctionDataToPricing; function transformCurrencyForKey(result, key) { var _a, _b; // Special case ETH if (key === exports.NULL_ETH_CURRENCY_ID) { return { ethToUsd: (_a = result.bundle) === null || _a === void 0 ? void 0 : _a.ethPrice, token: { symbol: 'ETH', name: 'Ethereum', id: exports.NULL_ETH_CURRENCY_ID, decimals: 18, derivedETH: 1, }, }; } const currency = result.tokens.find((token) => token.id === key); if (!currency) { throw new RequestError_1.RequestError('No currency in response'); } return { ethToUsd: (_b = result.bundle) === null || _b === void 0 ? void 0 : _b.ethPrice, token: currency, }; } exports.transformCurrencyForKey = transformCurrencyForKey; const setCurrencyDecimal = (amount, decimals) => { return new big_js_1.default(amount) .div(new big_js_1.default(10).pow(parseInt(decimals, 10) || 18)) .toString(); }; function addAuctionInformation(pricing, currencyInfos = {}) { var _a, _b, _c, _d, _e, _f, _g, _h, _j; const getCurrencyComputedValue = (currencyId, bidAmount) => { const currencyInfo = currencyInfos[currencyId]; if (!currencyInfo) { return; } const inETH = new big_js_1.default(currencyInfo.token.derivedETH) .mul(new big_js_1.default(bidAmount).div(new big_js_1.default(10).pow(parseInt(currencyInfo.token.decimals, 10)))) .toString(); return { inETH, inUSD: new big_js_1.default(inETH).mul(currencyInfo.ethToUsd).toString(), }; }; const handlePerpetualBid = (bidRaw) => { const { amount, currency, ...bid } = bidRaw; return { ...bid, pricing: { currency, amount, prettyAmount: setCurrencyDecimal(amount, currency.decimals), computedValue: getCurrencyComputedValue(currency.id, amount), }, }; }; const getBidPricing = (amount, currency) => { return { pricing: { currency, amount, prettyAmount: setCurrencyDecimal(amount, currency === null || currency === void 0 ? void 0 : currency.decimals), computedValue: currency ? getCurrencyComputedValue(currency.id, amount) : undefined, }, }; }; const transformAskCurrency = (ask) => { const { amount, currency, createdAtTimestamp, id } = ask; return { createdAtTimestamp, id, pricing: { currency, amount, prettyAmount: setCurrencyDecimal(amount, currency.decimals), computedValue: getCurrencyComputedValue(currency.id, amount), }, }; }; const getHighestReserveBid = () => { var _a; if ((_a = pricing.reserve) === null || _a === void 0 ? void 0 : _a.currentBid) { const { currentBid } = pricing.reserve; const computedValue = getCurrencyComputedValue(currentBid.currency.id, currentBid.amount); return { pricing: { amount: currentBid.amount, prettyAmount: setCurrencyDecimal(currentBid.amount, currentBid.currency.decimals), currency: transformCurrencyEth(currentBid.currency), computedValue, }, placedBy: currentBid.bidder.id, placedAt: currentBid.createdAtTimestamp, }; } return; }; const getHighestPerpetualBid = () => { var _a, _b; const sortedBids = (_b = (_a = pricing.perpetual) === null || _a === void 0 ? void 0 : _a.bids) === null || _b === void 0 ? void 0 : _b.map((bid) => ({ bid, computedValue: getCurrencyComputedValue(bid.currency.id, bid.amount), })).sort((a, b) => { if (a.computedValue && b.computedValue) { return new big_js_1.default(a.computedValue.inETH).sub(b.computedValue.inETH).toNumber() > 0 ? -1 : 1; } return new Date(a.bid.createdAtTimestamp).getTime() > new Date(b.bid.createdAtTimestamp).getTime() ? -1 : 1; }); if (!sortedBids || !sortedBids.length) { return; } return { pricing: { computedValue: sortedBids[0].computedValue, amount: sortedBids[0].bid.amount, prettyAmount: setCurrencyDecimal(sortedBids[0].bid.amount, sortedBids[0].bid.currency.decimals), currency: transformCurrencyEth(sortedBids[0].bid.currency), }, placedBy: sortedBids[0].bid.bidder.id, placedAt: sortedBids[0].bid.createdAtTimestamp, }; }; const nftPricingInformation = { reserve: pricing.reserve ? { ...pricing.reserve, currentBid: (() => { var _a, _b, _c; if (!pricing.reserve.currentBid) { return undefined; } const { amount, ...bidRaw } = (_a = pricing.reserve) === null || _a === void 0 ? void 0 : _a.currentBid; return { ...bidRaw, ...getBidPricing(amount, (_c = (_b = pricing.reserve) === null || _b === void 0 ? void 0 : _b.currentBid) === null || _c === void 0 ? void 0 : _c.currency), }; })(), current: { highestBid: getHighestReserveBid(), likelyHasEnded: ((_a = pricing.reserve) === null || _a === void 0 ? void 0 : _a.status) == zora_graph_types_1.ReserveAuctionStatus.Finished //ex: finished by BuyNow || (parseInt((_b = pricing.reserve) === null || _b === void 0 ? void 0 : _b.expectedEndTimestamp, 10) < new Date().getTime() / 1000), reserveMet: ((_c = pricing.reserve) === null || _c === void 0 ? void 0 : _c.firstBidTime) && pricing.reserve.firstBidTime !== '0', }, reservePrice: { currency: transformCurrencyEth({ id: pricing.reserve.reserveAndBuyNowCurrency.id, name: pricing.reserve.reserveAndBuyNowCurrency.name, symbol: pricing.reserve.reserveAndBuyNowCurrency.symbol, decimals: pricing.reserve.reserveAndBuyNowCurrency.decimals, }), amount: pricing.reserve.reservePrice, prettyAmount: setCurrencyDecimal(pricing.reserve.reservePrice, pricing.reserve.reserveAndBuyNowCurrency.decimals), computedValue: getCurrencyComputedValue(pricing.reserve.reserveAndBuyNowCurrency.id, pricing.reserve.reservePrice), }, buyNowPrice: { currency: transformCurrencyEth({ id: pricing.reserve.reserveAndBuyNowCurrency.id, name: pricing.reserve.reserveAndBuyNowCurrency.name, symbol: pricing.reserve.reserveAndBuyNowCurrency.symbol, decimals: pricing.reserve.reserveAndBuyNowCurrency.decimals, }), amount: pricing.reserve.buyNowPrice, prettyAmount: setCurrencyDecimal(pricing.reserve.buyNowPrice, pricing.reserve.reserveAndBuyNowCurrency.decimals), computedValue: getCurrencyComputedValue(pricing.reserve.reserveAndBuyNowCurrency.id, pricing.reserve.buyNowPrice), }, previousBids: ((_e = (_d = pricing.reserve) === null || _d === void 0 ? void 0 : _d.previousBids) === null || _e === void 0 ? void 0 : _e.map(({ amount, currency, ...previousBid }) => ({ ...previousBid, ...getBidPricing(amount, currency), })).sort((bidA, bidB) => { if (bidA.bidInactivatedAtBlockNumber > bidB.bidInactivatedAtBlockNumber) { return -1; } if (bidA.bidInactivatedAtBlockNumber < bidB.bidInactivatedAtBlockNumber) { return 1; } //for BuyNow two bids has same bidInactivatedAtBlockNumber if (bidA.bidType == zora_graph_types_1.ReserveAuctionBidType.Final) { return -1; } return 1; })) || [], } : undefined, perpetual: { ask: ((_f = pricing.perpetual) === null || _f === void 0 ? void 0 : _f.ask) ? transformAskCurrency(pricing.perpetual.ask) : undefined, bids: ((_g = pricing.perpetual) === null || _g === void 0 ? void 0 : _g.bids.map((bid) => handlePerpetualBid(bid))) || [], highestBid: getHighestPerpetualBid(), }, auctionType: (((_h = pricing.reserve) === null || _h === void 0 ? void 0 : _h.status) === zora_graph_types_1.ReserveAuctionStatus.Active || ((_j = pricing.reserve) === null || _j === void 0 ? void 0 : _j.status) === zora_graph_types_1.ReserveAuctionStatus.Finished) ? AuctionInfoTypes_1.AuctionType.RESERVE : pricing.perpetual ? AuctionInfoTypes_1.AuctionType.PERPETUAL : AuctionInfoTypes_1.AuctionType.NONE, status: AuctionState_1.AuctionStateInfo.LOADING, }; nftPricingInformation.status = AuctionState_1.getAuctionState(nftPricingInformation); return nftPricingInformation; } exports.addAuctionInformation = addAuctionInformation;