-
Notifications
You must be signed in to change notification settings - Fork 515
/
ProposalWarnings.tsx
318 lines (298 loc) · 9.69 KB
/
ProposalWarnings.tsx
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
import { MANGO_INSTRUCTION_FORWARDER } from '@components/instructions/tools'
import { ExclamationCircleIcon } from '@heroicons/react/solid'
import { useBufferAccountsAuthority } from '@hooks/queries/bufferAuthority'
import { useGovernanceByPubkeyQuery } from '@hooks/queries/governance'
import { useSelectedProposalTransactions } from '@hooks/queries/proposalTransaction'
import { useRealmConfigQuery } from '@hooks/queries/realmConfig'
import useRealm from '@hooks/useRealm'
import {
BPF_UPGRADE_LOADER_ID,
Proposal,
getNativeTreasuryAddress,
} from '@solana/spl-governance'
import { useMemo } from 'react'
import { useAsync } from 'react-async-hook'
const SetRealmConfigWarning = () => (
<div className="rounded-md bg-yellow-50 p-4">
<div className="flex">
<div className="flex-shrink-0">
<ExclamationCircleIcon
className="h-5 w-5 text-yellow-400"
aria-hidden="true"
/>
</div>
<div className="ml-3">
<h3 className="text-sm font-medium text-yellow-800">
Instructions like this one are dangerous
</h3>
<div className="mt-2">
<p className="text-sm text-yellow-700">
This proposal writes to your realm configuration, this could affect
how votes are counted. Both the instruction data AND accounts list
contain parameters. Do not pass this proposal if there are any
accounts you do not recognize.
</p>
</div>
</div>
</div>
</div>
)
const ThirdPartyInstructionWritesConfigWarning = () => (
<div className="rounded-md bg-red-50 p-4">
<div className="flex">
<div className="flex-shrink-0">
<ExclamationCircleIcon
className="h-5 w-5 text-red-400"
aria-hidden="true"
/>
</div>
<div className="ml-3">
<h3 className="text-sm font-medium text-red-800">
Danger: This instruction uses an unknown program to modify your Realm
</h3>
<div className="mt-2">
<p className="text-sm text-red-700">
This proposal writes to your realm configuration, this could affect
how votes are counted. Writing realm configuration using an unknown
program is highly unusual.
</p>
</div>
</div>
</div>
</div>
)
const SetGovernanceConfig = () => (
<div className="rounded-md bg-yellow-50 p-4">
<div className="flex">
<div className="flex-shrink-0">
<ExclamationCircleIcon
className="h-5 w-5 text-yellow-400"
aria-hidden="true"
/>
</div>
<div className="ml-3">
<h3 className="text-sm font-medium text-yellow-800">
Instructions like this one are dangerous
</h3>
<div className="mt-2">
<p className="text-sm text-yellow-700">
This proposal writes to your governance configuration, this could
affect how votes are counted. Both the instruction data AND accounts
list contain parameters. Do not pass this proposal if there are any
accounts you do not recognize.
</p>
</div>
</div>
</div>
</div>
)
const PossibleWrongGovernance = () => (
<div className="rounded-md bg-yellow-50 p-4">
<div className="flex">
<div className="flex-shrink-0">
<ExclamationCircleIcon
className="h-5 w-5 text-yellow-400"
aria-hidden="true"
/>
</div>
<div className="ml-3">
<h3 className="text-sm font-medium text-yellow-800">
Possible wrong governance pass, check accounts.
</h3>
</div>
</div>
</div>
)
const ProgramUpgrade = () => (
<div className="rounded-md bg-yellow-50 p-4">
<div className="flex">
<div className="flex-shrink-0">
<ExclamationCircleIcon
className="h-5 w-5 text-yellow-400"
aria-hidden="true"
/>
</div>
<div className="ml-3">
<h3 className="text-sm font-medium text-yellow-800">
Instructions like this one are dangerous
</h3>
<div className="mt-2">
<p className="text-sm text-yellow-700">
This proposal upgrade program check params carefully
</p>
</div>
</div>
</div>
</div>
)
const BufferAuthorityMismatch = () => (
<div className="rounded-md bg-red-50 p-4">
<div className="flex">
<div className="flex-shrink-0">
<ExclamationCircleIcon
className="h-5 w-5 text-red-400"
aria-hidden="true"
/>
</div>
<div className="ml-3">
<h3 className="text-sm font-medium text-red-800">
Danger alert: The current buffer authority does not match the DAO
wallet
</h3>
<div className="mt-2">
<p className="text-sm text-red-700">
The current authority can change the buffer account during vote.
</p>
</div>
</div>
</div>
</div>
)
const ForwardWarning = () => (
<div className="rounded-md bg-yellow-50 p-4">
<div className="flex">
<div className="flex-shrink-0">
<ExclamationCircleIcon
className="h-5 w-5 text-yellow-400"
aria-hidden="true"
/>
</div>
<div className="ml-3">
<h3 className="text-sm font-medium text-yellow-800">
Instruction use instruction forward program:{' '}
{MANGO_INSTRUCTION_FORWARDER}
</h3>
<div className="mt-2">
<p className="text-sm text-yellow-700">
This means one of instruction is executable only by given wallet
until time set in proposal, check time and wallet in instruction
panel
</p>
</div>
</div>
</div>
</div>
)
const useProposalSafetyCheck = (proposal: Proposal) => {
const config = useRealmConfigQuery().data?.result
const { realmInfo } = useRealm()
const { data: transactions } = useSelectedProposalTransactions()
const { data: bufferAuthorities } = useBufferAccountsAuthority()
const governance = useGovernanceByPubkeyQuery(proposal?.governance).data
?.result
const isUsingForwardProgram = transactions
?.flatMap((tx) =>
tx.account.instructions.flatMap((ins) => ins.programId.toBase58())
)
.filter((x) => x === MANGO_INSTRUCTION_FORWARDER).length
const treasuryAddress = useAsync(
async () =>
governance !== undefined
? getNativeTreasuryAddress(governance.owner, governance.pubkey)
: undefined,
[governance]
)
const walletsPassedToInstructions = transactions?.flatMap((tx) =>
tx.account.instructions?.flatMap((ins) =>
ins.accounts.map((acc) => acc.pubkey)
)
)
const proposalWarnings = useMemo(() => {
if (realmInfo === undefined || transactions === undefined) return []
const ixs = transactions.flatMap((pix) => pix.account.getAllInstructions())
const possibleWrongGovernance =
treasuryAddress.result &&
!!transactions?.length &&
!walletsPassedToInstructions?.find(
(x) =>
x &&
(governance?.pubkey?.equals(x) || treasuryAddress.result?.equals(x))
)
const proposalWarnings: (
| 'setGovernanceConfig'
| 'setRealmConfig'
| 'thirdPartyInstructionWritesConfig'
| 'possibleWrongGovernance'
| 'programUpgrade'
| 'usingMangoInstructionForwarder'
| 'bufferAuthorityMismatch'
| undefined
)[] = []
proposalWarnings.push(
...ixs.map((ix) => {
if (ix.programId.equals(realmInfo.programId) && ix.data[0] === 19) {
return 'setGovernanceConfig'
}
if (ix.programId.equals(realmInfo.programId) && ix.data[0] === 22) {
return 'setRealmConfig'
}
if (ix.programId.equals(BPF_UPGRADE_LOADER_ID)) {
return 'programUpgrade'
}
if (
ix.accounts.find(
(a) => a.isWritable && config && a.pubkey.equals(config.pubkey)
) !== undefined
) {
if (ix.programId.equals(realmInfo.programId)) {
return 'setRealmConfig'
} else {
return 'thirdPartyInstructionWritesConfig'
}
}
if (isUsingForwardProgram) {
return 'usingMangoInstructionForwarder'
}
})
)
if (possibleWrongGovernance) {
proposalWarnings.push('possibleWrongGovernance')
}
if (treasuryAddress.result) {
const treasury = treasuryAddress.result
if (
governance &&
bufferAuthorities?.some(
(authority) =>
!authority.equals(treasury) && !authority.equals(governance.pubkey)
)
) {
proposalWarnings.push('bufferAuthorityMismatch')
}
}
return proposalWarnings
}, [
realmInfo,
config,
transactions,
walletsPassedToInstructions,
governance?.pubkey,
treasuryAddress.result,
])
return proposalWarnings
}
const ProposalWarnings = ({ proposal }: { proposal: Proposal }) => {
const warnings = useProposalSafetyCheck(proposal)
return (
<>
{warnings?.includes('setGovernanceConfig') && <SetGovernanceConfig />}
{warnings?.includes('setRealmConfig') && <SetRealmConfigWarning />}
{warnings?.includes('thirdPartyInstructionWritesConfig') && (
<ThirdPartyInstructionWritesConfigWarning />
)}
{warnings?.includes('possibleWrongGovernance') && (
<PossibleWrongGovernance></PossibleWrongGovernance>
)}
{warnings?.includes('programUpgrade') && (
<ProgramUpgrade></ProgramUpgrade>
)}
{warnings?.includes('usingMangoInstructionForwarder') && (
<ForwardWarning></ForwardWarning>
)}
{warnings?.includes('bufferAuthorityMismatch') && (
<BufferAuthorityMismatch />
)}
</>
)
}
export default ProposalWarnings