Git Product home page Git Product logo

pytonlib's Introduction

PyTONLib

This is standalone Python library based on libtonlibjson, the functionality is similar to the ton-http-api with the following restrictions:

  • a client can connect to only one LiteServer;
  • a client is asyncronious;
  • no requests cache.

Installation

From PyPi

Currently, the library works for Windows, Mac and Linux only on Intel CPUs:

  • (Windows) Install OpenSSL v1.1.1 for Win64 from here.
  • Install Python 3 package: pip3 install pytonlib.

Docker

In this repo Compose file is provided to deploy the example of service with pytonlib:

docker-compose -f docker-compose.jupyter.yaml build
docker-compose -f docker-compose.jupyter.yaml up -d

Jupyter Notebook will be available on port 3100 (http://localhost:3100).

Examples

We recommend to use IPython or Jupyter Notebook for prototyping because they allow to run async code. An example of running async code from script could be found in the end of this section.

  • Connecting to the first LiteServer in mainnet config:
import requests
import asyncio

from pytonlib import TonlibClient


# downloading mainnet config
ton_config = requests.get('https://newton-blockchain.github.io/global.config.json').json()

# get running event loop
loop = asyncio.get_running_loop()

# init TonlibClient
client = TonlibClient(ls_index=0, # choose LiteServer index to connect
                      config=ton_config,
                      keystore='/tmp/ton_keystore',
                      loop=loop)

# init tonlibjson
await client.init()
  • Reading blocks info:
masterchain_info = await client.get_masterchain_info()
block_header = await client.get_block_header(**masterchain_info['last'])
shards = await client.get_shards(master_seqno=masterchain_info['last']['seqno'])
  • Reading Block Transactions for masterchain block:
masterchain_info = await client.get_masterchain_info()
txs = await client.get_block_transactions(**masterchain_info['last'], count=10)
  • Running async code from script:
import requests
import asyncio

from pytonlib import TonlibClient


async def main():
    loop = asyncio.get_running_loop()
    ton_config = requests.get('https://ton-blockchain.github.io/global.config.json').json()
    
    # init TonlibClient
    client = TonlibClient(ls_index=0, # choose LiteServer index to connect
                          config=ton_config,
                          keystore='/tmp/ton_keystore',
                          loop=loop)
    
    # init tonlibjson
    await client.init()
    
    # reading masterchain info
    masterchain_info = await client.get_masterchain_info()

    # closing session
    await client.close()


if __name__ == '__main__':
    asyncio.run(main())

Running tests

To run tests in asyncio mode use the following command:

PYTHONPATH=./ pytest --asyncio-mode=strict tests/

pytonlib's People

Contributors

kdimentionaltree avatar dungeon-master-666 avatar

Stargazers

Dr. Awesome Doge avatar

Watchers

Dr. Awesome Doge avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.