Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/UnitapApp/unitap-backend
Browse files Browse the repository at this point in the history
…into develop
  • Loading branch information
Mohamad Bastin committed Sep 18, 2023
2 parents 543393a + 49dc005 commit 5b50a6f
Show file tree
Hide file tree
Showing 9 changed files with 2,291 additions and 557 deletions.
4 changes: 4 additions & 0 deletions brightIDfaucet/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
'update-donation-receipt-status': {
"task": "faucet.tasks.update_donation_receipt_pending_status",
"schedule": 60,
},
"draw-prizetap-raffles": {
"task": "prizetap.tasks.draw_the_expired_raffles",
"schedule": 60
}
}

Expand Down
20 changes: 20 additions & 0 deletions core/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import time
from contextlib import contextmanager
from django.core.cache import cache

@contextmanager
def memcache_lock(lock_id, oid, lock_expire=60):
timeout_at = time.monotonic() + lock_expire
# cache.add fails if the key already exists
status = cache.add(lock_id, oid, lock_expire)
try:
yield status
finally:
# memcache delete is very slow, but we have to use it to take
# advantage of using add() for atomic locking
if time.monotonic() < timeout_at and status:
# don't release the lock if we exceeded the timeout
# to lessen the chance of releasing an expired lock
# owned by someone else
# also don't release the lock if we didn't acquire it
cache.delete(lock_id)
18 changes: 14 additions & 4 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytz
from time import time
from web3 import Web3
from web3.middleware import geth_poa_middleware
from web3.contract.contract import Contract, ContractFunction
from web3.types import Type, TxParams
from django.utils import timezone
Expand Down Expand Up @@ -50,23 +51,30 @@ def get_first_day_of_the_month():


class Web3Utils:
def __init__(self, rpc_url) -> None:
def __init__(self, rpc_url, poa = False) -> None:
self._rpc_url = rpc_url
self._w3 = None
self._account = None
self._contract = None
self._poa = poa

@property
def w3(self) -> Web3:
if self._w3 and self._w3.is_connected():
return self._w3

self._w3 = Web3(Web3.HTTPProvider(self._rpc_url))
if self.poa:
self._w3.middleware_onion.inject(geth_poa_middleware, layer=0)

if self._w3.is_connected():
return self._w3

raise Exception(f"RPC provider is not connected ({self._rpc_url})")

@property
def poa(self):
return self._poa

@property
def account(self):
Expand All @@ -83,7 +91,7 @@ def set_contract(self, address, abi):
self._contract = self.w3.eth.contract(address=address, abi=abi)

def contract_txn(self, func: Type[ContractFunction]):
signed_tx = self.build_contract_call(func)
signed_tx = self.build_contract_txn(func)
txn_hash = self.send_raw_tx(signed_tx)
return txn_hash.hex()

Expand All @@ -92,8 +100,10 @@ def contract_call(self, func: Type[ContractFunction], from_address=None):
return func.call({"from": from_address})
return func.call()

def build_contract_call(self, func: Type[ContractFunction]):
tx_data = func.build_transaction({"from": self.account.address})
def build_contract_txn(self, func: Type[ContractFunction]):
nonce = self.w3.eth.get_transaction_count(self.account.address)
tx_data = func.build_transaction(
{"from": self.account.address, "nonce": nonce})
return self.sign_tx(tx_data)

def sign_tx(self, tx_data: TxParams):
Expand Down
Loading

0 comments on commit 5b50a6f

Please sign in to comment.