From 3b135ab1c32645cccae4f731f5244bb596ea630f Mon Sep 17 00:00:00 2001 From: Dmytro Date: Wed, 16 Nov 2022 15:59:08 +0000 Subject: [PATCH] Add error hanlding - fix metaport#80 --- src/components/Widget/Widget.tsx | 107 +++++++++++++++++++------------ src/core/actions/.keep | 0 src/core/actions/action.ts | 2 +- src/core/actions/erc20.ts | 5 +- src/core/actions/eth.ts | 2 +- src/core/sfuel.ts | 28 +++++--- src/core/tokens/erc20.ts | 2 +- src/core/tokens/helper.ts | 2 +- src/core/tokens/index.ts | 63 ++++++++++-------- src/core/tokens/s2s.ts | 25 ++++---- tsconfig.json | 4 +- 11 files changed, 144 insertions(+), 96 deletions(-) delete mode 100644 src/core/actions/.keep diff --git a/src/components/Widget/Widget.tsx b/src/components/Widget/Widget.tsx index fefab67..00ae508 100644 --- a/src/components/Widget/Widget.tsx +++ b/src/components/Widget/Widget.tsx @@ -202,24 +202,29 @@ export function Widget(props) { async function tokenLookup() { setLoadingTokens(true); - let tokens = await getAvailableTokens( - mainnet, - sChain1, - sChain2, - chainName1, - chainName2, - configTokens, - false, - props.autoLookup - ); - await getTokenBalances( - tokens, - chainName1, - mainnet, - sChain1, - address - ); - setAvailableTokens(tokens); + try { + let tokens = await getAvailableTokens( + mainnet, + sChain1, + sChain2, + chainName1, + chainName2, + configTokens, + props.autoLookup + ); + await getTokenBalances( + tokens, + chainName1, + mainnet, + sChain1, + address + ); + setAvailableTokens(tokens); + } catch (err) { + log('_MP_ERROR: tokenLookup failed'); + log(err); + } + updateBalanceHandler(); setLoadingTokens(false); } @@ -249,7 +254,7 @@ export function Widget(props) { let balance = await sChain2.getERC20Balance(tokenContract, address); return externalEvents.balance(tokenSymbol, chainName2, balance); } - console.error('Error: can request balance only for active chains!'); // TODO: replace with error! + console.error('_MP_ERROR: can request balance only for active chains!'); // TODO: replace with error! } function requestBalanceHandler(e) { @@ -381,8 +386,11 @@ export function Widget(props) { }, [actionSteps, activeStep, amount, tokenId]); useEffect(() => { - const isUwrapAction = token && token.unwrappedSymbol && token.clone && activeStep === 2; // TODO: tmp fix for unwrap + // TODO: tmp fix for unwrap + const isUnwrapActionSteps = activeStep === 2 || activeStep === 3; + const isUwrapAction = token && token.unwrappedSymbol && token.clone && isUnwrapActionSteps; if (extChainId && chainId && extChainId !== chainId && !isUwrapAction) { + log('_MP_INFO: setting WrongNetworkMessage'); setErrorMessage(new WrongNetworkMessage(enforceMetamaskNetwork)); } else { setErrorMessage(undefined); @@ -412,13 +420,18 @@ export function Widget(props) { } async function checkWrappedTokens() { - log('Running checkWrappedTokens'); - const wrappedTokens = await getWrappedTokens(sChain1, chainName1, configTokens, address); - if (Object.entries(wrappedTokens).length === 0 && operationType !== OperationType.transfer) { - setAmount(''); - setOperationType(OperationType.transfer); + log('_MP_INFO: Running checkWrappedTokens'); + try { + const wrappedTokens = await getWrappedTokens(sChain1, chainName1, configTokens, address); + if (Object.entries(wrappedTokens).length === 0 && operationType !== OperationType.transfer) { + setAmount(''); + setOperationType(OperationType.transfer); + } + setWrappedTokens(wrappedTokens); + } catch (err) { + log('_MP_ERROR: checkWrappedTokens failed!'); + log(err); } - setWrappedTokens(wrappedTokens); } function setDefaultWrappedToken() { @@ -547,24 +560,38 @@ export function Widget(props) { async function initSFuelData() { if (sChain1 && chainName1) { - log(`initSFuelData - chain1`); - setSFuelData1(await getSFuelData( - props.chainsMetadata, - chainName1, - sChain1.web3, - address - )); + log(`_MP_INFO: initSFuelData - ${chainName1}`); + try { + const sFuelData1 = await getSFuelData( + props.chainsMetadata, + chainName1, + sChain1.web3, + address + ); + setSFuelData1(sFuelData1); + } catch (err) { + log(`_MP_ERROR: getSFuelData for ${chainName1} failed`); + log(err); + setSFuelData1({}); + } } else { setSFuelData1({}); } if (sChain2 && chainName2) { - log(`initSFuelData - chain2`); - setSFuelData2(await getSFuelData( - props.chainsMetadata, - chainName2, - sChain2.web3, - address - )); + log(`_MP_INFO: initSFuelData - ${chainName2}`); + try { + const sFuelData2 = await getSFuelData( + props.chainsMetadata, + chainName2, + sChain2.web3, + address + ); + setSFuelData2(sFuelData2); + } catch (err) { + log(`_MP_ERROR: getSFuelData for ${chainName2} failed`); + log(err); + setSFuelData2({}); + } } else { setSFuelData2({}); } diff --git a/src/core/actions/.keep b/src/core/actions/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/src/core/actions/action.ts b/src/core/actions/action.ts index 2075969..0f54261 100644 --- a/src/core/actions/action.ts +++ b/src/core/actions/action.ts @@ -24,7 +24,7 @@ import { MainnetChain, SChain } from '@skalenetwork/ima-js'; import TokenData from '../dataclasses/TokenData'; import { externalEvents } from '../events'; -import { toWei, fromWei } from '../convertation'; +import { toWei } from '../convertation'; export type ActionType = typeof Action; diff --git a/src/core/actions/erc20.ts b/src/core/actions/erc20.ts index d3ac901..3d3345d 100644 --- a/src/core/actions/erc20.ts +++ b/src/core/actions/erc20.ts @@ -225,7 +225,7 @@ export class UnWrapERC20S2S extends Action { static buttonText = 'Unwrap' static loadingText = 'Unwrapping' async execute() { - log('execute: UnWrapERC20S2S'); + log('UnWrapERC20S2S: execute'); await this.switchMetamaskChain(false); try { const amountWei = toWei(this.amount, this.tokenData.decimals); @@ -236,7 +236,8 @@ export class UnWrapERC20S2S extends Action { ); externalEvents.unwrapComplete(tx, this.chainName2, this.tokenData.keyname); } finally { - await this.switchMetamaskChain(true); + log('UnWrapERC20S2S: switchMetamaskChain back'); + this.switchMetamaskChain(true); } } diff --git a/src/core/actions/eth.ts b/src/core/actions/eth.ts index 486b01a..d43715f 100644 --- a/src/core/actions/eth.ts +++ b/src/core/actions/eth.ts @@ -27,7 +27,7 @@ import debug from 'debug'; import { externalEvents } from '../events'; import { toWei } from '../convertation'; import { TransferAction, Action } from './action'; -import { checkEthBalance, checkERC20Balance } from './checks'; +import { checkEthBalance } from './checks'; debug.enable('*'); diff --git a/src/core/sfuel.ts b/src/core/sfuel.ts index c54534b..e8a645b 100644 --- a/src/core/sfuel.ts +++ b/src/core/sfuel.ts @@ -1,3 +1,4 @@ + /** * @license * SKALE Metaport @@ -22,9 +23,14 @@ */ import Web3 from 'web3'; +import debug from 'debug'; import { DEFAULT_MIN_SFUEL_WEI } from './constants'; +debug.enable('*'); +const log = debug('metaport:Widget'); + + function getFaucetUrl(chainsMetadata: object, chainName: string): string { if (chainsMetadata && chainsMetadata[chainName]) return chainsMetadata[chainName].faucetUrl; } @@ -49,13 +55,19 @@ export async function getSFuelData( chainName: string, web3: Web3, address: string -): Promise{ - const minSfuelWei = getMinSfuelWei(chainsMetadata, chainName); - const balance = await getSfuelBalance(web3, address); - return { - faucetUrl: getFaucetUrl(chainsMetadata, chainName), - minSfuelWei, - balance, - ok: Number(balance) >= Number(minSfuelWei) +): Promise { + try { + const minSfuelWei = getMinSfuelWei(chainsMetadata, chainName); + const balance = await getSfuelBalance(web3, address); + return { + faucetUrl: getFaucetUrl(chainsMetadata, chainName), + minSfuelWei, + balance, + ok: Number(balance) >= Number(minSfuelWei) + } + } catch (e) { + log(`ERROR: getSFuelData for ${chainName} failed!`); + log(e); + return {}; } } diff --git a/src/core/tokens/erc20.ts b/src/core/tokens/erc20.ts index c0ad899..001b14a 100644 --- a/src/core/tokens/erc20.ts +++ b/src/core/tokens/erc20.ts @@ -105,7 +105,7 @@ export async function getWrappedTokens( log('Checking wrapped tokens...'); const wrappedTokens: interfaces.TokenDataTypesMap = getEmptyTokenDataMap(); if (configTokens && configTokens[chainName] && configTokens[chainName].erc20) { - for (const [symbol, configToken] of Object.entries(configTokens[chainName].erc20)) { + for (const [_symbol, configToken] of Object.entries(configTokens[chainName].erc20)) { if (!configToken.wraps) continue; const tokenKeyname = getTokenKeyname(configToken.symbol, configToken.address); const tokenContract = initContract('erc20wrap', configToken.address, sChain.web3); diff --git a/src/core/tokens/helper.ts b/src/core/tokens/helper.ts index d663b5c..4a95a11 100644 --- a/src/core/tokens/helper.ts +++ b/src/core/tokens/helper.ts @@ -33,7 +33,7 @@ export function getEmptyTokenDataMap(): interfaces.TokenDataTypesMap { export function getAvailableTokenNumers(availableTokens): number[] { - return Object.entries(availableTokens).map(([key, value]) => Object.entries(value).length); + return Object.entries(availableTokens).map(([_key, value]) => Object.entries(value).length); } diff --git a/src/core/tokens/index.ts b/src/core/tokens/index.ts index 2ad12a9..4681419 100644 --- a/src/core/tokens/index.ts +++ b/src/core/tokens/index.ts @@ -47,43 +47,50 @@ export async function getAvailableTokens( chainName1: string, chainName2: string, configTokens: interfaces.TokensMap, - force: boolean, autoLookup: boolean ): Promise { log('Collecting available tokens for ' + chainName1 + ' → ' + chainName2); const availableTokens = getEmptyTokenDataMap(); - log('Adding ETH to availableTokens'); - await addETHToken( - chainName1, - chainName2, - configTokens, - availableTokens - ); - if (isMainnet(chainName1) || isMainnet(chainName2)) { - log('Going to add M2S ERC20 tokens') - const sChain = isMainnet(chainName1) ? sChain2 : sChain1; - const schainName = isMainnet(chainName1) ? chainName2 : chainName1; - await addM2STokens( - mainnet, - sChain, - schainName, - configTokens, - availableTokens, - autoLookup - ); - } else { - await addS2STokens( - sChain1, - sChain2, + try { + log('Adding ETH to availableTokens'); + await addETHToken( chainName1, chainName2, configTokens, - availableTokens, - force + availableTokens ); + if (isMainnet(chainName1) || isMainnet(chainName2)) { + log('Going to add M2S ERC20 tokens') + const sChain = isMainnet(chainName1) ? sChain2 : sChain1; + const schainName = isMainnet(chainName1) ? chainName2 : chainName1; + await addM2STokens( + mainnet, + sChain, + schainName, + configTokens, + availableTokens, + autoLookup + ); + } else { + await addS2STokens( + sChain1, + sChain2, + chainName1, + chainName2, + configTokens, + availableTokens + ); + } + log('availableTokens'); + log(availableTokens); + } catch (e: unknown) { + log('ERROR: Something went wrong during getAvailableTokens procedure'); + if (typeof e === "string") { + log(e.toUpperCase()); + } else if (e instanceof Error) { + log(e.message); + } } - log('availableTokens'); - log(availableTokens); return availableTokens; } diff --git a/src/core/tokens/s2s.ts b/src/core/tokens/s2s.ts index a3c7dec..b8f058b 100644 --- a/src/core/tokens/s2s.ts +++ b/src/core/tokens/s2s.ts @@ -24,7 +24,7 @@ import debug from 'debug'; -import { SChain, MainnetChain } from '@skalenetwork/ima-js'; +import { SChain } from '@skalenetwork/ima-js'; import { initContract } from '../core'; import * as interfaces from '../interfaces/index'; @@ -44,7 +44,6 @@ export async function addS2STokens( chainName2: string, configTokens: interfaces.TokensMap, availableTokens: interfaces.TokenDataTypesMap, - force: boolean ): Promise { log('Add S2S Tokens'); await collectS2STokens( @@ -53,7 +52,6 @@ export async function addS2STokens( chainName1, configTokens, availableTokens, - force, false ); await collectS2STokens( @@ -62,7 +60,6 @@ export async function addS2STokens( chainName2, configTokens, availableTokens, - force, true ); } @@ -74,7 +71,6 @@ async function collectS2STokens( chainName: string, configTokens: interfaces.TokensMap, availableTokens: interfaces.TokenDataTypesMap, - force: boolean, isClone: boolean ): Promise { if (!configTokens[chainName]) return; @@ -88,7 +84,6 @@ async function collectS2STokens( configTokens[chainName][tokenType][tokenKeyname], availableTokens, isClone, - force, tokenType as TokenType ); } @@ -103,7 +98,6 @@ async function addTokenData( configToken: interfaces.Token, availableTokens: interfaces.TokenDataTypesMap, isClone: boolean, - force: boolean, tokenType: TokenType ): Promise { const cloneAddress = await getCloneAddress( @@ -152,12 +146,17 @@ async function getCloneAddress( tokenType: TokenType ): Promise { log(`Getting clone address for ${originTokenAddress} on a chain`); - const tokenCloneAddress = await sChain[tokenType].getTokenCloneAddress( - originTokenAddress, - originChainName - ); - if (tokenCloneAddress === ZERO_ADDRESS) return; - return tokenCloneAddress; + try { + const tokenCloneAddress = await sChain[tokenType].getTokenCloneAddress( + originTokenAddress, + originChainName + ); + if (tokenCloneAddress === ZERO_ADDRESS) return; + return tokenCloneAddress; + } catch (e) { + log(`getCloneAddress for ${originTokenAddress} - ${originChainName} failed`); + log(e); + } } diff --git a/tsconfig.json b/tsconfig.json index 2833b81..1f69af9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,7 +14,9 @@ "esModuleInterop": true, "baseUrl": "src", "types": ["node", "webpack-env", "mocha"], - "typeRoots": ["./src/types"] + "typeRoots": ["./src/types"], + "noUnusedLocals": false, /* Report errors on unused locals. */ + "noUnusedParameters": false /* Report errors on unused parameters. */ }, "include": ["src/**/*", "index.ts", "src/types/custom.d.ts"], "exclude": [