-
Notifications
You must be signed in to change notification settings - Fork 1
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 #121 from fetchoracle/fetchrng-support
FetchRNG implementation with ETH and PLS support
- Loading branch information
1 parent
79197bf
commit d6d3715
Showing
11 changed files
with
361 additions
and
38 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
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,26 @@ | ||
"""Datafeed for pseudorandom number from hashing multiple blockhashes together.""" | ||
from typing import Optional | ||
|
||
import os | ||
import chained_accounts | ||
from telliot_core.model.endpoints import RPCEndpoint | ||
|
||
from telliot_feeds.datafeed import DataFeed | ||
from telliot_feeds.queries.fetch_rng_custom import FetchRNGCustom | ||
from telliot_feeds.sources.custom_blockhash_aggregator import FetchRNGCustomManualSource | ||
|
||
FETCH_RNG_NAME = os.getenv('FETCH_RNG_NAME', "custom") | ||
START_TIME = int(os.getenv('START_TIME', 1653350400)) # 2022-5-24 00:00:00 GMT | ||
|
||
local_source = FetchRNGCustomManualSource() | ||
|
||
fetch_rng_custom_feed = DataFeed(source=local_source, query=FetchRNGCustom(name=FETCH_RNG_NAME, interval=START_TIME)) | ||
|
||
async def assemble_rng_datafeed( | ||
timestamp: int | ||
) -> Optional[DataFeed[float]]: | ||
"""Assembles a FetchRNG custom datafeed for the given start timestamp.""" | ||
local_source.set_timestamp(timestamp) | ||
feed = DataFeed(source=local_source, query=FetchRNGCustom(name=FETCH_RNG_NAME, interval=START_TIME)) | ||
|
||
return feed |
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,64 @@ | ||
import logging | ||
from dataclasses import field, dataclass | ||
from hexbytes import HexBytes | ||
from telliot_feeds.dtypes.value_type import ValueType | ||
from telliot_feeds.queries.abi_query import AbiQuery | ||
|
||
from eth_abi import decode_abi | ||
from eth_abi import encode_abi | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
@dataclass | ||
class FetchRNGCustomReturnType(ValueType): | ||
|
||
abi_type: str = "(bytes32,ufixed256x18)" | ||
|
||
def encode(self, value: tuple) -> bytes: | ||
"""An encoder for Fetch RNG response type | ||
Encodes a tuple of float values. | ||
""" | ||
if len(value) != 2 or not isinstance(value[0], HexBytes) or not isinstance(value[1], int): | ||
raise ValueError("Invalid response type") | ||
|
||
return encode_abi(["bytes32", "uint256"], value) | ||
|
||
def decode(self, bytes_val: bytes) -> tuple: | ||
"""A decoder for for Fetch RNG response type | ||
Decodes a tuple of float values. | ||
""" | ||
return decode_abi(["bytes32", "uint256"], bytes_val) | ||
|
||
@dataclass | ||
class FetchRNGCustom(AbiQuery): | ||
"""Returns a pseudorandom number generated from hashing together blockhashes from multiple chains. | ||
Attributes: | ||
name: | ||
name of the custom rng feed | ||
timestamp: | ||
time at which to take the most recent blockhashes (example: 1647624359) | ||
""" | ||
is_custom_rng: bool = field(default=True, init=False) | ||
is_managed_feed: bool = field(default=True, init=False) | ||
|
||
name: str | ||
interval: int | ||
|
||
#: ABI used for encoding/decoding parameters | ||
abi = [ | ||
{"name": "name", "type": "string"}, | ||
{"name": "interval", "type": "uint256"} | ||
] | ||
|
||
@property | ||
def value_type(self) -> ValueType: | ||
"""Data type returned for a FetchRNG query. | ||
- `bytes32`: 32 bytes hexadecimal value | ||
- `packed`: false | ||
""" | ||
#return ValueType(abi_type="bytes32", packed=False) | ||
return FetchRNGCustomReturnType() |
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.