-
Notifications
You must be signed in to change notification settings - Fork 37
Refactoring contracts to remove wrapper #543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import logging | ||
| from typing import Any | ||
|
|
||
| from multiversx_sdk import Address | ||
| from multiversx_sdk.abi import ( | ||
| AddressValue, | ||
| BigUIntValue, | ||
| BoolValue, | ||
| BytesValue, | ||
| StringValue, | ||
| ) | ||
|
|
||
| from multiversx_sdk_cli.config_env import get_address_hrp | ||
| from multiversx_sdk_cli.constants import ( | ||
| ADDRESS_PREFIX, | ||
| FALSE_STR_LOWER, | ||
| HEX_PREFIX, | ||
| MAINCHAIN_ADDRESS_HRP, | ||
| STR_PREFIX, | ||
| TRUE_STR_LOWER, | ||
| ) | ||
| from multiversx_sdk_cli.errors import BadUserInput | ||
|
|
||
| logger = logging.getLogger("args_converter") | ||
|
|
||
|
|
||
| def convert_args_to_typed_values(arguments: list[str]) -> list[Any]: | ||
| args: list[Any] = [] | ||
|
|
||
| for arg in arguments: | ||
| if arg.startswith(HEX_PREFIX): | ||
| args.append(BytesValue(_hex_to_bytes(arg))) | ||
| elif arg.isnumeric(): | ||
| args.append(BigUIntValue(int(arg))) | ||
| elif arg.startswith(ADDRESS_PREFIX): | ||
| args.append(AddressValue.new_from_address(Address.new_from_bech32(arg[len(ADDRESS_PREFIX) :]))) | ||
| elif arg.startswith(MAINCHAIN_ADDRESS_HRP): | ||
| # this flow will be removed in the future | ||
| logger.warning( | ||
| "Address argument has no prefix. This flow will be removed in the future. Please provide each address using the `addr:` prefix. (e.g. --arguments addr:erd1...)" | ||
| ) | ||
| args.append(AddressValue.new_from_address(Address.new_from_bech32(arg))) | ||
| elif arg.startswith(get_address_hrp()): | ||
| args.append(AddressValue.new_from_address(Address.new_from_bech32(arg))) | ||
| elif arg.lower() == FALSE_STR_LOWER: | ||
| args.append(BoolValue(False)) | ||
| elif arg.lower() == TRUE_STR_LOWER: | ||
| args.append(BoolValue(True)) | ||
| elif arg.startswith(STR_PREFIX): | ||
| args.append(StringValue(arg[len(STR_PREFIX) :])) | ||
| else: | ||
| raise BadUserInput( | ||
| f"Unknown argument type for argument: `{arg}`. Use `mxpy contract <sub-command> --help` to check all supported arguments" | ||
| ) | ||
|
|
||
| return args | ||
|
|
||
|
|
||
| def _hex_to_bytes(arg: str): | ||
| argument = arg[len(HEX_PREFIX) :] | ||
| argument = argument.upper() | ||
| argument = _ensure_even_length(argument) | ||
| return bytes.fromhex(argument) | ||
|
|
||
|
|
||
| def _ensure_even_length(string: str) -> str: | ||
| if len(string) % 2 == 1: | ||
| return "0" + string | ||
| return string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a default value of empty list could change existing behavior. If previous code expected None when no arguments were provided, this could cause issues. Consider checking if this change is backward compatible.