-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathdao_funcs.py
555 lines (494 loc) · 20.6 KB
/
dao_funcs.py
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
from __future__ import annotations
import asyncio
import json
import time
from decimal import Decimal
from typing import List, Optional
from chia.cmds.cmds_util import CMDTXConfigLoader, get_wallet_client, transaction_status_msg, transaction_submitted_msg
from chia.cmds.param_types import CliAmount
from chia.cmds.units import units
from chia.cmds.wallet_funcs import get_mojo_per_unit, get_wallet_type
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.bech32m import decode_puzzle_hash, encode_puzzle_hash
from chia.util.config import selected_network_address_prefix
from chia.util.ints import uint64
from chia.wallet.transaction_record import TransactionRecord
from chia.wallet.util.tx_config import DEFAULT_COIN_SELECTION_CONFIG
from chia.wallet.util.wallet_types import WalletType
async def add_dao_wallet(
wallet_rpc_port: Optional[int], fp: int, name: Optional[str], treasury_id: bytes32, filter_amount: uint64
) -> None:
print(f"Adding wallet for DAO: {treasury_id}")
print("This may take awhile.")
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
res = await wallet_client.create_new_dao_wallet(
mode="existing",
tx_config=CMDTXConfigLoader(reuse_puzhash=True).to_tx_config(units["chia"], config, fingerprint),
dao_rules=None,
amount_of_cats=None,
treasury_id=treasury_id,
filter_amount=filter_amount,
name=name,
)
print("Successfully created DAO Wallet")
print(f"DAO Treasury ID: {res.treasury_id.hex()}")
print(f"DAO Wallet ID: {res.wallet_id}")
print(f"CAT Wallet ID: {res.cat_wallet_id}")
print(f"DAOCAT Wallet ID: {res.dao_cat_wallet_id}")
async def create_dao_wallet(
wallet_rpc_port: Optional[int],
fp: int,
fee: uint64,
fee_for_cat: uint64,
name: Optional[str],
proposal_timelock: uint64,
soft_close: uint64,
attendance_required: uint64,
pass_percentage: uint64,
self_destruct: uint64,
oracle_delay: uint64,
proposal_minimum: uint64,
filter_amount: uint64,
cat_amount: CliAmount,
cli_tx_config: CMDTXConfigLoader,
push: bool,
) -> List[TransactionRecord]:
if proposal_minimum % 2 == 0:
proposal_minimum = uint64(1 + proposal_minimum)
print("Adding 1 mojo to proposal minimum amount")
dao_rules = {
"proposal_timelock": proposal_timelock,
"soft_close_length": soft_close,
"attendance_required": attendance_required,
"pass_percentage": pass_percentage,
"self_destruct_length": self_destruct,
"oracle_spend_delay": oracle_delay,
"proposal_minimum_amount": proposal_minimum,
}
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
conf_coins, _, _ = await wallet_client.get_spendable_coins(
wallet_id=1, coin_selection_config=DEFAULT_COIN_SELECTION_CONFIG
)
if len(conf_coins) < 2: # pragma: no cover
raise ValueError("DAO creation requires at least 2 xch coins in your wallet.")
res = await wallet_client.create_new_dao_wallet(
mode="new",
dao_rules=dao_rules,
amount_of_cats=cat_amount.convert_amount(units["mojo"]),
treasury_id=None,
filter_amount=filter_amount,
name=name,
fee=fee,
fee_for_cat=fee_for_cat,
tx_config=cli_tx_config.to_tx_config(units["chia"], config, fingerprint),
push=push,
)
if push:
print("Successfully created DAO Wallet")
print(f"DAO Treasury ID: {res.treasury_id.hex()}")
print(f"DAO Wallet ID: {res.wallet_id}")
print(f"CAT Wallet ID: {res.cat_wallet_id}")
print(f"DAOCAT Wallet ID: {res.dao_cat_wallet_id}")
return res.transactions
async def get_treasury_id(wallet_rpc_port: Optional[int], fp: int, wallet_id: int) -> None:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, _, _):
res = await wallet_client.dao_get_treasury_id(wallet_id=wallet_id)
treasury_id = res["treasury_id"]
print(f"Treasury ID: {treasury_id}")
async def get_rules(wallet_rpc_port: Optional[int], fp: int, wallet_id: int) -> None:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, _, _):
res = await wallet_client.dao_get_rules(wallet_id=wallet_id)
rules = res["rules"]
for rule, val in rules.items():
print(f"{rule}: {val}")
async def add_funds_to_treasury(
wallet_rpc_port: Optional[int],
fp: int,
wallet_id: int,
funding_wallet_id: int,
amount: CliAmount,
fee: uint64,
cli_tx_config: CMDTXConfigLoader,
push: bool,
) -> List[TransactionRecord]:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
try:
typ = await get_wallet_type(wallet_id=funding_wallet_id, wallet_client=wallet_client)
mojo_per_unit = get_mojo_per_unit(typ)
except LookupError: # pragma: no cover
print(f"Wallet id: {wallet_id} not found.")
return []
res = await wallet_client.dao_add_funds_to_treasury(
wallet_id=wallet_id,
funding_wallet_id=funding_wallet_id,
amount=amount.convert_amount(mojo_per_unit),
fee=fee,
tx_config=cli_tx_config.to_tx_config(units["chia"], config, fingerprint),
push=push,
)
if push:
start = time.time()
while time.time() - start < 10:
await asyncio.sleep(0.1)
tx = await wallet_client.get_transaction(res.tx_id)
if len(tx.sent_to) > 0:
print(transaction_submitted_msg(tx))
print(transaction_status_msg(fingerprint, res.tx_id))
return res.transactions
if push:
print(f"Transaction not yet submitted to nodes. TX ID: {res.tx_id.hex()}")
return res.transactions
async def get_treasury_balance(wallet_rpc_port: Optional[int], fp: int, wallet_id: int) -> None:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, _, _):
res = await wallet_client.dao_get_treasury_balance(wallet_id=wallet_id)
balances = res["balances"]
if not balances:
print("The DAO treasury currently has no funds")
return None
xch_mojos = get_mojo_per_unit(WalletType.STANDARD_WALLET)
cat_mojos = get_mojo_per_unit(WalletType.CAT)
for asset_id, balance in balances.items():
if asset_id == "xch":
print(f"XCH: {balance / xch_mojos}")
else:
print(f"{asset_id}: {balance / cat_mojos}")
async def list_proposals(wallet_rpc_port: Optional[int], fp: int, wallet_id: int, include_closed: bool) -> None:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, _, _):
res = await wallet_client.dao_get_proposals(wallet_id=wallet_id, include_closed=include_closed)
proposals = res["proposals"]
soft_close_length = res["soft_close_length"]
print("############################")
for prop in proposals:
print(f"Proposal ID: {prop['proposal_id']}")
prop_status = "CLOSED" if prop["closed"] else "OPEN"
print(f"Status: {prop_status}")
print(f"Votes for: {prop['yes_votes']}")
votes_against = prop["amount_voted"] - prop["yes_votes"]
print(f"Votes against: {votes_against}")
print("------------------------")
print(f"Proposals have {soft_close_length} blocks of soft close time.")
print("############################")
async def show_proposal(wallet_rpc_port: Optional[int], fp: int, wallet_id: int, proposal_id: str) -> None:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, _, config):
res = await wallet_client.dao_parse_proposal(wallet_id, proposal_id)
pd = res["proposal_dictionary"]
blocks_needed = pd["state"]["blocks_needed"]
passed = pd["state"]["passed"]
closable = pd["state"]["closable"]
status = "CLOSED" if pd["state"]["closed"] else "OPEN"
votes_needed = pd["state"]["total_votes_needed"]
yes_needed = pd["state"]["yes_votes_needed"]
ptype_val = pd["proposal_type"]
if (ptype_val == "s") and ("mint_amount" in pd):
ptype = "mint"
elif ptype_val == "s":
ptype = "spend"
elif ptype_val == "u":
ptype = "update"
else:
raise Exception(f"Unknown proposal type: {ptype_val!r}")
print("")
print(f"Details of Proposal: {proposal_id}")
print("---------------------------")
print("")
print(f"Type: {ptype.upper()}")
print(f"Status: {status}")
print(f"Passed: {passed}")
if not passed:
print(f"Yes votes needed: {yes_needed}")
if not pd["state"]["closed"]:
print(f"Closable: {closable}")
if not closable:
print(f"Total votes needed: {votes_needed}")
print(f"Blocks remaining: {blocks_needed}")
prefix = selected_network_address_prefix(config)
if ptype == "spend":
xch_conds = pd["xch_conditions"]
asset_conds = pd["asset_conditions"]
print("")
if xch_conds:
print("Proposal XCH Conditions")
for pmt in xch_conds:
puzzle_hash = encode_puzzle_hash(bytes32.from_hexstr(pmt["puzzle_hash"]), prefix)
amount = pmt["amount"]
print(f"Address: {puzzle_hash}\nAmount: {amount}\n")
if asset_conds:
print("Proposal asset Conditions")
for cond in asset_conds:
asset_id = cond["asset_id"]
print(f"Asset ID: {asset_id}")
conds = cond["conditions"]
for pmt in conds:
puzzle_hash = encode_puzzle_hash(bytes32.from_hexstr(pmt["puzzle_hash"]), prefix)
amount = pmt["amount"]
print(f"Address: {puzzle_hash}\nAmount: {amount}\n")
elif ptype == "update":
print("")
print("Proposed Rules:")
for key, val in pd["dao_rules"].items():
print(f"{key}: {val}")
elif ptype == "mint":
mint_amount = pd["mint_amount"]
address = encode_puzzle_hash(bytes32.from_hexstr(pd["new_cat_puzhash"]), prefix)
print("")
print(f"Amount of CAT to mint: {mint_amount}")
print(f"Address: {address}")
async def vote_on_proposal(
wallet_rpc_port: Optional[int],
fp: int,
wallet_id: int,
proposal_id: str,
vote_amount: uint64,
is_yes_vote: bool,
fee: uint64,
cli_tx_config: CMDTXConfigLoader,
push: bool,
) -> List[TransactionRecord]:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
res = await wallet_client.dao_vote_on_proposal(
wallet_id=wallet_id,
proposal_id=proposal_id,
vote_amount=vote_amount,
is_yes_vote=is_yes_vote,
fee=fee,
tx_config=cli_tx_config.to_tx_config(units["chia"], config, fingerprint),
push=push,
)
if push:
start = time.time()
while time.time() - start < 10:
await asyncio.sleep(0.1)
tx = await wallet_client.get_transaction(res.tx_id)
if len(tx.sent_to) > 0:
print(transaction_submitted_msg(tx))
print(transaction_status_msg(fingerprint, res.tx_id))
return res.transactions
if push:
print(f"Transaction not yet submitted to nodes. TX ID: {res.tx_id.hex()}")
return res.transactions
async def close_proposal(
wallet_rpc_port: Optional[int],
fp: int,
wallet_id: int,
fee: uint64,
proposal_id: str,
self_destruct: bool,
cli_tx_config: CMDTXConfigLoader,
push: bool,
) -> List[TransactionRecord]:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
res = await wallet_client.dao_close_proposal(
wallet_id=wallet_id,
proposal_id=proposal_id,
fee=fee,
self_destruct=self_destruct,
tx_config=cli_tx_config.to_tx_config(units["chia"], config, fingerprint),
push=push,
)
if push:
start = time.time()
while time.time() - start < 10:
await asyncio.sleep(0.1)
tx = await wallet_client.get_transaction(res.tx_id)
if len(tx.sent_to) > 0:
print(transaction_submitted_msg(tx))
print(transaction_status_msg(fingerprint, res.tx_id))
return res.transactions
if push:
print(f"Transaction not yet submitted to nodes. TX ID: {res.tx_id.hex()}")
return res.transactions
async def lockup_coins(
wallet_rpc_port: Optional[int],
fp: int,
wallet_id: int,
amount: CliAmount,
fee: uint64,
cli_tx_config: CMDTXConfigLoader,
push: bool,
) -> List[TransactionRecord]:
final_amount: uint64 = amount.convert_amount(units["cat"])
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
res = await wallet_client.dao_send_to_lockup(
wallet_id=wallet_id,
amount=final_amount,
fee=fee,
tx_config=cli_tx_config.to_tx_config(units["chia"], config, fingerprint),
push=push,
)
if push:
start = time.time()
while time.time() - start < 10:
await asyncio.sleep(0.1)
tx = await wallet_client.get_transaction(res.tx_id)
if len(tx.sent_to) > 0:
print(transaction_submitted_msg(tx))
print(transaction_status_msg(fingerprint, res.tx_id))
return res.transactions
if push:
print(f"Transaction not yet submitted to nodes. TX ID: {res.tx_id.hex()}")
return res.transactions
async def release_coins(
wallet_rpc_port: Optional[int],
fp: int,
wallet_id: int,
fee: uint64,
cli_tx_config: CMDTXConfigLoader,
push: bool,
) -> List[TransactionRecord]:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
res = await wallet_client.dao_free_coins_from_finished_proposals(
wallet_id=wallet_id,
fee=fee,
tx_config=cli_tx_config.to_tx_config(units["chia"], config, fingerprint),
push=push,
)
if push:
start = time.time()
while time.time() - start < 10:
await asyncio.sleep(0.1)
tx = await wallet_client.get_transaction(res.tx_id)
if len(tx.sent_to) > 0:
print(transaction_submitted_msg(tx))
print(transaction_status_msg(fingerprint, res.tx_id))
return res.transactions
if push:
print(f"Transaction not yet submitted to nodes. TX ID: {res.tx_id.hex()}")
return res.transactions
async def exit_lockup(
wallet_rpc_port: Optional[int],
fp: int,
wallet_id: int,
fee: uint64,
cli_tx_config: CMDTXConfigLoader,
push: bool,
) -> List[TransactionRecord]:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
res = await wallet_client.dao_exit_lockup(
wallet_id=wallet_id,
coins=[],
fee=fee,
tx_config=cli_tx_config.to_tx_config(units["chia"], config, fingerprint),
push=push,
)
if push:
start = time.time()
while time.time() - start < 10:
await asyncio.sleep(0.1)
tx = await wallet_client.get_transaction(res.tx_id)
if len(tx.sent_to) > 0:
print(transaction_submitted_msg(tx))
print(transaction_status_msg(fingerprint, res.tx_id))
return res.transactions
if push:
print(f"Transaction not yet submitted to nodes. TX ID: {res.tx_id.hex()}")
return res.transactions
async def create_spend_proposal(
wallet_rpc_port: Optional[int],
fp: int,
wallet_id: int,
fee: uint64,
vote_amount: Optional[int],
address: Optional[str],
amount: Optional[str],
asset_id: Optional[str],
additions_file: Optional[str],
cli_tx_config: CMDTXConfigLoader,
push: bool,
) -> List[TransactionRecord]:
if additions_file is None and (address is None or amount is None):
raise ValueError("Must include a json specification or an address / amount pair.")
if additions_file: # pragma: no cover
with open(additions_file) as f:
additions_dict = json.load(f)
additions = []
for addition in additions_dict:
addition["puzzle_hash"] = decode_puzzle_hash(addition["address"]).hex()
del addition["address"]
additions.append(addition)
else:
additions = None
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
wallet_type = await get_wallet_type(wallet_id=wallet_id, wallet_client=wallet_client)
mojo_per_unit = get_mojo_per_unit(wallet_type=wallet_type)
final_amount: Optional[uint64] = uint64(int(Decimal(amount) * mojo_per_unit)) if amount else None
res = await wallet_client.dao_create_proposal(
wallet_id=wallet_id,
proposal_type="spend",
additions=additions,
amount=final_amount,
inner_address=address,
asset_id=asset_id,
vote_amount=vote_amount,
fee=fee,
tx_config=cli_tx_config.to_tx_config(units["chia"], config, fingerprint),
push=push,
)
asset_id_name = asset_id if asset_id else "XCH"
print(f"Created spend proposal for asset: {asset_id_name}")
if push:
print("Successfully created proposal.")
print(f"Proposal ID: {res.proposal_id.hex()}")
return res.transactions
async def create_update_proposal(
wallet_rpc_port: Optional[int],
fp: int,
wallet_id: int,
fee: uint64,
vote_amount: Optional[uint64],
proposal_timelock: Optional[uint64],
soft_close_length: Optional[uint64],
attendance_required: Optional[uint64],
pass_percentage: Optional[uint64],
self_destruct_length: Optional[uint64],
oracle_spend_delay: Optional[uint64],
cli_tx_config: CMDTXConfigLoader,
push: bool,
) -> List[TransactionRecord]:
new_dao_rules = {
"proposal_timelock": proposal_timelock,
"soft_close_length": soft_close_length,
"attendance_required": attendance_required,
"pass_percentage": pass_percentage,
"self_destruct_length": self_destruct_length,
"oracle_spend_delay": oracle_spend_delay,
}
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
res = await wallet_client.dao_create_proposal(
wallet_id=wallet_id,
proposal_type="update",
new_dao_rules=new_dao_rules,
vote_amount=vote_amount,
fee=fee,
tx_config=cli_tx_config.to_tx_config(units["chia"], config, fingerprint),
push=push,
)
if push:
print("Successfully created proposal.")
print(f"Proposal ID: {res.proposal_id.hex()}")
return res.transactions
async def create_mint_proposal(
wallet_rpc_port: Optional[int],
fp: int,
wallet_id: int,
fee: uint64,
amount: uint64,
cat_target_address: str,
vote_amount: Optional[int],
cli_tx_config: CMDTXConfigLoader,
push: bool,
) -> List[TransactionRecord]:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
res = await wallet_client.dao_create_proposal(
wallet_id=wallet_id,
proposal_type="mint",
cat_target_address=cat_target_address,
amount=amount,
vote_amount=vote_amount,
fee=fee,
tx_config=cli_tx_config.to_tx_config(units["chia"], config, fingerprint),
push=push,
)
if push:
print("Successfully created proposal.")
print(f"Proposal ID: {res.proposal_id.hex()}")
return res.transactions