Skip to content

Index

call_view_function(function_sig, address, args, provider)

Calls a view function on the blockchain given a function signature and address.

This function connects to the blockchain using a Web3 HTTP provider, constructs a call to the specified contract address and function signature with the given arguments, and returns the result of the call.

Parameters: - function_sig (str): The function signature, including the input and output types, e.g., "some_func(uint256)(string)". - address (str): The address of the smart contract. - args (list[str]): The arguments for the function call. - provider (SubprocessProvider): The subprocess provider i.e. alchemy, infura, foundry, ganache etc.

Returns: - Any: The result of the function call.

How this function is identifying the network ? Read here: https://docs.apeworx.io/ape/stable/userguides/clis.html#network-tools.

Example:

>>> result = call_view_function("call_this_view_function(uint256)(string)", "0x80E097a70cacA11EB71B6401FB12D48A1A61Ef54", 6147190)
>>> print(result)

Source code in ape_utils/utils.py
def call_view_function(function_sig: str, address: str, args: list[str], provider: Node) -> Any:
    """
    Calls a view function on the blockchain given a function signature and address.

    This function connects to the blockchain using a Web3 HTTP provider, constructs a
    call to the specified contract address and function signature with the given arguments,
    and returns the result of the call.

    Parameters:
    - function_sig (str): The function signature, including the input and output types, e.g., "some_func(uint256)(string)".
    - address (str): The address of the smart contract.
    - args (list[str]): The arguments for the function call.
    - provider (SubprocessProvider): The subprocess provider i.e. alchemy, infura, foundry, ganache etc.

    Returns:
    - Any: The result of the function call.

    How this function is identifying the network ?
    Read here: https://docs.apeworx.io/ape/stable/userguides/clis.html#network-tools.

    Example:
    ```py
    >>> result = call_view_function("call_this_view_function(uint256)(string)", "0x80E097a70cacA11EB71B6401FB12D48A1A61Ef54", 6147190)
    >>> print(result)
    ```
    """  # noqa: E501
    w3 = Web3(Web3.HTTPProvider(provider.uri))

    # get_signature(address, function_sig)

    output = Call(address, [function_sig, *args])(_w3=w3)

    # console.print(f"[blue]Output: [green bold]{output}")
    return output