-
Notifications
You must be signed in to change notification settings - Fork 39
/
consolidate_outputs.py
67 lines (51 loc) · 2.45 KB
/
consolidate_outputs.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
import os
from dotenv import load_dotenv
from iota_sdk import ConsolidationParams, Utils, Wallet
# In this example we will consolidate basic outputs from an account with only an AddressUnlockCondition by sending
# them to the same address again.
# This example uses secrets in environment variables for simplicity which
# should not be done in production.
load_dotenv()
if 'STRONGHOLD_PASSWORD' not in os.environ:
raise Exception('.env STRONGHOLD_PASSWORD is undefined, see .env.example')
wallet = Wallet(os.environ['WALLET_DB_PATH'])
wallet.set_stronghold_password(os.environ['STRONGHOLD_PASSWORD'])
account = wallet.get_account('Alice')
# Sync account to make sure account is updated with outputs from previous
# examples.
account.sync()
print('Account synced')
# List unspent outputs before consolidation.
# The output we created with example `request_funds` and the basic output from `mint` have only one
# unlock condition and it is an `AddressUnlockCondition`, and so they are valid for consolidation. They have the
# same `AddressUnlockCondition`(the first address of the account), so they will be consolidated into one
# output.
outputs = account.unspent_outputs()
print('Outputs BEFORE consolidation:')
for i, output_data in enumerate(outputs):
print(f'OUTPUT #{i}')
print(f'- address: #{Utils.hex_to_bech32(output_data.address.pubKeyHash, "rms")}')
print(f'- amount: #{output_data.output.amount}')
print(f'- native tokens: #{output_data.output.nativeTokens}')
print('Sending consolidation transaction...')
# Consolidate unspent outputs and print the consolidation transaction ID
# Set `force` to true to force the consolidation even though the
# `output_threshold` isn't reached.
transaction = account.consolidate_outputs(ConsolidationParams(force=True))
print('Transaction sent: ', transaction.transactionId)
# Wait for the consolidation transaction to get confirmed
block_id = account.retry_transaction_until_included(transaction.transactionId)
print(
f'Transaction included: {os.environ["EXPLORER_URL"]}/block/{block_id}'
)
# Sync account
account.sync()
print('Account synced')
# Outputs after consolidation
outputs = account.unspent_outputs()
print('Outputs AFTER consolidation:')
for i, output_data in enumerate(outputs):
print(f'OUTPUT #{i}')
print(f'- address: #{Utils.hex_to_bech32(output_data.address.pubKeyHash, "rms")}')
print(f'- amount: #{output_data.output.amount}')
print(f'- native tokens: #{output_data.output.nativeTokens}')