From ee18f8c18144187290ae02264f59202b26ea5777 Mon Sep 17 00:00:00 2001 From: aeddaqqa Date: Wed, 9 Oct 2024 09:01:59 +0100 Subject: [PATCH 1/2] feat(MyMessenger): added supported tokens to acepted currencies --- src/helpers/usePartnerConfig.tsx | 39 +++++++++ src/views/partners/MyMessenger.tsx | 127 +++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) diff --git a/src/helpers/usePartnerConfig.tsx b/src/helpers/usePartnerConfig.tsx index f6b82713..62d3c81b 100644 --- a/src/helpers/usePartnerConfig.tsx +++ b/src/helpers/usePartnerConfig.tsx @@ -16,7 +16,9 @@ export const usePartnerConfig = () => { managerReadContract, wallet, CMAccountCreated, + contractCMAccountAddress, accountReadContract, + provider, } = useSmartContract() const activeNetwork = useAppSelector(getActiveNetwork) const auth = useAppSelector(state => state.appConfig.isAuth) @@ -368,6 +370,42 @@ export const usePartnerConfig = () => { [account, accountWriteContract], ) + const transferERC20 = useCallback( + async (address, value) => { + if (!account) { + console.error('Account is not initialized') + return + } + try { + const abi = [ + { + constant: true, + inputs: [{ name: '_owner', type: 'address' }], + name: 'balanceOf', + outputs: [{ name: 'balance', type: 'uint256' }], + type: 'function', + }, + ] + + // Create a contract instance + const contract = new ethers.Contract(address, abi, provider) + + // Call the balanceOf function + const balance = await contract.balanceOf(contractCMAccountAddress) + const tx = await accountWriteContract.transferERC20(address, wallet.address, value) + await tx.wait() + const afterBalance = await contract.balanceOf(contractCMAccountAddress) + const afterFormattedBalance = ethers.formatUnits(afterBalance, 18) + return afterFormattedBalance + } catch (error) { + const decodedError = accountWriteContract.interface.parseError(error.data) + console.error('Message:', error.message) + console.error(`Reason: ${decodedError?.name} (${decodedError?.args})`) + } + }, + [account, accountWriteContract], + ) + const setOffChainPaymentSupported = useCallback( async value => { if (!account) { @@ -454,6 +492,7 @@ export const usePartnerConfig = () => { }, [account, readFromContract]) return { + transferERC20, checkWithDrawRole, grantWithDrawRole, withDraw, diff --git a/src/views/partners/MyMessenger.tsx b/src/views/partners/MyMessenger.tsx index 39f01e85..449add38 100644 --- a/src/views/partners/MyMessenger.tsx +++ b/src/views/partners/MyMessenger.tsx @@ -19,6 +19,7 @@ import { import { ethers } from 'ethers' import React, { useCallback, useEffect, useMemo, useState } from 'react' import { useNavigate } from 'react-router' +import store from 'wallet/store' import Alert from '../../components/Alert' import DialogAnimate from '../../components/Animate/DialogAnimate' import MainButton from '../../components/MainButton' @@ -323,6 +324,8 @@ const MyMessenger = () => { const [isCAMSupported, setCAMSupported] = useState(false) const [isEditMode, setIsEditMode] = useState(false) const [tempOffChainPaymentSupported, setTempOffChainPaymentSupported] = useState(false) + const [tempSupportedTokens, setTempSupportedTokens] = useState([]) + const [supportedTokens, setSupportedTokens] = useState([]) const [tempCAMSupported, setTempCAMSupported] = useState(false) const { balanceOfAnAddress, getBalanceOfAnAddress } = useWalletBalance() const [isLoading, setIsLoading] = useState(false) @@ -335,6 +338,7 @@ const MyMessenger = () => { addSupportedToken, removeSupportedToken, getListOfBots, + transferERC20, } = usePartnerConfig() const { data: partner, refetch } = useFetchPartnerDataQuery({ companyName: '', @@ -380,6 +384,33 @@ const MyMessenger = () => { if (tempCAMSupported) await addSupportedToken(ethers.ZeroAddress) else await removeSupportedToken(ethers.ZeroAddress) } + if (tempSupportedTokens) { + const result = [] + + for (const address of supportedTokens) { + const foundToken = tempSupportedTokens.find( + token => + token.address.toLowerCase() === address.toLowerCase() && + token.supported, + ) + + if (foundToken) { + result.push({ + address: foundToken.address, + name: foundToken.name, + symbol: foundToken.symbol, + added: true, + }) + } else { + result.push({ + address: address, + added: false, + }) + } + } + + return result + } appDispatch( updateNotificationStatus({ message: 'Accepted currencies updated successfully', @@ -399,6 +430,32 @@ const MyMessenger = () => { const res = await getListOfBots() setBots(res) } + async function fetchSupportedTokens() { + const res = await getSupportedTokens() + setSupportedTokens(res) + } + + const tokens = useMemo(() => { + if ( + store.getters['Assets/networkErc20Tokens'] && + store.getters['Assets/networkErc20Tokens'].length > 0 + ) { + let tokens = store.getters['Assets/networkErc20Tokens'].map(elem => { + return { + address: elem.contract._address, + balance: elem.balanceBig.toLocaleString(), + name: elem.data.name, + symbol: elem.data.symbol, + decimal: elem.data.decimal, + supported: supportedTokens.find(token => token === elem.contract._address), + } + }) + setTempSupportedTokens([...tokens]) + return tokens + } + return [] + }, [supportedTokens]) + useEffect(() => { checkIfCamSupported() checkIfOffChainPaymentSupported() @@ -407,6 +464,7 @@ const MyMessenger = () => { useEffect(() => { fetchBots() + fetchSupportedTokens() }, []) function getServicesNames(services) { @@ -669,6 +727,75 @@ const MyMessenger = () => { )} + {tokens.length > 0 && + tokens.map((elem, index) => { + return ( + + + {elem.name}: {elem.balance} {elem.symbol} + + } + control={ + + !isEditMode + ? theme.palette.action.disabled + : theme.palette.secondary.main, + '&.Mui-checked': { + color: theme => + !isEditMode + ? theme.palette.action.disabled + : theme.palette.secondary.main, + }, + '&.MuiCheckbox-colorSecondary.Mui-checked': + { + color: theme => + !isEditMode + ? theme.palette.action + .disabled + : theme.palette.secondary + .main, + }, + }} + checked={ + isEditMode + ? tempSupportedTokens[index].supported + : elem.supported + } + onChange={e => { + let newArray = [...tempSupportedTokens] + newArray[index].supported = e.target.checked + setTempSupportedTokens(newArray) + }} + /> + } + /> + {!isEditMode && ( + + )} + + ) + })} Date: Wed, 9 Oct 2024 09:04:26 +0100 Subject: [PATCH 2/2] styling(MyMessenger): adjust link colors in light theme --- src/views/partners/MyMessenger.tsx | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/views/partners/MyMessenger.tsx b/src/views/partners/MyMessenger.tsx index 449add38..a099d982 100644 --- a/src/views/partners/MyMessenger.tsx +++ b/src/views/partners/MyMessenger.tsx @@ -547,7 +547,11 @@ const MyMessenger = () => { <> None. Visit the relevant{' '} theme.palette.text.primary, + textDecorationColor: 'inherit', + }} onClick={() => navigate('/partners/messenger-configuration/supplier') } @@ -570,7 +574,11 @@ const MyMessenger = () => { <> None. Visit the relevant{' '} theme.palette.text.primary, + textDecorationColor: 'inherit', + }} onClick={() => navigate( '/partners/messenger-configuration/distribution', @@ -602,7 +610,11 @@ const MyMessenger = () => { <> None. Visit the relevant{' '} theme.palette.text.primary, + textDecorationColor: 'inherit', + }} onClick={() => navigate('/partners/messenger-configuration/bots') }