Skip to content

Commit

Permalink
Merge branch 'feat/next' into integrate-delegation-factory
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Nov 28, 2023
2 parents f55036d + 210082e commit 13a1d27
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
9 changes: 4 additions & 5 deletions multiversx_sdk_cli/cli_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from pathlib import Path
from typing import Any, List

from multiversx_sdk_core import (Address, AddressComputer, TokenComputer,
Transaction)
from multiversx_sdk_core import Address, AddressComputer, Transaction
from multiversx_sdk_core.transaction_factories import TransactionsFactoryConfig
from multiversx_sdk_network_providers.proxy_network_provider import \
ProxyNetworkProvider
Expand Down Expand Up @@ -305,7 +304,7 @@ def deploy(args: Any):

sender = cli_shared.prepare_account(args)
config = TransactionsFactoryConfig(args.chain)
contract = SmartContract(config, TokenComputer())
contract = SmartContract(config)

address_computer = AddressComputer(NUMBER_OF_SHARDS)
contract_address = address_computer.compute_contract_address(deployer=sender.address, deployment_nonce=args.nonce)
Expand Down Expand Up @@ -342,7 +341,7 @@ def call(args: Any):

sender = cli_shared.prepare_account(args)
config = TransactionsFactoryConfig(args.chain)
contract = SmartContract(config, TokenComputer())
contract = SmartContract(config)
contract_address = Address.new_from_bech32(args.contract)

tx = contract.get_execute_transaction(sender, args)
Expand All @@ -360,7 +359,7 @@ def upgrade(args: Any):

sender = cli_shared.prepare_account(args)
config = TransactionsFactoryConfig(args.chain)
contract = SmartContract(config, TokenComputer())
contract = SmartContract(config)
contract_address = Address.new_from_bech32(args.contract)

tx = contract.get_upgrade_transaction(sender, args)
Expand Down
29 changes: 9 additions & 20 deletions multiversx_sdk_cli/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path
from typing import Any, List, Optional, Protocol, Sequence

from multiversx_sdk_core import Transaction, TransactionPayload
from multiversx_sdk_core import TokenComputer, Transaction, TransactionPayload
from multiversx_sdk_core.address import Address
from multiversx_sdk_core.transaction_factories import \
SmartContractTransactionsFactory
Expand Down Expand Up @@ -70,22 +70,9 @@ class IConfig(Protocol):
gas_limit_per_byte: int


class IToken(Protocol):
identifier: str
nonce: int


class ITokenComputer(Protocol):
def is_fungible(self, token: IToken) -> bool:
...

def extract_identifier_from_extended_identifier(self, identifier: str) -> str:
...


class SmartContract:
def __init__(self, config: IConfig, token_computer: ITokenComputer):
self._factory = SmartContractTransactionsFactory(config, token_computer)
def __init__(self, config: IConfig):
self._factory = SmartContractTransactionsFactory(config, TokenComputer())

def get_deploy_transaction(self, owner: Account, args: Any) -> Transaction:
arguments = args.arguments or []
Expand Down Expand Up @@ -164,20 +151,21 @@ def query_contract(
function: str,
arguments: List[Any],
value: int = 0,
caller: Optional[Address] = None
caller: Optional[IAddress] = None
) -> List[Any]:
response_data = query_detailed(contract_address, proxy, function, arguments, value, caller)
return_data = response_data.return_data
return [_interpret_return_data(data) for data in return_data]


def query_detailed(contract_address: IAddress, proxy: INetworkProvider, function: str, arguments: List[Any],
value: int = 0, caller: Optional[Address] = None) -> Any:
value: int = 0, caller: Optional[IAddress] = None) -> Any:
arguments = arguments or []
# Temporary workaround, until we use sdk-core's serializer.
prepared_arguments = [bytes.fromhex(_prepare_argument(arg)) for arg in arguments]
arguments_hex = [_prepare_argument(arg) for arg in arguments]
prepared_arguments_bytes = [bytes.fromhex(arg) for arg in arguments_hex]

query = ContractQuery(contract_address, function, value, prepared_arguments, caller)
query = ContractQuery(contract_address, function, value, prepared_arguments_bytes, caller)

response = proxy.query_contract(query)
# Temporary workaround, until we add "isSuccess" on the response class.
Expand Down Expand Up @@ -252,6 +240,7 @@ def hex_to_bytes(arg: str):
return bytes.fromhex(argument)


# only used for contract queries and stake operations
def _prepare_argument(argument: Any):
as_str = str(argument)
as_hex = _to_hex(as_str)
Expand Down
19 changes: 18 additions & 1 deletion multiversx_sdk_cli/tests/test_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from multiversx_sdk_cli.accounts import Account
from multiversx_sdk_cli.contract_verification import _create_request_signature
from multiversx_sdk_cli.contracts import (_interpret_as_number_if_safely,
_prepare_argument)
_prepare_argument,
prepare_args_for_factory)

logging.basicConfig(level=logging.INFO)

Expand Down Expand Up @@ -66,3 +67,19 @@ def test_interpret_as_number_if_safely():
assert _interpret_as_number_if_safely("") == 0
assert _interpret_as_number_if_safely("0x5") == 5
assert _interpret_as_number_if_safely("FF") == 255


def test_prepare_args_for_factories():
args = [
"0x5", "123", "false", "true",
"str:test-string",
"erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"
]

arguments = prepare_args_for_factory(args)
assert arguments[0] == b"\x05"
assert arguments[1] == 123
assert arguments[2] == False
assert arguments[3] == True
assert arguments[4] == "test-string"
assert arguments[5].to_bech32() == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "multiversx-sdk-cli"
version = "9.0.0b1"
version = "9.0.0"
authors = [
{ name="MultiversX" },
]
Expand Down

0 comments on commit 13a1d27

Please sign in to comment.