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

Refactor contract interactions #394

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions multiversx_sdk_cli/cli_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def _add_contract_arg(sub: Any):


def _add_function_arg(sub: Any):
sub.add_argument("--function", required=True, help="the function to call")
sub.add_argument("--function", required=True, type=str, help="the function to call")


def _add_arguments_arg(sub: Any):
Expand Down Expand Up @@ -321,7 +321,20 @@ def deploy(args: Any):
address_computer = AddressComputer(NUMBER_OF_SHARDS)
contract_address = address_computer.compute_contract_address(deployer=sender.address, deployment_nonce=args.nonce)

tx = contract.prepare_deploy_transaction(sender, args)
tx = contract.prepare_deploy_transaction(
owner=sender,
bytecode=Path(args.bytecode),
arguments=args.arguments,
upgradeable=args.metadata_upgradeable,
readable=args.metadata_readable,
payable=args.metadata_payable,
payable_by_sc=args.metadata_payable_by_sc,
gas_limit=int(args.gas_limit),
value=int(args.value),
nonce=int(args.nonce),
version=int(args.version),
options=int(args.options),
guardian=args.guardian)
tx = _sign_guarded_tx(args, tx)

logger.info("Contract address: %s", contract_address.to_bech32())
Expand Down Expand Up @@ -356,7 +369,18 @@ def call(args: Any):
contract = SmartContract(config)
contract_address = Address.new_from_bech32(args.contract)

tx = contract.prepare_execute_transaction(sender, args)
tx = contract.prepare_execute_transaction(
caller=sender,
contract=contract_address,
function=args.function,
arguments=args.arguments,
gas_limit=int(args.gas_limit),
value=int(args.value),
transfers=args.token_transfers,
nonce=int(args.nonce),
version=int(args.version),
options=int(args.options),
guardian=args.guardian)
tx = _sign_guarded_tx(args, tx)

_send_or_simulate(tx, contract_address, args)
Expand All @@ -374,7 +398,21 @@ def upgrade(args: Any):
contract = SmartContract(config)
contract_address = Address.new_from_bech32(args.contract)

tx = contract.prepare_upgrade_transaction(sender, args)
tx = contract.prepare_upgrade_transaction(
owner=sender,
contract=contract_address,
bytecode=Path(args.bytecode),
arguments=args.arguments,
upgradeable=args.metadata_upgradeable,
readable=args.metadata_readable,
payable=args.metadata_payable,
payable_by_sc=args.metadata_payable_by_sc,
gas_limit=int(args.gas_limit),
value=int(args.value),
nonce=int(args.nonce),
version=int(args.version),
options=int(args.options),
guardian=args.guardian)
tx = _sign_guarded_tx(args, tx)

_send_or_simulate(tx, contract_address, args)
Expand Down
129 changes: 80 additions & 49 deletions multiversx_sdk_cli/contracts.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import base64
import logging
from pathlib import Path
from typing import Any, List, Optional, Protocol, Sequence
from typing import Any, List, Optional, Protocol, Sequence, Union

from multiversx_sdk_core import (Token, TokenComputer, TokenTransfer,
Transaction, TransactionPayload)
from multiversx_sdk_core.address import Address
from multiversx_sdk_core.transaction_factories import \
SmartContractTransactionsFactory
from multiversx_sdk_network_providers.interface import IAddress, IContractQuery
from multiversx_sdk_network_providers.interface import IContractQuery

from multiversx_sdk_cli import errors
from multiversx_sdk_cli.accounts import Account
from multiversx_sdk_cli.constants import DEFAULT_HRP
from multiversx_sdk_cli.interfaces import IAddress
from multiversx_sdk_cli.utils import Object

logger = logging.getLogger("contracts")
Expand Down Expand Up @@ -75,76 +76,106 @@ class SmartContract:
def __init__(self, config: IConfig):
self._factory = SmartContractTransactionsFactory(config, TokenComputer())

