Skip to content

Commit

Permalink
chore: currentAccount is always defined in walletSlice redux state
Browse files Browse the repository at this point in the history
  • Loading branch information
khanti42 committed Feb 4, 2025
1 parent ee30916 commit 4d656b5
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 93 deletions.
3 changes: 1 addition & 2 deletions packages/wallet-ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import 'toastr2/dist/toastr.min.css';
import { NoMetamaskModal } from 'components/ui/organism/NoMetamaskModal';
import { MinVersionModal } from './components/ui/organism/MinVersionModal';
import { useHasMetamask } from 'hooks/useHasMetamask';
import { DUMMY_ADDRESS } from 'utils/constants';
import { DeployModal } from 'components/ui/organism/DeployModal';

library.add(fas, far);
Expand All @@ -40,7 +39,7 @@ function App() {
const { currentAccount } = useAppSelector((state) => state.wallet);
const { hasMetamask } = useHasMetamask();
const chainId = networks.items?.[networks.activeNetwork]?.chainId;
const address = currentAccount?.address ?? DUMMY_ADDRESS;
const address = currentAccount.address;

useEffect(() => {
if (!provider) {
Expand Down
3 changes: 1 addition & 2 deletions packages/wallet-ui/src/components/pages/Home/Home.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { Header } from 'components/ui/organism/Header';
import { SideBar } from 'components/ui/organism/SideBar';
import { RightPart, Wrapper, NoTransactions } from './Home.style';
import { useAppSelector } from 'hooks/redux';
import { DUMMY_ADDRESS } from 'utils/constants';

export const HomeView = () => {
const { erc20TokenBalanceSelected, transactions } = useAppSelector(
(state) => state.wallet,
);
const loader = useAppSelector((state) => state.UI.loader);
const currentAccount = useAppSelector((state) => state.wallet.currentAccount);
const address = currentAccount?.address ?? DUMMY_ADDRESS;
const address = currentAccount.address;
const { upgradeModalVisible } = useAppSelector((state) => state.modals);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const TransactionsListView = ({ transactions }: Props) => {
} = wallet;

useEffect(() => {
if (chainId && currentAccount && erc20TokenBalanceSelected.address) {
if (chainId && erc20TokenBalanceSelected.address) {
clearTimeout(timeoutHandle.current); // cancel the timeout that was in-flight
timeoutHandle.current = setTimeout(
() =>
Expand All @@ -45,7 +45,7 @@ export const TransactionsListView = ({ transactions }: Props) => {

useEffect(
() => {
if (chainId && currentAccount && erc20TokenBalanceSelected.address) {
if (chainId && erc20TokenBalanceSelected.address) {
clearTimeout(timeoutHandle.current); // cancel the timeout that was in-flight
getTransactions(
currentAccount.address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import {
import { openExplorerTab } from 'utils/utils';
import { useAppSelector } from 'hooks/redux';
import { useStarkNetSnap } from 'services';
import { DUMMY_ADDRESS } from 'utils/constants';

export const AccountDetailsModalView = () => {
const networks = useAppSelector((state) => state.networks);
const currentAccount = useAppSelector((state) => state.wallet.currentAccount);
const { getPrivateKeyFromAddress } = useStarkNetSnap();
const chainId = networks?.items[networks.activeNetwork]?.chainId;
const address = currentAccount?.address ?? DUMMY_ADDRESS;
const address = currentAccount.address;
const addressIndex = currentAccount?.addressIndex ?? 0;
return (
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { AccountsHeader } from './AccountsHeader';
import { VisibleAccountsList } from './VisibleAccountsList';
import { HiddenAccountsList } from './HiddenAccountsList';
import { DUMMY_ADDRESS } from 'utils/constants';
import { Account } from 'types';
import Toastr from 'toastr2';

Expand Down Expand Up @@ -65,7 +64,7 @@ export const AccountSwitchModalView = ({ full, starkName }: Props) => {
hiddenAccounts.push(account);
}
}
const currentAddress = currentAccount?.address ?? DUMMY_ADDRESS;
const currentAddress = currentAccount.address;
const displayName = full
? starkName ?? currentAddress
: starkName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,19 @@ export const AddTokenModalView = ({ closeModal }: Props) => {
enabled={enabled}
onClick={async () => {
try {
if (currentAccount) {
const newToken = await addErc20Token(
fields.address,
fields.name,
fields.symbol,
parseFloat(fields.decimal),
chainId,
currentAccount.address,
);
if (newToken) {
setErc20TokenBalance(newToken);
toastr.success('Token added successfully');
}
closeModal();
const newToken = await addErc20Token(
fields.address,
fields.name,
fields.symbol,
parseFloat(fields.decimal),
chainId,
currentAccount.address,
);
if (newToken) {
setErc20TokenBalance(newToken);
toastr.success('Token added successfully');
}
closeModal();
} catch (err) {
toastr.error('Error while adding token');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,30 @@ export const SendSummaryModalView = ({

useEffect(() => {
const fetchGasFee = () => {
if (currentAccount) {
setGasFeesError(false);
setEstimatingGas(true);
const amountBN = ethers.utils.parseUnits(
amount,
wallet.erc20TokenBalanceSelected.decimals,
);
const callData = address + ',' + amountBN.toString() + ',0';
estimateFees(
wallet.erc20TokenBalanceSelected.address,
ContractFuncName.Transfer,
callData,
currentAccount.address,
chainId,
selectedFeeToken === FeeToken.STRK
? constants.TRANSACTION_VERSION.V3
: undefined,
)
.then((response) => {
setGasFees(response);
setEstimatingGas(false);
})
.catch(() => {
toastr.error('Error when trying to calculate the gas fees');
});
}
setGasFeesError(false);
setEstimatingGas(true);
const amountBN = ethers.utils.parseUnits(
amount,
wallet.erc20TokenBalanceSelected.decimals,
);
const callData = address + ',' + amountBN.toString() + ',0';
estimateFees(
wallet.erc20TokenBalanceSelected.address,
ContractFuncName.Transfer,
callData,
currentAccount.address,
chainId,
selectedFeeToken === FeeToken.STRK
? constants.TRANSACTION_VERSION.V3
: undefined,
)
.then((response) => {
setGasFees(response);
setEstimatingGas(false);
})
.catch(() => {
toastr.error('Error when trying to calculate the gas fees');
});
};
fetchGasFee();
}, [currentAccount]);
Expand Down Expand Up @@ -171,45 +169,43 @@ export const SendSummaryModalView = ({
}, [amount, wallet.erc20TokenBalanceSelected]);

const handleConfirmClick = () => {
if (currentAccount) {
const amountBN = ethers.utils.parseUnits(
amount,
wallet.erc20TokenBalanceSelected.decimals,
);
const callData = address + ',' + amountBN.toString() + ',0';
sendTransaction(
wallet.erc20TokenBalanceSelected.address,
ContractFuncName.Transfer,
callData,
currentAccount.address,
gasFees.suggestedMaxFee,
chainId,
selectedFeeToken,
)
.then((result) => {
if (result) {
toastr.success('Transaction sent successfully');
getTransactions(
currentAccount.address,
wallet.erc20TokenBalanceSelected.address,
10,
chainId,
false,
true,
).catch((err) => {
console.error(
`handleConfirmClick: error from getTransactions: ${err}`,
);
});
} else {
toastr.info('Transaction rejected by user');
}
})
.catch(() => {
toastr.error('Error while sending the transaction');
});
closeModal && closeModal();
}
const amountBN = ethers.utils.parseUnits(
amount,
wallet.erc20TokenBalanceSelected.decimals,
);
const callData = address + ',' + amountBN.toString() + ',0';
sendTransaction(
wallet.erc20TokenBalanceSelected.address,
ContractFuncName.Transfer,
callData,
currentAccount.address,
gasFees.suggestedMaxFee,
chainId,
selectedFeeToken,
)
.then((result) => {
if (result) {
toastr.success('Transaction sent successfully');
getTransactions(
currentAccount.address,
wallet.erc20TokenBalanceSelected.address,
10,
chainId,
false,
true,
).catch((err) => {
console.error(
`handleConfirmClick: error from getTransactions: ${err}`,
);
});
} else {
toastr.info('Transaction rejected by user');
}
})
.catch(() => {
toastr.error('Error while sending the transaction');
});
closeModal && closeModal();
};

const totalAmountDisplay = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const SideBarView = () => {
const [addTokenOpen, setAddTokenOpen] = useState(false);
const { getStarkName } = useStarkNetSnap();
const [starkName, setStarkName] = useState<string | undefined>(undefined);
const address = currentAccount?.address ?? DUMMY_ADDRESS;
const address = currentAccount.address;
const addressIndex = currentAccount?.addressIndex ?? 0;
const ref = useRef<HTMLDivElement>();

Expand Down
13 changes: 10 additions & 3 deletions packages/wallet-ui/src/slices/walletSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { Account } from 'types';
import { Erc20TokenBalance } from 'types';
import { Transaction } from 'types';
import { ethers } from 'ethers';
import { DUMMY_ADDRESS } from 'utils/constants';

export interface WalletState {
connected: boolean;
isLoading: boolean;
forceReconnect: boolean;
accounts: Account[];
currentAccount?: Account;
currentAccount: Account;
erc20TokenBalances: Erc20TokenBalance[];
erc20TokenBalanceSelected: Erc20TokenBalance;
transactions: Transaction[];
Expand All @@ -22,7 +23,10 @@ const initialState: WalletState = {
isLoading: false,
forceReconnect: false,
accounts: [],
currentAccount: undefined,
currentAccount: {
address: DUMMY_ADDRESS,
addressIndex: 0,
} as Account,
erc20TokenBalances: [],
erc20TokenBalanceSelected: {} as Erc20TokenBalance,
transactions: [],
Expand Down Expand Up @@ -122,7 +126,10 @@ export const walletSlice = createSlice({
},
clearAccounts: (state) => {
state.accounts = [];
state.currentAccount = undefined;
state.currentAccount = {
address: DUMMY_ADDRESS,
addressIndex: 0,
} as Account;
},
resetWallet: (state) => {
return {
Expand Down

0 comments on commit 4d656b5

Please sign in to comment.