diff --git a/pdr_backend/predictoor/main.py b/pdr_backend/predictoor/main.py index 18305a028..189892432 100644 --- a/pdr_backend/predictoor/main.py +++ b/pdr_backend/predictoor/main.py @@ -15,6 +15,7 @@ last_block_time = 0 topics: Dict[str, dict] = {} +contract_map: Dict[str, PredictoorContract] = {} rpc_url = env.get_rpc_url_or_exit() subgraph_url = env.get_subgraph_or_exit() @@ -42,7 +43,9 @@ def process_block(block): print(f"Got new block: {block['number']} with {len(topics)} topics") for address in topics: topic = topics[address] - predictoor_contract = PredictoorContract(web3_config, address) + if contract_map.get(address) is None: + contract_map[address] = PredictoorContract(web3_config, address) + predictoor_contract = contract_map[address] epoch = predictoor_contract.get_current_epoch() seconds_per_epoch = predictoor_contract.get_secondsPerEpoch() seconds_till_epoch_end = ( diff --git a/pdr_backend/utils/constants.py b/pdr_backend/utils/constants.py index fcf0068f7..4b0a717e0 100644 --- a/pdr_backend/utils/constants.py +++ b/pdr_backend/utils/constants.py @@ -1,4 +1,6 @@ ZERO_ADDRESS = "0x0000000000000000000000000000000000000000" +MAX_UINT = 2**256 - 1 + SAPPHIRE_TESTNET_RPC = "https://testnet.sapphire.oasis.dev" SAPPHIRE_TESTNET_CHAINID = 23295 SAPPHIRE_MAINNET_RPC = "https://sapphire.oasis.io" diff --git a/pdr_backend/utils/contract.py b/pdr_backend/utils/contract.py index 4aef54e07..2f97be406 100644 --- a/pdr_backend/utils/contract.py +++ b/pdr_backend/utils/contract.py @@ -18,11 +18,11 @@ from web3.middleware import construct_sign_and_send_raw_middleware from web3.logs import DISCARD - from pdr_backend.utils.constants import ( ZERO_ADDRESS, SAPPHIRE_TESTNET_CHAINID, SAPPHIRE_MAINNET_CHAINID, + MAX_UINT, ) keys = KeyAPI(NativeECCBackend) @@ -127,6 +127,7 @@ def __init__(self, config: Web3Config, address: str): ) stake_token = self.get_stake_token() self.token = Token(config, stake_token) + self.last_allowance = 0 def is_valid_subscription(self): return self.contract_instance.functions.isValidSubscription( @@ -364,17 +365,18 @@ def submit_prediction( amount_wei = self.config.w3.to_wei(str(stake_amount), "ether") # Check allowance first, only approve if needed - current_allowance = self.token.allowance( - self.config.owner, self.contract_address - ) - if current_allowance < amount_wei: + if self.last_allowance <= 0: + self.last_allowance = self.token.allowance( + self.config.owner, self.contract_address + ) + if self.last_allowance < amount_wei: try: - self.token.approve(self.contract_address, amount_wei) + self.token.approve(self.contract_address, MAX_UINT) + self.last_allowance = MAX_UINT except Exception as e: print("Error while approving the contract to spend tokens:", e) return None - self.token.approve(self.contract_address, amount_wei) gasPrice = self.config.w3.eth.gas_price try: txhash = None @@ -405,7 +407,7 @@ def submit_prediction( predicted_value, amount_wei, prediction_ts ).transact({"from": self.config.owner, "gasPrice": gasPrice}) txhash = tx.hex() - + self.last_allowance -= amount_wei print(f"Submitted prediction, txhash: {txhash}") if not wait_for_receipt: return txhash