Skip to content

Commit

Permalink
fix v1beta1 gov prop decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Apr 5, 2024
1 parent d48ba48 commit 3d0384a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 34 deletions.
47 changes: 29 additions & 18 deletions packages/state/recoil/selectors/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ export const govProposalsSelector = selectorFamily<
v1Beta1Proposals = indexerProposals.flatMap(
({ data }): ProposalV1Beta1 | [] => {
try {
return ProposalV1Beta1.decode(fromBase64(data))
return ProposalV1Beta1.decode(fromBase64(data), undefined, true)
} catch {
return []
}
Expand Down Expand Up @@ -824,22 +824,26 @@ export const govProposalsSelector = selectorFamily<
pagination: undefined,
},
'proposals',
true,
true
)
total = v1Beta1Proposals.length
} else {
const response = await client.gov.v1beta1.proposals({
proposalStatus: status,
voter: '',
depositor: '',
pagination: {
key: new Uint8Array(),
offset: BigInt(offset || 0),
limit: BigInt(limit || 0),
countTotal: true,
reverse: true,
const response = await client.gov.v1beta1.proposals(
{
proposalStatus: status,
voter: '',
depositor: '',
pagination: {
key: new Uint8Array(),
offset: BigInt(offset || 0),
limit: BigInt(limit || 0),
countTotal: true,
reverse: true,
},
},
})
true
)
v1Beta1Proposals = response.proposals
total = Number(response.pagination?.total || 0)
}
Expand Down Expand Up @@ -911,7 +915,11 @@ export const govProposalSelector = selectorFamily<
return await decodeGovProposal({
version: GovProposalVersion.V1_BETA_1,
id: BigInt(proposalId),
proposal: ProposalV1Beta1.decode(fromBase64(indexerProposal.data)),
proposal: ProposalV1Beta1.decode(
fromBase64(indexerProposal.data),
undefined,
true
),
})
}
}
Expand All @@ -933,7 +941,7 @@ export const govProposalSelector = selectorFamily<
return await decodeGovProposal({
version: GovProposalVersion.V1,
id: BigInt(proposalId),
proposal: proposal,
proposal,
})
} catch (err) {
// Fallback to v1beta1 query if v1 not supported.
Expand All @@ -948,9 +956,12 @@ export const govProposalSelector = selectorFamily<
}

const proposal = (
await client.gov.v1beta1.proposal({
proposalId: BigInt(proposalId),
})
await client.gov.v1beta1.proposal(
{
proposalId: BigInt(proposalId),
},
true
)
).proposal
if (!proposal) {
throw new Error('Proposal not found')
Expand All @@ -959,7 +970,7 @@ export const govProposalSelector = selectorFamily<
return await decodeGovProposal({
version: GovProposalVersion.V1_BETA_1,
id: BigInt(proposalId),
proposal: proposal,
proposal,
})
},
})
Expand Down
13 changes: 9 additions & 4 deletions packages/stateful/server/makeGetGovStaticProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ export const makeGetGovProposalStaticProps = ({
version: GovProposalVersion.V1_BETA_1,
id: BigInt(proposalId),
proposal: ProposalV1Beta1.decode(
fromBase64(indexerProposal.data)
fromBase64(indexerProposal.data),
undefined,
true
),
})
}
Expand Down Expand Up @@ -281,9 +283,12 @@ export const makeGetGovProposalStaticProps = ({

if (!proposal) {
const proposalV1Beta1 = (
await client.gov.v1beta1.proposal({
proposalId: BigInt(proposalId),
})
await client.gov.v1beta1.proposal(
{
proposalId: BigInt(proposalId),
},
true
)
).proposal
if (!proposalV1Beta1) {
throw new Error('NOT_FOUND')
Expand Down
1 change: 1 addition & 0 deletions packages/utils/messages/protobuf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const decodeGovProposal = async (
if (govProposal.version === GovProposalVersion.V1_BETA_1) {
let title = govProposal.proposal.content?.title || ''
let description = govProposal.proposal.content?.description || ''

// If content not decoded and stuck as Any, decode as TextProposal to get
// the title and description.
if (
Expand Down
28 changes: 16 additions & 12 deletions packages/utils/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,30 @@ export const getAllRpcResponse = async <
R extends { pagination?: PageResponse; [key: string]: any },
K extends keyof R
>(
queryFn: (params: P) => Promise<R>,
queryFn: (params: P, useInterfaces?: boolean) => Promise<R>,
params: P,
key: K,
reverse = false
reverse = false,
useInterfaces = false
): Promise<R[K]> => {
let pagination: Partial<PageRequest> | undefined
const data = [] as any[]

do {
const response = await queryFn({
...params,
pagination: {
key: new Uint8Array(),
...pagination,
reverse,
// Get all.
offset: 0n,
limit: BigInt(Number.MAX_SAFE_INTEGER),
const response = await queryFn(
{
...params,
pagination: {
key: new Uint8Array(),
...pagination,
reverse,
// Get all.
offset: 0n,
limit: BigInt(Number.MAX_SAFE_INTEGER),
},
},
})
useInterfaces
)

pagination = response.pagination?.nextKey?.length
? {
Expand Down

0 comments on commit 3d0384a

Please sign in to comment.