-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from multiversx/sc-query-controller
Smart Contract queries controller
- Loading branch information
Showing
18 changed files
with
471 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[flake8] | ||
ignore = E501 | ||
ignore = E501, E722 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from multiversx_sdk.core.adapters.query_runner_adapter import \ | ||
QueryRunnerAdapter | ||
|
||
__all__ = ["QueryRunnerAdapter"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from typing import List, Optional, Protocol, Sequence | ||
|
||
from multiversx_sdk.core.address import Address | ||
from multiversx_sdk.core.contract_query import ContractQuery | ||
from multiversx_sdk.core.interfaces import IAddress | ||
from multiversx_sdk.core.smart_contract_query import ( | ||
SmartContractQuery, SmartContractQueryResponse) | ||
|
||
|
||
class IQuery(Protocol): | ||
def get_contract(self) -> IAddress: | ||
... | ||
|
||
def get_function(self) -> str: | ||
... | ||
|
||
def get_encoded_arguments(self) -> Sequence[str]: | ||
... | ||
|
||
def get_caller(self) -> Optional[IAddress]: | ||
... | ||
|
||
def get_value(self) -> int: | ||
... | ||
|
||
|
||
class IQueryResponse(Protocol): | ||
return_data: List[str] | ||
return_code: str | ||
return_message: str | ||
gas_used: int | ||
|
||
def get_return_data_parts(self) -> List[bytes]: | ||
... | ||
|
||
|
||
class INetworkProvider(Protocol): | ||
def query_contract(self, query: IQuery) -> IQueryResponse: | ||
... | ||
|
||
|
||
class QueryRunnerAdapter: | ||
def __init__(self, network_provider: INetworkProvider) -> None: | ||
self.network_provider = network_provider | ||
|
||
def run_query(self, query: SmartContractQuery) -> SmartContractQueryResponse: | ||
adapted_query = ContractQuery( | ||
contract=Address.new_from_bech32(query.contract), | ||
function=query.function, | ||
value=query.value if query.value else 0, | ||
encoded_arguments=[arg.hex() for arg in query.arguments], | ||
caller=Address.new_from_bech32(query.caller) if query.caller else None | ||
) | ||
|
||
adapted_query_response = self.network_provider.query_contract(adapted_query) | ||
|
||
return SmartContractQueryResponse( | ||
function=query.function, | ||
return_code=adapted_query_response.return_code, | ||
return_message=adapted_query_response.return_message, | ||
return_data_parts=adapted_query_response.get_return_data_parts() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
|
||
|
||
from typing import List, Optional, Sequence | ||
|
||
from multiversx_sdk.core.interfaces import IAddress | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from typing import Any, List, Optional, Protocol | ||
|
||
from multiversx_sdk.core.smart_contract_query import ( | ||
SmartContractQuery, SmartContractQueryResponse) | ||
|
||
|
||
class IQueryRunner(Protocol): | ||
def run_query(self, query: SmartContractQuery) -> SmartContractQueryResponse: | ||
... | ||
|
||
|
||
class SmartContractQueriesController: | ||
def __init__(self, query_runner: IQueryRunner) -> None: | ||
self.query_runner = query_runner | ||
|
||
def create_query( | ||
self, | ||
contract: str, | ||
function: str, | ||
arguments: List[bytes], | ||
caller: Optional[str] = None, | ||
value: Optional[int] = None | ||
) -> SmartContractQuery: | ||
return SmartContractQuery( | ||
contract=contract, | ||
function=function, | ||
arguments=arguments, | ||
caller=caller, | ||
value=value | ||
) | ||
|
||
def run_query(self, query: SmartContractQuery) -> SmartContractQueryResponse: | ||
query_response = self.query_runner.run_query(query) | ||
return query_response | ||
|
||
def parse_query_response(self, response: SmartContractQueryResponse) -> List[Any]: | ||
return response.return_data_parts |
103 changes: 103 additions & 0 deletions
103
multiversx_sdk/core/smart_contract_queries_controller_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import base64 | ||
|
||
import pytest | ||
|
||
from multiversx_sdk.core.adapters.query_runner_adapter import \ | ||
QueryRunnerAdapter | ||
from multiversx_sdk.core.smart_contract_queries_controller import \ | ||
SmartContractQueriesController | ||
from multiversx_sdk.core.smart_contract_query import ( | ||
SmartContractQuery, SmartContractQueryResponse) | ||
from multiversx_sdk.network_providers.contract_query_response import \ | ||
ContractQueryResponse | ||
from multiversx_sdk.network_providers.proxy_network_provider import \ | ||
ProxyNetworkProvider | ||
from multiversx_sdk.testutils.mock_network_provider import MockNetworkProvider | ||
|
||
|
||
class TestSmartContractQueriesController: | ||
provider = ProxyNetworkProvider("https://devnet-api.multiversx.com") | ||
query_runner = QueryRunnerAdapter(network_provider=provider) | ||
controller = SmartContractQueriesController(query_runner=query_runner) | ||
|
||
def test_create_query_without_arguments(self): | ||
contract = "erd1qqqqqqqqqqqqqpgqsnwuj85zv7t0wnxfetyqqyjvvg444lpk7uasxv8ktx" | ||
function = "getSum" | ||
|
||
query = self.controller.create_query( | ||
contract=contract, | ||
function=function, | ||
arguments=[] | ||
) | ||
assert query.contract == contract | ||
assert query.function == function | ||
assert query.arguments == [] | ||
assert query.caller is None | ||
assert query.value is None | ||
|
||
def test_create_query_with_arguments(self): | ||
contract = "erd1qqqqqqqqqqqqqpgqsnwuj85zv7t0wnxfetyqqyjvvg444lpk7uasxv8ktx" | ||
function = "getSum" | ||
|
||
query = self.controller.create_query( | ||
contract=contract, | ||
function=function, | ||
arguments=[int.to_bytes(7), "abba".encode()] | ||
) | ||
assert query.contract == contract | ||
assert query.function == function | ||
assert query.arguments == [int.to_bytes(7), "abba".encode()] | ||
assert query.caller is None | ||
assert query.value is None | ||
|
||
def test_run_query_with_mock_provider(self): | ||
network_provider = MockNetworkProvider() | ||
query_runner = QueryRunnerAdapter(network_provider=network_provider) | ||
controller = SmartContractQueriesController(query_runner) | ||
|
||
contract_query_response = ContractQueryResponse() | ||
contract_query_response.return_code = "ok" | ||
contract_query_response.return_data = [base64.b64encode("abba".encode()).decode()] | ||
|
||
network_provider.mock_query_contract_on_function("bar", contract_query_response) | ||
|
||
query = SmartContractQuery( | ||
contract="erd1qqqqqqqqqqqqqpgqvc7gdl0p4s97guh498wgz75k8sav6sjfjlwqh679jy", | ||
function="bar", | ||
arguments=[] | ||
) | ||
|
||
response = controller.run_query(query) | ||
assert response.return_code == "ok" | ||
assert response.return_data_parts == ["abba".encode()] | ||
|
||
def test_parse_query_response(self): | ||
network_provider = MockNetworkProvider() | ||
query_runner = QueryRunnerAdapter(network_provider=network_provider) | ||
controller = SmartContractQueriesController(query_runner) | ||
|
||
response = SmartContractQueryResponse( | ||
function="bar", | ||
return_code="ok", | ||
return_message="ok", | ||
return_data_parts=["abba".encode()] | ||
) | ||
|
||
parsed = controller.parse_query_response(response) | ||
assert parsed == ["abba".encode()] | ||
|
||
@pytest.mark.networkInteraction | ||
def test_run_query_on_network(self): | ||
contract = "erd1qqqqqqqqqqqqqpgqsnwuj85zv7t0wnxfetyqqyjvvg444lpk7uasxv8ktx" | ||
function = "getSum" | ||
|
||
query = self.controller.create_query( | ||
contract=contract, | ||
function=function, | ||
arguments=[] | ||
) | ||
|
||
query_response = self.controller.run_query(query) | ||
assert query_response.return_code == "ok" | ||
assert query_response.return_message == "" | ||
assert query_response.return_data_parts == [b'\x05'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import List, Optional | ||
|
||
|
||
class SmartContractQuery: | ||
def __init__( | ||
self, | ||
contract: str, | ||
function: str, | ||
arguments: List[bytes], | ||
caller: Optional[str] = None, | ||
value: Optional[int] = None | ||
) -> None: | ||
self.contract = contract | ||
self.function = function | ||
self.arguments = arguments | ||
self.caller = caller | ||
self.value = value | ||
|
||
|
||
class SmartContractQueryResponse: | ||
def __init__( | ||
self, | ||
function: str, | ||
return_code: str, | ||
return_message: str, | ||
return_data_parts: List[bytes] | ||
) -> None: | ||
self.function = function | ||
self.return_code = return_code | ||
self.return_message = return_message | ||
self.return_data_parts = return_data_parts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.