-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathselectors.ts
221 lines (208 loc) · 7.51 KB
/
selectors.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
import { RecoilValueReadOnly, selectorFamily, waitForAll } from 'recoil'
import {
DaoPreProposeApprovalSingleSelectors,
DaoPreProposeApproverSelectors,
} from '@dao-dao/state'
import { PreProposeModuleType, WithChainId } from '@dao-dao/types'
import { Proposal as DaoPreProposeApprovalSingleProposal } from '@dao-dao/types/contracts/DaoPreProposeApprovalSingle'
import { daoCoreProposalModulesSelector } from '../../../recoil'
/**
* Given the pre-propose approval ID of a pending proposal that has its approver
* set to a pre-propose-approver contract, retrieve the automatically-created
* proposal's ID in the approver's DAO.
*/
export const approverIdForPreProposeApprovalIdSelector = selectorFamily<
string,
WithChainId<{
// From proposal module (should be a dao-pre-propose-approval-single
// contract)
preProposeAddress: string
// From proposal module
proposalNumber: number
// Whether or not this proposal number refers to a pre-propose-approval
// proposal (as opposed to a proposal that was already approved and can be
// voted on).
isPreProposeApprovalProposal: boolean
// From PreProposeModuleApprovalConfig
approver: string
// From PreProposeModuleApprovalConfig
preProposeApproverContract: string
}>
>({
key: 'daoProposalSingleApproverIdForPreProposeApprovalId',
get:
({
chainId,
preProposeAddress,
proposalNumber,
isPreProposeApprovalProposal,
approver,
preProposeApproverContract,
}) =>
({ get }) => {
const preProposeApprovalNumber = isPreProposeApprovalProposal
? proposalNumber
: get(
// Get pre-propose proposal ID that was accepted to create this
// proposal.
DaoPreProposeApprovalSingleSelectors.queryExtensionSelector({
chainId,
contractAddress: preProposeAddress,
params: [
{
msg: {
completed_proposal_id_for_created_proposal_id: {
id: proposalNumber,
},
},
},
],
})
)
const [approverDaoProposalModules, approverProposalNumber] = get(
waitForAll([
// Get approver DAO's proposal modules to extract the prefix.
daoCoreProposalModulesSelector({
chainId,
coreAddress: approver,
}),
// Get approver proposal ID that was created to approve this
// pre-propose proposal.
DaoPreProposeApproverSelectors.queryExtensionSelector({
chainId,
contractAddress: preProposeApproverContract,
params: [
{
msg: {
approver_proposal_id_for_pre_propose_approval_id: {
id: preProposeApprovalNumber,
},
},
},
],
}) as RecoilValueReadOnly<number | null>,
])
)
// If no proposal number found, approver must not have been setup when
// this pre-propose approval proposal was created.
if (!approverProposalNumber) {
throw new Error(
'no approver proposal created for this pre-propose approval proposal'
)
}
// Get prefix of proposal module with dao-pre-propose-approver attached
// so we can link to the approver proposal.
const approverDaoApproverProposalModulePrefix =
approverDaoProposalModules.find(
(approverDaoProposalModule) =>
approverDaoProposalModule.prePropose?.type ===
PreProposeModuleType.Approver &&
approverDaoProposalModule.prePropose.address ===
preProposeApproverContract
)?.prefix
// The approver proposal module will not be found if it was disabled, so
// error since we can't determine the prefix.
if (!approverDaoApproverProposalModulePrefix) {
throw new Error(
`failed to find approver proposal module for ${approver}`
)
}
return `${approverDaoApproverProposalModulePrefix}${approverProposalNumber}`
},
})
/**
* Given an approver's proposal that approved a pre-propose approval proposal,
* retrieve the approved (completed) pre-propose approval proposal ID.
*/
export const approvedIdForPreProposeApproverIdSelector = selectorFamily<
string,
WithChainId<{
// From proposal module (should be a dao-pre-propose-approver contract)
preProposeAddress: string
// From proposal module
proposalNumber: number
// From PreProposeModuleApproverConfig
approvalDao: string
// From PreProposeModuleApprovalConfig
preProposeApprovalContract: string
}>
>({
key: 'daoProposalSingleApprovedIdForPreProposeApproverId',
get:
({
chainId,
preProposeAddress,
proposalNumber,
approvalDao,
preProposeApprovalContract,
}) =>
({ get }) => {
const [approvalDaoProposalModules, approvalProposalNumber] = get(
waitForAll([
// Get approval DAO's proposal modules to extract the prefix.
daoCoreProposalModulesSelector({
chainId,
coreAddress: approvalDao,
}),
// Get pre-propose-approval proposal ID that was approved by this
// proposal.
DaoPreProposeApproverSelectors.queryExtensionSelector({
chainId,
contractAddress: preProposeAddress,
params: [
{
msg: {
pre_propose_approval_id_for_approver_proposal_id: {
id: proposalNumber,
},
},
},
],
}) as RecoilValueReadOnly<number>,
])
)
// Get prefix of proposal module with dao-pre-propose-approval attached so
// we can link to the created proposal.
const approvalDaoApprovalProposalModulePrefix =
approvalDaoProposalModules.find(
(approvalDaoProposalModule) =>
approvalDaoProposalModule.prePropose?.type ===
PreProposeModuleType.Approval &&
approvalDaoProposalModule.prePropose.address ===
preProposeApprovalContract
)?.prefix
// The approval proposal module will not be found if it was disabled, so
// error since we can't determine the prefix.
if (!approvalDaoApprovalProposalModulePrefix) {
throw new Error(
`failed to find approval proposal module for ${approvalDao}`
)
}
// Get completed pre-propose proposal ID so we can extract the created
// proposal ID.
const completedPreProposeApprovalProposal: DaoPreProposeApprovalSingleProposal =
get(
DaoPreProposeApprovalSingleSelectors.queryExtensionSelector({
chainId,
contractAddress: preProposeApprovalContract,
params: [
{
msg: {
completed_proposal: {
id: approvalProposalNumber,
},
},
},
],
})
)
// Should never happen if the passed in approver proposal ID was executed
// and the proposal was created. Type-check for below.
if (!('approved' in completedPreProposeApprovalProposal.status)) {
throw new Error(
`pre-propose approval proposal ${approvalProposalNumber} was not approved`
)
}
return `${approvalDaoApprovalProposalModulePrefix}${completedPreProposeApprovalProposal.status.approved.created_proposal_id}`
},
})