From 7c88798ebfe28c2249784a9e116129b213518fed Mon Sep 17 00:00:00 2001
From: Hemanth Sai <ghshemanth@gmail.com>
Date: Mon, 8 Apr 2024 16:05:11 +0530
Subject: [PATCH] fix(authz): fix error while creating authz (#1214)

fix: fix authz

Co-authored-by: chary <57086313+charymalloju@users.noreply.github.com>
---
 .../src/store/features/authz/authzSlice.ts    | 35 +++++++++++--------
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/frontend/src/store/features/authz/authzSlice.ts b/frontend/src/store/features/authz/authzSlice.ts
index 999832c01..bf6f52548 100644
--- a/frontend/src/store/features/authz/authzSlice.ts
+++ b/frontend/src/store/features/authz/authzSlice.ts
@@ -388,20 +388,21 @@ export const authzSlice = createSlice({
         const addressMapping: Record<string, Authorization[]> = {};
         const allChainsAddressToGrants = state.AddressToChainAuthz;
 
-        grants && grants.forEach((grant: Authorization) => {
-          const granter = grant.granter;
-          const cosmosAddress = getAddressByPrefix(granter, 'cosmos');
-          if (!addressMapping[granter]) addressMapping[granter] = [];
-          if (!allChainsAddressToGrants[cosmosAddress])
-            allChainsAddressToGrants[cosmosAddress] = {};
-          if (!allChainsAddressToGrants[cosmosAddress][chainID])
-            allChainsAddressToGrants[cosmosAddress][chainID] = [];
-          allChainsAddressToGrants[cosmosAddress][chainID] = [
-            ...allChainsAddressToGrants[cosmosAddress][chainID],
-            grant,
-          ];
-          addressMapping[granter] = [...addressMapping[granter], grant];
-        });
+        grants &&
+          grants.forEach((grant: Authorization) => {
+            const granter = grant.granter;
+            const cosmosAddress = getAddressByPrefix(granter, 'cosmos');
+            if (!addressMapping[granter]) addressMapping[granter] = [];
+            if (!allChainsAddressToGrants[cosmosAddress])
+              allChainsAddressToGrants[cosmosAddress] = {};
+            if (!allChainsAddressToGrants[cosmosAddress][chainID])
+              allChainsAddressToGrants[cosmosAddress][chainID] = [];
+            allChainsAddressToGrants[cosmosAddress][chainID] = [
+              ...allChainsAddressToGrants[cosmosAddress][chainID],
+              grant,
+            ];
+            addressMapping[granter] = [...addressMapping[granter], grant];
+          });
         state.AddressToChainAuthz = allChainsAddressToGrants;
         state.chains[chainID].GrantsToMeAddressMapping = addressMapping;
         state.chains[chainID].getGrantsToMeLoading = {
@@ -461,6 +462,8 @@ export const authzSlice = createSlice({
     builder
       .addCase(txAuthzExec.pending, (state, action) => {
         const chainID = action.meta.arg.basicChainInfo.chainID;
+        if (!state.chains[chainID])
+          state.chains[chainID] = cloneDeep(defaultState);
         const actionType = action.meta.arg.type;
         state.chains[chainID].tx.status = TxStatus.PENDING;
         state.chains[chainID].tx.errMsg = '';
@@ -480,6 +483,8 @@ export const authzSlice = createSlice({
     builder
       .addCase(txCreateAuthzGrant.pending, (state, action) => {
         const { chainID } = action.meta.arg.basicChainInfo;
+        if (!state.chains[chainID])
+          state.chains[chainID] = cloneDeep(defaultState);
         state.chains[chainID].tx.status = TxStatus.PENDING;
         state.chains[chainID].tx.errMsg = '';
       })
@@ -519,6 +524,8 @@ export const authzSlice = createSlice({
     builder
       .addCase(txAuthzRevoke.pending, (state, action) => {
         const chainID = action.meta.arg.basicChainInfo.chainID;
+        if (!state.chains[chainID])
+          state.chains[chainID] = cloneDeep(defaultState);
         state.chains[chainID].tx.status = TxStatus.PENDING;
         state.chains[chainID].tx.errMsg = '';
       })