Skip to content

Commit

Permalink
Merge pull request #12 from oceanprotocol/issue1-use-block-timestamp-…
Browse files Browse the repository at this point in the history
…instead-of-block-number

Use block timestamp instead of block number
  • Loading branch information
trizin authored Aug 3, 2023
2 parents d139d02 + 6f78950 commit 18333df
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 64 deletions.
20 changes: 6 additions & 14 deletions pdr_backend/dfbuyer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"SUBGRAPH_URL", None
), "You must set SUBGRAPH_URL environment variable"

avergage_time_between_blocks = 0
last_block_time = 0
WEEK = 7 * 86400

Expand All @@ -38,7 +37,7 @@ def numbers_with_sum(n, k):
predictoor_contracts = []


def process_block(block, avergage_time_between_blocks):
def process_block(block):
global topics
""" Process each contract and see if we need to submit """
if not topics:
Expand All @@ -63,11 +62,9 @@ def process_block(block, avergage_time_between_blocks):
return
estimated_week_end = estimated_week_start + WEEK
print(f"estimated_week_end:{estimated_week_end}")
estimated_blocks_left = (
estimated_week_end - block["timestamp"]
) / avergage_time_between_blocks
print(f"estimated_blocks_left:{estimated_blocks_left}")
consume_target = random.uniform(0, consume_left / estimated_blocks_left * 100)
estimated_time_left = estimated_week_end - estimated_week_start
print(f"estimated_time_left:{estimated_time_left}")
consume_target = random.uniform(0, consume_left / estimated_time_left * 100)
print(f"consume_target:{consume_target}")
# do random allocation
buy_percentage_per_topic = numbers_with_sum(len(topics), 100)
Expand All @@ -87,16 +84,11 @@ def process_block(block, avergage_time_between_blocks):


def log_loop(blockno):
global avergage_time_between_blocks, last_block_time
global last_block_time
block = web3_config.w3.eth.get_block(blockno, full_transactions=False)
if block:
if last_block_time > 0:
avergage_time_between_blocks = (
avergage_time_between_blocks + (block["timestamp"] - last_block_time)
) / 2
last_block_time = block["timestamp"]
if avergage_time_between_blocks > 0:
process_block(block, avergage_time_between_blocks)
process_block(block)


def main():
Expand Down
43 changes: 16 additions & 27 deletions pdr_backend/predictoor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pdr_backend.utils.contract import PredictorContract, Web3Config


avergage_time_between_blocks = 0
last_block_time = 0
topics = []

Expand All @@ -24,7 +23,7 @@
owner = web3_config.owner


