UNPKG

@mojito-inc/connect-wallet

Version:

Connecting wallet via metamask, wallet connect, email

132 lines (129 loc) 6.11 kB
import { __awaiter } from '../_virtual/_tslib.js'; import { ethers } from 'ethers'; import { Connection, clusterApiUrl, PublicKey, LAMPORTS_PER_SOL } from '@solana/web3.js'; import { truncateDecimal } from './truncateDecimal.utils.js'; import { getABIFile } from './getABIFile.utils.js'; import { getCurrencyAddress } from './getCurrencyAddress.utils.js'; import { WalletProviderType } from '../constant/index.js'; function getNonNativeCurrencyBalancePaper(provider, currencyContractAddress, currency) { return __awaiter(this, void 0, void 0, function* () { try { const signerAddress = yield provider.getAddress(); const abiFile = getABIFile(currency); const contract = new ethers.Contract(currencyContractAddress, abiFile, provider); const balanceOf = yield contract.balanceOf(signerAddress); const balance = balanceOf / 1e18; return balance; } catch (error) { return 0; } }); } function getNonNativeCurrencyBalance(provider, currencyContractAddress, currency) { return __awaiter(this, void 0, void 0, function* () { try { const signer = provider.getSigner(); const signerAddress = yield signer.getAddress(); const abiFile = getABIFile(currency); const contract = new ethers.Contract(currencyContractAddress, abiFile, signer); const balanceOf = yield contract.balanceOf(signerAddress); const balance = balanceOf / 1e18; return balance; } catch (error) { return 0; } }); } const getBalance = (provider, walletAddress, chainId, providerType) => __awaiter(void 0, void 0, void 0, function* () { var _a; try { const currency = (chainId === 80001 || chainId === 137) ? 'WMATIC' : 'WETH'; const contractAddress = getCurrencyAddress(chainId); let balanceData = null; let nonNativeBalance = null; if (providerType === WalletProviderType.EMAIL) { balanceData = yield ((_a = provider === null || provider === void 0 ? void 0 : provider.provider) === null || _a === void 0 ? void 0 : _a.getBalance(walletAddress)); nonNativeBalance = yield getNonNativeCurrencyBalancePaper(provider, contractAddress, currency); } else { nonNativeBalance = yield getNonNativeCurrencyBalance(provider, contractAddress, currency); balanceData = yield (provider === null || provider === void 0 ? void 0 : provider.getBalance(walletAddress)); } const nativeBalance = ethers.utils.formatEther(String(balanceData)); return { native: +truncateDecimal(nativeBalance), nonNative: +truncateDecimal(nonNativeBalance), }; } catch (error) { return { native: 0, nonNative: 0, }; } }); const getNativeSolanaBalance = (address, network, customRPCUrl) => __awaiter(void 0, void 0, void 0, function* () { const connection = new Connection(customRPCUrl || clusterApiUrl(network)); const publicKey = new PublicKey(address); const balance = yield connection.getBalance(publicKey); return balance / LAMPORTS_PER_SOL; }); const getVinTokenAddress = (network, customRPCUrl) => { var _a, _b, _c; if (customRPCUrl) { if ((_a = customRPCUrl === null || customRPCUrl === void 0 ? void 0 : customRPCUrl.includes) === null || _a === void 0 ? void 0 : _a.call(customRPCUrl, 'devnet')) { return 'vinWUwHv4uuMPRD8x5YhYbHpFDudra2pmJFkFCxzhpz'; } if ((_b = customRPCUrl === null || customRPCUrl === void 0 ? void 0 : customRPCUrl.includes) === null || _b === void 0 ? void 0 : _b.call(customRPCUrl, 'mainnet')) { return '6B2X4NmSsmkiT8ytFEVt15igRSgsKNGZ3j3WWeidupE8'; } if ((_c = customRPCUrl === null || customRPCUrl === void 0 ? void 0 : customRPCUrl.includes) === null || _c === void 0 ? void 0 : _c.call(customRPCUrl, 'testnet')) { return 'vinWUwHv4uuMPRD8x5YhYbHpFDudra2pmJFkFCxzhpz'; } } if (network === 'devnet') { return 'vinWUwHv4uuMPRD8x5YhYbHpFDudra2pmJFkFCxzhpz'; } if (network === 'mainnet-beta') { return '6B2X4NmSsmkiT8ytFEVt15igRSgsKNGZ3j3WWeidupE8'; } if (network === 'testnet') { // replace testnet token address return 'vinWUwHv4uuMPRD8x5YhYbHpFDudra2pmJFkFCxzhpz'; } return 'vinWUwHv4uuMPRD8x5YhYbHpFDudra2pmJFkFCxzhpz'; }; const getNonNativeSolanaBalance = (address, network, customRPCUrl) => __awaiter(void 0, void 0, void 0, function* () { const connection = new Connection(customRPCUrl || clusterApiUrl(network)); const publicKey = new PublicKey(address); const vinTokenAddress = getVinTokenAddress(network, customRPCUrl); const tokenAccounts = yield connection.getParsedTokenAccountsByOwner(publicKey, { mint: new PublicKey(vinTokenAddress), }); if (tokenAccounts.value.length === 0) { return 0; } const accountData = tokenAccounts.value[0].account.data.parsed.info; const tokenBalance = accountData.tokenAmount.uiAmount; return tokenBalance !== null && tokenBalance !== void 0 ? tokenBalance : 0; }); const getSolanaBalance = (address, network, customRPCUrl) => __awaiter(void 0, void 0, void 0, function* () { try { // get Balance for solana const native = yield getNativeSolanaBalance(address, network !== null && network !== void 0 ? network : 'devnet', customRPCUrl); const nonNative = yield getNonNativeSolanaBalance(address, network !== null && network !== void 0 ? network : 'devnet', customRPCUrl); return { native, nonNative, }; } catch (err) { return { native: 0, nonNative: 0, }; } }); export { getBalance, getNonNativeCurrencyBalance, getNonNativeCurrencyBalancePaper, getSolanaBalance };