-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathwallet.py
158 lines (116 loc) · 5.45 KB
/
wallet.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
"""
This is a walk through of the Wallet class in pycardano.
This class offers a number of methods which simplify many basic use cases,
and abstracts away some of the lower level details.
If you need more advanced functionality, you can directly use the lower level classes and methods,
sometimes in tandem with the Wallet class.
Note: If you plan to use Blockfrost as your chain context, you can set the following environment variables:
`BLOCKFROST_ID_MAINNET` for mainnet
`BLOCKFROST_ID_PREPROD` for preprod
`BLOCKFROST_ID_PREVIEW` for preview
"""
from datetime import datetime
from multiprocessing import pool
from pycardano import *
from pycardano.wallet import Ada, Output, Token, TokenPolicy, Wallet
"""Create a new wallet"""
# Make sure to provide a name so you can easily load it later
# this will save the keys to ./keys/my_wallet.*
# payment and staking keys will be automatically generated, or loaded a wallet of the given name already exists
w = Wallet(
name="my_wallet"
) # set the parameter `network` to mainnet, preprod, or preview
w.sync() # query the wallet for its balance
w.utxos # list of wallets UTXOs
w.ada # view total amount of ADA
w.lovelace # view total amount of lovelace
w.tokens # get a list of all tokens in the wallet
"""Send ADA using the wallet"""
receiver = Address("addr_test1vrm9x2zsux7va6w892g38tvchnzahvcd9tykqf3ygnmwtaqyfg52x")
tx_id = w.send_ada(receiver, Ada(2)) # send 2 ADA to the receiver
"""Sending an entire UTxO"""
# useful if you have to send a refund, for example
tx_id = w.send_utxo(
receiver, w.utxos[0]
) # send the first UTXO in the wallet to the receiver
"""Empty an entire wallet"""
# Careful with this one!
tx_id = w.empty_wallet(receiver)
"""Sign data"""
# can sign a message with either the payment or stake keys
signed_message = w.sign_data(
"Hello world!", mode="payment"
) # default mode is "payment"
"""Mint a token"""
# first generate a policy
my_policy = TokenPolicy(name="my_policy") # give it a descriptive name
# generate a locking policy with expiration
# note: the actual policy locking time might be slightly off from the datetime provided
# this will save a file to ./policy/my_policy.policy
my_policy.generate_minting_policy(signers=w, expiration=datetime(2025, 5, 12, 12, 0, 0))
# create a token with metadata
metadata = {
"description": "This is my first NFT thanks to PyCardano",
"name": "PyCardano NFT example token 1",
"id": 1,
"image": "ipfs://QmRhTTbUrPYEw3mJGGhQqQST9k86v1DPBiTTWJGKDJsVFw",
}
my_nft = Token(policy=my_policy, amount=2, name="MY_NFT_1", metadata=metadata)
tx_id = w.mint_tokens(
to=receiver,
mints=my_nft, # can be a single Token or list of multiple
)
"""Burn a token"""
# Oops, we minted two. Let's burn one.
# There are two ways to do this:
# Method 2
# create a token object with the quantity you want to burn
# note you don't have to include metadata here since it's only relevant for minting, not burning
my_nft = Token(policy=my_policy, amount=1, name="MY_NFT_1")
# this will automatically switch the amount to negative and burn them
tx_id = w.burn_tokens(my_nft)
# Method 2
# this method might be relevant in case you want to mint and burn multiple tokens in one transaction
# set amount to negative integer to burn
my_nft = Token(policy=my_policy, amount=-1, name="MY_NFT_1")
# then use the mint_tokens method
tx_id = w.mint_tokens(
to=receiver,
mints=my_nft, # can be a single Token or list of multiple
)
"""Register a stake address and delegate to a pool"""
pool_hash = "pool17arkxvwwh8rx0ldrzc54ysmpq4pd6gwzl66qe84a0nmh7qx7ze5"
tx_id = w.delegate(pool_hash)
"""Withdraw staking rewards"""
# withdraws all rewards by default, otherwise set `withdraw_amount` to a specific quantity
tx_id = w.withdraw()
"""Fully Manual Transaction"""
# Let's make a monster transaction with all the bells and whistles
# Note: All the above examples are based on the following `transact` method
# so you can also pass any of the following parameters to the above methods as well as **kwargs
# e.g. `change_address`, `signers`, `message`, `await_confirmation`, etc.
my_nft = Token(policy=my_policy, amount=1, name="MY_NFT_1", metadata=metadata)
your_nft = Token(
policy=my_policy, amount=1, name="YOUR_NFT_1", metadata={"Name": "Your NFT"}
)
tx_id = w.transact(
inputs=w, # use all UTXOs in the wallet, can also specify unique UTxOs or addresses
outputs=[
Output(
w, Ada(0), [my_nft]
), # mint an NFT to myself, setting Ada(0) will automatically calculate the minimum amount of ADA needed
Output(receiver, Ada(10), [my_nft]), # send 10 ADA and an NFT to the receiver
],
mints=[
my_nft,
your_nft,
], # must list all mints/burns here, even if they are sent to yourself
# signers = [w, other_w], # if you want to sign with multiple wallets or keys, specify them here
delegations=pool_hash, # delegate to a pool
withdrawals=Ada(2), # withdraw 2 ADA
# change_address=w, # specify a change address, will default to itself if not specified
message="I love PyCardano", # attach a message to the transaction metadata [CIP-0020]
other_metadata={"247": {"random": "metadata"}}, # attach any other metadata
# submit=True # set to False to return the transaction body as CBOR
await_confirmation=True, # set to True to block the code and periodically check until the transaction is confirmed
)