-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchia_functions.ps1
522 lines (445 loc) · 15.7 KB
/
chia_functions.ps1
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
# PowerShell object to Json for chia rpc
function Edit-DoubleToSingleQuote {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline, mandatory)]
[string] $Value
)
$Value -replace "`"", "'"
}
function Edit-ChiaRpcJson {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline, mandatory)]
[string] $Json
)
$Json -replace '"', '\""'
}
function ConvertTo-ChiaRpcJson {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline, mandatory)]
[PSCustomObject] $object
)
$object | ConvertTo-Json | Edit-ChiaRpcJson
}
function Set-AddressPSVariables {
# https://github.com/Chia-Network/chia-blockchain/pull/13637
$keys = (chia keys show --json | ConvertFrom-Json).keys
foreach ($key in $keys) {
$label = $key.label
$address = $key.wallet_address
New-Variable -Name "addr_$($label)" -Value $address -Force -Scope 1
Write-Host "$address`t => `t`$addr_$($label)"
}
}
function Set-KeyPSVariable {
$keys = (chia keys show --json | ConvertFrom-Json).keys
foreach ($key in $keys) {
$label = $key.label
if ($label) {
$fingerprint = $key.fingerprint
$address = $key.wallet_address
New-Variable -Name "$($label)_fp" -Value $fingerprint -Force -Scope 1
Write-Host "`$$($label)_fp:`t`t$fingerprint"
New-Variable -Name "$($label)_addr" -Value $address -Force -Scope 1
Write-Host "`$$($label)_addr:`t`t$address"
}
}
}
function Set-ChiaPSVariables {
. Set-KeyPSVariable
}
function Reset-Simulator {
$env:CHIA_ROOT = "~/.chia/simulator/main"
$env:CHIA_KEYS_ROOT = "~/.chia_keys_sim_main"
cdv sim stop -wd
Remove-Item -Path "$($env:CHIA_ROOT)/db" -Recurse -Force
Remove-Item -Path "$($env:CHIA_ROOT)/wallet" -Recurse -Force
chia keys label show
cdv sim start -w
#cdv sim autofarm on
}
# Verify that wallet with the $FINGERPRINT is synced
function Wait-SyncedWallet {
param(
[Parameter(mandatory)]
[Int64] $Fingerprint
)
Write-Host "Wait-SyncedWallet: $fingerprint " -NoNewline
$sw = new-object system.diagnostics.stopwatch
$sw.Start()
chia wallet show -f $Fingerprint | Out-Null
[console]::CursorVisible = $false
$cursors = "\|/-"
$cursor_idx = 0
Write-Host "[" -NoNewline
$cursor_pos = $host.UI.RawUI.CursorPosition
Write-Host " ]" -NoNewline
do {
$sync_status = chia rpc wallet get_sync_status | ConvertFrom-Json
$host.UI.RawUI.CursorPosition = $cursor_pos
$cursor_idx++
if ($cursor_idx -ge $cursors.Length) {
$cursor_idx = 0
}
Write-Host $cursors[$cursor_idx] -NoNewline
Start-Sleep -Milliseconds 200
} until ($sync_status.synced)
$sw.Stop()
$cursor_pos.X -= 1
$host.UI.RawUI.CursorPosition = $cursor_pos
Write-Host " "
Write-Host "Wait-SyncedWallet: $($sw.Elapsed.TotalMinutes) minutes"
[console]::CursorVisible = $true
}
function Show-WalletBalance {
param(
[Parameter(mandatory)]
[Int64] $Fingerprint
)
Wait-SyncedWallet -Fingerprint $Fingerprint
chia wallet show -f $Fingerprint
}
# Derive Keys and Decode to Puzzle Hashes
function Get-DerivedPuzzleHashes {
param(
[Parameter(mandatory)]
[Int64] $Fingerprint,
[Parameter(mandatory)]
[Int] $Num,
[string] $AssetId
)
Write-Host "Get-DerivedPuzzleHashes: $fingerprint $num $AssetId"
$sw = new-object system.diagnostics.stopwatch
$sw.Start()
$puzzleHashes =
chia keys derive -f $Fingerprint wallet-address -n $Num
| ForEach-Object { $_ -replace '(^Wallet address )(.*)(: )', "" }
| ForEach-Object { Write-Host "." -NoNewline; cdv decode $_ }
$result = $puzzleHashes
if ($AssetId) {
$catPuzzleHashes =
$puzzleHashes
| ForEach-Object { Write-Host "." -NoNewline; cdv clsp cat_puzzle_hash -t $AssetId $_ }
$result = $catPuzzleHashes
}
$sw.Stop()
Write-Host ""
Write-Host "Get-DerivedPuzzleHashes: $($sw.Elapsed.TotalMinutes) minutes"
return $result
}
# Wait until spendable is greater or equal to the amount
function Wait-EnoughSpendable {
param(
[Parameter(mandatory)]
[int] $WalletId,
[Parameter(mandatory)]
[int64] $Amount,
[Int64] $Fingerprint
)
if ($Fingerprint) {
Wait-SyncedWallet -Fingerprint $Fingerprint
}
$walletIdJson =
[PSCustomObject]@{ wallet_id = $WalletId }
| ConvertTo-Json
| Edit-ChiaRpcJson
Write-Host "Wait-EnoughSpendable: $WalletId $Amount " -NoNewline
[console]::CursorVisible = $false
$cursors = "\|/-"
$cursor_idx = 0
Write-Host "[" -NoNewline
$cursor_pos = $host.UI.RawUI.CursorPosition
Write-Host " ]" -NoNewline
$sw = new-object system.diagnostics.stopwatch
$sw.Start()
do {
$spendableAmount = (chia rpc wallet get_wallet_balance $walletIdJson | ConvertFrom-Json).wallet_balance.spendable_balance
$host.UI.RawUI.CursorPosition = $cursor_pos
$cursor_idx++
if ($cursor_idx -ge $cursors.Length) {
$cursor_idx = 0
}
Write-Host $cursors[$cursor_idx] -NoNewline
Start-Sleep -Milliseconds 200
} until ($spendableAmount -ge $Amount)
$sw.Stop()
$cursor_pos.X -= 1
$host.UI.RawUI.CursorPosition = $cursor_pos
Write-Host " "
Write-Host "Wait-EnoughSpendable: Spendable: $spendableAmount"
Write-Host "Wait-EnoughSpendable: $($sw.Elapsed.TotalMinutes) minutes"
[console]::CursorVisible = $true
}
# Get coins to spend
function Get-Coins {
param(
[Parameter(mandatory)]
[int] $WalletId,
[Parameter(mandatory)]
[int64] $Amount,
[Int64] $Fingerprint
)
if ($Fingerprint) {
Wait-SyncedWallet -Fingerprint $Fingerprint
}
Write-Host "Get-Coins: $WalletId $Amount"
$json = [PSCustomObject]@{
wallet_id = $WalletId
amount = $Amount
}
| ConvertTo-Json
| Edit-ChiaRpcJson
$result = chia rpc wallet select_coins $json 2>&1
if ($result -like "Request failed:*"){
# $match = select-string "Request failed: (.*)" -inputobject $result
# $error_json = $match.Matches.groups[1].value -replace "'", """"
throw $result
} else {
# success
$coins = ($result | ConvertFrom-Json).coins
return $coins
}
}
# Wait for transaction to be done
function Wait-Transaction {
param(
[Parameter(Mandatory)]
[string] $TxnId,
[Int64] $Fingerprint
)
if ($Fingerprint) {
Wait-SyncedWallet -Fingerprint $Fingerprint
}
Write-Host "Wait-Transaction: $TxnId " -NoNewline
[console]::CursorVisible = $false
$cursors = "\|/-"
$cursor_idx = 0
Write-Host "[" -NoNewline
$cursor_pos = $host.UI.RawUI.CursorPosition
Write-Host " ]" -NoNewline
$sw = new-object system.diagnostics.stopwatch
$sw.Start()
$json = [PSCustomObject]@{
transaction_id = $TxnId
}
| ConvertTo-Json
| Edit-ChiaRpcJson
do {
$result = chia rpc wallet get_transaction $json | ConvertFrom-Json
$confirmed = $result.transaction.confirmed
$host.UI.RawUI.CursorPosition = $cursor_pos
$cursor_idx++
if ($cursor_idx -ge $cursors.Length) {
$cursor_idx = 0
}
Write-Host $cursors[$cursor_idx] -NoNewline
} until ($confirmed)
$sw.Stop()
$cursor_pos.X -= 1
$host.UI.RawUI.CursorPosition = $cursor_pos
Write-Host " "
Write-Host "Wait-Transaction: $($sw.Elapsed.TotalMinutes) minutes"
}
# Get CAT Sender Info from Parent-CoinSpend's solution
function Get-CAT-Sender-Info {
param(
[Parameter(Mandatory)]
$ParentCoinSpendSolution
)
$sender_puzzle_hash = brun '(f (r (f (r (r (r (r 1)))))))' $ParentCoinSpendSolution
$amount = brun '(f (r (r (f (r (r (r (r 1))))))))' $ParentCoinSpendSolution
return @{ puzzle_hash = $sender_puzzle_hash; amount = $amount}
}
# Wait for Full Node to be Synced with Progress Bar
function Wait-SyncedFullNode {
$PSStyle.Progress.View = 'Classic'
Write-Host "Syncing Full Node..."
$is_synced = $false
$synced_height = 0
do {
$response = chia rpc full_node get_blockchain_state | ConvertFrom-Json
$sync = $response.blockchain_state.sync
if ($sync.synced) {
$synced_height = $response.blockchain_state.peak.height
$is_synced = $true
} else {
if ($sync.sync_tip_height -le 0) {
continue
}
if ($sync.sync_progress_height -eq $sync.sync_tip_height) {
continue
}
$percentage = [Math]::floor(($sync.sync_progress_height/$sync.sync_tip_height) * 100)
$height_status = "$($sync.sync_progress_height)/$($sync.sync_tip_height) ($($percentage)%)"
Write-Progress -Activity "Syncing in Progress" -Status $height_status -PercentComplete $percentage
Start-Sleep -Milliseconds 50
}
}
until ($is_synced)
Write-Host "Full Node Synced at $($synced_height)."
}
function ConvertTo-HexString{
param(
[Parameter(ValueFromPipeline, Mandatory)]
[string] $Text
)
(
$Text
| Format-Hex -Encoding utf8
| select -Expand Bytes | % { '{0:x2}' -f $_ }
) -join ''
}
# chia keys derive -f $alice_fp wallet-address -n 20
# | % { $_ | Get-ObservedDerivedWalletAddress }
function Get-ObservedDerivedWalletAddress{
param(
[Parameter(ValueFromPipeline, Mandatory)]
[string] $Text
)
$pattern = "^Wallet address (?<idx>\d{1,}): (?<address>\w*)$"
$regex_matches = Select-String -Pattern $pattern -InputObject $Text
$idx = $regex_matches.Matches[0].Groups['idx'].Value
$address = $regex_matches.Matches[0].Groups['address'].Value
return [pscustomobject]@{ index = $idx; address = $address }
}
# chia keys derive -f $alice_fp child-key -t wallet -n 20
# | % { $_ | Get-ObservedDerivedPublicKey }
function Get-ObservedDerivedWalletPublicKey{
param(
[Parameter(ValueFromPipeline, Mandatory)]
[string] $Text
)
$pattern = "^Wallet public key (?<idx>\d{1,}): (?<pk>\w*)$"
$regex_matches = Select-String -Pattern $pattern -InputObject $Text
$idx = $regex_matches.Matches[0].Groups['idx'].Value
$address = $regex_matches.Matches[0].Groups['pk'].Value
return [pscustomobject]@{ index = $idx; public_key = $address }
}
function Get-Chia-Mempool-Size{
return chia rpc full_node get_blockchain_state | jq ".blockchain_state.mempool_size"
}
function Get-Chia-Block-Height{
return chia rpc full_node get_blockchain_state | jq ".blockchain_state.peak.height"
}
# https://docs.chia.net/full-node-rpc/#get_fee_estimate
function Get-Chia-Fee-Estimate{
[CmdletBinding()]
param (
[Parameter(Mandatory=$false)]
[ValidateSet("XCH", "CAT", "TAKE_OFFER", "CANCEL_OFFER", "NFT_SET_NFT_DID", "NFT_TRANSFER_NFT", "CREATE_NEW_POOL_WALLET", "PW_ABSORB_REWARDS", "CREATE_NEW_DID_WALLET")]
[string]$Type = "XCH",
[Parameter(Mandatory=$false)]
[int]$Time = 60, # target_times in seconds,
[Parameter(Mandatory=$false)]
[Int]$NumberOfCoins = 1,
[Parameter(Mandatory=$false)]
[switch]$Mojos = $false # e.g., true, 50000000 vs false 0.00005
)
# https://github.com/Chia-Network/chia-blockchain/blob/92499b64a26784081e76f2e1f00582033fe64da7/chia/rpc/full_node_rpc_api.py#L846
$tx_cost_estimates = @{
"xch" = 9401710
"cat" = 36382111
"take_offer" = 721393265
"cancel_offer" = 212443993
"nft_set_nft_did" = 115540006
"nft_transfer_nft" = 74385541 # burn or transfer
"create_new_pool_wallet" = 18055407
"pw_absorb_rewards" = 82668466
"create_new_did_wallet" = 57360396
}
$payload = @{cost = [Int64]($tx_cost_estimates[$Type.ToLower()] * $NumberOfCoins); target_times = @($Time) } | ConvertTo-Json
$fee = chia rpc full_node get_fee_estimate $payload | jq ".estimates.[0]"
if ($Mojos) {
return $fee
} else {
# use .ToString("N12") to format to 12 decimal places
return ($fee / 1000000000000).ToString("N12")
}
}
function Get-Chia-Fee-Ranges{
$mempool_items = (chia rpc full_node get_all_mempool_items | ConvertFrom-Json).mempool_items
$fees = $mempool_items.psobject.properties | % { $_.Value.fee }
$groups = @(
@{ name="0 mojo"; count = 0},
@{ name="1-1,000,000 (0.000001)"; count = 0},
@{ name="1,000,001-5,000,000 (0.000005)"; count = 0},
@{ name="5,000,001-50,000,000 (0.00005)"; count = 0},
@{ name="50,000,001-500,000,000 (0.0005)"; count = 0},
@{ name="< 1 XCH"; count = 0},
@{ name=">= 1 XCH"; count = 0},
@{ name="Total"; count = 0}
)
foreach ($fee in $fees) {
if ($fee -eq 0) {
$groups[0].count++
} elseif ($fee -le 1*1e6) {
$groups[1].count++
} elseif ($fee -le 5*1e6) {
$groups[2].count++
} elseif ($fee -le 5*1e7) {
$groups[3].count++
} elseif ($fee -le 5*1e8) {
$groups[4].count++
} elseif ($fee -lt 1e12) {
$groups[5].count++
} else {
$groups[6].count++
}
$groups[7].count++
}
return $groups | % { [pscustomobject]$_ }
}
function Get-Chia-Mempool-Info-By-Coin{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline)]
[string]$Name
)
$payload = @{coin_name = $Name} | ConvertTo-Json
$mempool_items = (chia rpc full_node get_mempool_items_by_coin_name $payload | ConvertFrom-Json).mempool_items
$mempool_info = $mempool_items | % { [PSCustomObject]@{
name = $_.spend_bundle_name
fee = $_.fee
cost = $_.cost
fee_per_cost = $_.fee / $_.cost
additions = $_.additions.Length
removals = $_.removals.Length
}}
return $mempool_info
}
# Get-Chia-Mempool-Info-By-Coin -Name "0xf5832bcf72821404b1c708a2db2c509e3d83b37545a74fb81502171ca637f307" |
# % { $_.name } |
# Get-Chia-Mempool-SpendBudle-By-Id
function Get-Chia-Mempool-SpendBudle-By-Id{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline)]
[string]$Id
)
$payload = @{tx_id = $Id} | ConvertTo-Json
return chia rpc full_node get_mempool_item_by_tx_id $payload | jq ".mempool_item.spend_bundle"
}
function Remove-Chia-Expired-Offers{
$logged_in_fingerprint = chia rpc wallet get_logged_in_fingerprint | jq ".fingerprint"
Write-Host "Logged in fingerprint: $logged_in_fingerprint"
$current_time = [int](Get-Date -UFormat %s -Millisecond 0)
$current_block_height = Get-Chia-Block-Height
Write-Host "Current Block Height: $current_block_height"
Write-Host "Current Time: $current_time"
$all_offers = chia rpc wallet get_all_offers | ConvertFrom-Json | % { $_.trade_records }
$expired_offers = $all_offers |
? { ($_.valid_times.max_height -and ($_.valid_times.max_height -lt $current_block_height)) -or ($_.valid_times.max_time -and ($_.valid_times.max_time -lt $current_time)) }
Write-Host "All offers: $($all_offers.Length)"
Write-Host "Expired offers: $($expired_offers.Length)"
foreach ($offer in $expired_offers) {
$payload = @{trade_id = $offer.trade_id; secure=$False} | ConvertTo-Json
$result = chia rpc wallet cancel_offer $payload | ConvertFrom-Json
if ($result.success) {
Write-Host "Cancelled offer: $($offer.trade_id)"
} else {
Write-Host "Failed to cancel offer: $($offer.trade_id)"
}
}
}