Skip to content

Commit 28bd211

Browse files
authored
feat: Implement separate state for multisig delegations (#1468)
2 parents 0e2151e + cc65493 commit 28bd211

File tree

5 files changed

+102
-5
lines changed

5 files changed

+102
-5
lines changed

frontend/src/app/(routes)/multisig/components/multisig-account/MultisigAccount.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useAppDispatch, useAppSelector } from '@/custom-hooks/StateHooks';
22
import useGetChainInfo from '@/custom-hooks/useGetChainInfo';
33
import {
4+
getDelegations as getMultisigDelegations,
45
getMultisigAccounts,
56
getMultisigBalances,
67
multisigByAddress,
@@ -10,7 +11,6 @@ import {
1011
resetUpdateTxnState,
1112
setVerifyDialogOpen,
1213
} from '@/store/features/multisig/multisigSlice';
13-
import { getDelegations } from '@/store/features/staking/stakeSlice';
1414
import { useEffect, useState } from 'react';
1515
import MultisigAccountHeader from './MultisigAccountHeader';
1616
import Copy from '@/components/common/Copy';
@@ -92,7 +92,7 @@ const MultisigAccount = ({
9292
})
9393
);
9494
dispatch(
95-
getDelegations({
95+
getMultisigDelegations({
9696
baseURLs: restURLs,
9797
address: multisigAddress,
9898
chainID,
@@ -202,7 +202,7 @@ const MultisigAccountInfo = ({
202202
(state) => state.multisig.multisigAccounts
203203
);
204204
const totalStaked = useAppSelector(
205-
(state) => state.staking.chains?.[chainID]?.delegations.totalStaked
205+
(state) => state.multisig?.delegations.totalStaked
206206
);
207207
const balance = useAppSelector((state) => state.multisig.balance.balance);
208208

frontend/src/app/(routes)/multisig/components/txns/ReDelegate.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const ReDelegate: React.FC<ReDelegateProps> = (props) => {
4848
);
4949

5050
const delegations = useAppSelector(
51-
(state: RootState) => state.staking.chains[chainID].delegations
51+
(state: RootState) => state.multisig.delegations
5252
)
5353

5454
useEffect(() => {

frontend/src/app/(routes)/multisig/components/txns/UnDelegate.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const UnDelegate: React.FC<UnDelegateProps> = (props) => {
4646
);
4747

4848
const delegations = useAppSelector(
49-
(state: RootState) => state.staking.chains[chainID].delegations
49+
(state: RootState) => state.multisig.delegations
5050
);
5151

5252
useEffect(() => {

frontend/src/store/features/multisig/multisigSlice.ts

+88
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { get } from 'lodash';
4848
import { getAuthToken } from '@/utils/localStorage';
4949
import multisigSigning from '@/app/(routes)/multisig/utils/multisigSigning';
5050
import { setError } from '../common/commonSlice';
51+
import stakingService from '../staking/stakingService';
5152

5253
const initialState: MultisigState = {
5354
createMultisigAccountRes: {
@@ -88,6 +89,21 @@ const initialState: MultisigState = {
8889
status: TxStatus.INIT,
8990
error: '',
9091
},
92+
delegations: {
93+
status: TxStatus.INIT,
94+
delegations: {
95+
delegation_responses: [],
96+
pagination: {
97+
next_key: '',
98+
total: '',
99+
},
100+
},
101+
hasDelegations: false,
102+
errMsg: '',
103+
pagination: undefined,
104+
delegatedTo: {},
105+
totalStaked: 0.0,
106+
},
91107
createTxnRes: {
92108
status: TxStatus.INIT,
93109
error: '',
@@ -287,6 +303,49 @@ export const getMultisigBalances = createAsyncThunk(
287303
}
288304
);
289305

306+
export const getDelegations = createAsyncThunk(
307+
'multisig/delegations',
308+
async (
309+
data: {
310+
baseURLs: string[];
311+
address: string;
312+
chainID: string;
313+
},
314+
{ rejectWithValue }
315+
) => {
316+
try {
317+
const delegations = [];
318+
let nextKey = null;
319+
const limit = 100;
320+
while (true) {
321+
const response = await stakingService.delegations(
322+
data.baseURLs,
323+
data.address,
324+
data.chainID,
325+
nextKey
326+
? {
327+
key: nextKey,
328+
limit: limit,
329+
}
330+
: {}
331+
);
332+
delegations.push(...(response.data?.delegation_responses || []));
333+
if (!response.data.pagination?.next_key) {
334+
break;
335+
}
336+
nextKey = response.data.pagination.next_key;
337+
}
338+
339+
return {
340+
delegations: delegations,
341+
};
342+
} catch (error) {
343+
if (error instanceof AxiosError) return rejectWithValue(error.message);
344+
return rejectWithValue(ERR_UNKNOWN);
345+
}
346+
}
347+
);
348+
290349
export const createTxn = createAsyncThunk(
291350
'multisig/createTxn',
292351
async (data: CreateTxnInputs, { rejectWithValue }) => {
@@ -731,6 +790,35 @@ export const multisigSlice = createSlice({
731790
const payload = action.payload as { message: string };
732791
state.balance.error = payload.message || '';
733792
});
793+
builder
794+
.addCase(getDelegations.pending, (state) => {
795+
state.delegations.status = TxStatus.PENDING;
796+
state.delegations.errMsg = '';
797+
})
798+
.addCase(getDelegations.fulfilled, (state, action) => {
799+
const delegation_responses = action.payload.delegations;
800+
if (delegation_responses?.length) {
801+
state.delegations.hasDelegations = true;
802+
}
803+
state.delegations.status = TxStatus.IDLE;
804+
state.delegations.delegations.delegation_responses =
805+
delegation_responses;
806+
state.delegations.errMsg = '';
807+
808+
let total = 0.0;
809+
for (let i = 0; i < delegation_responses.length; i++) {
810+
const delegation = delegation_responses[i];
811+
state.delegations.delegatedTo[
812+
delegation?.delegation?.validator_address
813+
] = true;
814+
total += parseFloat(delegation?.delegation?.shares);
815+
}
816+
state.delegations.totalStaked = total;
817+
})
818+
.addCase(getDelegations.rejected, (state, action) => {
819+
state.delegations.status = TxStatus.REJECTED;
820+
state.delegations.errMsg = action.error.message || '';
821+
});
734822
builder
735823
.addCase(createTxn.pending, (state) => {
736824
state.createTxnRes.status = TxStatus.PENDING;

frontend/src/types/multisig.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ interface MultisigState {
117117
deleteMultisigRes: TxRes;
118118
multisigAccount: MultisigAccount;
119119
balance: Balance;
120+
delegations: {
121+
status: TxStatus;
122+
delegations: GetDelegationsResponse;
123+
hasDelegations: boolean;
124+
errMsg: string;
125+
pagination: Pagination | undefined;
126+
delegatedTo: Record<string, boolean>;
127+
totalStaked: number;
128+
};
120129
createTxnRes: TxRes;
121130
updateTxnRes: TxRes;
122131
txns: Txns;

0 commit comments

Comments
 (0)