diff --git a/app/kudos/admin.py b/app/kudos/admin.py index 8aed7fd46d7..ba5c122fe5b 100644 --- a/app/kudos/admin.py +++ b/app/kudos/admin.py @@ -40,21 +40,18 @@ class TokenRequestAdmin(admin.ModelAdmin): readonly_fields = ['preview'] def response_change(self, request, obj): - if "_mint_kudos" in request.POST: - tx_id = obj.mint() - self.message_user(request, f"Mint submitted to chain: tx {tx_id}. Once this tx clears pls 'sync kudos'.") + from kudos.tasks import mint_token_request + try: + mint_token_request.delay(obj.id) + self.message_user(request, f"Mint/sync submitted to chain") + except Exception as e: + self.message_user(request, str(e)) + if "_change_owner" in request.POST: obj.to_address = '0x6239FF1040E412491557a7a02b2CBcC5aE85dc8F' obj.save() self.message_user(request, f"Changed owner to gitcoin") - if "_sync_kudos" in request.POST: - from kudos.management.commands.mint_all_kudos import sync_latest - sync_latest(0) - sync_latest(1) - sync_latest(2) - sync_latest(3) - self.message_user(request, f"Synced latest 3 kudos from open sea. If there is a new kudos on chain it will appear in the marketplace") return super().response_change(request, obj) def preview(self, instance): diff --git a/app/kudos/models.py b/app/kudos/models.py index cc6aa6f99a0..efa9db310f2 100644 --- a/app/kudos/models.py +++ b/app/kudos/models.py @@ -559,6 +559,9 @@ def __str__(self): """Return the string representation of a model.""" return f"Token: {self.token} num_uses_total: {self.num_uses_total}" + def get_absolute_url(self): + return settings.BASE_URL + f"kudos/redeem/{self.secret}" + @property def url(self): return f"/kudos/redeem/{self.secret}" diff --git a/app/kudos/tasks.py b/app/kudos/tasks.py new file mode 100644 index 00000000000..aaf035e6f7b --- /dev/null +++ b/app/kudos/tasks.py @@ -0,0 +1,58 @@ +import time + +from app.redis_service import RedisService +from celery import app +from celery.utils.log import get_task_logger +from dashboard.utils import get_web3 +from hexbytes import HexBytes +from kudos.models import KudosTransfer, TokenRequest + +logger = get_task_logger(__name__) + +redis = RedisService().redis + +# Lock timeout of 2 minutes (just in the case that the application hangs to avoid a redis deadlock) +LOCK_TIMEOUT = 60 * 2 + + +@app.shared_task(bind=True, max_retries=3) +def mint_token_request(self, token_req_id, retry=False): + """ + :param self: + :param token_req_id: + :return: + """ + with redis.lock("tasks:token_req_id:%s" % token_req_id, timeout=LOCK_TIMEOUT): + from kudos.management.commands.mint_all_kudos import sync_latest + from dashboard.utils import has_tx_mined + obj = TokenRequest.objects.get(pk=token_req_id) + tx_id = obj.mint() + if tx_id: + while not has_tx_mined(tx_id, obj.network): + time.sleep(1) + sync_latest(0) + sync_latest(1) + sync_latest(2) + sync_latest(3) + else: + self.retry(30) + + +@app.shared_task(bind=True, max_retries=3) +def redeem_bulk_kudos(self, kt_id, signed_rawTransaction, retry=False): + """ + :param self: + :param kt_id: + :param signed_rawTransaction: + :return: + """ + with redis.lock("tasks:redeem_bulk_kudos:%s" % kt_id, timeout=LOCK_TIMEOUT): + try: + obj = KudosTransfer.objects.get(pk=kt_id) + w3 = get_web3(obj.network) + obj.txid = w3.eth.sendRawTransaction(HexBytes(signed_rawTransaction)).hex() + obj.receive_txid = obj.txid + obj.save() + pass + except Exception as e: + self.retry(30) diff --git a/app/kudos/templates/transaction/receive_bulk.html b/app/kudos/templates/transaction/receive_bulk.html index 9626383e556..5a9e48c2c07 100644 --- a/app/kudos/templates/transaction/receive_bulk.html +++ b/app/kudos/templates/transaction/receive_bulk.html @@ -26,7 +26,13 @@