-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseProposal.ts
424 lines (398 loc) · 12.9 KB
/
useProposal.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
* This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course.
* © Copyright Utrecht University (Department of Information and Computing Sciences)
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { useEffect, useState } from 'react';
import { ProposalApproveSpendingAction } from '@/src/components/proposal/actions/ApproveSpendingAction';
import { ProposalChangeParamAction } from '@/src/components/proposal/actions/ChangeParamAction';
import {
FacetCutAction,
ProposalDiamondCutAction,
} from '@/src/components/proposal/actions/DiamondCutAction';
import { ProposalMergeAction } from '@/src/components/proposal/actions/MergeAction';
import { ProposalMintAction } from '@/src/components/proposal/actions/MintAction';
import { ProposalWhitelistAction } from '@/src/components/proposal/actions/WhitelistAction';
import { ProposalWithdrawAction } from '@/src/components/proposal/actions/WithdrawAction';
import { useDiamondSDKContext } from '@/src/context/DiamondGovernanceSDK';
import { useVotingPower } from '@/src/hooks/useVotingPower';
import { ACTIONS } from '@/src/lib/constants/actions';
import {
AddressVotes,
DiamondGovernanceClient,
IPartialVotingFacet,
IPartialVotingProposalFacet,
Proposal,
ProposalStatus,
VoteOption,
} from '@secureseco-dao/diamond-governance-sdk';
import { BigNumber, ContractTransaction } from 'ethers';
import { useAccount } from 'wagmi';
export type CanVote = {
Yes: boolean;
No: boolean;
Abstain: boolean;
};
export type UseProposalData = {
loading: boolean;
error: string | null;
proposal: Proposal | null;
canExecute: boolean;
canVote: CanVote;
votes: AddressVotes[] | null;
refetch: () => void;
};
export type UseProposalProps = {
id: string | undefined;
address: string | undefined;
useDummyData?: boolean;
};
// #region Dummy data
/**
* Dummy mint tokens action
*/
export const dummyMintAction: ProposalMintAction = {
method: ACTIONS.mint_tokens.method,
interface: ACTIONS.mint_tokens.interface,
params: {
_addresses: [
'0x2B868C8ed12EAD37ef76457e7B6443192e231442',
'0x23868C8ed12EAD37ef76457e7B6443192e231442',
],
_amounts: [
BigNumber.from('0x4563918244F40000'),
BigNumber.from('0x4563918244F40000'),
],
},
};
/**
* Dummy withdraw assets actions.
* Includes a variant for each token type.
*/
export const dummyWithdrawActions: ProposalWithdrawAction[] = [
{
interface: ACTIONS.withdraw_assets.interface,
method: ACTIONS.withdraw_assets.method.native,
params: {
_to: '0x2B868C8ed12EAD37ef76457e7B6443192e231442',
_value: BigNumber.from('0x4563918244F40000'),
},
},
{
interface: ACTIONS.withdraw_assets.interface,
method: ACTIONS.withdraw_assets.method.erc20,
params: {
_from: '0x23868C8ed12EAD37ef76457e7B6443192e231442',
_to: '0x2B868C8ed12EAD37ef76457e7B6443192e231442',
_amount: BigNumber.from('0x4563918244F40000'),
_contractAddress: '0xA6FA4fB5f76172d178d61B04b0ecd319C5d1C0aa', // Token address of WETH on mumbai
},
},
{
interface: ACTIONS.withdraw_assets.interface,
method: ACTIONS.withdraw_assets.method.erc721,
params: {
_from: '0x23868C8ed12EAD37ef76457e7B6443192e231442',
_to: '0x2B868C8ed12EAD37ef76457e7B6443192e231442',
_tokenId: BigNumber.from(1),
_contractAddress: '0x042FF2201D1015730d2414DaBF5d627e3e69e7CA', // Test11 NFT on mumbai
},
},
{
interface: ACTIONS.withdraw_assets.interface,
method: ACTIONS.withdraw_assets.method.erc1155,
params: {
_from: '0x23868C8ed12EAD37ef76457e7B6443192e231442',
_to: '0x2B868C8ed12EAD37ef76457e7B6443192e231442',
_tokenId: BigNumber.from(1),
_amount: BigNumber.from(1),
_contractAddress: '0x29Ba2441Cc4a5Da648f9abb284bc99FDF94dc446', // Some ERC1155 on mumbai
},
},
];
/**
* Dummy merge pull request action
*/
export const dummyMergeAction: ProposalMergeAction = {
method: ACTIONS.merge_pr.method,
interface: ACTIONS.merge_pr.interface,
params: {
_owner: 'githubtraining',
_repo: 'hellogitworld',
_pull_number: '174',
_sha: '6dcb09b5b57875f334f61aebed695e2e4193db5e',
},
};
/**
* Dummy change params action
*/
export const dummyChangeParamsAction: ProposalChangeParamAction = {
method: 'setMaxSingleWalletPower(uint32)',
interface: 'IPartialVotingProposalFacet',
params: {
_maxSingleWalletPower: 100000,
},
};
/**
* Dummy whitelist member action
*/
export const dummyWhitelistMemberAction: ProposalWhitelistAction = {
method: 'whitelist(address)',
interface: 'IMembershipWhitelisting',
params: {
_address: '0x123456789009876543211234567890',
},
};
/**
* Dummy Diamond cut action
*/
export const dummyDiamondCutAction: ProposalDiamondCutAction = {
method: 'diamondCut(tuple[])',
interface: 'IDiamondCut',
params: {
_diamondCut: [
{
facetAddress: '0x11111678900987654321234567890987654321',
action: FacetCutAction.Add,
functionSelectors: null!,
initCalldata: null!,
},
{
facetAddress: '0x22222678900987654321234567890987654321',
action: FacetCutAction.Remove,
functionSelectors: null!,
initCalldata: null!,
},
{
facetAddress: '0x33333378900987654321234567890987654321',
action: FacetCutAction.Replace,
functionSelectors: null!,
initCalldata: null!,
},
],
},
};
/**
* Dummy Approve spending action
*/
export const dummyApproveSpendingAction: ProposalApproveSpendingAction = {
interface: 'DAO',
method: 'ApproveERC20',
params: {
spender: '0x1eF2db73AfFdd73D4e219638b5ffB62a564f17ea',
amount: BigNumber.from(1000),
_contractAddress: '0xA5E81f58AF7ab276c1B86f12E882B2Dfe4e0b095',
},
};
/**
* Dummy proposal data, representing what is returned by the SDK.
* @note The Proposal type returned by the SDK is auto-generated, and therefore contains a lot of unnecessary fields, which are ignored here by casting to the right types.
*/
export const dummyProposal: Proposal = {
id: 0,
fromHexString: undefined,
getStatus: () => ProposalStatus.Active,
CanVote: () => Promise.resolve(true),
Vote: () => Promise.resolve({} as ContractTransaction),
Execute: () => Promise.resolve({} as ContractTransaction),
CanExecute: () => Promise.resolve(true),
Refresh: () => Promise.resolve(),
data: {
allowFailureMap: BigNumber.from('0x00'),
actions: [],
executed: BigNumber.from('0x21B5D8F'),
open: true,
metadata:
'0x697066733a2f2f516d51766d38383964544231315452516e37664b4a356545586e624d76376b5437574a4a62677a686472377a3166',
parameters: {
earlyExecution: true,
endDate: BigNumber.from('0x645be01d'),
startDate: BigNumber.from('0x64593d1d'),
maxSingleWalletPower: BigNumber.from('0x14'),
minParticipationThresholdPower: BigNumber.from('0x01'),
snapshotBlock: BigNumber.from('0x021b5d8c'),
supportThreshold: 1,
votingMode: 1,
} as IPartialVotingProposalFacet.ProposalParametersStructOutput,
creator: '0x2B868C8ed12EAD37ef76457e7B6443192e231442',
executor: '0x2B868C8ed12EAD37ef76457e7B6443192e231442',
voterList: ['0x2B868C8ed12EAD37ef76457e7B6443192e231442'],
tally: {
yes: BigNumber.from('0x4563918244F40000'),
no: BigNumber.from('0x00'),
abstain: BigNumber.from('0x00'),
} as IPartialVotingProposalFacet.TallyStructOutput,
},
metadata: {
title: 'Title',
description: 'Description',
resources: [],
body: '<p>This is the body</p>',
},
status: ProposalStatus.Active,
actions: [dummyDiamondCutAction],
startDate: new Date('2023-05-08T18:19:09.000Z'),
endDate: new Date('2023-05-10T18:19:09.000Z'),
creationDate: new Date('2023-05-08T18:09:09.000Z'),
} as unknown as Proposal;
export const dummyVotes: AddressVotes[] = [
{
address: '0x2B868C8ed12EAD37ef76457e7B6443192e231442',
votes: [
{
option: VoteOption.Yes,
amount: BigNumber.from('0x4563918244F40000'),
} as unknown as IPartialVotingFacet.PartialVoteStructOutput,
],
},
];
// #endregion
/**
* Hook to fetch a proposal from the DAO that the current Diamond Governance client has been instantiated with.
* @param props The properties to configure the hook.
* @param props.id The id of the proposal to fetch.
* @param props.address The address of the currently connected wallet.
* @param props.useDummyData Whether to use dummy data instead of fetching the proposal from the DAO.
* @returns An object containing the proposal, loading state, error state, whether the proposal can be executed, whether the user can vote on the proposal, the votes on the proposal and a method to refetch the proposal.
*/
export const useProposal = ({
id,
address,
useDummyData = false,
}: UseProposalProps): UseProposalData => {
/*
The anonProposal (anonymous proposal) is shown when no wallet is connected
The regular proposal is shown when a wallet is connected
This was necessary because there was an issue where if the same state
variable was used for both, somehow the signer in the proposal would stick
to the zero address, even after a signer of connected wallet becomes available and
the proposal was refreshed. Possibly due to delayed state updates
*/
const [proposal, setProposal] = useState<Proposal | null>(null);
const [anonProposal, setAnonProposal] = useState<Proposal | null>(null);
const [canExecute, setCanExecute] = useState<boolean>(false);
const [canVote, setCanVote] = useState<CanVote>({
Yes: false,
No: false,
Abstain: false,
});
const [votes, setVotes] = useState<AddressVotes[] | null>(null);
const [loading, setLoading] = useState<boolean>(true);
const [error, setError] = useState<string | null>(null);
const { anonClient, client } = useDiamondSDKContext();
const { isConnected } = useAccount();
const { proposalVotingPower } = useVotingPower({
address,
proposal: proposal ?? undefined,
});
/**
* Fetch a proposal from the SDK
* @param client Diamond SDK client
* @param anon Whether or not an anonymous client is being used
*/
const fetchProposal = async (
client?: DiamondGovernanceClient,
anon = false
) => {
if (!client) return;
if (!id) {
setError('Proposal not found');
setLoading(false);
return;
}
try {
const daoProposal: Proposal = await client.sugar.GetProposal(+id);
if (daoProposal) {
if (anon) setAnonProposal(daoProposal);
else setProposal(daoProposal);
setError(null);
setLoading(false);
fetchVotes(daoProposal);
if (!anon) {
checkCanExecute(daoProposal);
checkCanVote(daoProposal);
}
} else {
setError('Proposal not found');
setLoading(false);
setVotes(null);
}
} catch (e) {
console.error(e);
if (e instanceof Error && e.message === 'Invalid id')
setError('Proposal not found');
else setError('An error occurred');
setLoading(false);
}
};
const fetchVotes = async (proposal: Proposal) => {
if (!client || !proposal) return;
try {
const votes = await proposal.GetVotes();
setVotes(votes);
} catch (e) {
console.error(e);
}
};
//** Set dummy data for development without querying SDK */
const setDummyData = () => {
setLoading(false);
setError(null);
setProposal(dummyProposal);
setVotes(dummyVotes);
};
useEffect(() => {
if (useDummyData) return setDummyData();
fetchProposal(client ?? anonClient, !client);
}, [client, anonClient, id]);
const checkCanExecute = async (proposal: Proposal) => {
// Fetch if the current proposal can be executed
try {
const canExecuteData = await proposal.CanExecute();
setCanExecute(canExecuteData);
} catch (e) {
console.error('Error fetching canExecute', e);
}
};
const checkCanVote = async (proposal: Proposal) => {
if (!proposalVotingPower) return;
try {
const values = [VoteOption.Abstain, VoteOption.Yes, VoteOption.No];
const canVoteData = await Promise.all(
values.map((vote) =>
proposal.CanVote(vote, BigNumber.from(proposalVotingPower))
)
);
setCanVote({
Yes: canVoteData[1],
No: canVoteData[2],
Abstain: canVoteData[0],
});
} catch (e) {
console.error('Error fetching canVote', e);
}
};
useEffect(() => {
if (!proposal || useDummyData) return;
checkCanExecute(proposal);
checkCanVote(proposal);
}, [proposalVotingPower]);
return {
loading,
error,
proposal: isConnected ? proposal : anonProposal,
canExecute,
canVote,
votes,
// Only allow refetching if not using dummy data
refetch: () => {
if (proposal && !useDummyData) {
proposal.Refresh();
checkCanExecute(proposal);
checkCanVote(proposal);
}
},
};
};