Skip to content

Commit

Permalink
small fixes & unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Aug 19, 2024
1 parent 358e0bb commit 9a81fd3
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 12 deletions.
4 changes: 3 additions & 1 deletion multiversx_sdk/controllers/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@


class IAccount(Protocol):
address: IAddress
@property
def address(self) -> IAddress:
...

def sign(self, data: bytes) -> bytes:
...
8 changes: 1 addition & 7 deletions multiversx_sdk/controllers/token_management_controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List, Protocol

from multiversx_sdk.controllers.interfaces import IAccount
from multiversx_sdk.controllers.network_provider_wrapper import ProviderWrapper
from multiversx_sdk.converters.transactions_converter import \
TransactionsConverter
Expand Down Expand Up @@ -32,13 +33,6 @@ def get_network_config(self) -> INetworkConfig:
...


class IAccount(Protocol):
address: IAddress

def sign(self, data: bytes) -> bytes:
...


class TokenManagementController:
def __init__(self, chain_id: str, network_provider: INetworkProvider) -> None:
self.factory = TokenManagementTransactionsFactory(TransactionsFactoryConfig(chain_id))
Expand Down
2 changes: 1 addition & 1 deletion multiversx_sdk/controllers/transfers_controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional, Sequence

from multiversx_sdk.controllers.smart_contract_controller import IAccount
from multiversx_sdk.controllers.interfaces import IAccount
from multiversx_sdk.core.interfaces import IAddress
from multiversx_sdk.core.tokens import TokenTransfer
from multiversx_sdk.core.transaction import Transaction
Expand Down
19 changes: 16 additions & 3 deletions multiversx_sdk/facades/entrypoints.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

from typing import Any, Dict, Optional, Sequence, Tuple, Union
from typing import Any, Dict, List, Optional, Protocol, Sequence, Tuple, Union

from multiversx_sdk.controllers.account_controller import AccountController
from multiversx_sdk.controllers.delegation_controller import \
Expand Down Expand Up @@ -29,6 +28,20 @@
from multiversx_sdk.wallet.user_verifer import UserVerifier


class IAbi(Protocol):
def encode_endpoint_input_parameters(self, endpoint_name: str, values: List[Any]) -> List[bytes]:
...

def encode_constructor_input_parameters(self, values: List[Any]) -> List[bytes]:
...

def encode_upgrade_constructor_input_parameters(self, values: List[Any]) -> List[bytes]:
...

def decode_endpoint_output_parameters(self, endpoint_name: str, encoded_values: List[bytes]) -> List[Any]:
...


class NetworkEntrypoint:
def __init__(self,
network_provider_url: str,
Expand Down Expand Up @@ -109,7 +122,7 @@ def create_account_controller(self) -> AccountController:
def create_relayed_controller(self) -> RelayedController:
return RelayedController(self.chain_id)

def create_smart_contract_controller(self, abi: Any) -> SmartContractController:
def create_smart_contract_controller(self, abi: Optional[IAbi] = None) -> SmartContractController:
return SmartContractController(self.chain_id, self.network_provider, abi)

def create_token_management_controller(self) -> TokenManagementController:
Expand Down
112 changes: 112 additions & 0 deletions multiversx_sdk/facades/entrypoints_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import time
from pathlib import Path

import pytest

from multiversx_sdk.abi.abi import Abi
from multiversx_sdk.core.address import Address
from multiversx_sdk.facades.account import Account
from multiversx_sdk.facades.entrypoints import DevnetEntrypoint

testutils = Path(__file__).parent.parent / "testutils"


class TestEntrypoint:
entrypoint = DevnetEntrypoint()
alice_pem = testutils / "testwallets" / "alice.pem"

def test_native_transfer(self):
controller = self.entrypoint.create_transfers_controller()
sender = Account.new_from_pem(self.alice_pem)
sender.nonce = 77777

transaction = controller.create_transaction_for_transfer(
sender=sender,
nonce=sender.get_nonce_then_increment(),
receiver=sender.address,
native_transfer_amount=0,
data="hello".encode()
)

assert transaction.signature.hex() == "69bc7d1777edd0a901e6cf94830475716205c5efdf2fd44d4be31badead59fc8418b34f0aa3b2c80ba14aed5edd30031757d826af58a1abb690a0bee89ba9309"

@pytest.mark.networkInteraction
def test_contract_flow(self):
abi = Abi.load(testutils / "testdata" / "adder.abi.json")
sender = Account.new_from_pem(self.alice_pem)
sender.nonce = self.entrypoint.recall_account_nonce(sender.address)

controller = self.entrypoint.create_smart_contract_controller(abi)
bytecode = (testutils / "testdata" / "adder.wasm").read_bytes()
transaction = controller.create_transaction_for_deploy(
sender=sender,
nonce=sender.get_nonce_then_increment(),
bytecode=bytecode,
gas_limit=10_000_000,
arguments=[0]
)

tx_hash = self.entrypoint.send_transaction(transaction)
outcome = controller.await_completed_deploy(tx_hash)

assert len(outcome.contracts) == 1

contract_address = Address.new_from_bech32(outcome.contracts[0].address)

transaction = controller.create_transaction_for_execute(
sender=sender,
nonce=sender.get_nonce_then_increment(),
contract=contract_address,
gas_limit=10_000_000,
function="add",
arguments=[7]
)

tx_hash = self.entrypoint.send_transaction(transaction)

retries = 10
while retries:
time.sleep(0.5)
try:
self.entrypoint.await_completed_transaction(tx_hash)
break
except:
retries -= 1

query_result = controller.query_contract(
contract=contract_address,
function="getSum",
arguments=[]
)

assert len(query_result) == 1
assert query_result[0] == 7

def test_create_relayed_transaction(self):
tranasfer_controller = self.entrypoint.create_transfers_controller()
sender = Account.new_from_pem(self.alice_pem)
sender.nonce = 77777

bob_pem = testutils / "testwallets" / "bob.pem"
relayer = Account.new_from_pem(bob_pem)
relayer.nonce = 7

transaction = tranasfer_controller.create_transaction_for_transfer(
sender=sender,
nonce=sender.get_nonce_then_increment(),
receiver=sender.address,
native_transfer_amount=0,
data="hello".encode(),
)
transaction.relayer = relayer.address.to_bech32()

relayed_controller = self.entrypoint.create_relayed_controller()
relayed_transaction = relayed_controller.create_relayed_v3_transaction(
sender=relayer,
nonce=relayer.get_nonce_then_increment(),
inner_transactions=[transaction]
)

assert len(relayed_transaction.inner_transactions) == 1
assert relayed_transaction.sender == relayed_transaction.inner_transactions[0].relayer
assert relayed_transaction.chain_id == "D"

0 comments on commit 9a81fd3

Please sign in to comment.