-
Notifications
You must be signed in to change notification settings - Fork 22
/
proposal-module-adapter.ts
396 lines (347 loc) · 11.4 KB
/
proposal-module-adapter.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import { Chain } from '@chain-registry/types'
import { QueryClient } from '@tanstack/react-query'
import { CSSProperties, ComponentType, ReactNode } from 'react'
import { FieldPath, FieldValues } from 'react-hook-form'
import { RecoilValueReadOnly } from 'recoil'
import { Action, ActionCategoryMaker, ActionMaker } from './actions'
import {
DaoInfoCard,
LinkWrapperProps,
ProposalVoterProps,
SelfRelayExecuteModalProps,
} from './components'
import { Expiration } from './contracts'
import {
CheckedDepositInfo,
Duration,
ProposalStatus,
} from './contracts/common'
import { Proposal as DaoPreProposeApprovalProposal } from './contracts/DaoPreProposeApprovalSingle'
import { VetoConfig } from './contracts/DaoProposalSingle.v2'
import {
DaoCreationGetInstantiateInfo,
DaoCreationVotingConfigItem,
PreProposeModule,
ProposalDraft,
ProposalModule,
} from './dao'
import { ContractVersion } from './features'
import { LoadingData } from './misc'
import { ProposalCreatedCardProps, ProposalTimestampInfo } from './proposal'
export type IProposalModuleAdapterCommon<FormData extends FieldValues = any> = {
// Fields
fields: {
// Make this a function so it doesn't return the same instance of the form
// data each time.
makeDefaultNewProposalForm: () => FormData
newProposalFormTitleKey: FieldPath<FormData>
updateConfigActionMaker: ActionMaker
updatePreProposeConfigActionMaker?: ActionMaker
// Any extra actions added by the proposal module.
actionCategoryMakers?: ActionCategoryMaker[]
}
// Selectors
selectors: {
proposalCount: ProposalCountSelector
reverseProposalInfos: ReverseProposalInfosSelector
reversePreProposePendingProposalInfos?: ReversePreProposePendingProposalInfosSelector
reversePreProposeCompletedProposalInfos?: ReversePreProposeCompletedProposalInfosSelector
depositInfo: DepositInfoSelector
maxVotingPeriod: MaxVotingPeriodSelector
}
// Hooks
hooks: {
useProposalDaoInfoCards: () => DaoInfoCard[]
}
// Components
components: {
NewProposal: ComponentType<BaseNewProposalProps>
}
}
export type IProposalModuleAdapter<Vote extends unknown = any> = {
// Functions
functions: {
getProposalInfo: () => Promise<CommonProposalInfo | undefined>
}
// Hooks
hooks: {
useProposalRefreshers: () => ProposalRefreshers
useLoadingProposalExecutionTxHash: () => LoadingData<string | undefined>
useLoadingProposalStatus: () => LoadingData<ProposalStatus>
useLoadingVoteOptions: () => LoadingData<ProposalVoteOption<Vote>[]>
// Return when no wallet connected.
useLoadingWalletVoteInfo: () =>
| undefined
| LoadingData<WalletVoteInfo<Vote>>
useCastVote: (onSuccess?: () => void | Promise<void>) => {
castVote: (vote: Vote) => Promise<void>
castingVote: boolean
}
useLoadingPreProposeApprovalProposal: () => LoadingData<
PreProposeApprovalProposalWithMeteadata | undefined
>
}
// Components
components: {
ProposalStatusAndInfo: ComponentType<BaseProposalStatusAndInfoProps>
ProposalVoter: ComponentType<BaseProposalVoterProps>
ProposalInnerContentDisplay: ComponentType<BaseProposalInnerContentDisplayProps>
ProposalWalletVote: ComponentType<BaseProposalWalletVoteProps<Vote>>
ProposalVotes: ComponentType<BaseProposalVotesProps>
ProposalVoteTally: ComponentType
ProposalLine: ComponentType<BaseProposalLineProps>
PreProposeApprovalProposalStatusAndInfo?: ComponentType<BasePreProposeProposalStatusAndInfoProps>
PreProposeApprovalInnerContentDisplay?: ComponentType<BasePreProposeApprovalInnerContentDisplayProps>
PreProposeApprovalProposalLine?: ComponentType<BaseProposalLineProps>
}
}
export type ProposalModuleAdapter<
DaoCreationExtraVotingConfig extends FieldValues = any,
Vote extends unknown = any,
FormData extends FieldValues = any
> = {
id: string
contractNames: string[]
loadCommon: (
options: IProposalModuleAdapterCommonOptions
) => IProposalModuleAdapterCommon<FormData>
load: (options: IProposalModuleAdapterOptions) => IProposalModuleAdapter<Vote>
queries: {
proposalCount: {
indexerFormula?: string
cosmWasmQuery: Record<string, unknown>
}
}
functions: {
fetchPrePropose?: FetchPreProposeFunction
fetchVetoConfig?: FetchVetoConfig
}
daoCreation: {
// Voting config added to the common voting config.
extraVotingConfig?: {
default: DaoCreationExtraVotingConfig
items?: DaoCreationVotingConfigItem[]
advancedItems?: DaoCreationVotingConfigItem[]
advancedWarningI18nKeys?: string[]
}
getInstantiateInfo: DaoCreationGetInstantiateInfo<DaoCreationExtraVotingConfig>
}
}
export type IProposalModuleAdapterCommonOptions = {
chain: Chain
coreAddress: string
proposalModule: ProposalModule
}
export type IProposalModuleAdapterCommonInitialOptions = Omit<
IProposalModuleAdapterCommonOptions,
'proposalModule'
>
export type IProposalModuleAdapterOptions = {
/**
* The DAO's native chain.
*/
chain: Chain
/**
* The DAO's core contract address.
*/
coreAddress: string
/**
* The proposal module.
*/
proposalModule: ProposalModule
/**
* The proposal ID unique across all proposal modules. They include the
* proposal module's prefix, the proposal number within the proposal module,
* and potentially an asterisk in the middle to indicate a
* pre-propose-approval proposal.
*/
proposalId: string
/**
* The proposal number used by the proposal module to identify this proposal.
*/
proposalNumber: number
/**
* Whether or not this refers to a pre-propose-approval proposal. If this is
* true, the proposal ID should contain an asterisk (*) between the proposal
* module prefix and proposal number.
*/
isPreProposeApprovalProposal: boolean
}
export type IProposalModuleAdapterInitialOptions = Omit<
IProposalModuleAdapterOptions,
| 'proposalModule'
| 'proposalId'
| 'proposalNumber'
| 'isPreProposeApprovalProposal'
>
/**
* Proposal module adapter context. This is specific to a single proposal in a
* single proposal module.
*/
export type IProposalModuleContext = {
id: string
options: IProposalModuleAdapterOptions
adapter: IProposalModuleAdapter
common: IProposalModuleAdapterCommon
}
/**
* Common proposal module adapter context. This is not specific to any single
* proposal, but is specific to a single proposal module. This could be used to
* retrieve the current proposal module's config for example.
*/
export type IProposalModuleCommonContext = {
id: string
options: IProposalModuleAdapterCommonOptions
common: IProposalModuleAdapterCommon
}
// Internal Adapter Types
export type FetchPreProposeFunction = (
queryClient: QueryClient,
chainId: string,
proposalModuleAddress: string,
version: ContractVersion | null
) => Promise<PreProposeModule | null>
export type FetchVetoConfig = (
chainId: string,
proposalModuleAddress: string,
version: ContractVersion | null
) => Promise<VetoConfig | null>
export type ReverseProposalInfosSelector = (data: {
startBefore: number | undefined
limit: number | undefined
}) => RecoilValueReadOnly<CommonProposalListInfo[]>
export type ProposalCountSelector = RecoilValueReadOnly<number>
export type ReversePreProposePendingProposalInfosSelector = (data: {
startBefore: number | undefined
limit: number | undefined
}) => RecoilValueReadOnly<CommonProposalListInfo[]>
export type ReversePreProposeCompletedProposalInfosSelector = (data: {
startBefore: number | undefined
limit: number | undefined
}) => RecoilValueReadOnly<CommonProposalListInfo[]>
export type DepositInfoSelector = RecoilValueReadOnly<
CheckedDepositInfo | undefined
>
export type MaxVotingPeriodSelector = RecoilValueReadOnly<Duration>
export type CommonProposalListInfo = {
id: string
proposalNumber: number
timestamp: Date | undefined
isOpen: boolean
// If true, will be not be shown in the proposal list. This is used for
// example to hide completed pre-propose proposals that were approved, since
// those show up as normal proposals. No need to double count.
hideFromList?: boolean
}
export type CommonProposalInfo = {
id: string
title: string
description: string
expiration: Expiration | null
createdAtEpoch: number | null
createdByAddress: string
}
export type BaseProposalStatusAndInfoProps = {
inline?: boolean
// Open self-relay modal to execute a proposal and relay IBC packets.
openSelfRelayExecute: (
props: Pick<
SelfRelayExecuteModalProps,
'uniqueId' | 'chainIds' | 'crossChainPackets' | 'transaction'
>
) => void
onExecuteSuccess: () => void | Promise<void>
onCloseSuccess: () => void | Promise<void>
onVetoSuccess: () => void | Promise<void>
} & {
voter: BaseProposalVoterProps
}
export type BaseProposalVoterProps = {
onVoteSuccess: () => void | Promise<void>
} & Pick<ProposalVoterProps, 'seenAllActionPages'>
export type BaseProposalVotesProps = {
/**
* An optional class name.
*/
className?: string
}
export type BasePreProposeProposalStatusAndInfoProps = Pick<
BaseProposalStatusAndInfoProps,
'inline'
>
export type BaseProposalInnerContentDisplayProps<
FormData extends FieldValues = any
> = {
// Once proposal messages are loaded, the inner component is responsible for
// setting the duplicate form data for the duplicate button in the header.
setDuplicateFormData?: (data: FormData) => void
actionsForMatching: Action[]
// Called when the user has viewed all action pages.
setSeenAllActionPages?: () => void
}
export type BasePreProposeApprovalInnerContentDisplayProps =
BaseProposalInnerContentDisplayProps
export type BaseProposalWalletVoteProps<T> = {
vote: T | undefined
fallback: 'pending' | 'hasNoVote'
}
export type BaseProposalLineProps = {
href: string
onClick?: () => void
LinkWrapper: ComponentType<LinkWrapperProps>
}
export type BaseNewProposalProps<FormData extends FieldValues = any> = {
onCreateSuccess: (props: ProposalCreatedCardProps) => void
draft?: ProposalDraft<FormData>
saveDraft: () => void
drafts: ProposalDraft[]
loadDraft?: (index: number) => void
unloadDraft: () => void
draftSaving: boolean
deleteDraft: (index: number) => void
proposalModuleSelector: ReactNode
// If true, will display actions as read only. This is useful when prompting a
// proposal to be created from preset actions. Default: false.
actionsReadOnlyMode?: boolean
/**
* The ProposalDaoInfoCards stateful component that renders the proposal
* module's config cards.
*/
ProposalDaoInfoCards: ComponentType
}
export type WalletVoteInfo<T> = {
// Present if voted.
vote: T | undefined
couldVote: boolean
canVote: boolean
votingPowerPercent: number
}
export type ProposalRefreshers = {
refreshProposal: () => void
refreshProposalAndAll: () => void
refreshing: boolean
}
export type ProposalVoteOption<Vote> = {
Icon: ComponentType<{ className: string; style?: CSSProperties }>
label: string
value: Vote
color?: string
}
export type PercentOrMajorityValue = {
/**
* Whether or not to use majority instead of percent.
*/
majority: boolean
/**
* The percent to use when `majority` is false.
*/
value: number
}
export type PreProposeApprovalProposalWithMeteadata =
DaoPreProposeApprovalProposal & {
timestampDisplay: ProposalTimestampInfo['display']
// If this pre-propose-approval proposal is being approved by a
// pre-propose-approver proposal in another DAO, this is the approval
// proposal ID.
approverProposalId?: string
}