forked from akure/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proposal.ts
104 lines (99 loc) · 3.04 KB
/
proposal.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { DurationUnits, ProposalVetoConfig } from '@dao-dao/types'
import {
ProposalStatus,
ProposalStatusKey,
} from '@dao-dao/types/contracts/common'
import {
ProposalStatus as PreProposeStatus,
ProposalStatusKey as PreProposeStatusKey,
} from '@dao-dao/types/contracts/DaoPreProposeApprovalSingle'
import { VetoConfig } from '@dao-dao/types/contracts/DaoProposalSingle.v2'
import {
convertDurationToDurationWithUnits,
convertDurationWithUnitsToDuration,
} from './conversion'
// Get the status key from the weirdly-formatted status enum.
export const keyFromPreProposeStatus = (
status: PreProposeStatus
): PreProposeStatusKey => Object.keys(status)[0] as PreProposeStatusKey
/**
* Returns the flattened key of the proposal status.
*
* @param {ProposalStatus} status - The proposal status.
* @return {ProposalStatusKey} The flattened key of the proposal status.
*/
export const getProposalStatusKey = (
status: ProposalStatus
): ProposalStatusKey =>
typeof status === 'string'
? status
: typeof status === 'object' && status
? (Object.keys(status)[0] as any)
: (() => {
throw new Error('Invalid proposal status.')
})()
/**
* Convert veto config into the veto object expected by proposal modules.
*/
export const convertVetoConfigToCosmos = (
veto: ProposalVetoConfig
): VetoConfig | null =>
veto.enabled
? {
vetoer:
// If more than one address set, there should be a cw1-whitelist
// contract created. Otherwise, use the first address.
veto.addresses.length > 1
? veto.cw1WhitelistAddress || ''
: veto.addresses.length === 1
? veto.addresses[0].address
: '',
timelock_duration: convertDurationWithUnitsToDuration(
veto.timelockDuration
),
early_execute: veto.earlyExecute,
veto_before_passed: veto.vetoBeforePassed,
}
: null
/**
* Convert proposal module veto config into custom veto type. If config is
* empty, returns default.
*/
export const convertCosmosVetoConfigToVeto = (
veto: VetoConfig | null | undefined,
// If provided, `veto.vetoer` should be a cw1-whitelist contract address, and
// this should be its list of admins.
cw1WhitelistAdmins?: string[]
): ProposalVetoConfig =>
veto
? {
enabled: true,
addresses: cw1WhitelistAdmins?.map((address) => ({
address,
})) || [
{
address: veto.vetoer,
},
],
cw1WhitelistAddress: cw1WhitelistAdmins ? veto.vetoer : undefined,
timelockDuration: convertDurationToDurationWithUnits(
veto.timelock_duration
),
earlyExecute: veto.early_execute,
vetoBeforePassed: veto.veto_before_passed,
}
: {
enabled: false,
addresses: [
{
address: '',
},
],
cw1WhitelistAddress: undefined,
timelockDuration: {
value: 1,
units: DurationUnits.Weeks,
},
earlyExecute: true,
vetoBeforePassed: false,
}