forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgov.ts
64 lines (62 loc) · 2.27 KB
/
gov.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {
GOVERNANCE_PROPOSAL_TYPES,
GovProposalDecodedContent,
GovProposalVersion,
GovProposalWithDecodedContent,
GovernanceProposalActionData,
} from '@dao-dao/types'
import { Cosmos_govv1beta1Content_ToAmino } from '@dao-dao/types/protobuf/codegen/cosmos/gov/v1beta1/tx'
export const govProposalToDecodedContent = (
proposal: GovProposalWithDecodedContent
): GovProposalDecodedContent =>
proposal.version === GovProposalVersion.V1_BETA_1
? {
version: proposal.version,
title: proposal.title,
description: proposal.description,
decodedContent: proposal.decodedContent,
}
: {
version: proposal.version,
title: proposal.title || proposal.legacyContent?.[0]?.title || '',
description:
proposal.description ||
proposal.legacyContent?.[0]?.description ||
'',
decodedMessages: proposal.decodedMessages,
legacyContent: proposal.legacyContent,
}
/**
* Utility function to convert governance proposal action data to the decoded
* proposal content type, which gracefully handles both v1beta1 and v1 gov
* proposal types. This is used when rendering both existing governance
* proposals and previewing new proposals before submitting.
*
* @param data Data from the governance proposal action.
* @returns Decoded governance proposal content.
*/
export const govProposalActionDataToDecodedContent = (
data: GovernanceProposalActionData
): GovProposalDecodedContent =>
data.version === GovProposalVersion.V1_BETA_1 || data.useV1LegacyContent
? {
version: GovProposalVersion.V1_BETA_1,
title: data.title,
description: data.description,
decodedContent:
// Decode if necessary.
'value' in data.legacyContent &&
data.legacyContent.value instanceof Uint8Array
? GOVERNANCE_PROPOSAL_TYPES.find(
({ typeUrl }) => typeUrl === data.legacyContent.typeUrl
)?.fromProtoMsg(data.legacyContent) ||
(Cosmos_govv1beta1Content_ToAmino(data.legacyContent) as any)
: data.legacyContent,
}
: {
version: data.version,
title: data.title,
description: data.description,
decodedMessages: data.msgs,
legacyContent: [],
}