-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathnft_transfer.py
More file actions
59 lines (46 loc) · 2.34 KB
/
nft_transfer.py
File metadata and controls
59 lines (46 loc) · 2.34 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
from ton_core import Address, NetworkGlobalID, to_nano
from tonutils.clients import ToncenterClient
from tonutils.contracts import NFTTransferBuilder, WalletV4R2
# Mnemonic phrase — 24 words (TON-native) or 12/18/24 words (BIP-39 import)
# Used to derive the wallet's private key
MNEMONIC = "word1 word2 word3 ..."
# NFT item address (specific NFT token)
NFT_ITEM_ADDRESS = Address("EQ...")
# Destination address (new NFT owner)
DESTINATION_ADDRESS = Address("UQ...")
async def main() -> None:
# Initialize HTTP client for TON blockchain interaction
# NetworkGlobalID.MAINNET (-239) for production
# NetworkGlobalID.TESTNET (-3) for testing
client = ToncenterClient(network=NetworkGlobalID.MAINNET)
await client.connect()
# Create wallet instance from mnemonic (full access mode)
# Returns: (wallet, public_key, private_key, mnemonic)
# Wallet must be the current owner of the NFT
wallet, _, _, _ = WalletV4R2.from_mnemonic(client, MNEMONIC)
# Transfer NFT to new owner
# NFTTransferBuilder constructs proper NFT transfer message
# destination: new owner address
# nft_address: specific NFT item contract address
# forward_payload: optional message forwarded to new owner (visible in notification)
# forward_amount: nanotons sent to new owner with ownership_assigned notification (TEP-62)
# Must be > 0 to trigger ownership_assigned to new owner's contract
# amount: TON attached to NFT item for gas fees (covers transfer + forward)
# Typical: 0.05 TON is sufficient, increase if forward_amount is higher
msg = await wallet.transfer_message(
NFTTransferBuilder(
destination=DESTINATION_ADDRESS,
nft_address=NFT_ITEM_ADDRESS,
forward_payload="Hello from tonutils!",
forward_amount=1, # 1 nanoton triggers ownership_assigned notification to recipient
amount=to_nano(0.05), # 0.05 TON for gas (covers NFT transfer fees)
)
)
# Normalized hash of the signed external message (computed locally before sending)
# Not a blockchain transaction hash — use it to track whether the message
# was accepted on-chain (e.g. via explorers, API queries, or your own checks)
print(f"Transaction hash: {msg.normalized_hash}")
await client.close()
if __name__ == "__main__":
import asyncio
asyncio.run(main())