Skip to content

Types Module

ValueType dataclass

Bases: Serializable

Value Type

A ValueType specifies the data structure of value included in the TellorX.Oracle.submitValue() used in response to tip request.

The type is specified per eth-abi grammar, i.e.

  • https://eth-abi.readthedocs.io/en/latest/grammar.html
Source code in telliot_feeds/dtypes/value_type.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@dataclass
class ValueType(Serializable):
    """Value Type

    A ValueType specifies the data structure of ``value`` included in
    the ``TellorX.Oracle.submitValue()`` used in response to
    tip request.

    The type is specified per eth-abi grammar, i.e.

    - https://eth-abi.readthedocs.io/en/latest/grammar.html
    """

    # ABI Encoding type string
    abi_type: str = "uint256"

    #: True if the value should be encoded using packed bytes format.
    packed: bool = False

    def encode(self, value: Any) -> bytes:
        """Encode a value using the ABI Type string."""
        if self.packed:
            return encode_single_packed(self.abi_type, value)
        else:
            return encode_single(self.abi_type, value)

    def decode(self, bytes_val: bytes) -> Any:
        """Decode bytes into a value using abi type string."""
        return decode_single(self.abi_type, bytes_val)

encode(value)

Encode a value using the ABI Type string.

Source code in telliot_feeds/dtypes/value_type.py
32
33
34
35
36
37
def encode(self, value: Any) -> bytes:
    """Encode a value using the ABI Type string."""
    if self.packed:
        return encode_single_packed(self.abi_type, value)
    else:
        return encode_single(self.abi_type, value)

decode(bytes_val)

Decode bytes into a value using abi type string.

Source code in telliot_feeds/dtypes/value_type.py
39
40
41
def decode(self, bytes_val: bytes) -> Any:
    """Decode bytes into a value using abi type string."""
    return decode_single(self.abi_type, bytes_val)

UnsignedFloatType dataclass

Bases: ValueType

Unsigned Float Type

This class specifies the a floating point value using an ABI data type. It also provides encoding/decoding to/from floating point values.

Source code in telliot_feeds/dtypes/float_type.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
@dataclass
class UnsignedFloatType(ValueType):
    """Unsigned Float Type

    This class specifies the a floating point value
    using an ABI data type.  It also provides encoding/decoding
    to/from floating point values.

    """

    #: ABI Encoding for Unsigned Float value (default = ufixed256x6)
    abi_type: str = "ufixed256x6"

    @property
    def decimals(self) -> int:
        """Get precision from abi type"""
        mxn = self.abi_type[6:]
        m, n = mxn.split("x")
        return int(n)

    @property
    def nbits(self) -> int:
        """Get number of bits from abi type"""
        mxn = self.abi_type[6:]
        m, n = mxn.split("x")
        return int(m)

    def encode(self, value: float) -> bytes:
        """An encoder for float values

        This encoder converts a float value to the SpotPrice ABI
        data type.
        """

        decimal_value = Decimal(value).quantize(Decimal(10) ** -self.decimals)

        return super().encode(decimal_value)

    def decode(self, bytes_val: bytes) -> Any:
        """A decoder for float values

        This decoder converts from the SpotPrice ABI data type to
        a floating point value.
        """
        nbytes = self.nbits / 8

        if self.packed:
            if len(bytes_val) != nbytes:
                raise ValueError(f"Value must be {nbytes} bytes")

        intval = int.from_bytes(bytes_val, "big", signed=False)

        return intval / 10.0**self.decimals

decimals: int property

Get precision from abi type

nbits: int property

Get number of bits from abi type

encode(value)

An encoder for float values

This encoder converts a float value to the SpotPrice ABI data type.

Source code in telliot_feeds/dtypes/float_type.py
38
39
40
41
42
43
44
45
46
47
def encode(self, value: float) -> bytes:
    """An encoder for float values

    This encoder converts a float value to the SpotPrice ABI
    data type.
    """

    decimal_value = Decimal(value).quantize(Decimal(10) ** -self.decimals)

    return super().encode(decimal_value)

decode(bytes_val)

A decoder for float values

This decoder converts from the SpotPrice ABI data type to a floating point value.

Source code in telliot_feeds/dtypes/float_type.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def decode(self, bytes_val: bytes) -> Any:
    """A decoder for float values

    This decoder converts from the SpotPrice ABI data type to
    a floating point value.
    """
    nbytes = self.nbits / 8

    if self.packed:
        if len(bytes_val) != nbytes:
            raise ValueError(f"Value must be {nbytes} bytes")

    intval = int.from_bytes(bytes_val, "big", signed=False)

    return intval / 10.0**self.decimals