Skip to content
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

Added suport for contract queries using abi file #436

Merged
merged 11 commits into from
Jul 30, 2024
Merged
20 changes: 16 additions & 4 deletions multiversx_sdk_cli/cli_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from multiversx_sdk_cli.constants import NUMBER_OF_SHARDS
from multiversx_sdk_cli.contract_verification import \
trigger_contract_verification
from multiversx_sdk_cli.contracts import SmartContract, query_contract
from multiversx_sdk_cli.contracts import SmartContract
from multiversx_sdk_cli.cosign_transaction import cosign_transaction
from multiversx_sdk_cli.dependency_checker import check_if_rust_is_installed
from multiversx_sdk_cli.docker import is_docker_installed, run_docker
Expand Down Expand Up @@ -132,6 +132,7 @@ def setup_parser(args: List[str], subparsers: Any) -> Any:
sub = cli_shared.add_command_subparser(subparsers, "contract", "query",
"Query a Smart Contract (call a pure function)")
_add_contract_arg(sub)
_add_contract_abi_arg(sub)
cli_shared.add_proxy_arg(sub)
_add_function_arg(sub)
_add_arguments_arg(sub)
Expand Down Expand Up @@ -443,17 +444,28 @@ def upgrade(args: Any):
def query(args: Any):
logger.debug("query")

# workaround so we can use the function bellow
# workaround so we can use the function below to set chainID
args.chain = ""
cli_shared.prepare_chain_id_in_args(args)

config = TransactionsFactoryConfig(args.chain)
abi = Abi.load(Path(args.abi)) if args.abi else None
contract = SmartContract(config, abi)

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

proxy = ProxyNetworkProvider(args.proxy)
function = args.function
arguments: List[Any] = args.arguments or []

result = query_contract(contract_address, proxy, function, arguments)
result = contract.query_contract(
contract_address=contract_address,
proxy=proxy,
function=function,
arguments=arguments,
should_prepare_args=should_prepare_args
)

utils.dump_out_json(result)


Expand Down
18 changes: 15 additions & 3 deletions multiversx_sdk_cli/cli_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
dns_address_for_name, name_hash, register,
registration_cost, resolve, validate_name,
version)
from multiversx_sdk_cli.errors import ArgumentsNotProvidedError


def setup_parser(args: List[str], subparsers: Any) -> Any:
Expand All @@ -32,7 +33,7 @@ def setup_parser(args: List[str], subparsers: Any) -> Any:

sub = cli_shared.add_command_subparser(subparsers, "dns", "validate-name", "Asks one of the DNS contracts to validate a name. Can be useful before registering it.")
_add_name_arg(sub)
sub.add_argument("--shard-id", default=0, help="shard id of the contract to call (default: %(default)s)")
sub.add_argument("--shard-id", type=int, default=0, help="shard id of the contract to call (default: %(default)s)")
cli_shared.add_proxy_arg(sub)
sub.set_defaults(func=dns_validate_name)

Expand All @@ -41,12 +42,12 @@ def setup_parser(args: List[str], subparsers: Any) -> Any:
sub.set_defaults(func=get_name_hash)

sub = cli_shared.add_command_subparser(subparsers, "dns", "registration-cost", "Gets the registration cost from a DNS smart contract, by default the one with shard id 0.")
sub.add_argument("--shard-id", default=0, help="shard id of the contract to call (default: %(default)s)")
sub.add_argument("--shard-id", type=int, default=0, help="shard id of the contract to call (default: %(default)s)")
cli_shared.add_proxy_arg(sub)
sub.set_defaults(func=get_registration_cost)

sub = cli_shared.add_command_subparser(subparsers, "dns", "version", "Asks the contract for its version")
sub.add_argument("--shard-id", default=0, help="shard id of the contract to call (default: %(default)s)")
sub.add_argument("--shard-id", type=int, default=0, help="shard id of the contract to call (default: %(default)s)")
sub.add_argument("--all", action="store_true", default=False, help="prints a list of all DNS contracts and their current versions (default: %(default)s)")
cli_shared.add_proxy_arg(sub)
sub.set_defaults(func=get_version)
Expand All @@ -70,13 +71,21 @@ def _add_name_arg(sub: Any):
sub.add_argument("name", help="the name for which to check")


def _ensure_proxy_is_provided(args: Any):
if not args.proxy:
raise ArgumentsNotProvidedError("'--proxy' argument not provided")


def dns_resolve(args: Any):
_ensure_proxy_is_provided(args)

addr = resolve(args.name, ProxyNetworkProvider(args.proxy))
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))


Expand All @@ -97,10 +106,13 @@ 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)))


def get_version(args: Any):
_ensure_proxy_is_provided(args)

proxy = ProxyNetworkProvider(args.proxy)
if args.all:
t = PrettyTable(['Shard ID', 'Contract address (bech32)', 'Contract address (hex)', 'Version'])
Expand Down
32 changes: 17 additions & 15 deletions multiversx_sdk_cli/cli_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,7 @@ def wallet_new(args: Any):
shard = args.shard

if shard is not None:
if shard not in CURRENT_SHARDS:
raise BadUserInput(f"Wrong shard provided. Choose between {CURRENT_SHARDS}")

is_wallet_generated = False
for _ in range(MAX_ITERATIONS_FOR_GENERATING_WALLET):
mnemonic = Mnemonic.generate()
pubkey = mnemonic.derive_key().generate_public_key()
generated_address_shard = get_shard_of_pubkey(pubkey.buffer, NUMBER_OF_SHARDS)

if shard == generated_address_shard:
is_wallet_generated = True
break

if not is_wallet_generated:
raise WalletGenerationError(f"Couldn't generate wallet in shard {shard}")
mnemonic = _generate_mnemonic_with_shard_constraint(shard)
else:
mnemonic = Mnemonic.generate()

Expand Down Expand Up @@ -169,6 +155,22 @@ def wallet_new(args: Any):
logger.info(f"Wallet ({format}) saved: {outfile}")


def _generate_mnemonic_with_shard_constraint(shard: int) -> Mnemonic:

if shard not in CURRENT_SHARDS:
raise BadUserInput(f"Wrong shard provided. Choose between {CURRENT_SHARDS}")

for _ in range(MAX_ITERATIONS_FOR_GENERATING_WALLET):
mnemonic = Mnemonic.generate()
pubkey = mnemonic.derive_key().generate_public_key()
generated_address_shard = get_shard_of_pubkey(pubkey.buffer, NUMBER_OF_SHARDS)

if shard == generated_address_shard:
return mnemonic

raise WalletGenerationError(f"Couldn't generate wallet in shard {shard}")


def convert_wallet(args: Any):
infile = Path(args.infile).expanduser().resolve() if args.infile else None
outfile = Path(args.outfile).expanduser().resolve() if args.outfile else None
Expand Down
4 changes: 2 additions & 2 deletions multiversx_sdk_cli/contract_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ def _create_request_signature(account: Account, contract_address: Address, reque


def query_status_with_task_id(url: str, task_id: str, interval: int = 10):
logger.info(f"Please wait while we verify your contract. This may take a while.")
logger.info("Please wait while we verify your contract. This may take a while.")
old_status = ""

while True:
_, _, response = _do_get(f"{url}/tasks/{task_id}")
status = response.get("status", "")

if status == "finished":
logger.info(f"Verification finished!")
logger.info("Verification finished!")
dump_out_json(response)
break
elif status != old_status:
Expand Down
Loading
Loading