Skip to content

Commit

Permalink
wip(gov): add get proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
Hemanthghs committed Nov 25, 2023
1 parent 86ebba2 commit 6c9340b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
8 changes: 6 additions & 2 deletions frontend/src/store/features/gov/govService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import Axios, { AxiosResponse } from 'axios';
import { convertPaginationToParams, cleanURL } from '../../../utils/util';
import { GetProposalsInVotingResponse, ProposalVote } from '@/types/gov';
import {
GetProposalsInVotingResponse,
GovProposal,
ProposalVote,
} from '@/types/gov';

const proposalsURL = '/cosmos/gov/v1beta1/proposals';
const proposalTallyURL = (id: number): string =>
Expand Down Expand Up @@ -56,7 +60,7 @@ const fetchVoterVote = (
const fetchProposal = (
baseURL: string,
proposalId: number
): Promise<AxiosResponse> =>
): Promise<AxiosResponse<{ proposal: GovProposal }>> =>
Axios.get(`${cleanURL(baseURL)}${proposalsURL}/${proposalId}`);

const fetchDepositParams = (baseURL: string): Promise<AxiosResponse> =>
Expand Down
69 changes: 69 additions & 0 deletions frontend/src/store/features/gov/govSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import {
GovPagination,
DepositParams,
GetDepositParamsInputs,
GetProposalInputs,
} from '@/types/gov';
import { PROPOSAL_STATUS_VOTING_PERIOD } from '@/utils/constants';

interface Chain {
active: {
Expand Down Expand Up @@ -48,6 +50,10 @@ interface Chains {
interface GovState {
chains: Chains;
defaultState: Chain;
proposalInfo: {
status: TxStatus;
errMsg: string;
};
}

const EMPTY_PAGINATION = {
Expand Down Expand Up @@ -100,8 +106,29 @@ const initialState: GovState = {
proposalTally: {},
},
},
proposalInfo: {
status: TxStatus.INIT,
errMsg: '',
},
};

export const getProposal = createAsyncThunk(
'gov/proposal-info',
async (data: GetProposalInputs, { rejectWithValue }) => {
try {
const response = await govService.proposal(data.baseURL, data.proposalId);
return {
chainID: data.chainID,
data: response.data,
};
} catch (error) {
if (error instanceof AxiosError)
return rejectWithValue({ message: error.message });
return rejectWithValue({ message: ERR_UNKNOWN });
}
}
);

export const getProposalsInDeposit = createAsyncThunk(
'gov/deposit-proposals',
async (data: GetProposalsInDepositInputs, { rejectWithValue, dispatch }) => {
Expand Down Expand Up @@ -402,6 +429,48 @@ export const govSlice = createSlice({
state.chains[chainID].depositParams.status = TxStatus.REJECTED;
state.chains[chainID].depositParams.errMsg = payload.message || '';
});

// get one proposal
builder
.addCase(getProposal.pending, (state, action) => {
const chainID = action.meta?.arg?.chainID;
if (!state.chains[chainID])
state.chains[chainID] = cloneDeep(initialState.defaultState);
state.proposalInfo.status = TxStatus.PENDING;
})
.addCase(getProposal.fulfilled, (state, action) => {
const chainID = action.meta.arg?.chainID || '';
if (
action.payload.data?.proposal.status === PROPOSAL_STATUS_VOTING_PERIOD
) {
const currentProposalsState = state.chains[chainID].active.proposals;
if (!currentProposalsState.length) {
const result = {
status: TxStatus.IDLE,
errMsg: '',
proposals: [action.payload.data.proposal],
};
state.chains[chainID].active = result;
}
} else {
const currentProposalsState = state.chains[chainID].deposit.proposals;
if (!currentProposalsState.length) {
const result = {
status: TxStatus.IDLE,
errMsg: '',
proposals: [action.payload.data.proposal],
};
state.chains[chainID].deposit = result;
}
}
state.proposalInfo.status = TxStatus.IDLE;
state.proposalInfo.errMsg = '';
})
.addCase(getProposal.rejected, (state, action) => {
state.proposalInfo.status = TxStatus.REJECTED;
const payload = action.payload as { message: string };
state.proposalInfo.errMsg = payload.message || '';
});
},
});

Expand Down
10 changes: 8 additions & 2 deletions frontend/src/types/gov.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface GovPagination {
}

interface GetProposalsInVotingResponse {
proposals: ActiveProposal[];
proposals: GovProposal[];
pagination: GovPagination;
}

Expand Down Expand Up @@ -145,4 +145,10 @@ interface GetProposalTallyInputs {
interface GetDepositParamsInputs {
baseURL: string;
chainID: string;
}
}

interface GetProposalInputs {
baseURL: string;
proposalId: number;
chainID: string;
}
3 changes: 2 additions & 1 deletion frontend/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const GAS_FEE = 860000
export const GAS_FEE = 860000
export const PROPOSAL_STATUS_VOTING_PERIOD = "PROPOSAL_STATUS_VOTING_PERIOD"

0 comments on commit 6c9340b

Please sign in to comment.