def prepare_deploy_transaction(self, owner: Account, args: Any) -> Transaction:
arguments = args.arguments or []
arguments = prepare_args_for_factory(arguments)
def prepare_deploy_transaction(self,
owner: Account,
bytecode: Path,
arguments: Union[List[str], None],
upgradeable: bool,
readable: bool,
payable: bool,
payable_by_sc: bool,
gas_limit: int,
value: int,
nonce: int,
version: int,
options: int,
guardian: str) -> Transaction:
args = prepare_args_for_factory(arguments) if arguments else []

tx = self._factory.create_transaction_for_deploy(
sender=owner.address,
bytecode=Path(args.bytecode),
gas_limit=int(args.gas_limit),
arguments=arguments,
native_transfer_amount=int(args.value),
is_upgradeable=args.metadata_upgradeable,
is_readable=args.metadata_readable,
is_payable=args.metadata_payable,
is_payable_by_sc=args.metadata_payable_by_sc
bytecode=bytecode,
gas_limit=gas_limit,
arguments=args,
native_transfer_amount=value,
is_upgradeable=upgradeable,
is_readable=readable,
is_payable=payable,
is_payable_by_sc=payable_by_sc
)
tx.nonce = int(args.nonce)
tx.version = int(args.version)
tx.options = int(args.options)
tx.guardian = args.guardian
tx.nonce = nonce
tx.version = version
tx.options = options
tx.guardian = guardian
tx.signature = bytes.fromhex(owner.sign_transaction(tx))

return tx

def prepare_execute_transaction(self, caller: Account, args: Any) -> Transaction:
contract_address = Address.new_from_bech32(args.contract)
arguments = args.arguments or []
arguments = prepare_args_for_factory(arguments)

value = int(args.value)
transfers = args.token_transfers
def prepare_execute_transaction(self,
caller: Account,
contract: Address,
function: str,
arguments: Union[List[str], None],
gas_limit: int,
value: int,
transfers: Union[List[str], None],
nonce: int,
version: int,
options: int,
guardian: str) -> Transaction:
token_transfers = self._prepare_token_transfers(transfers) if transfers else []
args = prepare_args_for_factory(arguments) if arguments else []

tx = self._factory.create_transaction_for_execute(
sender=caller.address,
contract=contract_address,
function=args.function,
gas_limit=int(args.gas_limit),
arguments=arguments,
contract=contract,
function=function,
gas_limit=gas_limit,
arguments=args,
native_transfer_amount=value,
token_transfers=token_transfers
)
tx.nonce = int(args.nonce)
tx.version = int(args.version)
tx.options = int(args.options)
tx.guardian = args.guardian
tx.nonce = nonce
tx.version = version
tx.options = options
tx.guardian = guardian
tx.signature = bytes.fromhex(caller.sign_transaction(tx))

return tx

def prepare_upgrade_transaction(self, owner: Account, args: Any):
contract_address = Address.new_from_bech32(args.contract)
arguments = args.arguments or []
arguments = prepare_args_for_factory(arguments)
def prepare_upgrade_transaction(self,
owner: Account,
contract: IAddress,
bytecode: Path,
arguments: Union[List[str], None],
upgradeable: bool,
readable: bool,
payable: bool,
payable_by_sc: bool,
gas_limit: int,
value: int,
nonce: int,
version: int,
options: int,
guardian: str) -> Transaction:
args = prepare_args_for_factory(arguments) if arguments else []

tx = self._factory.create_transaction_for_upgrade(
sender=owner.address,
contract=contract_address,
bytecode=Path(args.bytecode),
gas_limit=int(args.gas_limit),
arguments=arguments,
native_transfer_amount=int(args.value),
is_upgradeable=args.metadata_upgradeable,
is_readable=args.metadata_readable,
is_payable=args.metadata_payable,
is_payable_by_sc=args.metadata_payable_by_sc
contract=contract,
bytecode=bytecode,
gas_limit=gas_limit,
arguments=args,
native_transfer_amount=value,
is_upgradeable=upgradeable,
is_readable=readable,
is_payable=payable,
is_payable_by_sc=payable_by_sc
)
tx.nonce = int(args.nonce)
tx.version = int(args.version)
tx.options = int(args.options)
tx.guardian = args.guardian
tx.nonce = nonce
tx.version = version
tx.options = options
tx.guardian = guardian
tx.signature = bytes.fromhex(owner.sign_transaction(tx))

return tx
Expand Down
Loading