Skip to content

Commit

Permalink
hotfix that makes kudos celery redemptions actually work reliably
Browse files Browse the repository at this point in the history
  • Loading branch information
owocki committed Apr 1, 2020
1 parent 87a9578 commit 6652c0a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
26 changes: 23 additions & 3 deletions app/kudos/tasks.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand Down Expand Up @@ -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):
Expand All @@ -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)))
5 changes: 3 additions & 2 deletions app/kudos/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 6652c0a

Please sign in to comment.