def process_block(block, avergage_time_between_blocks):
def process_block(block):
global topics
""" Process each contract and if needed, get a prediction, submit it and claim revenue for past epoch """
if not topics:
Expand All @@ -40,44 +39,38 @@ def process_block(block, avergage_time_between_blocks):
topic = topics[address]
predictor_contract = PredictorContract(web3_config, address)
epoch = predictor_contract.get_current_epoch()
blocks_per_epoch = predictor_contract.get_blocksPerEpoch()
blocks_till_epoch_end = (
epoch * blocks_per_epoch + blocks_per_epoch - block["number"]
seconds_per_epoch = predictor_contract.get_secondsPerEpoch()
seconds_till_epoch_end = (
epoch * seconds_per_epoch + seconds_per_epoch - block["number"]
)
print(
f"\t{topic['name']} (at address {topic['address']} is at epoch {epoch}, blocks_per_epoch: {blocks_per_epoch}, blocks_till_epoch_end: {blocks_till_epoch_end}"
f"\t{topic['name']} (at address {topic['address']} is at epoch {epoch}, seconds_per_epoch: {seconds_per_epoch}, seconds_till_epoch_end: {seconds_till_epoch_end}"
)
if epoch > topic["last_submited_epoch"] and blocks_till_epoch_end <= int(
os.getenv("BLOCKS_TILL_EPOCH_END", 5)
if epoch > topic["last_submited_epoch"] and seconds_till_epoch_end <= int(
os.getenv("SECONDS_TILL_EPOCH_END", 5)
):
"""Try to estimate timestamp of prediction"""
soonest_block = (epoch + 2) * blocks_per_epoch
now = datetime.now(timezone.utc).timestamp()
estimated_time = (
now + (soonest_block - block["number"]) * avergage_time_between_blocks
)
target_time = (epoch + 2) * seconds_per_epoch

""" Let's fetch the prediction """
(predicted_value, predicted_confidence) = predict_function(
topic, estimated_time
topic, target_time
)
if predicted_value is not None and predicted_confidence > 0:
"""We have a prediction, let's submit it"""
stake_amount = os.getenv("STAKE_AMOUNT", 1) * predicted_confidence / 100
stake_amount = os.getenv("STAKE_AMOUNT", 1) * predicted_confidence / 100 # TODO have a customizable function to handle this
print(
f"Contract:{predictor_contract.contract_address} - Submiting prediction for slot:{soonest_block}"
f"Contract:{predictor_contract.contract_address} - Submiting prediction for slot:{target_time}"
)
predictor_contract.submit_prediction(
predicted_value, stake_amount, soonest_block, False
predicted_value, stake_amount, target_time, False
)
else:
print(
f"We do not submit, prediction function returned ({predicted_value}, {predicted_confidence})"
)
# let's get the payout for previous epoch. We don't care if it fails...
trueValSubmitTimeoutBlock = (
predictor_contract.get_trueValSubmitTimeoutBlock()
)
slot = epoch * blocks_per_epoch - trueValSubmitTimeoutBlock - 1
slot = epoch * seconds_per_epoch - seconds_per_epoch
print(
f"Contract:{predictor_contract.contract_address} - Claiming revenue for slot:{slot}"
)
Expand All @@ -87,15 +80,11 @@ def process_block(block, avergage_time_between_blocks):


def log_loop(blockno):
global avergage_time_between_blocks, last_block_time
global last_block_time
block = web3_config.w3.eth.get_block(blockno, full_transactions=False)
if block:
if last_block_time > 0:
avergage_time_between_blocks = (
avergage_time_between_blocks + (block["timestamp"] - last_block_time)
) / 2
last_block_time = block["timestamp"]
process_block(block, avergage_time_between_blocks)
process_block(block)


def main():
Expand Down
10 changes: 5 additions & 5 deletions pdr_backend/trader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ def process_block(block):
topic = topics[address]
predictor_contract = PredictorContract(web3_config, address)
epoch = predictor_contract.get_current_epoch()
blocks_per_epoch = predictor_contract.get_blocksPerEpoch()
blocks_till_epoch_end = (
epoch * blocks_per_epoch + blocks_per_epoch - block["number"]
seconds_per_epoch = predictor_contract.get_secondsPerEpoch()
seconds_till_epoch_end = (
epoch * seconds_per_epoch + seconds_per_epoch - block["timestamp"]
)
print(
f"\t{topic['name']} (at address {topic['address']} is at epoch {epoch}, blocks_per_epoch: {blocks_per_epoch}, blocks_till_epoch_end: {blocks_till_epoch_end}"
f"\t{topic['name']} (at address {topic['address']} is at epoch {epoch}, seconds_per_epoch: {seconds_per_epoch}, seconds_till_epoch_end: {seconds_till_epoch_end}"
)
if epoch > topic["last_submited_epoch"] and epoch > 0:
topic["last_submited_epoch"] = epoch
print(f"Read new prediction")
""" Let's get the prediction and trade it """
prediction = predictor_contract.get_agg_predval(block["number"])
prediction = predictor_contract.get_agg_predval(epoch * seconds_per_epoch)
print(f"Got {prediction}.")
if prediction is not None:
trade(topic, prediction)
Expand Down
30 changes: 14 additions & 16 deletions pdr_backend/trueval/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


class NewTrueVal(Thread):
def __init__(self, topic, predictor_contract, current_block_num, epoch):
def __init__(self, topic, predictor_contract, current_ts, epoch):
# set a default value
self.values = {
"last_submited_epoch": epoch,
Expand All @@ -29,23 +29,21 @@ def __init__(self, topic, predictor_contract, current_block_num, epoch):
self.topic = topic
self.epoch = epoch
self.predictor_contract = predictor_contract
self.current_block_num = current_block_num
self.current_ts = current_ts

def run(self):
"""Get timestamp of previous epoch-2 , get the price"""
""" Get timestamp of previous epoch-1, get the price """
""" Compare and submit trueval """
blocks_per_epoch = self.predictor_contract.get_blocksPerEpoch()
initial_block = self.predictor_contract.get_block(
(self.epoch - 2) * blocks_per_epoch
)
end_block = self.predictor_contract.get_block(
(self.epoch - 1) * blocks_per_epoch
)
slot = (self.epoch - 1) * blocks_per_epoch
seconds_per_epoch = self.predictor_contract.get_secondsPerEpoch()
initial_ts = (self.epoch - 2) * seconds_per_epoch

end_ts = (self.epoch - 1) * seconds_per_epoch

slot = (self.epoch - 1) * seconds_per_epoch

(true_val, float_value, cancel_round) = get_true_val(
self.topic, initial_block["timestamp"], end_block["timestamp"]
self.topic, initial_ts, end_ts
)
print(
f"Contract:{self.predictor_contract.contract_address} - Submiting true_val {true_val} for slot:{slot}"
Expand Down Expand Up @@ -76,16 +74,16 @@ def process_block(block):
topic = topics[address]
predictor_contract = PredictorContract(web3_config, address)
epoch = predictor_contract.get_current_epoch()
blocks_per_epoch = predictor_contract.get_blocksPerEpoch()
blocks_till_epoch_end = (
epoch * blocks_per_epoch + blocks_per_epoch - block["number"]
seconds_per_epoch = predictor_contract.get_secondsPerEpoch()
seconds_till_epoch_end = (
epoch * seconds_per_epoch + seconds_per_epoch - block["timestamp"]
)
print(
f"\t{topic['name']} (at address {topic['address']} is at epoch {epoch}, blocks_per_epoch: {blocks_per_epoch}, blocks_till_epoch_end: {blocks_till_epoch_end}"
f"\t{topic['name']} (at address {topic['address']} is at epoch {epoch}, seconds_per_epoch: {seconds_per_epoch}, seconds_till_epoch_end: {seconds_till_epoch_end}"
)
if epoch > topic["last_submited_epoch"] and epoch > 1:
"""Let's make a prediction & claim rewards"""
thr = NewTrueVal(topic, predictor_contract, block["number"], epoch)
thr = NewTrueVal(topic, predictor_contract, block["timestamp"], epoch)
thr.run()
address = thr.values["contract_address"].lower()
new_epoch = thr.values["last_submited_epoch"]
Expand Down
4 changes: 2 additions & 2 deletions pdr_backend/utils/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ def submit_prediction(
print(e)
return None

def get_trueValSubmitTimeoutEpoch(self):
return self.contract_instance.functions.trueValSubmitTimeoutEpoch().call()
def get_trueValSubmitTimeout(self):
return self.contract_instance.functions.trueValSubmitTimeout().call()

def get_prediction(self, slot):
return self.contract_instance.functions.getPrediction(slot).call(
Expand Down

0 comments on commit 18333df

Please sign in to comment.