import React from 'react' ; import PropTypes from 'prop-types' ; import { isAddress } from 'web3-utils' ; import idx from 'idx' ; import { useAccountData } from '../../context/accountData' ; import { useAdminData } from '../../context/adminData' ; import useTab from './useTab' ; import { errorToast } from '../../util/tabTools' ; import AccountTab from '../../components/AccountTab/AccountTab' ; import LoadingPage from '../../components/LoadingPage/LoadingPage' ; import NoContract from '../../components/Flashes/NoContract' ; import { PENDING_ADDITION , FAIL_ADDITION , PENDING_REMOVAL , FAIL_REMOVAL , SUCCESS , FAIL } from '../../constants/transactions' ; type AccountTabContainerProps = { isOpen : boolean ; } ; type Account = { address : string ; identifier : string ; status : string ; } ; const AccountTabContainer = ( { isOpen } ) => { const { isAdmin , dataReady : adminDataReady } = useAdminData ( ) ; const { allowlist , isReadOnly , dataReady , accountRulesContract } = useAccountData ( ) ; const { list , modals , toggleModal , addTransaction , updateTransaction , deleteTransaction , openToast } = useTab ( allowlist , ( identifier ) => ( { address : identifier } ) ) ; if ( ! ! accountRulesContract ) { const handleAdd = async ( value ) => { try { const tx = await accountRulesContract ! . functions . addAccount ( value ) ; toggleModal ( 'add' ) ( false ) ; addTransaction ( value , PENDING_ADDITION ) ; const receipt = await tx . wait ( 1 ) ; const addEvent = receipt . events ! . filter ( e => e . event && e . event === 'AccountAdded' ) . pop ( ) ; if ( ! addEvent ) { openToast ( value , FAIL , `Error while processing account: ${ value }` ) ; } else { const addSuccessResult = idx ( addEvent , _ => _ . args [ 0 ] ) ; if ( addSuccessResult === undefined ) { openToast ( value , FAIL , `Error while adding account: ${ value }` ) ; } else if ( Boolean ( addSuccessResult ) ) { openToast ( value , SUCCESS , `New account added: ${ value }` ) ; } else { openToast ( value , FAIL , `Account "${ value }" is already added` ) ; } } deleteTransaction ( value ) ; } catch ( e ) { toggleModal ( 'add' ) ( false ) ; updateTransaction ( value , FAIL_ADDITION ) ; errorToast ( e , value , openToast , ( ) => openToast ( value , FAIL , 'Could not add account' , `${ value } was unable to be added. Please try again.` ) ) ; } } ; const handleRemove = async ( value ) => { try { const est = await accountRulesContract ! . estimate . removeAccount ( value ) ; const tx = await accountRulesContract ! . functions . removeAccount ( value , { gasLimit : est . toNumber ( ) * 2 } ) ; toggleModal ( 'remove' ) ( false ) ; addTransaction ( value , PENDING_REMOVAL ) ; await tx . wait ( 1 ) ; openToast ( value , SUCCESS , `Removal of account processed: ${ value }` ) ; deleteTransaction ( value ) ; } catch ( e ) { console . log ( 'error' , e ) ; toggleModal ( 'remove' ) ( false ) ; updateTransaction ( value , FAIL_REMOVAL ) ; errorToast ( e , value , openToast , ( ) => openToast ( value , FAIL , 'Could not remove account' , `${ value } was unable to be removed. Please try again.` ) ) ; } } ; const isValidAccount = ( address ) => { let isValidAddress = isAddress ( address ) ; if ( ! isValidAddress ) { return { valid : false } ; } let isDuplicateAccount = list . filter ( ( item ) => address . toLowerCase ( ) === item . address . toLowerCase ( ) ) . length > 0 ; if ( isDuplicateAccount ) { return { valid : false , msg : 'Account address is already added.' } ; } return { valid : true } ; } ; const allDataReady = dataReady && adminDataReady ; if ( isOpen && allDataReady ) { return ( < AccountTab list = { list } modals = { modals } toggleModal = { toggleModal } handleAdd = { handleAdd } handleRemove = { handleRemove } isAdmin = { isAdmin } deleteTransaction = { deleteTransaction } isValid = { isValidAccount } isOpen = { isOpen } isReadOnly = { isReadOnly ! } / > ) ; } else if ( isOpen && ! allDataReady ) { return < LoadingPage />; } else { return < div />; } } else if ( isOpen && ! accountRulesContract ) { return < NoContract tabName = "Account Rules" / > ; } else { return < div />; } } ; AccountTabContainer . propTypes = { isOpen : PropTypes . bool . isRequired } ; export default AccountTabContainer ;