wallet_factory - Infernet Client
Description: Infernet Client Documentation

Skip to content

# Module: `wallet_factory`

Class for interacting with Infernet's `WalletFactory` contract on-chain.

The `WalletFactory` contract is responsible for creating new payment wallets for users.

### Public Methods

- `create_wallet(owner: ChecksumAddress) -> InfernetWallet`: Create a new payment wallet for the given owner.
- `is_valid_wallet(wallet: ChecksumAddress) -> bool`: Check if a given wallet is a valid payment wallet.

### Example Usage

    ```python
    from infernet_client.chain.wallet_factory import WalletFactory
    from infernet_client.chain.rpc import RPC

    rpc = RPC("http://localhost:8545")
    wallet_factory = WalletFactory("0x123...", rpc)
    wallet = await wallet_factory.create_wallet("0x456...")

    is_valid = await wallet_factory.is_valid_wallet(wallet.address)
    ```

## `WalletFactory`

Source code in `src/infernet_client/chain/wallet_factory.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
    85
    86
    87
    88
    89
    90
    91
    class WalletFactory:
        def __init__(self: WalletFactory, address: ChecksumAddress, rpc: RPC) -> None:
            """
            Class for interacting with Infernet's `WalletFactory` contract on-chain.

            Args:
                address (ChecksumAddress): Address of the `WalletFactory` contract.
                rpc (RPC): RPC object for interacting with the chain.

            Returns:
                WalletFactory: Instance of the `WalletFactory` class.
            """

            self.address = address
            self._rpc = rpc
            self._contract = self._rpc.get_contract(address=address, abi=WALLET_FACOTRY_ABI)

        async def create_wallet(
            self: WalletFactory,
            owner: ChecksumAddress,
        ) -> InfernetWallet:
            """
            Create a new payment wallet for the given owner.

            Args:
                owner (ChecksumAddress): Address of the wallet owner.

            Returns:
                InfernetWallet: Instance of the `InfernetWallet` class.
            """
            fn: AsyncContractFunction = self._contract.functions.createWallet(owner)
            new_wallet: ChecksumAddress = await fn.call()
            tx_hash = await fn.transact()
            await self._rpc.get_tx_receipt(tx_hash)
            assert await self.is_valid_wallet(new_wallet)
            log.info(f"created payment wallet {new_wallet} tx_hash={tx_hash.hex()}")
            return InfernetWallet(new_wallet, self._rpc)

        async def is_valid_wallet(self, wallet: ChecksumAddress) -> bool:
            """
            Check if a given wallet is a valid payment wallet. Reads from the `isValidWallet`
            function of the `WalletFactory` contract.

            Args:
                wallet (ChecksumAddress): Address of the wallet.

            Returns:
                bool: True if the wallet is a valid payment wallet, False otherwise.
            """
            return cast(bool, await self._contract.functions.isValidWallet(wallet).call())

### `__init__(address, rpc)`

Class for interacting with Infernet's `WalletFactory` contract on-chain.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`address` | `ChecksumAddress` | Address of the `WalletFactory` contract. | required
`rpc` | `RPC` | RPC object for interacting with the chain. | required

Returns:

Name | Type | Description
--- | --- | ---
`WalletFactory` | `None` | Instance of the `WalletFactory` class.

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

    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    def __init__(self: WalletFactory, address: ChecksumAddress, rpc: RPC) -> None:
        """
        Class for interacting with Infernet's `WalletFactory` contract on-chain.

        Args:
            address (ChecksumAddress): Address of the `WalletFactory` contract.
            rpc (RPC): RPC object for interacting with the chain.

        Returns:
            WalletFactory: Instance of the `WalletFactory` class.
        """

        self.address = address
        self._rpc = rpc
        self._contract = self._rpc.get_contract(address=address, abi=WALLET_FACOTRY_ABI)

### `create_wallet(owner)` `async`

Create a new payment wallet for the given owner.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`owner` | `ChecksumAddress` | Address of the wallet owner. | required

Returns:

Name | Type | Description
--- | --- | ---
`InfernetWallet` | `InfernetWallet` | Instance of the `InfernetWallet` class.

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

    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    async def create_wallet(
        self: WalletFactory,
        owner: ChecksumAddress,
    ) -> InfernetWallet:
        """
        Create a new payment wallet for the given owner.

        Args:
            owner (ChecksumAddress): Address of the wallet owner.

        Returns:
            InfernetWallet: Instance of the `InfernetWallet` class.
        """
        fn: AsyncContractFunction = self._contract.functions.createWallet(owner)
        new_wallet: ChecksumAddress = await fn.call()
        tx_hash = await fn.transact()
        await self._rpc.get_tx_receipt(tx_hash)
        assert await self.is_valid_wallet(new_wallet)
        log.info(f"created payment wallet {new_wallet} tx_hash={tx_hash.hex()}")
        return InfernetWallet(new_wallet, self._rpc)

### `is_valid_wallet(wallet)` `async`

Check if a given wallet is a valid payment wallet. Reads from the `isValidWallet` function of the `WalletFactory` contract.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`wallet` | `ChecksumAddress` | Address of the wallet. | required

Returns:

Name | Type | Description
--- | --- | ---
`bool` | `bool` | True if the wallet is a valid payment wallet, False otherwise.

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

    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    async def is_valid_wallet(self, wallet: ChecksumAddress) -> bool:
        """
        Check if a given wallet is a valid payment wallet. Reads from the `isValidWallet`
        function of the `WalletFactory` contract.

        Args:
            wallet (ChecksumAddress): Address of the wallet.

        Returns:
            bool: True if the wallet is a valid payment wallet, False otherwise.
        """
        return cast(bool, await self._contract.functions.isValidWallet(wallet).call())