token - Infernet Client
Description: Infernet Client Documentation

Skip to content

# Module: `token`

A simple class to interact with ERC20 tokens.

### Public Methods

- `balance_of(address: ChecksumAddress) -> Wei`: Get the balance of an address.
- `transfer(to: ChecksumAddress, amount: Wei) -> TxReceipt`: Transfer tokens to an address.

### Example

    from web3.types import ChecksumAddress, Wei

    from infernet_client.chain.rpc import RPC
    from infernet_client.chain.token import Token

    async def main():
        rpc = RPC("http://localhost:8545")
        token = Token("0x123456789012345678901234567890123, rpc)
        balance = await token.balance_of("0x123456789012345678901234567890123")

        token.transfer("0x123456789012345678901234567890123", Wei(100))

        print(balance)

## `Token`

Source code in `src/infernet_client/chain/token.py`

    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    class Token:
        def __init__(self, address: ChecksumAddress, rpc: RPC):
            """
            Create a new Token instance.

            Args:
                address: The address of the token contract.
                rpc: The RPC instance to use for interacting with the chain.

            Returns:
                A new Token instance.
            """
            self.address = address
            self._rpc = rpc
            self._contract = rpc.get_contract(
                address=address,
                abi=ERC20_ABI,
            )

        async def balance_of(self, address: ChecksumAddress) -> Wei:
            """
            Get the balance of an address.

            Args:
                address: The address to get the balance of.
            """

            return cast(Wei, await self._contract.functions.balanceOf(address).call())

        async def transfer(self, to: ChecksumAddress, amount: Wei) -> TxReceipt:
            """
            Transfer tokens to an address.

            Args:
                to: The address to transfer tokens to.
                amount: The amount of tokens to transfer.

            Returns:
                The transaction receipt.
            """

            tx = await self._contract.functions.transfer(to, amount).transact()
            return await self._rpc.get_tx_receipt(tx)

### `__init__(address, rpc)`

Create a new Token instance.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`address` | `ChecksumAddress` | The address of the token contract. | required
`rpc` | `RPC` | The RPC instance to use for interacting with the chain. | required

Returns:

Type | Description
--- | ---
| A new Token instance.

Source code in `src/infernet_client/chain/token.py`

    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    def __init__(self, address: ChecksumAddress, rpc: RPC):
        """
        Create a new Token instance.

        Args:
            address: The address of the token contract.
            rpc: The RPC instance to use for interacting with the chain.

        Returns:
            A new Token instance.
        """
        self.address = address
        self._rpc = rpc
        self._contract = rpc.get_contract(
            address=address,
            abi=ERC20_ABI,
        )

### `balance_of(address)` `async`

Get the balance of an address.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`address` | `ChecksumAddress` | The address to get the balance of. | required

Source code in `src/infernet_client/chain/token.py`

    61
    62
    63
    64
    65
    66
    67
    68
    69
    async def balance_of(self, address: ChecksumAddress) -> Wei:
        """
        Get the balance of an address.

        Args:
            address: The address to get the balance of.
        """

        return cast(Wei, await self._contract.functions.balanceOf(address).call())

### `transfer(to, amount)` `async`

Transfer tokens to an address.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`to` | `ChecksumAddress` | The address to transfer tokens to. | required
`amount` | `Wei` | The amount of tokens to transfer. | required

Returns:

Type | Description
--- | ---
`TxReceipt` | The transaction receipt.

Source code in `src/infernet_client/chain/token.py`

    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    async def transfer(self, to: ChecksumAddress, amount: Wei) -> TxReceipt:
        """
        Transfer tokens to an address.

        Args:
            to: The address to transfer tokens to.
            amount: The amount of tokens to transfer.

        Returns:
            The transaction receipt.
        """

        tx = await self._contract.functions.transfer(to, amount).transact()
        return await self._rpc.get_tx_receipt(tx)