Skip to content

Commit

Permalink
Merge pull request #83 from skalenetwork/hotfix/alter-unwrap-flow
Browse files Browse the repository at this point in the history
Add error handling, alter transfer+unwrap token flow
  • Loading branch information
dmytrotkk authored Nov 16, 2022
2 parents fa516a3 + 3b135ab commit 6f9f924
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 96 deletions.
107 changes: 67 additions & 40 deletions src/components/Widget/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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({});
}
Expand Down
Empty file removed src/core/actions/.keep
Empty file.
2 changes: 1 addition & 1 deletion src/core/actions/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/core/actions/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/actions/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('*');
Expand Down
28 changes: 20 additions & 8 deletions src/core/sfuel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/**
* @license
* SKALE Metaport
Expand All @@ -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;
}
Expand All @@ -49,13 +55,19 @@ export async function getSFuelData(
chainName: string,
web3: Web3,
address: string
): Promise<object>{
const minSfuelWei = getMinSfuelWei(chainsMetadata, chainName);
const balance = await getSfuelBalance(web3, address);
return {
faucetUrl: getFaucetUrl(chainsMetadata, chainName),
minSfuelWei,
balance,
ok: Number(balance) >= Number(minSfuelWei)
): Promise<object> {
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 {};
}
}
2 changes: 1 addition & 1 deletion src/core/tokens/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/core/tokens/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
63 changes: 35 additions & 28 deletions src/core/tokens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,43 +47,50 @@ export async function getAvailableTokens(
chainName1: string,
chainName2: string,
configTokens: interfaces.TokensMap,
force: boolean,
autoLookup: boolean
): Promise<interfaces.TokenDataTypesMap> {
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;
}

Expand Down
Loading

0 comments on commit 6f9f924

Please sign in to comment.