From 6652c0a0c7b9da1c71050287cef9d8493136c3ce Mon Sep 17 00:00:00 2001 From: owocki Date: Wed, 1 Apr 2020 15:35:55 -0600 Subject: [PATCH] hotfix that makes kudos celery redemptions actually work reliably --- app/kudos/tasks.py | 26 +++++++++++++++++++++++--- app/kudos/views.py | 5 +++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/app/kudos/tasks.py b/app/kudos/tasks.py index 610bd7c0414..19e36fedd0d 100644 --- a/app/kudos/tasks.py +++ b/app/kudos/tasks.py @@ -1,12 +1,16 @@ import time +from django.conf import settings + 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 +from kudos.utils import kudos_abi from marketing.mails import notify_kudos_minted +from web3 import Web3 logger = get_task_logger(__name__) @@ -48,11 +52,10 @@ def mint_token_request(self, token_req_id, retry=False): @app.shared_task(bind=True, max_retries=10) -def redeem_bulk_kudos(self, kt_id, signed_rawTransaction, retry=False): +def redeem_bulk_kudos(self, kt_id, retry=False): """ :param self: :param kt_id: - :param signed_rawTransaction: :return: """ with redis.lock("tasks:all_kudos_requests", timeout=LOCK_TIMEOUT): @@ -68,11 +71,28 @@ def redeem_bulk_kudos(self, kt_id, signed_rawTransaction, retry=False): obj = KudosTransfer.objects.get(pk=kt_id) w3 = get_web3(obj.network) - obj.txid = w3.eth.sendRawTransaction(HexBytes(signed_rawTransaction)).hex() + token = obj.kudos_token_cloned_from + if token.owner_address.lower() != '0x6239FF1040E412491557a7a02b2CBcC5aE85dc8F'.lower(): + raise Exception("kudos isnt owned by Gitcoin; cowardly refusing to spend Gitcoin's ETH minting it") + kudos_owner_address = settings.KUDOS_OWNER_ACCOUNT + kudos_owner_address = Web3.toChecksumAddress(kudos_owner_address) + kudos_contract_address = Web3.toChecksumAddress(settings.KUDOS_CONTRACT_MAINNET) + contract = w3.eth.contract(Web3.toChecksumAddress(kudos_contract_address), abi=kudos_abi()) + nonce = w3.eth.getTransactionCount(kudos_owner_address) + tx = contract.functions.clone(Web3.toChecksumAddress(obj.receive_address), token.token_id, 1).buildTransaction({ + 'nonce': nonce, + 'gas': 500000, + 'gasPrice': gas_price, + 'value': int(token.price_finney / 1000.0 * 10**18), + }) + private_key = settings.KUDOS_PRIVATE_KEY + signed = w3.eth.account.signTransaction(tx, private_key) + obj.txid = w3.eth.sendRawTransaction(signed.rawTransaction).hex() obj.receive_txid = obj.txid obj.save() while not has_tx_mined(obj.txid, obj.network): time.sleep(1) pass except Exception as e: + print(e) self.retry(countdown=(30 * (self.request.retries + 1))) diff --git a/app/kudos/views.py b/app/kudos/views.py index cf1dc6516e9..47d83eb8bdf 100644 --- a/app/kudos/views.py +++ b/app/kudos/views.py @@ -46,6 +46,7 @@ from dashboard.views import record_user_action from gas.utils import recommend_min_gas_price_to_confirm_in_time from git.utils import get_emails_by_category, get_emails_master, get_github_primary_email +from kudos.tasks import redeem_bulk_kudos from kudos.utils import kudos_abi from marketing.mails import new_kudos_request from ratelimit.decorators import ratelimit @@ -738,6 +739,7 @@ def redeem_bulk_coupon(coupon, profile, address, ip_address, save_addr=False): receive_txid=txid, tx_status='pending', receive_tx_status='pending', + receive_address=address, ) # save to DB @@ -765,8 +767,7 @@ def redeem_bulk_coupon(coupon, profile, address, ip_address, save_addr=False): maybe_market_kudos_to_email(kudos_transfer) if retry_later: - from kudos.tasks import redeem_bulk_kudos - redeem_bulk_kudos.delay(kudos_transfer.id, signed.rawTransaction.hex()) + redeem_bulk_kudos.delay(kudos_transfer.id) return True, None, kudos_transfer