From a39a12111e7deb2e42e05e7156fd15a0af0a36ac Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Fri, 1 Nov 2024 14:03:53 +0200 Subject: [PATCH 1/2] use newest sdk and add client name --- multiversx_sdk_cli/cli_accounts.py | 4 ++- multiversx_sdk_cli/cli_contracts.py | 16 ++++----- multiversx_sdk_cli/cli_delegation.py | 4 ++- multiversx_sdk_cli/cli_dns.py | 15 +++++--- multiversx_sdk_cli/cli_shared.py | 19 +++++++--- multiversx_sdk_cli/cli_transactions.py | 20 +++++------ multiversx_sdk_cli/config.py | 6 ++++ multiversx_sdk_cli/custom_network_provider.py | 36 ------------------- multiversx_sdk_cli/interfaces.py | 7 +--- .../tests/test_cli_transactions.py | 17 +++++++++ multiversx_sdk_cli/tests/test_proxy.py | 4 ++- pyproject.toml | 4 +-- requirements.txt | 2 +- 13 files changed, 77 insertions(+), 77 deletions(-) delete mode 100644 multiversx_sdk_cli/custom_network_provider.py diff --git a/multiversx_sdk_cli/cli_accounts.py b/multiversx_sdk_cli/cli_accounts.py index 9ae5933a..f7a5afe5 100644 --- a/multiversx_sdk_cli/cli_accounts.py +++ b/multiversx_sdk_cli/cli_accounts.py @@ -4,6 +4,7 @@ from multiversx_sdk import Address, ProxyNetworkProvider from multiversx_sdk_cli import cli_shared, utils +from multiversx_sdk_cli.config import get_config_for_network_providers logger = logging.getLogger("cli.accounts") @@ -33,7 +34,8 @@ def _add_address_arg(sub: Any): def get_account(args: Any): proxy_url = args.proxy address = args.address - proxy = ProxyNetworkProvider(proxy_url) + config = get_config_for_network_providers() + proxy = ProxyNetworkProvider(url=proxy_url, config=config) account = proxy.get_account(Address.new_from_bech32(address)) if args.balance: diff --git a/multiversx_sdk_cli/cli_contracts.py b/multiversx_sdk_cli/cli_contracts.py index 8f7c14a9..9516bcf4 100644 --- a/multiversx_sdk_cli/cli_contracts.py +++ b/multiversx_sdk_cli/cli_contracts.py @@ -10,6 +10,7 @@ from multiversx_sdk_cli import cli_shared, projects, utils from multiversx_sdk_cli.cli_output import CLIOutputBuilder +from multiversx_sdk_cli.config import get_config_for_network_providers from multiversx_sdk_cli.constants import NUMBER_OF_SHARDS from multiversx_sdk_cli.contract_verification import \ trigger_contract_verification @@ -100,7 +101,7 @@ def setup_parser(args: List[str], subparsers: Any) -> Any: cli_shared.add_tx_args(args, sub, with_receiver=False, with_data=False) _add_function_arg(sub) _add_arguments_arg(sub) - _add_token_transfers_args(sub) + cli_shared.add_token_transfers_args(sub) sub.add_argument("--wait-result", action="store_true", default=False, help="signal to wait for the transaction result - only valid if --send is set") sub.add_argument("--timeout", default=100, help="max num of seconds to wait for result" @@ -243,12 +244,6 @@ def _add_arguments_arg(sub: Any): "E.g. [{ 'to': 'erd1...', 'amount': 10000000000 }]") -def _add_token_transfers_args(sub: Any): - sub.add_argument("--token-transfers", nargs='+', - help="token transfers for transfer & execute, as [token, amount] " - "E.g. --token-transfers NFT-123456-0a 1 ESDT-987654 100000000") - - def _add_metadata_arg(sub: Any): sub.add_argument("--metadata-not-upgradeable", dest="metadata_upgradeable", action="store_false", help="‼ mark the contract as NOT upgradeable (default: upgradeable)") @@ -448,14 +443,15 @@ def query(args: Any): args.chain = "" cli_shared.prepare_chain_id_in_args(args) - config = TransactionsFactoryConfig(args.chain) + factory_config = TransactionsFactoryConfig(args.chain) abi = Abi.load(Path(args.abi)) if args.abi else None - contract = SmartContract(config, abi) + contract = SmartContract(factory_config, abi) arguments, should_prepare_args = _get_contract_arguments(args) contract_address = Address.new_from_bech32(args.contract) - proxy = ProxyNetworkProvider(args.proxy) + network_provider_config = get_config_for_network_providers() + proxy = ProxyNetworkProvider(url=args.proxy, config=network_provider_config) function = args.function result = contract.query_contract( diff --git a/multiversx_sdk_cli/cli_delegation.py b/multiversx_sdk_cli/cli_delegation.py index aafbaa4b..cc6dcaa2 100644 --- a/multiversx_sdk_cli/cli_delegation.py +++ b/multiversx_sdk_cli/cli_delegation.py @@ -3,6 +3,7 @@ from multiversx_sdk import ProxyNetworkProvider, TransactionsFactoryConfig from multiversx_sdk_cli import cli_shared, errors, utils +from multiversx_sdk_cli.config import get_config_for_network_providers from multiversx_sdk_cli.delegation import DelegationOperations @@ -209,7 +210,8 @@ def do_create_delegation_contract(args: Any): def get_contract_address_by_deploy_tx_hash(args: Any): args = utils.as_object(args) - proxy = ProxyNetworkProvider(args.proxy) + config = get_config_for_network_providers() + proxy = ProxyNetworkProvider(url=args.proxy, config=config) transaction = proxy.get_transaction(args.create_tx_hash) transaction_events = transaction.logs.events diff --git a/multiversx_sdk_cli/cli_dns.py b/multiversx_sdk_cli/cli_dns.py index 80e93747..703f29a1 100644 --- a/multiversx_sdk_cli/cli_dns.py +++ b/multiversx_sdk_cli/cli_dns.py @@ -4,6 +4,7 @@ from prettytable import PrettyTable from multiversx_sdk_cli import cli_shared +from multiversx_sdk_cli.config import get_config_for_network_providers from multiversx_sdk_cli.constants import ADDRESS_ZERO_BECH32 from multiversx_sdk_cli.dns import (compute_dns_address_for_shard_id, dns_address_for_name, name_hash, register, @@ -79,14 +80,17 @@ def _ensure_proxy_is_provided(args: Any): def dns_resolve(args: Any): _ensure_proxy_is_provided(args) - addr = resolve(args.name, ProxyNetworkProvider(args.proxy)) + config = get_config_for_network_providers() + addr = resolve(args.name, ProxyNetworkProvider(url=args.proxy, config=config)) if addr.to_hex() != Address.new_from_bech32(ADDRESS_ZERO_BECH32).to_hex(): print(addr.to_bech32()) def dns_validate_name(args: Any): _ensure_proxy_is_provided(args) - validate_name(args.name, args.shard_id, ProxyNetworkProvider(args.proxy)) + + config = get_config_for_network_providers() + validate_name(args.name, args.shard_id, ProxyNetworkProvider(url=args.proxy, config=config)) def get_name_hash(args: Any): @@ -107,13 +111,16 @@ def get_dns_address_for_name_hex(args: Any): def get_registration_cost(args: Any): _ensure_proxy_is_provided(args) - print(registration_cost(args.shard_id, ProxyNetworkProvider(args.proxy))) + + config = get_config_for_network_providers() + print(registration_cost(args.shard_id, ProxyNetworkProvider(url=args.proxy, config=config))) def get_version(args: Any): _ensure_proxy_is_provided(args) - proxy = ProxyNetworkProvider(args.proxy) + config = get_config_for_network_providers() + proxy = ProxyNetworkProvider(url=args.proxy, config=config) if args.all: t = PrettyTable(['Shard ID', 'Contract address (bech32)', 'Contract address (hex)', 'Version']) for shard_id in range(0, 256): diff --git a/multiversx_sdk_cli/cli_shared.py b/multiversx_sdk_cli/cli_shared.py index 1236914d..d239de14 100644 --- a/multiversx_sdk_cli/cli_shared.py +++ b/multiversx_sdk_cli/cli_shared.py @@ -14,7 +14,6 @@ load_password) from multiversx_sdk_cli.constants import (DEFAULT_TX_VERSION, TRANSACTION_OPTIONS_TX_GUARDED) -from multiversx_sdk_cli.custom_network_provider import CustomNetworkProvider from multiversx_sdk_cli.errors import ArgumentsNotProvidedError from multiversx_sdk_cli.interfaces import ITransaction from multiversx_sdk_cli.ledger.ledger_functions import do_get_ledger_address @@ -141,6 +140,12 @@ def add_omit_fields_arg(sub: Any): sub.add_argument("--omit-fields", default="[]", type=str, required=False, help="omit fields in the output payload (default: %(default)s)") +def add_token_transfers_args(sub: Any): + sub.add_argument("--token-transfers", nargs='+', + help="token transfers for transfer & execute, as [token, amount] " + "E.g. --token-transfers NFT-123456-0a 1 ESDT-987654 100000000") + + def parse_omit_fields_arg(args: Any) -> List[str]: literal = args.omit_fields parsed = ast.literal_eval(literal) @@ -182,7 +187,8 @@ def prepare_nonce_in_args(args: Any): if args.recall_nonce: account = prepare_account(args) - account.sync_nonce(ProxyNetworkProvider(args.proxy)) + network_provider_config = config.get_config_for_network_providers() + account.sync_nonce(ProxyNetworkProvider(url=args.proxy, config=network_provider_config)) args.nonce = account.nonce @@ -191,7 +197,8 @@ def prepare_chain_id_in_args(args: Any): raise ArgumentsNotProvidedError("chain ID cannot be decided: `--chain` or `--proxy` should be provided") if args.chain and args.proxy: - proxy = ProxyNetworkProvider(args.proxy) + network_provider_config = config.get_config_for_network_providers() + proxy = ProxyNetworkProvider(url=args.proxy, config=network_provider_config) fetched_chain_id = proxy.get_network_config().chain_id if args.chain != fetched_chain_id: @@ -204,7 +211,8 @@ def prepare_chain_id_in_args(args: Any): if args.chain: return elif args.proxy: - proxy = ProxyNetworkProvider(args.proxy) + network_provider_config = config.get_config_for_network_providers() + proxy = ProxyNetworkProvider(url=args.proxy, config=network_provider_config) args.chain = proxy.get_network_config().chain_id @@ -253,7 +261,8 @@ def check_options_for_guarded_tx(options: int): def send_or_simulate(tx: ITransaction, args: Any, dump_output: bool = True) -> CLIOutputBuilder: - proxy = CustomNetworkProvider(args.proxy) + network_provider_config = config.get_config_for_network_providers() + proxy = ProxyNetworkProvider(url=args.proxy, config=network_provider_config) is_set_wait_result = hasattr(args, "wait_result") and args.wait_result is_set_send = hasattr(args, "send") and args.send diff --git a/multiversx_sdk_cli/cli_transactions.py b/multiversx_sdk_cli/cli_transactions.py index a701961b..d8a8ae1d 100644 --- a/multiversx_sdk_cli/cli_transactions.py +++ b/multiversx_sdk_cli/cli_transactions.py @@ -2,10 +2,12 @@ from pathlib import Path from typing import Any, List +from multiversx_sdk import ProxyNetworkProvider + from multiversx_sdk_cli import cli_shared, utils from multiversx_sdk_cli.cli_output import CLIOutputBuilder +from multiversx_sdk_cli.config import get_config_for_network_providers from multiversx_sdk_cli.cosign_transaction import cosign_transaction -from multiversx_sdk_cli.custom_network_provider import CustomNetworkProvider from multiversx_sdk_cli.errors import NoWalletProvided from multiversx_sdk_cli.transactions import (compute_relayed_v1_data, do_prepare_transaction, @@ -20,7 +22,7 @@ def setup_parser(args: List[str], subparsers: Any) -> Any: sub = cli_shared.add_command_subparser(subparsers, "tx", "new", f"Create a new transaction.{CLIOutputBuilder.describe()}") _add_common_arguments(args, sub) - _add_token_transfers_args(sub) + cli_shared.add_token_transfers_args(sub) cli_shared.add_outfile_arg(sub, what="signed transaction, hash") cli_shared.add_broadcast_args(sub, relay=True) cli_shared.add_proxy_arg(sub) @@ -65,12 +67,6 @@ def _add_common_arguments(args: List[str], sub: Any): sub.add_argument("--data-file", type=str, default=None, help="a file containing transaction data") -def _add_token_transfers_args(sub: Any): - sub.add_argument("--token-transfers", nargs='+', - help="token transfers for transfer & execute, as [token, amount] " - "E.g. --token-transfers NFT-123456-0a 1 ESDT-987654 100000000") - - def create_transaction(args: Any): args = utils.as_object(args) @@ -97,7 +93,9 @@ def send_transaction(args: Any): tx = load_transaction_from_file(args.infile) output = CLIOutputBuilder() - proxy = CustomNetworkProvider(args.proxy) + + config = get_config_for_network_providers() + proxy = ProxyNetworkProvider(url=args.proxy, config=config) try: tx_hash = proxy.send_transaction(tx) @@ -110,7 +108,9 @@ def send_transaction(args: Any): def get_transaction(args: Any): args = utils.as_object(args) omit_fields = cli_shared.parse_omit_fields_arg(args) - proxy = CustomNetworkProvider(args.proxy) + + config = get_config_for_network_providers() + proxy = ProxyNetworkProvider(url=args.proxy, config=config) transaction = proxy.get_transaction(args.hash, True) output = CLIOutputBuilder().set_transaction_on_network(transaction, omit_fields).build() diff --git a/multiversx_sdk_cli/config.py b/multiversx_sdk_cli/config.py index 3b54049f..db067f65 100644 --- a/multiversx_sdk_cli/config.py +++ b/multiversx_sdk_cli/config.py @@ -2,6 +2,8 @@ from pathlib import Path from typing import Any, Dict, List +from multiversx_sdk import NetworkProviderConfig + from multiversx_sdk_cli import errors, utils SDK_PATH = Path("~/multiversx-sdk").expanduser().resolve() @@ -242,3 +244,7 @@ def get_dependency_directory(key: str, tag: str) -> Path: def get_dependency_parent_directory(key: str) -> Path: return SDK_PATH / key + + +def get_config_for_network_providers() -> NetworkProviderConfig: + return NetworkProviderConfig(client_name="mxpy") diff --git a/multiversx_sdk_cli/custom_network_provider.py b/multiversx_sdk_cli/custom_network_provider.py deleted file mode 100644 index e11c9870..00000000 --- a/multiversx_sdk_cli/custom_network_provider.py +++ /dev/null @@ -1,36 +0,0 @@ -from typing import Any, Dict, Optional, Protocol - -from multiversx_sdk import GenericError, ProxyNetworkProvider - -from multiversx_sdk_cli.errors import ProxyError -from multiversx_sdk_cli.interfaces import ISimulateResponse, ITransaction - - -class ITransactionOnNetwork(Protocol): - hash: str - is_completed: Optional[bool] - - def to_dictionary(self) -> Dict[str, Any]: - ... - - -class CustomNetworkProvider: - def __init__(self, url: str) -> None: - self._provider = ProxyNetworkProvider(url) - - def send_transaction(self, transaction: ITransaction) -> str: - try: - hash = self._provider.send_transaction(transaction) - return hash - except GenericError as ge: - url = ge.url - message = ge.data.get("error", "") - data = ge.data.get("data", "") - code = ge.data.get("code", "") - raise ProxyError(message, url, data, code) - - def get_transaction(self, tx_hash: str, with_process_status: Optional[bool] = False) -> ITransactionOnNetwork: - return self._provider.get_transaction(tx_hash, with_process_status) - - def simulate_transaction(self, transaction: ITransaction) -> ISimulateResponse: - return self._provider.simulate_transaction(transaction) diff --git a/multiversx_sdk_cli/interfaces.py b/multiversx_sdk_cli/interfaces.py index e0db9a2f..e59c3c8f 100644 --- a/multiversx_sdk_cli/interfaces.py +++ b/multiversx_sdk_cli/interfaces.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, Protocol, Sequence +from typing import Any, Dict, Protocol class IAddress(Protocol): @@ -25,11 +25,6 @@ class ITransaction(Protocol): guardian: str signature: bytes guardian_signature: bytes - relayer: str - - @property - def inner_transactions(self) -> Sequence["ITransaction"]: - ... class IAccount(Protocol): diff --git a/multiversx_sdk_cli/tests/test_cli_transactions.py b/multiversx_sdk_cli/tests/test_cli_transactions.py index 1ac07470..dea29655 100644 --- a/multiversx_sdk_cli/tests/test_cli_transactions.py +++ b/multiversx_sdk_cli/tests/test_cli_transactions.py @@ -88,5 +88,22 @@ def test_create_multi_transfer_transaction(capsys: Any): assert signature == "575b029d52ff5ffbfb7bab2f04052de88a6f7d022a6ad368459b8af9acaed3717d3f95db09f460649a8f405800838bc2c432496bd03c9039ea166bd32b84660e" +def test_create_multi_transfer_transaction_with_single_egld_transfer(capsys: Any): + return_code = main([ + "tx", "new", + "--pem", str(testdata_path / "alice.pem"), + "--receiver", "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx", + "--nonce", "7", + "--gas-limit", "1300000", + "--token-transfers", "EGLD-000000", "1000000000000000000", + "--chain", "T", + ]) + assert return_code == 0 + tx = _read_stdout(capsys) + tx_json = json.loads(tx) + data = tx_json["emittedTransactionData"] + assert data == "MultiESDTNFTTransfer@8049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f8@01@45474c442d303030303030@@0de0b6b3a7640000" + + def _read_stdout(capsys: Any) -> str: return capsys.readouterr().out.strip() diff --git a/multiversx_sdk_cli/tests/test_proxy.py b/multiversx_sdk_cli/tests/test_proxy.py index 8cff09a5..8234f7fb 100644 --- a/multiversx_sdk_cli/tests/test_proxy.py +++ b/multiversx_sdk_cli/tests/test_proxy.py @@ -2,6 +2,7 @@ from multiversx_sdk_cli.accounts import Account from multiversx_sdk_cli.cli import main +from multiversx_sdk_cli.config import get_config_for_network_providers def test_get_account(): @@ -20,7 +21,8 @@ def test_get_account(): def test_sync_nonce(): account = Account(address=Address.new_from_bech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th")) - proxy = ProxyNetworkProvider("https://testnet-api.multiversx.com") + config = get_config_for_network_providers() + proxy = ProxyNetworkProvider("https://testnet-api.multiversx.com", config=config) account.sync_nonce(proxy) assert True if account.nonce else False diff --git a/pyproject.toml b/pyproject.toml index 4463bf73..566c5d78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "multiversx-sdk-cli" -version = "9.8.0" +version = "9.8.1" authors = [ { name="MultiversX" }, ] @@ -28,7 +28,7 @@ dependencies = [ "requests-cache", "rich==13.3.4", "argcomplete==3.2.2", - "multiversx-sdk==0.13.0" + "multiversx-sdk==0.16.2" ] [project.scripts] diff --git a/requirements.txt b/requirements.txt index 2048553c..a44a9c25 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,4 @@ requests-cache rich==13.3.4 argcomplete==3.2.2 -multiversx-sdk==0.13.0 +multiversx-sdk==0.16.2 From 8cdb2d7452b56939377df349c54aad4e4f0a7c42 Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Fri, 1 Nov 2024 16:00:00 +0200 Subject: [PATCH 2/2] update to latest sdk-py --- pyproject.toml | 2 +- requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 566c5d78..aa46997b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,7 @@ dependencies = [ "requests-cache", "rich==13.3.4", "argcomplete==3.2.2", - "multiversx-sdk==0.16.2" + "multiversx-sdk==0.16.3" ] [project.scripts] diff --git a/requirements.txt b/requirements.txt index a44a9c25..46320488 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,4 @@ requests-cache rich==13.3.4 argcomplete==3.2.2 -multiversx-sdk==0.16.2 +multiversx-sdk==0.16.3