Unlock the Power of TON Blockchain: A Step-by-Step Guide to Sending Coins/Jettons using Python
Image by Yann - hkhazo.biz.id

Unlock the Power of TON Blockchain: A Step-by-Step Guide to Sending Coins/Jettons using Python

Posted on

Are you ready to tap into the vast potential of the TON blockchain? In this comprehensive guide, we’ll take you on a journey to send coins/jettons on the TON blockchain using Python. Buckle up, and let’s dive in!

What is TON Blockchain?

TON (The Open Network) is a decentralized, open-source blockchain platform created by Telegram. It’s designed to be fast, scalable, and flexible, making it an attractive choice for developers and users alike. TON’s native cryptocurrency is called Gram (GRM), but for the purpose of this article, we’ll focus on sending coins/jettons, which are a type of token on the TON network.

Prerequisites

Before we begin, make sure you have the following installed on your system:

  • Python 3.7 or higher
  • TON devnet wallet (download from the official TON website)
  • tonclient Python library (install using pip: pip install tonclient)
  • A basic understanding of Python programming

Setting up the Environment

Let’s set up our environment by creating a new Python project and installing the required libraries.

mkdir ton-send-coins
cd ton-send-coins
python -m venv venv
source venv/bin/activate
pip install tonclient

Now that we have our environment set up, let’s create a new Python file called ton_send_coins.py and start coding!

Importing Libraries and Setting up the TON Client

In our ton_send_coins.py file, add the following code:

import asyncio
from tonclient import TonClient, Abi, deploy_localhost

# Set up the TON client
client = TonClient()

# Set up the devnet wallet
wallet = client.get_localhost_wallet()

# Set the GRM (Gram) cryptocurrency as the default
client.set_default_currency('GRM')

Creating a New Wallet and Funding it

Next, we’ll create a new wallet and fund it with some GRM to use for sending coins/jettons.

# Create a new wallet
new_wallet = client.create_new_wallet()

# Get the wallet address
wallet_address = new_wallet['address']

# Fund the new wallet with 10 GRM (you can adjust the amount as needed)
client.fund_wallet(wallet_address, 10)

print(f'New wallet address: {wallet_address}')

Defining the Coin/Jetton Contract

In this example, we’ll use a simple coin/jetton contract. Create a new file called coin_contract.abi with the following content:

{
  "ABI version": 2,
  "header": ["pubkey", "time", "expire"],
  "functions": [
    {
      "name": "constructor",
      "inputs": [
        {"name": "owner", "type": "address"}
      ],
      "outputs": []
    },
    {
      "name": "transfer",
      "inputs": [
        {"name": "amount", "type": "uint128"},
        {"name": "recipient", "type": "address"}
      ],
      "outputs": []
    }
  ]
}

This contract has two functions: a constructor to set the owner of the coin/jetton and a transfer function to send the coin/jetton to another address.

Deploying the Coin/Jetton Contract

Now, let’s deploy the coin/jetton contract to the TON network.

# Deploy the contract
contract_address = client.deploy_localhost('coin_contract.abi', '0x1234567890abcdef')

print(f'Contract address: {contract_address}')

Sending Coins/Jettons

Finally, let’s send some coins/jettons from our funded wallet to another address.

# Set the recipient address
recipient_address = '0x9876543210fedcba'

# Set the amount of coins/jettons to send (in GRM)
amount = 1

# Call the transfer function
client.call(contract_address, 'transfer', {'amount': amount, 'recipient': recipient_address})

print(f'Sent {amount} coins/jettons to {recipient_address}')

Full Code

Here’s the full code for your reference:

import asyncio
from tonclient import TonClient, Abi, deploy_localhost

# Set up the TON client
client = TonClient()

# Set up the devnet wallet
wallet = client.get_localhost_wallet()

# Set the GRM (Gram) cryptocurrency as the default
client.set_default_currency('GRM')

# Create a new wallet
new_wallet = client.create_new_wallet()

# Get the wallet address
wallet_address = new_wallet['address']

# Fund the new wallet with 10 GRM
client.fund_wallet(wallet_address, 10)

print(f'New wallet address: {wallet_address}')

# Deploy the contract
contract_address = client.deploy_localhost('coin_contract.abi', '0x1234567890abcdef')

print(f'Contract address: {contract_address}')

# Set the recipient address
recipient_address = '0x9876543210fedcba'

# Set the amount of coins/jettons to send (in GRM)
amount = 1

# Call the transfer function
client.call(contract_address, 'transfer', {'amount': amount, 'recipient': recipient_address})

print(f'Sent {amount} coins/jettons to {recipient_address}')

Conclusion

And that’s it! You’ve successfully sent coins/jettons on the TON blockchain using Python. This is just the beginning of your TON blockchain journey. Explore the vast possibilities of the TON ecosystem and create innovative solutions that change the world.

Remember to check the TON documentation and official resources for more information on the TON blockchain and its features.

Tips and Resources
TON Official Website
TON GitHub Repository
TON Documentation

Happy coding, and see you in the TON ecosystem!

Here are 5 Questions and Answers about “Send coins/jettons on TON blockchain using Python”:

Frequently Asked Question

Get ready to explore the world of TON blockchain and Python programming!

What is TON blockchain and how does it relate to sending coins/jettons using Python?

TON blockchain is a decentralized blockchain platform that allows for fast, scalable, and secure transactions. In the context of sending coins/jettons, TON blockchain provides a platform for developers to create and deploy decentralized applications (dApps) that can handle coin transactions. Using Python, developers can interact with the TON blockchain, create smart contracts, and send coins/jettons programmatically.

What are the prerequisites for sending coins/jettons on TON blockchain using Python?

To get started, you’ll need to have Python installed on your machine, as well as a TON blockchain wallet and a basic understanding of Python programming. Additionally, you’ll need to have the TON blockchain SDK (Software Development Kit) installed, which provides a set of libraries and tools for interacting with the TON blockchain.

How do I create a TON blockchain wallet using Python?

Creating a TON blockchain wallet using Python involves using the TON blockchain SDK to generate a new wallet address and private key. You can use the `tonos-cli` library in Python to create a new wallet and store the private key securely. The `tonos-cli` library provides a range of functions for working with the TON blockchain, including creating and managing wallets.

What is the process for sending coins/jettons on TON blockchain using Python?

To send coins/jettons on the TON blockchain using Python, you’ll need to create a new transaction using the `tonos-cli` library. This involves specifying the sender and recipient addresses, the amount of coins/jettons to be sent, and any additional transaction details. Once the transaction is created, you can use the `tonos-cli` library to broadcast the transaction to the TON blockchain network.

How do I handle errors and exceptions when sending coins/jettons on TON blockchain using Python?

When sending coins/jettons on the TON blockchain using Python, it’s essential to handle errors and exceptions properly to ensure that your code is robust and reliable. You can use try-except blocks to catch and handle exceptions raised by the `tonos-cli` library, and implement logging and debugging mechanisms to diagnose and resolve issues.