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

[predictoor] Infinite token approval and cache allowance #48

Merged
merged 6 commits into from
Aug 16, 2023
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
5 changes: 4 additions & 1 deletion pdr_backend/predictoor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 = (
Expand Down
2 changes: 2 additions & 0 deletions pdr_backend/utils/constants.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
18 changes: 10 additions & 8 deletions pdr_backend/utils/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading