From 2da4a8dda9da026c1b85166feff77f3ffb84fe55 Mon Sep 17 00:00:00 2001 From: Dmytro Date: Tue, 29 Oct 2024 17:45:57 +0000 Subject: [PATCH] Update community pool logic, remove legacy components --- package.json | 4 +-- skale-network | 2 +- src/components/CommunityPool.tsx | 7 +++-- src/components/TransferETA.tsx | 53 -------------------------------- src/components/TransferETF.tsx | 50 ------------------------------ src/core/community_pool.ts | 16 +++++++--- src/core/constants.ts | 6 ++-- src/core/helper.ts | 12 +++++++- src/index.ts | 5 --- 9 files changed, 34 insertions(+), 121 deletions(-) delete mode 100644 src/components/TransferETA.tsx delete mode 100644 src/components/TransferETF.tsx diff --git a/package.json b/package.json index d40d2d2..d704a49 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@skalenetwork/metaport", - "version": "3.0.1", + "version": "3.1.0", "description": "SKALE Metaport Widget", "keywords": [ "skale", @@ -101,4 +101,4 @@ "prettier -w" ] } -} +} \ No newline at end of file diff --git a/skale-network b/skale-network index 9a1ec68..d3fd0ca 160000 --- a/skale-network +++ b/skale-network @@ -1 +1 @@ -Subproject commit 9a1ec681673ec392389e09a1a6abbda9a1c0e9d7 +Subproject commit d3fd0ca5e4581a8e57123c780f903b2c0e4d52d6 diff --git a/src/components/CommunityPool.tsx b/src/components/CommunityPool.tsx index 8cecfb4..ccfeade 100644 --- a/src/components/CommunityPool.tsx +++ b/src/components/CommunityPool.tsx @@ -44,6 +44,7 @@ import { fromWei } from '../core/convertation' import { withdraw, recharge } from '../core/community_pool' import { BALANCE_UPDATE_INTERVAL_MS, + COMMUNITY_POOL_DECIMALS, DEFAULT_ERC20_DECIMALS, MINIMUM_RECHARGE_AMOUNT } from '../core/constants' @@ -126,9 +127,9 @@ export default function CommunityPool() { if (loading === 'recharge') return 'Recharging...' if (loading === 'activate') return 'Activating account...' if (Number(amount) > Number(accountBalanceEther)) return 'Insufficient ETH balance' + if (amount === '' || amount === '0' || !amount) return 'Enter an amount' if (Number(amount) < MINIMUM_RECHARGE_AMOUNT) return `Recharge amount should be bigger than ${MINIMUM_RECHARGE_AMOUNT}` - if (amount === '' || amount === '0' || !amount) return 'Enter an amount' return 'Recharge exit gas wallet' } @@ -210,7 +211,7 @@ export default function CommunityPool() { @@ -224,7 +225,7 @@ export default function CommunityPool() { diff --git a/src/components/TransferETA.tsx b/src/components/TransferETA.tsx deleted file mode 100644 index 4adb897..0000000 --- a/src/components/TransferETA.tsx +++ /dev/null @@ -1,53 +0,0 @@ -import React, { useEffect } from 'react' -import Tooltip from '@mui/material/Tooltip' -import InfoIcon from '@mui/icons-material/Info' -import Skeleton from '@mui/material/Skeleton' - -import { isMainnet } from '../core/helper' -import { IMA_M2S_WAIT, IMA_S2S_WAIT, IMA_HUB_WAIT } from '../core/constants' - -import { cls, cmn } from '../core/css' - -import { TokenData } from '../core/dataclasses' - -export default function TransferETA(props: { token: TokenData; toChain: string }) { - const [eta, setEta] = React.useState() - const [isLoaded, setIsLoaded] = React.useState(false) - - async function calcETA() { - setIsLoaded(false) - let baseETA = 0 - const fromMainnet = isMainnet(props.token.chain) - const toMainnet = isMainnet(props.toChain) - baseETA += fromMainnet || toMainnet ? IMA_M2S_WAIT : IMA_S2S_WAIT - if (props.token.connections[props.toChain] && props.token.connections[props.toChain].hub) { - baseETA += IMA_HUB_WAIT - } - setEta(baseETA) - setIsLoaded(true) - } - - useEffect(() => { - if (props.token && props.toChain) calcETA() - }, [props.token, props.toChain]) - - const tooltipText = 'Estimated transfer time (may vary depending on the network load)' - - return ( - -
-
-

ETA

