Skip to content

Commit

Permalink
feat: functions with multiple parameters can be called using the call…
Browse files Browse the repository at this point in the history
… method
  • Loading branch information
Aviksaikat committed Jul 14, 2024
1 parent 65a1145 commit 0d314d4
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 10 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,27 @@ ape_utils call --function-sig "function_signature" --address "contract_address"
### Examples
#### Calling a view function
#### Calling a view function with single parameter
To call a view function with the signature `call_this_view_function(uint256)(string)` on a contract at address `0x80E097a70cacA11EB71B6401FB12D48A1A61Ef54` with an argument `6147190`, you can use:
```bash
# function which takes a single input parameter
ape_utils call --function-sig "call_this_view_function(uint256)(string)" --address "0x80E097a70cacA11EB71B6401FB12D48A1A61Ef54" --args 6147190 --network :sepolia:infura
```
#### Calling a view function with multiple parameter
```bash
# function which takes multiple input parameters
ape_utils call --function-sig 'couple_param_function(uint256,string)(string)' --address '0x894A02d4574318a9da4EEc7884a7D0c095E65507' --args "[6147190,'string']" --network :sepolia
```
```bash
# function with 3 input parameters
ape_utils call --function-sig 'multiple_param_function(uint256,string,address)(string)' --address '0x894A02d4574318a9da4EEc7884a7D0c095E65507' --args "[6147190,'string', '0x894A02d4574318a9da4EEc7884a7D0c095E65507']" --network :sepolia
```
### Use as ape plugin
```bash
Expand Down
53 changes: 53 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Examples

#### Calling a view function with single parameter

To call a view function with the signature `call_this_view_function(uint256)(string)` on a contract at address `0x80E097a70cacA11EB71B6401FB12D48A1A61Ef54` with an argument `6147190`, you can use:

```bash
# function which takes a single input parameter
ape_utils call --function-sig "call_this_view_function(uint256)(string)" --address "0x80E097a70cacA11EB71B6401FB12D48A1A61Ef54" --args 6147190 --network :sepolia:infura
```

#### Calling a view function with multiple parameter

```bash
# function which takes multiple input parameters
ape_utils call --function-sig 'couple_param_function(uint256,string)(string)' --address '0x894A02d4574318a9da4EEc7884a7D0c095E65507' --args "[6147190,'string']" --network :sepolia
```

```bash
# function with 3 input parameters
ape_utils call --function-sig 'multiple_param_function(uint256,string,address)(string)' --address '0x894A02d4574318a9da4EEc7884a7D0c095E65507' --args "[6147190,'string', '0x894A02d4574318a9da4EEc7884a7D0c095E65507']" --network :sepolia
```

### Use as ape plugin

```bash
ape utils --help
ape utils call --function-sig "call_this_view_function(uint256)(string)" --address "0x80E097a70cacA11EB71B6401FB12D48A1A61Ef54" --args 6147190 --network :sepolia:infura
```

#### ABI encode the given function

```sh
ape_utils encode --signature 'call_this_view_function(uint256 arg1, string addr)' 1234 '0xdeadbeef'
```

#### ABI Decode input data

```sh
ape_utils decode --signature 'call_this_view_function(uint256 arg1, string addr)' '0x00000000000000000000000000000000000000000000000000000000000004d20000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000a3078646561646265656600000000000000000000000000000000000000000000'
```

#### Encode the given function with function selector

```sh
ape_utils encode --signature "call_this_view_function(uint256 arg1)" 1234
```

#### Decode the given function with function selector

```sh
ape_utils decode --signature "call_this_view_function(uint256 arg1)" "0x1e4f420d00000000000000000000000000000000000000000000000000000000000004d2"
```
2 changes: 1 addition & 1 deletion src/ape_utils/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "0.0.4"
version = "0.0.5"
22 changes: 17 additions & 5 deletions src/ape_utils/_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import logging
from typing import Any

Expand Down Expand Up @@ -54,6 +55,15 @@
}


class PythonLiteralOption(click.Option):
@classmethod
def type_cast_value(cls, _: Any, value: Any) -> Any:
try:
return ast.literal_eval(value)
except: # noqa: E722
raise click.BadParameter(value) # noqa: B904


@click.group(cls=rclick.RichGroup)
@click.version_option(version=version, prog_name="ape_utils")
def cli() -> None:
Expand All @@ -70,21 +80,23 @@ def cli() -> None:
help="The function signature (e.g., function_name(input param type)(output param type)).",
)
@click.option("--address", required=True, help="The address of the smart contract.")
@click.option("--args", required=True, help="The arguments for the function call.", type=int)
@click.option("--args", required=True, help="The arguments for the function call.", cls=PythonLiteralOption)
@network_option(default="ethereum:local:node", required=True)
def call_view_function_from_cli(function_sig: str, address: str, args: int, provider: Node) -> None:
def call_view_function_from_cli(function_sig: str, address: str, args: str, provider: Node) -> None:
"""
Calls a view function on the blockchain given a function signature and address.
How this function is identifying the network ?
Read here: https://docs.apeworx.io/ape/stable/userguides/clis.html#network-tools. Using ape's native network parsing
Using ape's native network parsing.
"""
try:
# console.print(provider.web3.provider)
# console.print(dir(provider.web3))
output = call_view_function(function_sig, address, args, provider)
parsed_args = list(args)
# console.print(f"{parsed_args=}")
output = call_view_function(function_sig, address, parsed_args, provider)
console.print(f"[blue bold]Output: [green]{output}")
except Exception as e:
console.print(f"Error: [red]{e!s}")
raise e


@click.command(cls=rclick.RichCommand)
Expand Down
11 changes: 8 additions & 3 deletions src/ape_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
console = Console()


def call_view_function(function_sig: str, address: str, args: int, provider: Node) -> Any:
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.
Expand All @@ -27,21 +27,26 @@ def call_view_function(function_sig: str, address: str, args: int, provider: Nod
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 (int): The arguments for the function call.
- 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)
output = Call(address, [function_sig, *args])(_w3=w3)

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

0 comments on commit 0d314d4

Please sign in to comment.