-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangeVotingSetting.ts
More file actions
412 lines (371 loc) · 17.8 KB
/
changeVotingSetting.ts
File metadata and controls
412 lines (371 loc) · 17.8 KB
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
//class which initial with parentAddressOrEns, childAddressOrEns, subDaoPluginAddress
import {Client, MultisigClient, TokenVotingClient} from "../lib/sdk";
import {AllowedNetwork} from "../lib/constants";
import {DaoDetails, VoteValues} from "@aragon/sdk-client";
import {
getVotingPluginAddress,
installSubDaoPlugin,
iterateSteps,
MULTISIG_PLUGIN_ID,
TOKEN_VOTING_PLUGIN_ID
} from "./installSubDAOPlugin";
import {
DAO_ABI,
getABIEntry,
GovernanceERC20ABI,
MULTISIG_ABI,
queryABIEntry,
SUB_DAO_ADMIN_ABI,
TOKEN_VOTING_ABI
} from '../lib/abis'
import {getWallet, hexToBytes} from "../lib/helpers";
import {ethers} from "ethers";
import {DaoAction} from "@aragon/sdk-client-common";
import {toBytes} from "../../../subdao-plugin/utils/helpers";
import { parse, ArgumentConfig } from 'ts-command-line-args';
const meta = import.meta as any;
export class ChangeVotingSettingClient {
readonly parentAddressOrEns:string;
readonly childAddressOrEns:string;
readonly subDaoPluginAddress:string;
readonly network: AllowedNetwork;
readonly client:object;
private initialized:boolean=false;
parentDaoDetails:DaoDetails;
childDaoDetails:DaoDetails;
childContractAddress:string;
parentContractAddress:string;
parentVotingPluginContractAddress:any;
parentVotingPluginType:any;
childVotingPluginContractAddress:any;
childVotingPluginType:any;
parentVotingClient:object;
votingToken:string;
constructor(parentAddressOrEns:string,childAddressOrEns:string,subDaoPluginAddress:string,network:AllowedNetwork) {
//initial values
this.parentAddressOrEns=parentAddressOrEns;
this.childAddressOrEns=childAddressOrEns;
this.subDaoPluginAddress=subDaoPluginAddress;
this.network=network;
this.client = Client(network);
// console.log("constructor finished");
}
private async getVotingToken(){
const deployer=getWallet();
const Contract=this.connectContract(deployer,this.childVotingPluginContractAddress,TOKEN_VOTING_ABI);
try {
const result = await Contract["getVotingToken"]();
return result;
} catch (error) {
console.error('Error calling contract function:', error);
}
}
async initial(){
//get parent
this.parentDaoDetails = await this.client.methods.getDao(this.parentAddressOrEns);
if (!this.parentDaoDetails) throw new Error('DAO not found');
//get child detail
this.childDaoDetails = await this.client.methods.getDao(this.childAddressOrEns);
if (!this.childDaoDetails) throw new Error('DAO not found');
this.childContractAddress = this.childDaoDetails.address;
this.parentContractAddress = this.parentDaoDetails.address;
//parentVotingPluginType
const { votingPluginAddress: parentVotingPluginContractAddress, votingPluginType: parentVotingPluginType } =
getVotingPluginAddress(this.parentDaoDetails);
this.parentVotingPluginContractAddress=parentVotingPluginContractAddress;
this.parentVotingPluginType=parentVotingPluginType;
//childVotingPluginType
const { votingPluginAddress: childVotingPluginContractAddress, votingPluginType: childVotingPluginType } =
getVotingPluginAddress(this.childDaoDetails);
this.childVotingPluginContractAddress=childVotingPluginContractAddress;
this.childVotingPluginType=childVotingPluginType;
if (parentVotingPluginType=== TOKEN_VOTING_PLUGIN_ID){
this.parentVotingClient = TokenVotingClient(this.network);
}
else if(parentVotingPluginType === MULTISIG_PLUGIN_ID){
this.parentVotingClient = MultisigClient(this.network);
}
if(childVotingPluginType===TOKEN_VOTING_PLUGIN_ID){
this.votingToken=await this.getVotingToken();
}
this.initialized=true;
// console.log("initial finished");
}
private connectContract(deployer:ethers.Wallet,votingPluginAddress,contractABI){
return new ethers.Contract(votingPluginAddress, contractABI, deployer);
}
private async isMemberMultisig(address:string){
const deployer=getWallet();
const multisigContract=this.connectContract(deployer,this.childVotingPluginContractAddress,MULTISIG_ABI);
try {
const result = await multisigContract["isMember"](address);
console.log('Function result:', result);
return result;
} catch (error) {
console.error('Error calling contract function:', error);
}
}
async isMember(address:string,votingAbi:any){
const deployer=getWallet();
const Contract=this.connectContract(deployer,this.childVotingPluginContractAddress,votingAbi);
try {
const result = await Contract["isMember"](address);
return result;
} catch (error) {
console.error('Error calling contract function:', error);
}
}
async balanceToken(address:string){
const deployer=getWallet();
const Contract=this.connectContract(deployer,this.votingToken,GovernanceERC20ABI);
try {
const result = await Contract["balanceOf"](address);
return result;
} catch (error) {
console.error('Error calling contract function:', error);
}
}
private async checkIsAnyRepetetive(newApprovers:string[],votingAbi:any){
for (let approver of newApprovers) {
let isMember=await this.isMember(approver,votingAbi)
if(isMember) throw new Error(`this new approver: ${approver} is already a member `);
}
}
private async sendProposal(proposalMetadata,daoActions:DaoAction[]){
if (this.parentVotingPluginType === TOKEN_VOTING_PLUGIN_ID) {
const metadataUri: string = await this.parentVotingClient.methods.pinMetadata(proposalMetadata);
const createProposalSteps = this.parentVotingClient.methods.createProposal({
metadataUri,
pluginAddress: this.parentVotingPluginContractAddress,
actions: daoActions,
creatorVote: VoteValues.YES, // creator votes yes
executeOnPass: true, // execute on pass
startDate: new Date(0), // Start immediately
endDate: new Date(0), // uses minimum voting duration
});
await iterateSteps(createProposalSteps);
}
else if (this.parentVotingPluginType === MULTISIG_PLUGIN_ID) {
const metadataUri: string = await this.parentVotingClient.methods.pinMetadata(proposalMetadata);
const createProposalSteps = this.parentVotingClient.methods.createProposal({
metadataUri,
pluginAddress: this.parentVotingPluginContractAddress,
actions: daoActions,
approve: true,
tryExecution: true,
startDate: new Date(0), // Start immediately
endDate: new Date(new Date().setFullYear(new Date().getFullYear() + 10)), // uses minimum voting duration
});
await iterateSteps(createProposalSteps);
}
}
private encodedFunctionData(contractABI,functionFragment:string,values:ReadonlyArray<any>){
const contractABIFormated=getABIEntry(contractABI);
const contractInterface = new ethers.utils.Interface(contractABIFormated);
return contractInterface.encodeFunctionData(functionFragment, values);
}
async multisigAddAddresses(newApprovers:string[]){
if (!this.initialized) throw new Error('This instance has not initialized yet, first call initial() method');
if (newApprovers.length==0) throw new Error('There is not any approver to add');
await this.checkIsAnyRepetetive(newApprovers,MULTISIG_ABI);
//porposal parent -> subdaoadminplugin -> childvoting(multisig)
const encodedFunctionDataMultisig=this.encodedFunctionData(MULTISIG_ABI,'addAddresses',[newApprovers])
const updateVotingSettingDaoAction: DaoAction[] = [
{
to: this.childVotingPluginContractAddress,
value: BigInt(0),
data: hexToBytes(encodedFunctionDataMultisig),
},
];
const encodedFunctionDataSubDAOAdmin=this.encodedFunctionData(SUB_DAO_ADMIN_ABI,'execute',[updateVotingSettingDaoAction])
const executeSubDAOAdminDaoAction: DaoAction[] = [
{
to: this.subDaoPluginAddress,
value: BigInt(0),
data: hexToBytes(encodedFunctionDataSubDAOAdmin),
},
];
//TODO
const proposalMetadata = {
title: 'Add new members',
summary: 'Add new member to the multisig',
description: `By installing this plugin, the plugin will grant the execution access of child da to parent`,
resources: [
{
url: 'https://patterns.community',
name: 'Pattern',
},
],
media: {
header: 'https://assets-global.website-files.com/65410dc30116ce87ecbef5cd/654f75bc7db9aa282ca87e21_Logo%2Btext%20White.png',
logo: 'https://assets-global.website-files.com/65410dc30116ce87ecbef5cd/654f75bc7db9aa282ca87e21_Logo%2Btext%20White.png',
},
};
await this.sendProposal(proposalMetadata,executeSubDAOAdminDaoAction);
}
async checkAreAllMember(formerapprovers:string[],votingAbi:any){
for (let approver of formerapprovers) {
let isMember=await this.isMember(approver,votingAbi)
if(!isMember) throw new Error(`this former approver: ${approver} is already not a member `);
}
}
async multisigRemoveAddresses(formerapprovers:string[]){
if (!this.initialized) throw new Error('This instance has not initialized yet, first call initial() method');
if (formerapprovers.length==0) throw new Error('There is not any formerapprover to remove');
await this.checkAreAllMember(formerapprovers,MULTISIG_ABI);
//porposal parent -> subdaoadminplugin -> childvoting(multisig)
const encodedFunctionDataMultisig=this.encodedFunctionData(MULTISIG_ABI,'removeAddresses',[formerapprovers])
const updateVotingSettingDaoAction: DaoAction[] = [
{
to: this.childVotingPluginContractAddress,
value: BigInt(0),
data: hexToBytes(encodedFunctionDataMultisig),
},
];
const encodedFunctionDataSubDAOAdmin=this.encodedFunctionData(SUB_DAO_ADMIN_ABI,'execute',[updateVotingSettingDaoAction])
const executeSubDAOAdminDaoAction: DaoAction[] = [
{
to: this.subDaoPluginAddress,
value: BigInt(0),
data: hexToBytes(encodedFunctionDataSubDAOAdmin),
},
];
//TODO
const proposalMetadata = {
title: 'Remove former members',
summary: 'Remove some member of this multisig',
description: `By installing this plugin, the plugin will grant the execution access of child da to parent`,
resources: [
{
url: 'https://patterns.community',
name: 'Pattern',
},
],
media: {
header: 'https://assets-global.website-files.com/65410dc30116ce87ecbef5cd/654f75bc7db9aa282ca87e21_Logo%2Btext%20White.png',
logo: 'https://assets-global.website-files.com/65410dc30116ce87ecbef5cd/654f75bc7db9aa282ca87e21_Logo%2Btext%20White.png',
},
};
await this.sendProposal(proposalMetadata,executeSubDAOAdminDaoAction);
}
async tokenVotingIncreaseAddressVotingPower(newApprovers:string[],amounts:number[]){
if (!this.initialized) throw new Error('This instance has not initialized yet, first call initial() method');
if (newApprovers.length==0) throw new Error('There is not any approver to add');
if (newApprovers.length!=amounts.length) throw new Error('Length of newApprovers and howMuch must be equal ');
// await this.checkIsAnyRepetetive(newApprovers,TOKEN_VOTING_ABI);
// parent porposal -> subdao plugin -> Token address
const mintDaoAction: DaoAction[]=[]
for (let indexOfApprovers in newApprovers){
let encodedFunctionDataERC20=this.encodedFunctionData(GovernanceERC20ABI,'mint',[newApprovers[indexOfApprovers],amounts[indexOfApprovers].toString()])
let daoActionToken:DaoAction={
to:this.votingToken,
value:BigInt(0),
data:hexToBytes(encodedFunctionDataERC20),
};
mintDaoAction.push(daoActionToken);
}
const encodedFunctionDataSubDAOAdmin=this.encodedFunctionData(SUB_DAO_ADMIN_ABI,'execute',[mintDaoAction])
const executeSubDAOAdminDaoAction: DaoAction[] = [
{
to: this.subDaoPluginAddress,
value: BigInt(0),
data: hexToBytes(encodedFunctionDataSubDAOAdmin),
},
];
//TODO
const proposalMetadata = {
title: 'mint the token for ',
summary: 'Granting parent DAO execution permission on child dao',
description: `By installing this plugin, the plugin will grant the execution access of child da to parent`,
resources: [
{
url: 'https://patterns.community',
name: 'Pattern',
},
],
media: {
header: 'https://assets-global.website-files.com/65410dc30116ce87ecbef5cd/654f75bc7db9aa282ca87e21_Logo%2Btext%20White.png',
logo: 'https://assets-global.website-files.com/65410dc30116ce87ecbef5cd/654f75bc7db9aa282ca87e21_Logo%2Btext%20White.png',
},
};
await this.sendProposal(proposalMetadata,executeSubDAOAdminDaoAction);
}
}
if (meta.main) {
interface MyArgs {
childDaoAddress: string;
parentDaoAddress: string;
network: string;
subDaoPluginAddress:string;
func:string;
otherArgs: string[];
}
const config: ArgumentConfig<MyArgs> = {
childDaoAddress: {
type: String,
alias: 'c',
description: 'The child dao address or ENS',
},
parentDaoAddress: {
type: String,
alias: 'p',
description: 'The parent dao address or ENS',
},
network: {
type: String,
alias: 'n',
description: 'The network, can be one of [mainnet, goerli, polygon, mumbai, base, baseGoerli, local]',
},
subDaoPluginAddress:{
type: String,
alias: 's',
description: 'The Subdao-plugin that installed on child dao address'
},
func:{
type: String,
alias: 'f',
description: 'The function that you want do on subdao'
},
otherArgs: {
type: String,
multiple: true,
defaultOption: true,
},
};
const args = parse<MyArgs>(config);
const childDAO = args.childDaoAddress;
const parentDAO = args.parentDaoAddress;
const network: AllowedNetwork = args.network as AllowedNetwork;
const subAdminPlugin:string = args.subDaoPluginAddress;
const func:string=args.func;
const changeVotingClient = new ChangeVotingSettingClient(parentDAO, childDAO, subAdminPlugin, network);
await changeVotingClient.initial();
switch (func) {
case "multisigAddAddresses":
if (args.otherArgs.length!=1) throw new Error('inputs is not compatible');
let newaddresses: string[]=args.otherArgs[0].slice(1, -1).split(',');
await changeVotingClient.multisigAddAddresses(newaddresses)
break;
case "multisigRemoveAddresses":
if (args.otherArgs.length!=1) throw new Error('inputs is not compatible');
let formeraddresses: string[] = args.otherArgs[0].slice(1, -1).split(',');
await changeVotingClient.multisigRemoveAddresses(formeraddresses)
break;
case "tokenVotingIncreaseAddressVotingPower":
if (args.otherArgs.length!=2) throw new Error('inputs is not compatible');
let addresses: string[] = args.otherArgs[0].slice(1, -1).split(',');
let amount:number[]=args.otherArgs[1].slice(1, -1).split(',').map(Number);
await changeVotingClient.tokenVotingIncreaseAddressVotingPower(addresses,amount)
break;
}
// const changeVotingClient=new ChangeVotingSettingClient('parentdaotest.dao.eth','childmultisig3.dao.eth','0xFf8353c4a56B39824bDd2C6Eea4d25bCA03Dd5Ed','goerli')
// multisigAddAddresses
// await changeVotingClient.multisigAddAddresses(['0x612A6506e7cdD093598D876d19c9e231737E72Be'])
// bun changevotingsetting -c childmultisig3.dao.eth -p parentdaotest.dao.eth -n goerli -s 0xFf8353c4a56B39824bDd2C6Eea4d25bCA03Dd5Ed -f multisigAddAddresses '["0x612A6506e7cdD093598D876d19c9e231737E72Be"]'
// multisigRemoveAddresses
// await changeVotingClient.multisigRemoveAddresses(['0x612A6506e7cdD093598D876d19c9e231737E72Be']);
// bun changevotingsetting -c childmultisig3.dao.eth -p parentdaotest.dao.eth -n goerli -s 0xFf8353c4a56B39824bDd2C6Eea4d25bCA03Dd5Ed -f multisigRemoveAddresses '["0x612A6506e7cdD093598D876d19c9e231737E72Be"]'
// tokenVotingIncreaseAddressVotingPower
// await changeVotingClient.tokenVotingIncreaseAddressVotingPower(['0x612A6506e7cdD093598D876d19c9e231737E72Be'],[1])//
// bun changevotingsetting -c childdaotokenvoting.dao.eth -p parentdaotest.dao.eth -n goerli -s 0xFf8353c4a56B39824bDd2C6Eea4d25bCA03Dd5Ed -f tokenVotingIncreaseAddressVotingPower "['0x612A6506e7cdD093598D876d19c9e231737E72Be']" "[1]"
}