Ejemplo n.º 1
0
async def test_async_trc20_transfer():
    priv_key = PrivateKey(
        bytes.fromhex(
            "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")
    )
    async with AsyncTron(network='nile') as client:
        contract = await client.get_contract(
            'THi2qJf6XmvTJSpZHc17HgQsmJop6kb3ia')
        print(
            'Balance', await
            contract.functions.balanceOf('TGQgfK497YXmjdgvun9Bg5Zu3xE15v17cu'))
        txb = await contract.functions.transfer(
            'TVjsyZ7fYF3qLF6BQgPmTEZy1xrNNyVAAA', 1_000)
        txb = txb.with_owner('TGQgfK497YXmjdgvun9Bg5Zu3xE15v17cu').fee_limit(
            5_000_000)
        txn = await txb.build()
        txn = txn.sign(priv_key).inspect()
        txn_ret = await txn.broadcast()

        print(txn)
        # wait
        receipt = await txn_ret.wait()
        print(receipt)
        if 'contractResult' in receipt:
            print(
                'result:',
                contract.functions.transfer.parse_output(
                    receipt['contractResult'][0]))

        # result
        print(await txn_ret.result())
Ejemplo n.º 2
0
async def test_async_contract_create():
    # TGQgfK497YXmjdgvun9Bg5Zu3xE15v17cu
    priv_key = PrivateKey(
        bytes.fromhex(
            "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")
    )
    async with AsyncTron(network='nile') as client:
        bytecode = "608060405234801561001057600080fd5b5060c78061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c806360fe47b11460375780636d4ce63c146062575b600080fd5b606060048036036020811015604b57600080fd5b8101908080359060200190929190505050607e565b005b60686088565b6040518082815260200191505060405180910390f35b8060008190555050565b6000805490509056fea2646970667358221220c8daade51f673e96205b4a991ab6b94af82edea0f4b57be087ab123f03fc40f264736f6c63430006000033"
        abi = [{
            "inputs": [],
            "name":
            "get",
            "outputs": [{
                "internalType": "uint256",
                "name": "retVal",
                "type": "uint256"
            }],
            "stateMutability":
            "view",
            "type":
            "function",
        }]

        cntr = AsyncContract(name="SimpleStore", bytecode=bytecode, abi=abi)

        txb = client.trx.deploy_contract('TGQgfK497YXmjdgvun9Bg5Zu3xE15v17cu',
                                         cntr).fee_limit(1_000_000)
        txn = await txb.build()
        txn = txn.sign(priv_key).inspect()
        txn_ret = await txn.broadcast()

        print(txn_ret)
        result = await txn_ret.wait()
        print(result)
        print('Created:', result['contract_address'])
Ejemplo n.º 3
0
async def test_async_client_timeout():
    from httpx import TimeoutException

    # must be a timeout
    async with AsyncTron(network='nile', conf={'timeout': 0.0001}) as client:
        with pytest.raises(TimeoutException):
            await client.get_block()
Ejemplo n.º 4
0
async def test_async_manual_client():
    from httpx import AsyncClient, Timeout, Limits
    from tronpy.providers.async_http import AsyncHTTPProvider
    from tronpy.defaults import CONF_NILE

    _http_client = AsyncClient(limits=Limits(max_connections=100,
                                             max_keepalive_connections=20),
                               timeout=Timeout(timeout=10, connect=5, read=5))
    provider = AsyncHTTPProvider(CONF_NILE, client=_http_client)
    client = AsyncTron(provider=provider)

    priv_key = PrivateKey(
        bytes.fromhex(
            "8888888888888888888888888888888888888888888888888888888888888888")
    )
    txb = (client.trx.transfer("TJzXt1sZautjqXnpjQT4xSCBHNSYgBkDr3",
                               "TVjsyZ7fYF3qLF6BQgPmTEZy1xrNNyVAAA",
                               1_000).memo("test memo").fee_limit(1_000_000))
    txn = await txb.build()
    txn_ret = await txn.sign(priv_key).broadcast()

    print(txn_ret)
    print(await txn_ret.wait())

    # must call .close at end to release connections
    await client.close()
Ejemplo n.º 5
0
async def test_async_client_get_contract():
    async with AsyncTron() as client:
        cntr = await client.get_contract("TMDRdYAcXbQDajbGFy4rgXcNLYswuYsfk1")
        print(cntr)

        print(cntr.abi)
        # print(client.get_contract("TTjacDH5PL8hpWirqU7HQQNZDyF723PuCg"))

        print(await cntr.functions.name())
Ejemplo n.º 6
0
async def test_async_query_event_logs():
    async with AsyncTron() as client:
        txi = await client.get_transaction_info('eb47b9779a759203899d46f2bda75c0335405f7bcba838aad8781697f216b177')
        await asyncio.sleep(1)  # due to tron official node's freq limit
        cnr = await client.get_contract('TEkxiTehnzSmSe2XqrBj4w32RUN966rdz8')
        events = list(cnr.events.Transfer.process_receipt(txi))
        assert events
        assert events[0]['event'] == 'Transfer'
        assert events[0]['address'] == 'TEkxiTehnzSmSe2XqrBj4w32RUN966rdz8'
        assert events[0]['args'] == {
            'from': 'TMuY43m8TQ2hZ1naSiDyGujosVSMZoWLrq',
            'to': 'TXX1i3BWKBuTxUmTERCztGyxSSpRagEcjX',
            'value': 459155742
        }
