Skip to main content

No project description provided

Project description

Tron Api For Python

Tron grpc and http api

Demo

import json

import grpc

from tron_api import tron_utils
from tron_api.grpc.api.api_pb2 import EmptyMessage, Return
from tron_api.grpc.api.api_pb2_grpc import WalletStub
from tron_api.grpc.core.Tron_pb2 import Block, Transaction
from tron_api.grpc.core.contract.balance_contract_pb2 import TransferContract
from tron_api.http.http_api_impl import TronSolidityNodeHttpApi, TronFullNodeHttpApi


private_key = bytes.fromhex("72015f629b750c4ce520209fa866b056a29d7957a1e29645c28ce72fe44fede1")
owner_address = tron_utils.ADDR("TECwVujGXYNTXJ3sWiJJhjRZS3zgptvENe")
to_address = tron_utils.ADDR("TECwVujGXYNTXJ3sWiJJhjRZS3zgptvENe")


class GrpcDemo:
    def __init__(self):
        channel = grpc.insecure_channel("172.18.0.3:50051")
        self.wallet_stub = WalletStub(channel)

    def get_demo(self):
        block: Block = self.wallet_stub.GetNowBlock(EmptyMessage())
        print(block)

    def transfer_demo(self):
        # grpc demo
        tran: Transaction = self.wallet_stub.CreateTransaction(TransferContract(
            owner_address=owner_address,
            to_address=to_address,
            amount=10
        ))
        tran.signature.append(tron_utils.sign_message(private_key, tran.raw_data.SerializeToString()))
        grpc_res: Return = self.wallet_stub.BroadcastTransaction(tran)
        print(grpc_res)


class HttpDemo:
    def __init__(self):
        self.http_api = TronFullNodeHttpApi(gateway="http://172.18.0.3:8090")

    def get_demo(self):
        status, block = self.http_api.getnowblock()
        print(block)

    def transfer_demo(self):
        # http demo
        status, transaction = self.http_api.createtransaction(json_data={
            'owner_address': owner_address.hex(),
            'to_address': to_address.hex(),
            'amount': 2 * 1_000_000,
        })
        transaction = json.loads(transaction)
        transaction["signature"] = [
            tron_utils.sign_message(private_key, bytes.fromhex(transaction.get("raw_data_hex"))).hex()]
        status, res = self.http_api.broadcasttransaction(json_data=transaction)
        print(status, res)


if __name__ == '__main__':
    print(tron_utils.private_key_to_address(private_key=private_key))

    g, h = GrpcDemo(), HttpDemo()
    g.get_demo()
    h.get_demo()
    # g.transfer_demo()
    # h.transfer_demo()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tron_api_python-0.1.0.tar.gz (71.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tron_api_python-0.1.0-py3-none-any.whl (91.3 kB view details)

Uploaded Python 3

File details

Details for the file tron_api_python-0.1.0.tar.gz.

File metadata

  • Download URL: tron_api_python-0.1.0.tar.gz
  • Upload date:
  • Size: 71.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for tron_api_python-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d1f2ef61cd6a87cf29282300c2fbec0c76e450a9a4f15bc3b95651f2d7b994d4
MD5 450c8aa25fbc57a552ee777c351eeb0b
BLAKE2b-256 d83001bfc67a826317c8169a3d649540776d265e8ed90ee6f9166d6052dcbae0

See more details on using hashes here.

File details

Details for the file tron_api_python-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tron_api_python-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4fb70f284bb0baf2340036decd0eabbbf91923f3ff694c5a396b64cfd7e355b2
MD5 94fb0cef79d3519559102c95eab9145a
BLAKE2b-256 09c176ebf9b57b74821103ed6073587dccc0c6cf6826784ec9eba2dc20dffcac

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page