- -
- {isLoaded ? ( -

- ~{eta}-{eta + 1} min -

- ) : ( - - )} -
-
- ) -} diff --git a/src/components/TransferETF.tsx b/src/components/TransferETF.tsx deleted file mode 100644 index df1a97b..0000000 --- a/src/components/TransferETF.tsx +++ /dev/null @@ -1,50 +0,0 @@ -import React, { useEffect } from 'react' -import Tooltip from '@mui/material/Tooltip' -import InfoIcon from '@mui/icons-material/Info' -import Skeleton from '@mui/material/Skeleton' - -import { isMainnet } from '../core/helper' -import { cls, cmn } from '../core/css' - -function roundDown(number, decimals) { - decimals = decimals || 0 - return Math.floor(number * Math.pow(10, decimals)) / Math.pow(10, decimals) -} - -export default function TransferETF(props: { fromChain: string }) { - const [etf, setEtf] = React.useState() - const [isLoaded, setIsLoaded] = React.useState(false) - - async function calcETF() { - setIsLoaded(false) - const fromMainnet = isMainnet(props.fromChain) - let baseETF = 0 - if (fromMainnet) baseETF = 2.5 - setEtf(baseETF) - setIsLoaded(true) - } - - useEffect(() => { - if (props.fromChain) calcETF() - }, [props.fromChain]) - - const tooltipText = - 'Estimated transaction fee (You pay only for Mainnet transactions, all transfers within SKALE are free)' - const etfText = etf === 0 ? 'Free' : `~${roundDown(etf, 3)} USD` - - return ( - -
-
-

Estimated Transaction Fee

- -
- {isLoaded ? ( -

{etfText}

- ) : ( - - )} -
-
- ) -} diff --git a/src/core/community_pool.ts b/src/core/community_pool.ts index 388927b..7d085a1 100644 --- a/src/core/community_pool.ts +++ b/src/core/community_pool.ts @@ -38,10 +38,11 @@ import { RECHARGE_MULTIPLIER, MINIMUM_RECHARGE_AMOUNT, COMMUNITY_POOL_WITHDRAW_GAS_LIMIT, + COMMUNITY_POOL_ESTIMATE_GAS_LIMIT, DEFAULT_ERROR_MSG, BALANCE_UPDATE_INTERVAL_MS } from './constants' -import { delay } from './helper' +import { delay, roundUp } from './helper' import MetaportCore from './metaport' import * as dataclasses from '../core/dataclasses' @@ -83,14 +84,21 @@ export async function getCommunityPoolData( const chainHash = ethers.id(chainName1) const activeM = await mainnet.communityPool.contract.activeUsers(address, chainHash) + const feeData = await mainnet.communityPool.contract.runner.provider.getFeeData() const rraWei = await mainnet.communityPool.contract.getRecommendedRechargeAmount( chainHash, - address + address, + { + gasPrice: feeData.gasPrice, + gasLimit: COMMUNITY_POOL_ESTIMATE_GAS_LIMIT + } ) const rraEther = fromWei(rraWei as string, DEFAULT_ERC20_DECIMALS) - let recommendedAmount = parseFloat(rraEther as string) * RECHARGE_MULTIPLIER - if (recommendedAmount < MINIMUM_RECHARGE_AMOUNT) recommendedAmount = MINIMUM_RECHARGE_AMOUNT + let recommendedAmount = roundUp(parseFloat(rraEther as string) * RECHARGE_MULTIPLIER) + if (recommendedAmount < MINIMUM_RECHARGE_AMOUNT && recommendedAmount !== 0) { + recommendedAmount = MINIMUM_RECHARGE_AMOUNT + } const communityPoolData = { exitGasOk: activeM && activeS && rraWei === 0n, diff --git a/src/core/constants.ts b/src/core/constants.ts index 25620db..f226953 100644 --- a/src/core/constants.ts +++ b/src/core/constants.ts @@ -104,11 +104,13 @@ export const FAUCET_DATA = faucetJson // community pool -export const RECHARGE_MULTIPLIER = 1.2 -export const MINIMUM_RECHARGE_AMOUNT = 0.005 +export const RECHARGE_MULTIPLIER = 1.1 +export const MINIMUM_RECHARGE_AMOUNT = 0.000005 +export const COMMUNITY_POOL_ESTIMATE_GAS_LIMIT = 1000000n export const COMMUNITY_POOL_WITHDRAW_GAS_LIMIT = 150000n export const _BALANCE_UPDATE_INTERVAL_SECONDS = 10 export const BALANCE_UPDATE_INTERVAL_MS = _BALANCE_UPDATE_INTERVAL_SECONDS * 1000 +export const COMMUNITY_POOL_DECIMALS = 6 export const SFUEL_RESERVE_AMOUNT = 0.01 diff --git a/src/core/helper.ts b/src/core/helper.ts index b249410..3acf8bb 100644 --- a/src/core/helper.ts +++ b/src/core/helper.ts @@ -23,7 +23,7 @@ import { getAddress } from 'ethers' -import { MAINNET_CHAIN_NAME } from './constants' +import { COMMUNITY_POOL_DECIMALS, MAINNET_CHAIN_NAME } from './constants' import { TransferRequestStatus } from './dataclasses' export function eqArrays(arr1, arr2) { @@ -67,3 +67,13 @@ export function sortObjectByKeys(obj: { [key: string]: any }): { [key: string]: return sortedObject } + +export function roundDown(num: number, decimals: number = COMMUNITY_POOL_DECIMALS): number { + decimals = decimals || 0 + return Math.floor(num * Math.pow(10, decimals)) / Math.pow(10, decimals) +} + +export function roundUp(num: number, decimals: number = COMMUNITY_POOL_DECIMALS): number { + const factor = Math.pow(10, decimals) + return Math.round(num * factor) / factor +} diff --git a/src/index.ts b/src/index.ts index cd9d135..7a0503f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,8 +23,6 @@ import TokenBalance from './components/TokenBalance' import AmountInput from './components/AmountInput' import SwitchDirection from './components/SwitchDirection' import SkStepper from './components/Stepper' -import TransferETF from './components/TransferETF' -import TransferETA from './components/TransferETA' import AmountErrorMessage from './components/AmountErrorMessage' import DestTokenBalance from './components/DestTokenBalance' import ErrorMessage from './components/ErrorMessage' @@ -45,7 +43,6 @@ import { ERC_ABIS } from './core/contracts' import { sendTransaction } from './core/transactions' import { Station, StationData } from './core/sfuel' - import { getWidgetTheme as getMetaportTheme } from './core/themes' import { @@ -78,8 +75,6 @@ export { AmountInput, SwitchDirection, SkStepper, - TransferETF, - TransferETA, AmountErrorMessage, TokenBalance, DestTokenBalance,