diff --git a/src/helpers/partnerConfigurationContext.tsx b/src/helpers/partnerConfigurationContext.tsx index 03bb109d..46f992b1 100644 --- a/src/helpers/partnerConfigurationContext.tsx +++ b/src/helpers/partnerConfigurationContext.tsx @@ -1,5 +1,13 @@ import { ethers } from 'ethers' -import React, { createContext, useContext, useEffect, useReducer } from 'react' +import React, { + createContext, + useCallback, + useContext, + useEffect, + useReducer, + useState, +} from 'react' +import { useNavigate } from 'react-router' import { usePartnerConfig } from './usePartnerConfig' import { useSmartContract } from './useSmartContract' @@ -374,9 +382,11 @@ const PartnerConfigContext = createContext() // Context provider component export const PartnerConfigurationProvider = ({ children }) => { const [state, dispatch] = useReducer(reducer, initialState) + const [firstLoad, setFirstLoad] = useState(false) const partnerConfig = usePartnerConfig() - const { accountReadContract } = useSmartContract() - + const { accountReadContract, accountWriteContract, contractCMAccountAddress, needsUpgrade } = + useSmartContract() + const path = window.location.pathname useEffect(() => { if (partnerConfig.account) { partnerConfig.getAllServices().then(result => { @@ -390,6 +400,23 @@ export const PartnerConfigurationProvider = ({ children }) => { } }, [partnerConfig.account]) + const navigate = useNavigate() + + const checkIfCMAccountneedsUpgrade = useCallback(async () => { + let res = await needsUpgrade() + setFirstLoad(true) + if (res) navigate('/partners/upgrade') + }, [contractCMAccountAddress, accountWriteContract]) + + useEffect(() => { + if ( + contractCMAccountAddress && + path.includes('partners/messenger-configuration') && + !firstLoad + ) { + checkIfCMAccountneedsUpgrade() + } + }, [contractCMAccountAddress, accountWriteContract, path]) useEffect(() => { if (accountReadContract) { partnerConfig.getSupportedServices().then(res => { diff --git a/src/helpers/usePartnerConfig.tsx b/src/helpers/usePartnerConfig.tsx index af172170..f0e021be 100644 --- a/src/helpers/usePartnerConfig.tsx +++ b/src/helpers/usePartnerConfig.tsx @@ -16,9 +16,7 @@ export const usePartnerConfig = () => { managerReadContract, wallet, CMAccountCreated, - contractCMAccountAddress, accountReadContract, - provider, } = useSmartContract() const activeNetwork = useAppSelector(getActiveNetwork) const auth = useAppSelector(state => state.appConfig.isAuth) diff --git a/src/helpers/useSmartContract.tsx b/src/helpers/useSmartContract.tsx index 02798c05..13335e1a 100644 --- a/src/helpers/useSmartContract.tsx +++ b/src/helpers/useSmartContract.tsx @@ -30,6 +30,7 @@ type SmartContractProviderProps = { export const SmartContractProvider: React.FC = ({ children }) => { const [provider, setProvider] = useState(null) + const [needUpgrade, setNeedUpgrade] = useState(false) const [managerReadContract, setManagerReadContract] = useState(null) const [managerWriteContract, setManagerWriteContract] = useState(null) const [accountReadContract, setAccountReadContract] = useState(null) @@ -84,7 +85,7 @@ export const SmartContractProvider: React.FC = ({ ch } } - const upgradeCMAccount = useCallback(async () => { + const needsUpgrade = useCallback(async () => { try { if (accountWriteContract) { const implementation = await managerReadContract.getAccountImplementation() @@ -94,10 +95,25 @@ export const SmartContractProvider: React.FC = ({ ch IMPLEMENTATION_SLOT, ) const implAddr = '0x' + implAddrPadded.slice(-40) - console.log({ implementation, implAddr }) + if (ethers.getAddress(implementation) !== ethers.getAddress(implAddr)) { + setNeedUpgrade(true) + return true + } + return false + } + } catch (error) { + const decodedError = accountWriteContract.interface.parseError(error.data) + console.error('Message:', error.message) + console.error(`Reason: ${decodedError?.name} (${decodedError?.args})`) + } + }, [accountWriteContract]) + + const upgradeCMAccount = useCallback(async () => { + try { + if (accountWriteContract) { + const implementation = await managerReadContract.getAccountImplementation() const tx = await accountWriteContract.upgradeToAndCall(implementation, '0x') const receipt = await tx.wait() - console.log({ receipt }) return receipt } } catch (error) { @@ -262,8 +278,10 @@ export const SmartContractProvider: React.FC = ({ ch } const value = { + needUpgrade, getCMAccountMappings, upgradeCMAccount, + needsUpgrade, contractCMAccountAddress, setContractCMAccountAddress, wallet, diff --git a/src/layout/RoutesSuite.tsx b/src/layout/RoutesSuite.tsx index 7e6bd62c..783f2391 100644 --- a/src/layout/RoutesSuite.tsx +++ b/src/layout/RoutesSuite.tsx @@ -20,6 +20,7 @@ import CreatedOffers from '../views/partners/CreatedOffers' import Foundation from '../views/partners/Foundation' import ManageBots, { BasicManageBots } from '../views/partners/ManageBots' import Partner from '../views/partners/Partner' +import UpgradeCMAccount from '../views/partners/UpgradeCMAccount' import MultisigWallet from '../views/settings/MultisigWallet' import VerifyWallet from '../views/settings/VerifyWallet' import Settings from '../views/settings/index' @@ -113,15 +114,11 @@ export default function RoutesSuite() { }> } /> + } /> } /> } /> } /> - }> - } /> - {/* } /> */} - {/* } /> */} - {/* } /> */} - + } /> } /> } /> diff --git a/src/views/partners/UpgradeCMAccount.tsx b/src/views/partners/UpgradeCMAccount.tsx new file mode 100644 index 00000000..436b305d --- /dev/null +++ b/src/views/partners/UpgradeCMAccount.tsx @@ -0,0 +1,80 @@ +import { Box, Link, Typography } from '@mui/material' +import React, { useState } from 'react' +import { Navigate, useNavigate } from 'react-router' +import MainButton from '../../components/MainButton' +import { useSmartContract } from '../../helpers/useSmartContract' +import { useAppDispatch, useAppSelector } from '../../hooks/reduxHooks' +import { updateNotificationStatus } from '../../redux/slices/app-config' + +const UpgradeCMAccount = () => { + const navigate = useNavigate() + const [loading, setLoading] = useState(false) + const { contractCMAccountAddress, upgradeCMAccount } = useSmartContract() + const auth = useAppSelector(state => state.appConfig.isAuth) + const appDispatch = useAppDispatch() + if (!auth) return + async function upgrade() { + setLoading(true) + await upgradeCMAccount() + appDispatch( + updateNotificationStatus({ + message: 'Camion Messenger account upgraded successfully.', + severity: 'success', + }), + ) + setLoading(false) + navigate('/partners/messenger-configuration/mymessenger') + } + return ( + + Housekeeping needed + + new implementation of the Camino Messenger Account smart contract is available. + While current functionality is not disrupted, we need your confirmation to upgrade + you to the new smart contrtact. This operation will incur a minimal cost (0.02 CAM). + + + Upgrade Camino Messenger Account {contractCMAccountAddress}, + theme.palette.text.primary, + textDecorationColor: 'inherit', + cursor: 'pointer', + }} + onClick={() => navigate('/partners/messenger-configuration/mymessenger')} + > + {' '} + or Skip and go to My Account. + + + + theme.palette.mode === 'dark' ? '#020617' : '#F1F5F9', + }} + > + + Upgrade + + + + ) +} + +export default UpgradeCMAccount