diff --git a/src/telliot_feeds/dtypes/value_type.py b/src/telliot_feeds/dtypes/value_type.py index 32d1786f..4d372dbe 100644 --- a/src/telliot_feeds/dtypes/value_type.py +++ b/src/telliot_feeds/dtypes/value_type.py @@ -24,7 +24,7 @@ class ValueType(Serializable): """ # ABI Encoding type string - abi_type: str = "uint256" + abi_type: str = "string" #: True if the value should be encoded using packed bytes format. packed: bool = False diff --git a/src/telliot_feeds/queries/string_query.py b/src/telliot_feeds/queries/string_query.py index 62dc5102..8a5e8d71 100644 --- a/src/telliot_feeds/queries/string_query.py +++ b/src/telliot_feeds/queries/string_query.py @@ -4,14 +4,30 @@ # Copyright (c) 2021-, Tellor Development Community # Distributed under the terms of the MIT License. from dataclasses import dataclass +from typing import Any from typing import Optional +from eth_abi import decode +from eth_abi import encode + from telliot_feeds.dtypes.value_type import ValueType -from telliot_feeds.queries.json_query import JsonQuery +from telliot_feeds.queries.abi_query import AbiQuery + + +class StringQueryValueType(ValueType): + """A ValueType for string queries.""" + + def encode(self, value: str) -> Any: + """Encode a string value.""" + return encode([self.abi_type], [value]) # type: ignore + + def decode(self, bytes_val: bytes) -> Any: + """Decode bytes into a string value.""" + return decode([self.abi_type], bytes_val) # type: ignore @dataclass -class StringQuery(JsonQuery): +class StringQuery(AbiQuery): """Static Oracle Query A text query supports a question in the form of an arbitrary @@ -21,7 +37,13 @@ class StringQuery(JsonQuery): #: Static query text text: Optional[str] + def __init__(self, text: Optional[str]): + self.text = text + + #: ABI used for encoding/decoding parameters + abi = [{"name": "text", "type": "string"}] + @property - def value_type(self) -> ValueType: + def value_type(self) -> StringQueryValueType: """Returns a default text response type.""" - return ValueType(abi_type="string", packed=False) + return StringQueryValueType() diff --git a/tests/queries/test_string_query.py b/tests/queries/test_string_query.py new file mode 100644 index 00000000..d90a8d56 --- /dev/null +++ b/tests/queries/test_string_query.py @@ -0,0 +1,19 @@ +from telliot_feeds.queries.string_query import StringQuery + + +def test_string_query_data(): + + q = StringQuery("What is the meaning of life") + + expected_value = "Please refer to: https://en.wikipedia.org/wiki/Meaning_of_life" + expected_value_encoded = "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003e506c6561736520726566657220746f3a2068747470733a2f2f656e2e77696b6970656469612e6f72672f77696b692f4d65616e696e675f6f665f6c6966650000" # noqa: E501 + response_encoded = q.value_type.encode(expected_value) + response_decoded = q.value_type.decode(bytes.fromhex(expected_value_encoded))[0] + + assert response_encoded.hex() == expected_value_encoded + assert response_decoded == expected_value + + expected_query_data = "00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000b537472696e67517565727900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001b5768617420697320746865206d65616e696e67206f66206c6966650000000000" # noqa: E501 + expected_query_id = "5ad9f178a75e92c8beb8e1e6e2956377ad50f2288c9a82ccfdc69a18651deff5" + assert q.query_data.hex() == expected_query_data + assert q.query_id.hex() == expected_query_id