From 644fd2668e0ff5a63b6618c27768cfbbebd95d93 Mon Sep 17 00:00:00 2001 From: Dmytro Date: Mon, 23 Oct 2023 16:41:09 +0100 Subject: [PATCH] metaport#192 handle small transfer amounts --- src/components/ErrorMessage.tsx | 32 +++++++++++------------ src/core/actions/action.ts | 8 ++---- src/core/actions/checks.ts | 12 ++++++++- src/core/actions/erc20.ts | 5 ++-- src/store/MetaportStore.ts | 45 ++++++++++++++++++++++----------- 5 files changed, 62 insertions(+), 40 deletions(-) diff --git a/src/components/ErrorMessage.tsx b/src/components/ErrorMessage.tsx index 88b4ac7..0912fed 100644 --- a/src/components/ErrorMessage.tsx +++ b/src/components/ErrorMessage.tsx @@ -54,24 +54,24 @@ export default function Error(props: { errorMessage: ErrorMessage }) {

Logs are available in your browser's developer console

- -
-
- + {props.errorMessage.showTips ?
+
+
+ +
+

+ When transferring from SKALE to Ethereum Mainnet, there are frequency limitations. +

-

- When transferring from SKALE to Ethereum Mainnet, there are frequency limitations. -

-
-
-
- +
+
+ +
+

+ Sometimes transfers may take more time than expected. +

-

- Sometimes transfers may take more time than expected. -

-
- +
: null} ${this.chainName2}`) + const amountWei = toWei(this.amount, this.token.meta.decimals) externalEvents.actionStateUpdated({ actionName: this.constructor.name, actionState: currentState, @@ -175,7 +171,7 @@ export class Action { chainName2: this.chainName2, address: this.address, amount: this.amount, - amountWei: this.amountWei, + amountWei: amountWei, tokenId: this.tokenId }, transactionHash, diff --git a/src/core/actions/checks.ts b/src/core/actions/checks.ts index 1470b8b..97bed94 100644 --- a/src/core/actions/checks.ts +++ b/src/core/actions/checks.ts @@ -25,7 +25,7 @@ import debug from 'debug' import { Contract } from 'ethers' import { MainnetChain, SChain } from '@skalenetwork/ima-js' -import { fromWei } from '../convertation' +import { fromWei, toWei } from '../convertation' import { TokenData } from '../dataclasses/TokenData' import * as interfaces from '../interfaces' import { addressesEqual } from '../helper' @@ -73,6 +73,16 @@ export async function checkERC20Balance( ): Promise { const checkRes: interfaces.CheckRes = { res: false } if (!amount || Number(amount) === 0) return checkRes + try { + toWei(amount, tokenData.meta.decimals) + } catch (err) { + if (err.fault && err.fault === 'underflow') { + checkRes.msg = 'The amount is too small' + } else { + checkRes.msg = 'Incorrect amount' + } + return checkRes + } try { const balance = await tokenContract.balanceOf(address) log(`address: ${address}, balanceWei: ${balance}, amount: ${amount}`) diff --git a/src/core/actions/erc20.ts b/src/core/actions/erc20.ts index 5985f32..a1a13d7 100644 --- a/src/core/actions/erc20.ts +++ b/src/core/actions/erc20.ts @@ -116,9 +116,10 @@ export class WrapSFuelERC20S extends Action { async execute() { log('WrapSFuelERC20S:execute - starting') this.updateState('wrap') + const amountWei = toWei(this.amount, this.token.meta.decimals) const tx = await this.sChain1.erc20.fundExit(this.token.keyname, { address: this.address, - value: this.amountWei + value: amountWei }) const block = await this.sChain1.provider.getBlock(tx.blockNumber) this.updateState('wrapDone', tx.hash, block.timestamp) @@ -212,7 +213,7 @@ export class UnWrapERC20 extends Action { this.updateState('unwrapDone', tx.hash, block.timestamp) } - async preAction() {} + async preAction() { } } export class UnWrapERC20S extends Action { diff --git a/src/store/MetaportStore.ts b/src/store/MetaportStore.ts index f8e05c0..934ab9b 100644 --- a/src/store/MetaportStore.ts +++ b/src/store/MetaportStore.ts @@ -191,21 +191,36 @@ export const useMetaportStore = create()((set, get) => ({ loading: true, btnText: 'Checking balance...' }) - const stepMetadata = get().stepsMetadata[get().currentStep] - const actionClass = ACTIONS[stepMetadata.type] - await new actionClass( - get().mpc, - stepMetadata.from, - stepMetadata.to, - address, - amount, - get().tokenId, - get().token, - get().setAmountErrorMessage, - get().setBtnText, - null, - null - ).preAction() + try { + const stepMetadata = get().stepsMetadata[get().currentStep] + const actionClass = ACTIONS[stepMetadata.type] + await new actionClass( + get().mpc, + stepMetadata.from, + stepMetadata.to, + address, + amount, + get().tokenId, + get().token, + get().setAmountErrorMessage, + get().setBtnText, + null, + null + ).preAction() + } catch (err) { + console.error(err) + const msg = err.code && err.fault ? `${err.code} - ${err.fault}` : 'Something went wrong' + set({ + errorMessage: new dataclasses.TransactionErrorMessage( + err.message, + get().errorMessageClosedFallback, + msg, + false + ) + }) + } finally { + set({ loading: false }) + } } set({ loading: false }) },