InjectiveLabs / sdk-python

Injective Exchange API for Python clients
Apache License 2.0
41 stars 27 forks source link

Multiple Transacations and GetCw20Balance #302

Open thehood1 opened 8 months ago

thehood1 commented 8 months ago

Hello,

There is GetCw20Balance in python API docs, but there isn't example code. Can you please add it?

Also, is it possible to broadcast two or more different transactions at once, or transactions must be broadcasted one by one? If it is possible which msg I should use?

Thanks

apri-me commented 2 months ago

Here's an example of how you can get cw20 balances via python SDK.

import asyncio
#
from pyinjective import AsyncClient
from pyinjective.core.network import Network
#
from pprint import pprint

async def main():
    client = AsyncClient(Network.mainnet())
    result = await client.exchange_explorer_api.fetch_cw20_balance('inj...')
    pprint(result)

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

Output will be something like below:

{'field': [{'account': 'inj...',
            'balance': '....',
            'contractAddress': 'inj...',
            'cw20Metadata': {'tokenInfo': {'decimals': '...',
                                           'name': '...',
                                           'symbol': '...',
                                           'totalSupply': ''}},
            'updatedAt': '...'},
           {'account': 'inj...',
            'balance': '....',
            'contractAddress': 'inj...',
            'cw20Metadata': {'tokenInfo': {'decimals': '...',
                                           'name': '...',
                                           'symbol': '...',
                                           'totalSupply': ''}},
            'updatedAt': '...'}]}

Also, You can send multiple messages through one tx. Examples are provided in the API docs, I just show you how you can put multiple messages in one tx.

tx = (
      Transaction()
      .with_messages(*msgs)
      .with_sequence(sequence)
      .with_account_num(client.get_number())
      .with_chain_id(chain_id)
)