Skip to content

Commit

Permalink
style: rename & reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
gtors committed Apr 17, 2024
1 parent 7d5c068 commit beec9ff
Show file tree
Hide file tree
Showing 77 changed files with 5,632 additions and 1,531 deletions.
138 changes: 81 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# TONsdk
[![PyPI](https://img.shields.io/pypi/v/tonsdk?color=blue)](https://pypi.org/project/tonsdk/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/tonsdk)](https://pypi.org/project/tonsdk/)
[![Downloads](https://static.pepy.tech/badge/tonsdk)](https://pepy.tech/project/tonsdk)
[![PyPI](https://img.shields.io/pypi/v/tonsdk?color=blue)](https://pypi.org/project/tonsdk_ng/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/tonsdk_ng)](https://pypi.org/project/tonsdk_ng/)
[![Downloads](https://static.pepy.tech/badge/tonsdk_ng)](https://pepy.tech/project/tonsdk_ng)

## Description
This low-level Python library allows you to work with the [TON blockchain](https://ton.org/).
Expand All @@ -13,107 +13,118 @@ This low-level Python library allows you to work with the [TON blockchain](https
## How to install

```bash
pip install tonsdk
pip install tonsdk_ng
```

## How to use

You can find examples in [examples](https://github.com/tonfactory/tonsdk/tree/master/examples) folder
You can find examples in [examples](https://github.com/gtors/tonsdk_ng/tree/master/examples) folder

## General usage examples
### Create mnemonic, init wallet class, create external message to deploy the wallet

```python
from tonsdk.contract.wallet import WalletVersionEnum, Wallets
from tonsdk.utils import bytes_to_b64str
from tonsdk.crypto import mnemonic_new
from tonsdk_ng.contract.wallet import Wallets, WalletVersionEnum
from tonsdk_ng.crypto import mnemonic_new
from tonsdk_ng.utils import bytes_to_b64str


wallet_workchain = 0
wallet_version = WalletVersionEnum.v3r2
wallet_version = WalletVersionEnum.v4r2
wallet_mnemonics = mnemonic_new()

_mnemonics, _pub_k, _priv_k, wallet = Wallets.from_mnemonics(
wallet_mnemonics, wallet_version, wallet_workchain)
wallet_mnemonics,
wallet_version,
wallet_workchain,
)
query = wallet.create_init_external_message()
base64_boc = bytes_to_b64str(query["message"].to_boc(False))

print("""
Mnemonic: {}
print(
f"""
Mnemonic: {wallet_mnemonics}
Raw address: {}
Raw address: {wallet.address.to_string()}
Bounceable, url safe, user friendly address: {}
Bounceable, url safe, user friendly address: {wallet.address.to_string(True, True, True)}
Base64boc to deploy the wallet: {}
""".format(wallet_mnemonics,
wallet.address.to_string(),
wallet.address.to_string(True, True, True),
base64_boc))
Base64boc to deploy the wallet: {base64_boc}
"""
)
```

### Transfer NFT & Jettons by creating a transfer message from an owner wallet
```python
from tonsdk.contract.token.nft import NFTItem
from tonsdk.contract.token.ft import JettonWallet
from tonsdk.utils import Address, to_nano
from tonsdk_ng.contract.token.ft import JettonWallet
from tonsdk_ng.contract.token.nft import NFTItem
from tonsdk_ng.utils import Address, to_nano

body = NFTItem().create_transfer_body(
Address("New Owner Address")
)
body = NFTItem().create_transfer_body(Address("New Owner Address"))
query = wallet.create_transfer_message(
"NFT Item Address",
to_nano(0.05, "ton"),
0, # owner wallet seqno
payload=body
payload=body,
)
nft_boc = bytes_to_b64str(query["message"].to_boc(False))

body = JettonWallet().create_transfer_body(
Address("Destination address"),
to_nano(40000, "ton") # jettons amount
Address("Destination address"), to_nano(40000, "ton") # jettons amount
)
query = wallet.create_transfer_message(
"Jetton Wallet Address",
to_nano(0.05, "ton"),
0, # owner wallet seqno
payload=body
payload=body,
)
jettons_boc = bytes_to_b64str(query["message"].to_boc(False))

print("""
Base64boc to transfer the NFT item: {}
print(
f"""
Base64boc to transfer the NFT item: {nft_boc}
Base64boc to transfer the jettons: {}
""".format(nft_boc, jettons_boc))
Base64boc to transfer the jettons: {jettons_boc}
"""
)
```

### Clients usage example (dirty)

*Note - to use these clients you should install tvm_valuetypes and aiohttp packages*

```python
from abc import ABC, abstractmethod
import asyncio
from abc import ABC, abstractmethod

import aiohttp
from tvm_valuetypes import serialize_tvm_stack

from tonsdk.provider import ToncenterClient, SyncTonlibClient, prepare_address, address_state
from tonsdk.utils import TonCurrencyEnum, from_nano
from tonsdk.boc import Cell
from tonsdk_ng.boc import Cell
from tonsdk_ng.provider import (
SyncTonlibClient,
ToncenterClient,
address_state,
prepare_address,
)
from tonsdk_ng.utils import TonCurrencyEnum, from_nano


class AbstractTonClient(ABC):
@abstractmethod
def _run(self, to_run, *, single_query=True):
raise NotImplemented

def get_address_information(self, address: str,
currency_to_show: TonCurrencyEnum = TonCurrencyEnum.ton):
def get_address_information(
self,
address: str,
currency_to_show: TonCurrencyEnum = TonCurrencyEnum.ton,
):
return self.get_addresses_information([address], currency_to_show)[0]

def get_addresses_information(self, addresses,
currency_to_show: TonCurrencyEnum = TonCurrencyEnum.ton):
def get_addresses_information(
self, addresses, currency_to_show: TonCurrencyEnum = TonCurrencyEnum.ton
):
if not addresses:
return []

Expand All @@ -131,16 +142,19 @@ class AbstractTonClient(ABC):
result["balance"] = 0
else:
result["balance"] = from_nano(
int(result["balance"]), currency_to_show)
int(result["balance"]), currency_to_show
)

return results

def seqno(self, addr: str):
addr = prepare_address(addr)
result = self._run(self.provider.raw_run_method(addr, "seqno", []))

if 'stack' in result and ('@type' in result and result['@type'] == 'smc.runResult'):
result['stack'] = serialize_tvm_stack(result['stack'])
if "stack" in result and (
"@type" in result and result["@type"] == "smc.runResult"
):
result["stack"] = serialize_tvm_stack(result["stack"])

return result

Expand All @@ -151,15 +165,20 @@ class AbstractTonClient(ABC):
class TonCenterTonClient(AbstractTonClient):
def __init__(self):
self.loop = asyncio.get_event_loop()
self.provider = ToncenterClient(base_url="https://testnet.toncenter.com/api/v2/",
api_key="eb542b65e88d2da318fb7c163b9245e4edccb2eb8ba11cabda092cdb6fbc3395")
self.provider = ToncenterClient(
base_url="https://testnet.toncenter.com/api/v2/",
api_key="eb542b65e88d2da318fb7c163b9245e4edccb2eb8ba11cabda092cdb6fbc3395",
)

def _run(self, to_run, *, single_query=True):
try:
return self.loop.run_until_complete(
self.__execute(to_run, single_query))
self.__execute(to_run, single_query)
)

except Exception: # ToncenterWrongResult, asyncio.exceptions.TimeoutError, aiohttp.client_exceptions.ClientConnectorError
except (
Exception
): # ToncenterWrongResult, asyncio.exceptions.TimeoutError, aiohttp.client_exceptions.ClientConnectorError
raise

async def __execute(self, to_run, single_query):
Expand All @@ -171,25 +190,29 @@ class TonCenterTonClient(AbstractTonClient):

tasks = []
for task in to_run:
tasks.append(task["func"](
session, *task["args"], **task["kwargs"]))
tasks.append(
task["func"](session, *task["args"], **task["kwargs"])
)

return await asyncio.gather(*tasks)


class TonLibJsonTonClient(AbstractTonClient):
def __init__(self):
self.loop = asyncio.get_event_loop()
self.provider = SyncTonlibClient(config="./.tonlibjson/testnet.json",
keystore="./.tonlibjson/keystore",
cdll_path="./.tonlibjson/linux_libtonlibjson.so") # or macos_libtonlibjson.dylib
self.provider = SyncTonlibClient(
config="./.tonlibjson/testnet.json",
keystore="./.tonlibjson/keystore",
cdll_path="./.tonlibjson/linux_libtonlibjson.so",
) # or macos_libtonlibjson.dylib
self.provider.init()

def _run(self, to_read, *, single_query=True):
try:
if not single_query:
queries_order = {query_id: i for i,
query_id in enumerate(to_read)}
queries_order = {
query_id: i for i, query_id in enumerate(to_read)
}
return self.provider.read_results(queries_order)

else:
Expand All @@ -204,7 +227,8 @@ client = TonCenterTonClient()

# use client to get any addr information
addr_info = client.get_address_information(
"EQAhE3sLxHZpsyZ_HecMuwzvXHKLjYx4kEUehhOy2JmCcHCT")
"EQAhE3sLxHZpsyZ_HecMuwzvXHKLjYx4kEUehhOy2JmCcHCT"
)

# get your wallet seqno
seqno = client.seqno(wallet.address.to_string())
Expand Down
8 changes: 4 additions & 4 deletions examples/tokens/jetton/mint.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from tonsdk.contract.token.ft import JettonMinter, JettonWallet
from tonsdk.contract import Address
from tonsdk.utils import to_nano, bytes_to_b64str
from tonsdk.contract.wallet import Wallets, WalletVersionEnum
from tonsdk_ng.contract.token.ft import JettonMinter, JettonWallet
from tonsdk_ng.contract import Address
from tonsdk_ng.utils import to_nano, bytes_to_b64str
from tonsdk_ng.contract.wallet import Wallets, WalletVersionEnum


def create_jetton_minter():
Expand Down
6 changes: 3 additions & 3 deletions examples/tokens/jetton/transfer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from tonsdk.contract.token.ft import JettonWallet
from tonsdk.utils import to_nano, bytes_to_b64str, Address
from tonsdk.contract.wallet import Wallets, WalletVersionEnum
from tonsdk_ng.contract.token.ft import JettonWallet
from tonsdk_ng.utils import to_nano, bytes_to_b64str, Address
from tonsdk_ng.contract.wallet import Wallets, WalletVersionEnum


"""your wallet mnemonics"""
Expand Down
8 changes: 4 additions & 4 deletions examples/tokens/nft/mint.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from tonsdk.contract.token.nft import NFTCollection, NFTItem
from tonsdk.contract import Address
from tonsdk.utils import to_nano, bytes_to_b64str
from tonsdk.contract.wallet import Wallets, WalletVersionEnum
from tonsdk_ng.contract.token.nft import NFTCollection, NFTItem
from tonsdk_ng.contract import Address
from tonsdk_ng.utils import to_nano, bytes_to_b64str
from tonsdk_ng.contract.wallet import Wallets, WalletVersionEnum


def create_collection():
Expand Down
6 changes: 3 additions & 3 deletions examples/tokens/nft/transfer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from tonsdk.contract.token.nft import NFTItem
from tonsdk.utils import to_nano, bytes_to_b64str, Address
from tonsdk.contract.wallet import Wallets, WalletVersionEnum
from tonsdk_ng.contract.token.nft import NFTItem
from tonsdk_ng.utils import to_nano, bytes_to_b64str, Address
from tonsdk_ng.contract.wallet import Wallets, WalletVersionEnum


"""your wallet mnemonics"""
Expand Down
4 changes: 2 additions & 2 deletions examples/types/cell.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from tonsdk.boc import begin_cell, Cell
from tonsdk.utils import Address
from tonsdk_ng.boc import begin_cell, Cell
from tonsdk_ng.utils import Address

cell = begin_cell()\
.store_uint(4, 32)\
Expand Down
4 changes: 2 additions & 2 deletions examples/types/slice.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from tonsdk.boc import Slice, begin_cell
from tonsdk.utils import Address
from tonsdk_ng.boc import Slice, begin_cell
from tonsdk_ng.utils import Address

cell = begin_cell()\
.store_uint(4, 32)\
Expand Down
4 changes: 2 additions & 2 deletions examples/wallets/highload.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from tonsdk.contract.wallet import WalletVersionEnum, Wallets
from tonsdk.utils import Address, bytes_to_b64str, b64str_to_bytes, to_nano
from tonsdk_ng.contract.wallet import WalletVersionEnum, Wallets
from tonsdk_ng.utils import Address, bytes_to_b64str, b64str_to_bytes, to_nano

mnemonics = []
_mnemonics, _pub_k, _priv_k, wallet = Wallets.from_mnemonics(
Expand Down
6 changes: 3 additions & 3 deletions examples/wallets/multisig/deploy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from tonsdk.contract.wallet import MultiSigWallet, MultiSigOrder, MultiSigOrderBuilder
from tonsdk.crypto import mnemonic_new, mnemonic_to_wallet_key
from tonsdk.utils import Address, bytes_to_b64str, b64str_to_bytes, to_nano
from tonsdk_ng.contract.wallet import MultiSigWallet, MultiSigOrder, MultiSigOrderBuilder
from tonsdk_ng.crypto import mnemonic_new, mnemonic_to_wallet_key
from tonsdk_ng.utils import Address, bytes_to_b64str, b64str_to_bytes, to_nano


"""import or generate mnemonics"""
Expand Down
6 changes: 3 additions & 3 deletions examples/wallets/multisig/offchain_signatures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from tonsdk.contract.wallet import MultiSigWallet, MultiSigOrder, MultiSigOrderBuilder
from tonsdk.crypto import mnemonic_new, mnemonic_to_wallet_key
from tonsdk.utils import Address, bytes_to_b64str, b64str_to_bytes, to_nano
from tonsdk_ng.contract.wallet import MultiSigWallet, MultiSigOrder, MultiSigOrderBuilder
from tonsdk_ng.crypto import mnemonic_new, mnemonic_to_wallet_key
from tonsdk_ng.utils import Address, bytes_to_b64str, b64str_to_bytes, to_nano


"""import or generate mnemonics"""
Expand Down
6 changes: 3 additions & 3 deletions examples/wallets/multisig/onchain_signatures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from tonsdk.contract.wallet import MultiSigWallet, MultiSigOrder, MultiSigOrderBuilder
from tonsdk.crypto import mnemonic_new, mnemonic_to_wallet_key, verify_sign
from tonsdk.utils import Address, bytes_to_b64str, b64str_to_bytes, to_nano, sign_message
from tonsdk_ng.contract.wallet import MultiSigWallet, MultiSigOrder, MultiSigOrderBuilder
from tonsdk_ng.crypto import mnemonic_new, mnemonic_to_wallet_key, verify_sign
from tonsdk_ng.utils import Address, bytes_to_b64str, b64str_to_bytes, to_nano, sign_message


"""import or generate mnemonics"""
Expand Down
6 changes: 3 additions & 3 deletions examples/wallets/wallet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from tonsdk.crypto import mnemonic_new
from tonsdk.contract.wallet import Wallets, WalletVersionEnum
from tonsdk.utils import to_nano, bytes_to_b64str
from tonsdk_ng.crypto import mnemonic_new
from tonsdk_ng.contract.wallet import Wallets, WalletVersionEnum
from tonsdk_ng.utils import to_nano, bytes_to_b64str


mnemonics = ['broken', 'decade', 'unit', 'bird', 'enrich', 'great', 'nurse', 'offer', 'rescue',
Expand Down
Loading

0 comments on commit beec9ff

Please sign in to comment.