Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Jul 17, 2024
1 parent 73fa3e4 commit 7057b2d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
23 changes: 13 additions & 10 deletions multiversx_sdk_cli/cli_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def _add_arguments_arg(sub: Any):
help="arguments for the contract transaction, as [number, bech32-address, ascii string, "
"boolean] or hex-encoded. E.g. --arguments 42 0x64 1000 0xabba str:TOK-a1c2ef true erd1[..]")
sub.add_argument("--arguments-file", type=str, help="a json file containing the arguments. ONLY if abi file is provided. "
"E.g. { 'to': 'erd1...', 'amount': 10000000000 }")
"E.g. [{ 'to': 'erd1...', 'amount': 10000000000 }]")


def _add_token_transfers_args(sub: Any):
Expand Down Expand Up @@ -328,13 +328,13 @@ def deploy(args: Any):
abi = Abi.load(Path(args.abi)) if args.abi else None
contract = SmartContract(config, abi)

arguments, args_from_file = _get_contract_arguments(args)
arguments, should_prepare_args = _get_contract_arguments(args)

tx = contract.prepare_deploy_transaction(
owner=sender,
bytecode=Path(args.bytecode),
arguments=arguments,
args_from_file=args_from_file,
should_prepare_args=should_prepare_args,
upgradeable=args.metadata_upgradeable,
readable=args.metadata_readable,
payable=args.metadata_payable,
Expand Down Expand Up @@ -383,15 +383,15 @@ def call(args: Any):
abi = Abi.load(Path(args.abi)) if args.abi else None
contract = SmartContract(config, abi)

arguments, args_from_file = _get_contract_arguments(args)
arguments, should_prepare_args = _get_contract_arguments(args)
contract_address = Address.new_from_bech32(args.contract)

tx = contract.prepare_execute_transaction(
caller=sender,
contract=contract_address,
function=args.function,
arguments=arguments,
args_from_file=args_from_file,
should_prepare_args=should_prepare_args,
gas_limit=int(args.gas_limit),
value=int(args.value),
transfers=args.token_transfers,
Expand All @@ -416,15 +416,15 @@ def upgrade(args: Any):
abi = Abi.load(Path(args.abi)) if args.abi else None
contract = SmartContract(config, abi)

arguments, args_from_file = _get_contract_arguments(args)
arguments, should_prepare_args = _get_contract_arguments(args)
contract_address = Address.new_from_bech32(args.contract)

tx = contract.prepare_upgrade_transaction(
owner=sender,
contract=contract_address,
bytecode=Path(args.bytecode),
arguments=arguments,
args_from_file=args_from_file,
should_prepare_args=should_prepare_args,
upgradeable=args.metadata_upgradeable,
readable=args.metadata_readable,
payable=args.metadata_payable,
Expand Down Expand Up @@ -461,12 +461,15 @@ def _get_contract_arguments(args: Any) -> Tuple[List[Any], bool]:
json_args = json.loads(Path(args.arguments_file).expanduser().read_text()) if args.arguments_file else None

if json_args and args.arguments:
raise Exception("Both '--arguments' and '--arguments-file' provided.")
raise Exception("Provide either '--arguments' or '--arguments-file'.")

if json_args:
return json_args, True
if not args.abi:
raise Exception("Can't use '--arguments-file' without providing the Abi file.")

return json_args, False
else:
return args.arguments, False
return args.arguments, True


def _send_or_simulate(tx: Transaction, contract_address: IAddress, args: Any):
Expand Down
16 changes: 8 additions & 8 deletions multiversx_sdk_cli/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def prepare_deploy_transaction(self,
owner: Account,
bytecode: Path,
arguments: Union[List[Any], None],
args_from_file: bool,
should_prepare_args: bool,
upgradeable: bool,
readable: bool,
payable: bool,
Expand All @@ -94,7 +94,7 @@ def prepare_deploy_transaction(self,
options: int,
guardian: str) -> Transaction:
args = arguments if arguments else []
if not args_from_file:
if should_prepare_args:
args = self.prepare_args_for_factory(args)

tx = self._factory.create_transaction_for_deploy(
Expand All @@ -121,7 +121,7 @@ def prepare_execute_transaction(self,
contract: Address,
function: str,
arguments: Union[List[Any], None],
args_from_file: bool,
should_prepare_args: bool,
gas_limit: int,
value: int,
transfers: Union[List[str], None],
Expand All @@ -132,7 +132,7 @@ def prepare_execute_transaction(self,
token_transfers = self._prepare_token_transfers(transfers) if transfers else []

args = arguments if arguments else []
if not args_from_file:
if should_prepare_args:
args = self.prepare_args_for_factory(args)

tx = self._factory.create_transaction_for_execute(
Expand All @@ -157,7 +157,7 @@ def prepare_upgrade_transaction(self,
contract: IAddress,
bytecode: Path,
arguments: Union[List[str], None],
args_from_file: bool,
should_prepare_args: bool,
upgradeable: bool,
readable: bool,
payable: bool,
Expand All @@ -169,7 +169,7 @@ def prepare_upgrade_transaction(self,
options: int,
guardian: str) -> Transaction:
args = arguments if arguments else []
if not args_from_file:
if should_prepare_args:
args = self.prepare_args_for_factory(args)

tx = self._factory.create_transaction_for_upgrade(
Expand Down Expand Up @@ -279,12 +279,12 @@ def _interpret_as_number_if_safely(as_hex: str) -> Optional[int]:
Makes sure the string can be safely converted to an int (and then back to a string).
See:
- https://stackoverflow.com/questions/73693104/valueerror-exceeds-the-limit-4300-for-integer-string-conversion
- https://stackoverflow.com/questions/73693104/valueerror-exceeds-the-limit-4300-for-integer-string-conversion
- https://github.com/python/cpython/issues/95778
"""
try:
return int(str(int(as_hex or "0", 16)))
except:
except Exception:
return None


Expand Down
6 changes: 3 additions & 3 deletions multiversx_sdk_cli/tests/test_cli_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_contract_deploy():
str(output_file),
]
)
assert Path.is_file(output_file) == True
assert Path.is_file(output_file)


def test_contract_upgrade():
Expand Down Expand Up @@ -136,7 +136,7 @@ def test_contract_upgrade():
str(output_file),
]
)
assert Path.is_file(output_file) == True
assert Path.is_file(output_file)


def test_contract_call():
Expand Down Expand Up @@ -165,7 +165,7 @@ def test_contract_call():
str(output_file),
]
)
assert Path.is_file(output_file) == True
assert Path.is_file(output_file)


def test_contract_transfer_and_execute(capsys: Any):
Expand Down

0 comments on commit 7057b2d

Please sign in to comment.