UNPKG

@lido-sdk/react

Version:

This project is being slowly deprecated and may not receive further updates. Check out [modern Lido SDK](https://github.com/lidofinance/lido-ethereum-sdk/pulls) to access latest functionality. It is actively maintained and is built for interacting with Li

57 lines (52 loc) 2.13 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var tslib_es6 = require('../node_modules/tslib/tslib.es6.js'); var contracts = require('@lido-sdk/contracts'); var react = require('react'); var useSDK = require('./useSDK.js'); var useMountedState = require('./useMountedState.js'); const useTokenToWallet = (address, image) => { const [loading, setLoading] = useMountedState.useMountedState(false); const { providerRpc, providerWeb3, onError } = useSDK.useSDK(); const handleAdd = react.useCallback(() => tslib_es6.__awaiter(void 0, void 0, void 0, function* () { const provider = providerWeb3 === null || providerWeb3 === void 0 ? void 0 : providerWeb3.provider; if (!(provider === null || provider === void 0 ? void 0 : provider.request)) return false; try { setLoading(true); const contract = contracts.getERC20Contract(address, providerRpc); const [symbol, decimals] = yield Promise.all([ contract.symbol(), contract.decimals(), ]); const result = yield provider.request({ method: 'wallet_watchAsset', params: { type: 'ERC20', options: { address, symbol, decimals, image, }, // eslint-disable-next-line @typescript-eslint/no-explicit-any }, }); return !!result; } catch (error) { onError(error); return false; } finally { setLoading(false); } }), [address, image, providerWeb3, providerRpc, setLoading, onError]); const canAdd = !!(providerWeb3 === null || providerWeb3 === void 0 ? void 0 : providerWeb3.provider.isMetaMask); const addToken = canAdd ? handleAdd : undefined; return { addToken, loading, }; }; exports.useTokenToWallet = useTokenToWallet;