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

Fix manual string query #752

Merged
merged 4 commits into from
Feb 20, 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
2 changes: 1 addition & 1 deletion src/telliot_feeds/dtypes/value_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 26 additions & 4 deletions src/telliot_feeds/queries/string_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
19 changes: 19 additions & 0 deletions tests/queries/test_string_query.py
Original file line number Diff line number Diff line change
@@ -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
Loading