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.

76 lines (75 loc) 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.reverseResolveEnsAddresses = void 0; const tslib_1 = require("tslib"); const bytes = tslib_1.__importStar(require("@ethersproject/bytes")); const address_1 = require("@ethersproject/address"); const FetchWithTimeout_1 = require("./FetchWithTimeout"); const urls_1 = require("../constants/urls"); const addresses_1 = require("../constants/addresses"); function parseHexNumber(hex) { return parseInt(bytes.hexStripZeros(`0x${hex}`), 16); } function processReturnData(result) { let pieces = []; for (let i = 2; i < result.length; i += 64) { pieces.push(result.substr(i, 64)); } const numberEntries = parseHexNumber(pieces[1]); const addresses = []; const offsets = []; for (let i = 0; i < numberEntries; i++) { offsets.push(parseHexNumber(pieces[i + 2])); } for (let i = 0; i < numberEntries; i++) { let pieceId = offsets[i] / 32 + 2; let strLen = parseHexNumber(pieces[pieceId]); const strHex = result.substr((pieceId + 1) * 64 + 2, strLen * 2); addresses.push(Buffer.from(strHex, 'hex').toString()); } return addresses; } async function reverseResolveEnsAddresses(addresses, networkId, timeout) { if (!addresses_1.ENS_REVERSE_LOOKUP_CONTRACT_BY_NETWORK[networkId]) { throw new Error('Undefined ENS lookup for network'); } const mapping = addresses.reduce((last, at) => { if (!address_1.isAddress(at)) { return last; } last[at] = at; return last; }, {}); const mappingKeys = Object.keys(mapping); const requestData = bytes.hexConcat([ '0xcbf8b66c', bytes.hexConcat(['0x20', bytes.arrayify(mappingKeys.length), ...mappingKeys].map((el) => bytes.zeroPad(el, 32))), ]); const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ jsonrpc: '2.0', id: '1', method: 'eth_call', params: [ { to: addresses_1.ENS_REVERSE_LOOKUP_CONTRACT_BY_NETWORK[networkId], data: requestData }, 'latest', ], }), }; const fetcher = new FetchWithTimeout_1.FetchWithTimeout(timeout, 'application/json'); const result = await fetcher.fetch(urls_1.RPC_URL_BY_NETWORK[networkId], requestOptions); //todo use infura or FallbackProvider const json = await result.json(); const resultAddresses = processReturnData(json.result); if (resultAddresses.length !== mappingKeys.length) { throw new Error('Wrong address return length'); } return mappingKeys.reduce((last, at, index) => { last[at] = resultAddresses[index]; return last; }, {}); } exports.reverseResolveEnsAddresses = reverseResolveEnsAddresses;