forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer_nft.py
More file actions
176 lines (142 loc) · 5.76 KB
/
transfer_nft.py
File metadata and controls
176 lines (142 loc) · 5.76 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
"""
uv run examples/transfer_nft.py
python examples/transfer_nft.py
"""
import os
import sys
from dotenv import load_dotenv
from hiero_sdk_python import (
Client,
AccountId,
PrivateKey,
Network,
TransferTransaction,
)
from hiero_sdk_python.account.account_create_transaction import AccountCreateTransaction
from hiero_sdk_python.hapi.services.basic_types_pb2 import TokenType
from hiero_sdk_python.hbar import Hbar
from hiero_sdk_python.response_code import ResponseCode
from hiero_sdk_python.tokens.nft_id import NftId
from hiero_sdk_python.tokens.supply_type import SupplyType
from hiero_sdk_python.tokens.token_associate_transaction import TokenAssociateTransaction
from hiero_sdk_python.tokens.token_create_transaction import TokenCreateTransaction
from hiero_sdk_python.tokens.token_mint_transaction import TokenMintTransaction
load_dotenv()
def setup_client():
"""Initialize and set up the client with operator account"""
# Initialize network and client
network = Network(os.getenv('NETWORK'))
client = Client(network)
# Set up operator account
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
client.set_operator(operator_id, operator_key)
return client, operator_id, operator_key
def create_test_account(client):
"""Create a new account for testing"""
# Generate private key for new account
new_account_private_key = PrivateKey.generate()
new_account_public_key = new_account_private_key.public_key()
# Create new account with initial balance of 1 HBAR
transaction = (
AccountCreateTransaction()
.set_key(new_account_public_key)
.set_initial_balance(Hbar(1))
.freeze_with(client)
)
receipt = transaction.execute(client)
# Check if account creation was successful
if receipt.status != ResponseCode.SUCCESS:
print(f"Account creation failed with status: {ResponseCode(receipt.status).name}")
sys.exit(1)
# Get account ID from receipt
account_id = receipt.account_id
print(f"New account created with ID: {account_id}")
return account_id, new_account_private_key
def create_nft(client, operator_id, operator_key):
"""Create a non-fungible token"""
transaction = (
TokenCreateTransaction()
.set_token_name("MyExampleNFT")
.set_token_symbol("EXNFT")
.set_decimals(0)
.set_initial_supply(0)
.set_treasury_account_id(operator_id)
.set_token_type(TokenType.NON_FUNGIBLE_UNIQUE)
.set_supply_type(SupplyType.FINITE)
.set_max_supply(100)
.set_admin_key(operator_key)
.set_supply_key(operator_key)
.set_freeze_key(operator_key)
.freeze_with(client)
)
receipt = transaction.execute(client)
# Check if nft creation was successful
if receipt.status != ResponseCode.SUCCESS:
print(f"NFT creation failed with status: {ResponseCode(receipt.status).name}")
sys.exit(1)
# Get token ID from receipt
nft_token_id = receipt.token_id
print(f"NFT created with ID: {nft_token_id}")
return nft_token_id
def mint_nft(client, nft_token_id, operator_key):
"""Mint a non-fungible token"""
transaction = (
TokenMintTransaction()
.set_token_id(nft_token_id)
.set_metadata(b"My NFT Metadata 1")
.freeze_with(client)
)
receipt = transaction.execute(client)
if receipt.status != ResponseCode.SUCCESS:
print(f"NFT minting failed with status: {ResponseCode(receipt.status).name}")
sys.exit(1)
print(f"NFT minted with serial number: {receipt.serial_numbers[0]}")
return NftId(nft_token_id, receipt.serial_numbers[0])
def associate_nft(client, account_id, token_id, account_private_key):
"""Associate a non-fungible token with an account"""
# Associate the token_id with the new account
associate_transaction = (
TokenAssociateTransaction()
.set_account_id(account_id)
.add_token_id(token_id)
.freeze_with(client)
.sign(account_private_key) # Has to be signed by new account's key
)
receipt = associate_transaction.execute(client)
if receipt.status != ResponseCode.SUCCESS:
print(f"NFT association failed with status: {ResponseCode(receipt.status).name}")
sys.exit(1)
print("NFT successfully associated with account")
def transfer_nft_token(client, nft_id, sender_id, receiver_id):
"""Transfer the NFT from the sender to the receiver account"""
# Transfer nft to the new account
transfer_transaction = (
TransferTransaction()
.add_nft_transfer(nft_id, sender_id, receiver_id)
.freeze_with(client)
)
receipt = transfer_transaction.execute(client)
# Check if nft transfer was successful
if receipt.status != ResponseCode.SUCCESS:
print(f"NFT transfer failed with status: {ResponseCode(receipt.status).name}")
sys.exit(1)
print(f"Successfully transferred NFT to account {receiver_id}")
def main():
"""
Demonstrates the nft transfer functionality by:
1. Creating a new account
2. Creating a nft
3. Minting a nft
4. Associating the nft with the new account
5. Transferring the nft to the new account
"""
client, operator_id, operator_key = setup_client()
account_id, new_account_private_key = create_test_account(client)
token_id = create_nft(client, operator_id, operator_key)
nft_id = mint_nft(client, token_id, operator_key)
associate_nft(client, account_id, token_id, new_account_private_key)
# Transfer the NFT to the new account
transfer_nft_token(client, nft_id, operator_id, account_id)
if __name__ == "__main__":
main()