Ejemplo n.º 7
0
async def test_async_query_account():
    async with AsyncTron() as client:
        # There are many TRC10 token named `BTT`
        with pytest.raises(Exception):
            btt = await client.get_asset_from_name("BTT")
            print(btt)

        bals = await client.get_account_asset_balances("TCrahg7N9cB1SwN21WzVMqxCptbRdvQata")
        print(bals)
        assert len(bals) > 0

        bal = await client.get_account_asset_balance("TCrahg7N9cB1SwN21WzVMqxCptbRdvQata", 1002928)
        print(bal)
        assert bal > 0
Ejemplo n.º 8
0
async def test_async_const_functions():
    async with AsyncTron(network='nile') as client:
        contract = await client.get_contract(CNR_ADDR)
        assert contract

        assert 'name' in dir(contract.functions)

        print(dir(contract.functions))
        print(repr(await contract.functions.name()))
        print(repr(await contract.functions.decimals()))

        assert await contract.functions.totalSupply() > 0

        for f in contract.functions:
            print(f)
Ejemplo n.º 9
0
async def test_async_const_functions():
    async with AsyncTron(network='nile') as client:
        contract = await client.get_contract(
            'THi2qJf6XmvTJSpZHc17HgQsmJop6kb3ia')
        assert contract

        assert 'name' in dir(contract.functions)

        print(dir(contract.functions))
        print(repr(await contract.functions.name()))
        print(repr(await contract.functions.decimals()))

        assert await contract.functions.totalSupply() > 0

        for f in contract.functions:
            print(f)
Ejemplo n.º 10
0
async def test_client_transfer_trc10():
    async with AsyncTron(network='nile') as client:
        priv_key = PrivateKey(
            bytes.fromhex(
                "ebf7c9cad1ca710553c22669fd3c7c70832e7024c1a32da69bbc5ad19dcc8992"
            ))

        txb = (client.trx.asset_transfer(
            "TGxv9UXRNMh4E6b33iuH1pqJfBffz6hXnV",
            "TVjsyZ7fYF3qLF6BQgPmTEZy1xrNNyVAAA",
            1_000,
            token_id=1000047).memo("test transfer coin").fee_limit(0))
        txn = await txb.build()
        txn.inspect()
        txn = txn.sign(priv_key)
        txn_ret = await txn.broadcast()
        print(txn_ret)
Ejemplo n.º 11
0
async def test_async_client():
    async with AsyncTron(network='nile') as client:
        print(client)
        priv_key = PrivateKey(
            bytes.fromhex(
                "8888888888888888888888888888888888888888888888888888888888888888"
            ))

        txb = (client.trx.transfer(
            "TJzXt1sZautjqXnpjQT4xSCBHNSYgBkDr3",
            "TVjsyZ7fYF3qLF6BQgPmTEZy1xrNNyVAAA",
            1_000).memo("test memo").fee_limit(100_000_000))
        txn = await txb.build()
        txn.inspect()
        txn_ret = await txn.sign(priv_key).broadcast()

        print(txn_ret)
        print(await txn_ret.wait())
Ejemplo n.º 12
0
async def test_async_client_sign_offline():
    async with AsyncTron(network='nile') as client:
        priv_key = PrivateKey(
            bytes.fromhex(
                "8888888888888888888888888888888888888888888888888888888888888888"
            ))
        tx = await client.trx.transfer(
            "TJzXt1sZautjqXnpjQT4xSCBHNSYgBkDr3",
            "TVjsyZ7fYF3qLF6BQgPmTEZy1xrNNyVAAA",
            1).memo("test memo").fee_limit(100_000_000).build()
        tx_j = tx.to_json()
        # offline
        tx_offline = await AsyncTransaction.from_json(
            tx_j)  # tx_offline._client is None so it's offline
        tx_offline.sign(priv_key)
        tx_j2 = tx_offline.to_json()
        # online
        tx_2 = await AsyncTransaction.from_json(tx_j2, client=client)
        await tx_2.broadcast()
Ejemplo n.º 13
0
async def test_async_trc20_transfer():
    async with AsyncTron(network='nile') as client:
        contract = await client.get_contract(CNR_ADDR)
        print('Balance', await contract.functions.balanceOf(FROM_ADDR))
        txb = await contract.functions.transfer(TO_ADDR, 1_000)
        txb = txb.with_owner(FROM_ADDR).fee_limit(5_000_000)
        txn = await txb.build()
        txn = txn.sign(FROM_PRIV_KEY).inspect()
        txn_ret = await txn.broadcast()

        print(txn)
        # wait
        receipt = await txn_ret.wait()
        print(receipt)
        if 'contractResult' in receipt:
            print(
                'result:',
                contract.functions.transfer.parse_output(
                    receipt['contractResult'][0]))

        # result
        print(await txn_ret.result())
Ejemplo n.º 14
0
def test_async_client_keygen():
    client = AsyncTron()
    print(client.generate_address())
    print(client.get_address_from_passphrase('A'))