wallet - Infernet Client
Description: Infernet Client Documentation

Skip to content

# Module: `wallet`

Class for interacting with an Infernet `Wallet` contract.

### Attributes

- `address`: The address of the wallet contract

### Public Methods

- `approve(spender: ChecksumAddress, token: ChecksumAddress, amount: int) -> TxReceipt`: Approve a spender to spend a certain amount of tokens
- `owner() -> ChecksumAddress`: Get the owner of the wallet
- `get_balance() -> int`: Get the native balance of the wallet
- `get_token_balance(token: ChecksumAddress) -> int`: Get the balance of a token in the wallet
- `withdraw(token: ChecksumAddress, amount: int) -> TxReceipt`: Withdraw an amount of unlocked tokens (only by wallet owner)

## `InfernetWallet`

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

     34
     35
     36
     37
     38
     39
     40
     41
     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
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    class InfernetWallet:
        def __init__(self, address: ChecksumAddress, rpc: RPC):
            """
            Args:
                address: The address of the wallet contract
                rpc: The RPC object to interact with the blockchain
            """
            self.address = address
            self._rpc = rpc
            self._contract = rpc.get_contract(address=address, abi=WALLET_ABI)

        async def approve(
            self, spender: ChecksumAddress, token: ChecksumAddress, amount: int
        ) -> TxReceipt:
            """
            Approve a spender to spend a certain amount of tokens

            Args:
                spender: The address of the spender
                token: The address of the token to approve
                amount: The amount to approve

            Returns:
                The transaction receipt
            """
            tx_hash = await self._contract.functions.approve(
                spender, token, amount
            ).transact()
            receipt = await self._rpc.get_tx_receipt(tx_hash)
            assert await self._contract.functions.allowance(spender, token).call() == amount
            return receipt

        async def owner(self) -> ChecksumAddress:
            """
            Get the owner of the wallet

            Returns:
                The address of the owner
            """
            return Web3.to_checksum_address(await self._contract.functions.owner().call())

        async def get_balance(self) -> int:
            """
            Get the native balance of the wallet

            Returns:
                The native balance
            """
            return await self._rpc.get_balance(self.address)

        async def get_token_balance(self, token: ChecksumAddress) -> int:
            """
            Get the balance of a token in the wallet

            Args:
                token: The address of the token

            Returns:
                The balance of the token
            """
            return await Token(token, self._rpc).balance_of(self.address)

        async def withdraw(self, token: ChecksumAddress, amount: int) -> TxReceipt:
            """
            Withdraw tokens not locked in escrow. Only usable by wallet owner

            Args:
                token: The address of the token
                amount: The amount to withdraw

            Returns:
                The transaction receipt
            """
            tx_hash = await self._contract.functions.withdraw(token, amount).transact()
            receipt = await self._rpc.get_tx_receipt(tx_hash)
            return receipt

### `__init__(address, rpc)`

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`address` | `ChecksumAddress` | The address of the wallet contract | required
`rpc` | `RPC` | The RPC object to interact with the blockchain | required

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

    35
    36
    37
    38
    39
    40
    41
    42
    43
    def __init__(self, address: ChecksumAddress, rpc: RPC):
        """
        Args:
            address: The address of the wallet contract
            rpc: The RPC object to interact with the blockchain
        """
        self.address = address
        self._rpc = rpc
        self._contract = rpc.get_contract(address=address, abi=WALLET_ABI)

### `approve(spender, token, amount)` `async`

Approve a spender to spend a certain amount of tokens

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`spender` | `ChecksumAddress` | The address of the spender | required
`token` | `ChecksumAddress` | The address of the token to approve | required
`amount` | `int` | The amount to approve | required

Returns:

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

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

    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    async def approve(
        self, spender: ChecksumAddress, token: ChecksumAddress, amount: int
    ) -> TxReceipt:
        """
        Approve a spender to spend a certain amount of tokens

        Args:
            spender: The address of the spender
            token: The address of the token to approve
            amount: The amount to approve

        Returns:
            The transaction receipt
        """
        tx_hash = await self._contract.functions.approve(
            spender, token, amount
        ).transact()
        receipt = await self._rpc.get_tx_receipt(tx_hash)
        assert await self._contract.functions.allowance(spender, token).call() == amount
        return receipt

### `get_balance()` `async`

Get the native balance of the wallet

Returns:

Type | Description
--- | ---
`int` | The native balance

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

    75
    76
    77
    78
    79
    80
    81
    82
    async def get_balance(self) -> int:
        """
        Get the native balance of the wallet

        Returns:
            The native balance
        """
        return await self._rpc.get_balance(self.address)

### `get_token_balance(token)` `async`

Get the balance of a token in the wallet

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`token` | `ChecksumAddress` | The address of the token | required

Returns:

Type | Description
--- | ---
`int` | The balance of the token

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

    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    async def get_token_balance(self, token: ChecksumAddress) -> int:
        """
        Get the balance of a token in the wallet

        Args:
            token: The address of the token

        Returns:
            The balance of the token
        """
        return await Token(token, self._rpc).balance_of(self.address)

### `owner()` `async`

Get the owner of the wallet

Returns:

Type | Description
--- | ---
`ChecksumAddress` | The address of the owner

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

    66
    67
    68
    69
    70
    71
    72
    73
    async def owner(self) -> ChecksumAddress:
        """
        Get the owner of the wallet

        Returns:
            The address of the owner
        """
        return Web3.to_checksum_address(await self._contract.functions.owner().call())

### `withdraw(token, amount)` `async`

Withdraw tokens not locked in escrow. Only usable by wallet owner

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`token` | `ChecksumAddress` | The address of the token | required
`amount` | `int` | The amount to withdraw | required

Returns:

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

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

     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    async def withdraw(self, token: ChecksumAddress, amount: int) -> TxReceipt:
        """
        Withdraw tokens not locked in escrow. Only usable by wallet owner

        Args:
            token: The address of the token
            amount: The amount to withdraw

        Returns:
            The transaction receipt
        """
        tx_hash = await self._contract.functions.withdraw(token, amount).transact()
        receipt = await self._rpc.get_tx_receipt(tx_hash)
        return receipt