staker-hooks
Advanced tools
Sorry, the diff of this file is too big to display
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports.default = exports.useTokensPrice = void 0; | ||
| var _react = _interopRequireWildcard(require("react")); | ||
| var _toLower2 = _interopRequireDefault(require("lodash/toLower")); | ||
| var _react2 = _interopRequireDefault(require("fast-deep-equal/react")); | ||
| var _coingecko_token_map = _interopRequireDefault(require("./coingecko_token_map.json")); | ||
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
| 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; } | ||
| const useTokensPrice = (tokens = [], fetch) => { | ||
| const [tokenPrices, setTokenPrices] = (0, _react.useState)([]); | ||
| const tokensRef = (0, _react.useRef)(tokens); | ||
| if (!(0, _react2.default)(tokensRef.current, tokens)) { | ||
| tokensRef.current = tokens; | ||
| } | ||
| const coingGeckoSimplePriceAPI = (0, _react.useCallback)(() => { | ||
| // console.log('tokens %O', TOKEN_MAP) | ||
| const tokenIdMap = tokens.map(symbol => _coingecko_token_map.default.find(entry => entry.symbol === (0, _toLower2.default)(symbol))).filter(entry => entry !== undefined); // console.log('tokenIdMap %O', tokenIdMap) | ||
| return tokenIdMap.length > 0 ? `https://api.coingecko.com/api/v3/simple/price?ids=${tokenIdMap.map(token => token.id).join('%2C')}&vs_currencies=usd` : ''; | ||
| }, [tokensRef.current]); | ||
| (0, _react.useEffect)(() => { | ||
| async function getPrices() { | ||
| const API = coingGeckoSimplePriceAPI(tokens); | ||
| if (API) { | ||
| const data = await fetch(API, { | ||
| method: 'GET', | ||
| headers: { | ||
| 'Content-Type': 'application/json' | ||
| } | ||
| }); | ||
| const result = data.json(); // console.log('json %O', result) | ||
| setTokenPrices(result); | ||
| } | ||
| } | ||
| if (Array.isArray(tokens) && tokens.length > 0) { | ||
| getPrices(); | ||
| } | ||
| }, [coingGeckoSimplePriceAPI, tokensRef.current]); | ||
| return [tokenPrices]; | ||
| }; | ||
| exports.useTokensPrice = useTokensPrice; | ||
| var _default = useTokensPrice; | ||
| exports.default = _default; |
Sorry, the diff of this file is too big to display
| import React, { useState, useEffect, useCallback, useRef } from 'react' | ||
| import _toLower from 'lodash/toLower' | ||
| import isDeepEqual from 'fast-deep-equal/react'; | ||
| import TOKEN_MAP from './coingecko_token_map.json' | ||
| export const useTokensPrice = (tokens = [], fetch) => { | ||
| const [tokenPrices, setTokenPrices] = useState([]) | ||
| const tokensRef = useRef(tokens); | ||
| if (!isDeepEqual(tokensRef.current, tokens)) { | ||
| tokensRef.current = tokens | ||
| } | ||
| const coingGeckoSimplePriceAPI = useCallback(() => { | ||
| // console.log('tokens %O', TOKEN_MAP) | ||
| const tokenIdMap = tokens | ||
| .map((symbol) => | ||
| TOKEN_MAP.find((entry) => entry.symbol === _toLower(symbol)) | ||
| ) | ||
| .filter((entry) => entry !== undefined) | ||
| // console.log('tokenIdMap %O', tokenIdMap) | ||
| return tokenIdMap.length > 0 | ||
| ? `https://api.coingecko.com/api/v3/simple/price?ids=${tokenIdMap | ||
| .map((token) => token.id) | ||
| .join('%2C')}&vs_currencies=usd` | ||
| : '' | ||
| }, [tokensRef.current]) | ||
| useEffect(() => { | ||
| async function getPrices() { | ||
| const API = coingGeckoSimplePriceAPI(tokens) | ||
| if (API) { | ||
| const data = await fetch(API, { | ||
| method: 'GET', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }) | ||
| const result = data.json() | ||
| // console.log('json %O', result) | ||
| setTokenPrices(result) | ||
| } | ||
| } | ||
| if (Array.isArray(tokens) && tokens.length > 0) { | ||
| getPrices() | ||
| } | ||
| }, [coingGeckoSimplePriceAPI, tokensRef.current]) | ||
| return [tokenPrices] | ||
| } | ||
| export default useTokensPrice |
+13
-0
@@ -31,2 +31,15 @@ "use strict"; | ||
| }); | ||
| }); | ||
| var _useTokensPrice = require("./useTokensPrice"); | ||
| Object.keys(_useTokensPrice).forEach(function (key) { | ||
| if (key === "default" || key === "__esModule") return; | ||
| if (key in exports && exports[key] === _useTokensPrice[key]) return; | ||
| Object.defineProperty(exports, key, { | ||
| enumerable: true, | ||
| get: function () { | ||
| return _useTokensPrice[key]; | ||
| } | ||
| }); | ||
| }); |
@@ -16,2 +16,6 @@ "use strict"; | ||
| var _react2 = _interopRequireDefault(require("fast-deep-equal/react")); | ||
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
| /* eslint-disable require-jsdoc */ | ||
@@ -21,3 +25,9 @@ const useEthscanBalance = (addresses, chainId = _stakerFreenodes.CHAIN_ETHEREUM) => { | ||
| const [loading, setLoading] = (0, _react.useState)(true); | ||
| const addrRef = (0, _react.useRef)(addresses); | ||
| if (!Array.isArray(addresses) || addresses.length === 0) return [false, []]; | ||
| if (!(0, _react2.default)(addrRef.current, addresses)) { | ||
| addrRef.current = addresses; | ||
| } | ||
| const nodeUrl = (0, _stakerFreenodes.getNodeURL)(chainId); | ||
@@ -45,4 +55,4 @@ const contractAddress = _stakerContracts.ETHSCAN_CONTRACT[chainId]; | ||
| fetchEthBalance(); // run once | ||
| }, []); | ||
| fetchEthBalance(); | ||
| }, [addrRef.current]); | ||
| return [loading, balance]; | ||
@@ -49,0 +59,0 @@ }; |
@@ -16,2 +16,6 @@ "use strict"; | ||
| var _react2 = _interopRequireDefault(require("fast-deep-equal/react")); | ||
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
| /* eslint-disable require-jsdoc */ | ||
@@ -21,3 +25,9 @@ const useEthscanTokensBalance = (addresses, chainId = _stakerFreenodes.CHAIN_ETHEREUM, tokenMap = []) => { | ||
| const [loading, setLoading] = (0, _react.useState)(true); | ||
| const addrRef = (0, _react.useRef)(addresses); | ||
| if (!Array.isArray(addresses) || addresses.length === 0) return [false, []]; | ||
| if (!(0, _react2.default)(addrRef.current, addresses)) { | ||
| addrRef.current = addresses; | ||
| } | ||
| const nodeUrl = (0, _stakerFreenodes.getNodeURL)(chainId); | ||
@@ -50,3 +60,3 @@ const contractAddress = _stakerContracts.ETHSCAN_CONTRACT[chainId]; | ||
| } | ||
| }, []); | ||
| }, [addrRef.current]); | ||
| return [loading, balance]; | ||
@@ -53,0 +63,0 @@ }; |
+6
-8
| { | ||
| "name": "staker-hooks", | ||
| "version": "0.43.2", | ||
| "version": "0.44.0", | ||
| "description": "React Hooks for Ethereum and compatible chains", | ||
@@ -21,6 +21,3 @@ "files": [ | ||
| "ethereum", | ||
| "xdai", | ||
| "bsc", | ||
| "matic", | ||
| "polygon" | ||
| "coingecko" | ||
| ], | ||
@@ -35,4 +32,5 @@ "author": "Fred Lin <gasolin+saihubot@gmail.com>", | ||
| "@mycrypto/eth-scan": "^3.3.1", | ||
| "staker-contracts": "^0.43.2", | ||
| "staker-freenodes": "^0.43.1" | ||
| "fast-deep-equal": "^3.1.3", | ||
| "staker-contracts": "^0.44.0", | ||
| "staker-freenodes": "^0.44.0" | ||
| }, | ||
@@ -55,3 +53,3 @@ "peerDependencies": { | ||
| }, | ||
| "gitHead": "73bcdf37c115ee071f3233a8f1bb32d6c5ba249a" | ||
| "gitHead": "e7270874144e5c94fa8fdefb6e7e31c302ad11d4" | ||
| } |
+18
-1
@@ -15,3 +15,3 @@ # staker-hooks | ||
| ## Usage | ||
| ## Get native and token balances | ||
@@ -49,2 +49,19 @@ ``` | ||
| ``` | ||
| ## Get tokens price | ||
| ``` | ||
| import React from 'react' | ||
| import {useTokensPrice} from 'staker-hooks'; | ||
| export const Balances = () => { | ||
| const [prices] = useTokensPrice(['ETH','BTC']) | ||
| return ( | ||
| <Text> | ||
| {JSON.stringify(prices)} | ||
| </Text> | ||
| ) | ||
| } | ||
| ``` | ||
| ## License | ||
@@ -51,0 +68,0 @@ |
+1
-0
| export * from './useEthscanBalance' | ||
| export * from './useEthscanTokensBalance' | ||
| export * from './useTokensPrice' |
| /* eslint-disable require-jsdoc */ | ||
| import {useEffect, useState} from 'react'; | ||
| import {useEffect, useRef, useState} from 'react'; | ||
| import {getEtherBalances} from '@mycrypto/eth-scan'; | ||
| import {getNodeURL, CHAIN_ETHEREUM} from 'staker-freenodes'; | ||
| import {CHAIN_BASETOKEN, ETHSCAN_CONTRACT} from 'staker-contracts'; | ||
| import isDeepEqual from 'fast-deep-equal/react'; | ||
@@ -10,3 +11,9 @@ export const useEthscanBalance = (addresses, chainId = CHAIN_ETHEREUM) => { | ||
| const [loading, setLoading] = useState(true); | ||
| const addrRef = useRef(addresses); | ||
| if (!Array.isArray(addresses) || addresses.length === 0) return [false, []]; | ||
| if (!isDeepEqual(addrRef.current, addresses)) { | ||
| addrRef.current = addresses | ||
| } | ||
| const nodeUrl = getNodeURL(chainId); | ||
@@ -29,2 +36,3 @@ const contractAddress = ETHSCAN_CONTRACT[chainId]; | ||
| }); | ||
| setBalance(data); | ||
@@ -35,4 +43,3 @@ setLoading(false); | ||
| fetchEthBalance(); | ||
| // run once | ||
| }, []); | ||
| }, [addrRef.current]); | ||
| return [loading, balance]; | ||
@@ -39,0 +46,0 @@ }; |
| /* eslint-disable require-jsdoc */ | ||
| import {useEffect, useState} from 'react'; | ||
| import {useEffect, useRef, useState} from 'react'; | ||
| import {getTokensBalances} from '@mycrypto/eth-scan'; | ||
| import {getNodeURL, CHAIN_ETHEREUM} from 'staker-freenodes' | ||
| import {ETHSCAN_CONTRACT, TOKEN_CONTRACTS} from 'staker-contracts' | ||
| import isDeepEqual from 'fast-deep-equal/react'; | ||
@@ -10,3 +11,9 @@ export const useEthscanTokensBalance = (addresses, chainId = CHAIN_ETHEREUM, tokenMap = []) => { | ||
| const [loading, setLoading] = useState(true); | ||
| const addrRef = useRef(addresses); | ||
| if (!Array.isArray(addresses) || addresses.length === 0) return [false, []]; | ||
| if (!isDeepEqual(addrRef.current, addresses)) { | ||
| addrRef.current = addresses | ||
| } | ||
| const nodeUrl = getNodeURL(chainId); | ||
@@ -46,3 +53,3 @@ const contractAddress = ETHSCAN_CONTRACT[chainId]; | ||
| } | ||
| }, []); | ||
| }, [addrRef.current]); | ||
@@ -49,0 +56,0 @@ return [loading, balance]; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Network access
Supply chain riskThis module accesses the network.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
767635
6744.11%13
44.44%3215
1583.25%72
30.91%5
25%1
Infinity%5
Infinity%+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
Updated
Updated