UNPKG

staker

Version:

Command line Utillity for Ethereum stakers

47 lines (39 loc) 2.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.useEthscanBalance = void 0; var _react = _interopRequireWildcard(require("react")); var _ethScan = require("@mycrypto/eth-scan"); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /* eslint-disable require-jsdoc */ const useEthscanBalance = (addresses, nodeUrl, tokenSymbol = 'ETH', contractAddress = '') => { const [balance, setBalance] = (0, _react.useState)([]); const [loading, setLoading] = (0, _react.useState)(true); if (!addresses) return null; const data = []; (0, _react.useEffect)(() => { async function fetchEthBalance() { const ethBalances = contractAddress ? await (0, _ethScan.getEtherBalances)(nodeUrl, addresses, { contractAddress }) : await (0, _ethScan.getEtherBalances)(nodeUrl, addresses); Object.values(ethBalances).map((val, idx) => { if (val === 0n) return; data.push({ address: addresses[idx], token: tokenSymbol, balance: (Number(val) / 10 ** 18).toFixed(8), source: '' }); }); setBalance(data); setLoading(false); } fetchEthBalance(); }, [addresses]); return [loading, balance]; }; exports.useEthscanBalance = useEthscanBalance; var _default = useEthscanBalance; exports.default = _default;