def get_bounty_id_from_web3(issue_url, network, start_bounty_id, direction='up'): issue_url = normalizeURL(issue_url) web3 = get_web3(network) # iterate through all the bounties bounty_enum = start_bounty_id more_bounties = True while more_bounties: try: # pull and process each bounty print(f'** get_bounty_id_from_web3; looking at {bounty_enum}') bounty = get_bounty(bounty_enum, network) url = bounty.get('data', {}).get('payload', {}).get('webReferenceURL', False) if url == issue_url: return bounty['id'] except BountyNotFoundException: more_bounties = False except UnsupportedSchemaException: pass finally: # prepare for next loop if direction == 'up': bounty_enum += 1 else: bounty_enum -= 1 return None
def get_bounty_id(issue_url, network): issue_url = normalizeURL(issue_url) bounty_id = get_bounty_id_from_db(issue_url, network) if bounty_id: return bounty_id all_known_stdbounties = Bounty.objects.filter( web3_type='bounties_network', network=network).order_by('-standard_bounties_id') methodology = 'start_from_web3_latest' try: highest_known_bounty_id = get_highest_known_bounty_id(network) bounty_id = get_bounty_id_from_web3(issue_url, network, highest_known_bounty_id, direction='down') except NoBountiesException: methodology = 'start_from_db' last_known_bounty_id = 0 if all_known_stdbounties.exists(): last_known_bounty_id = all_known_stdbounties.first( ).standard_bounties_id bounty_id = get_bounty_id_from_web3(issue_url, network, last_known_bounty_id, direction='up') return bounty_id
def process_change(bountyContract, url, txid, network): url = normalizeURL(url) didChange, old_bounty, new_bounty = syncBountywithWeb3(bountyContract, url, network) print("{} changed, {}".format(didChange, url)) if didChange: print("- processing changes") process_bounty_changes(old_bounty, new_bounty, txid)
def get_bounty_id_from_db(issue_url, network): issue_url = normalizeURL(issue_url) bounties = Bounty.objects.filter(github_url=issue_url, network=network, web3_type='bounties_network') if not bounties.exists(): return None return bounties.first().standard_bounties_id
def sync_web3(request): #setup result = {} issueURL = request.POST.get('issueURL', False) bountydetails = request.POST.getlist('bountydetails[]', []) if issueURL: issueURL = normalizeURL(issueURL) if not len(bountydetails): #create a bounty sync request result['status'] = 'OK' for existing_bsr in BountySyncRequest.objects.filter(github_url=issueURL, processed=False): existing_bsr.processed = True existing_bsr.save() else: #normalize data bountydetails[0] = int(bountydetails[0]) bountydetails[1] = str(bountydetails[1]) bountydetails[2] = str(bountydetails[2]) bountydetails[3] = str(bountydetails[3]) bountydetails[4] = bool(bountydetails[4] == 'true') bountydetails[5] = bool(bountydetails[5] == 'true') bountydetails[6] = str(bountydetails[6]) bountydetails[7] = int(bountydetails[7]) bountydetails[8] = str(bountydetails[8]) bountydetails[9] = int(bountydetails[9]) bountydetails[10] = str(bountydetails[10]) print(bountydetails) contract_address = request.POST.get('contract_address') network = request.POST.get('network') didChange, old_bounty, new_bounty = process_bounty_details(bountydetails, issueURL, contract_address, network) print("{} changed, {}".format(didChange, issueURL)) if didChange: print("- processing changes"); process_bounty_changes(old_bounty, new_bounty, None) BountySyncRequest.objects.create( github_url=issueURL, processed=False, ) return JsonResponse(result)
def sync_web3(request): """Sync web3 for legacy.""" # setup result = {} issue_url = request.POST.get('issueURL', False) bountydetails = request.POST.getlist('bountydetails[]', []) if issue_url: issue_url = normalizeURL(issue_url) if not bountydetails: # create a bounty sync request result['status'] = 'OK' for existing_bsr in BountySyncRequest.objects.filter( github_url=issue_url, processed=False): existing_bsr.processed = True existing_bsr.save() else: # normalize data bountydetails[0] = int(bountydetails[0]) bountydetails[1] = str(bountydetails[1]) bountydetails[2] = str(bountydetails[2]) bountydetails[3] = str(bountydetails[3]) bountydetails[4] = bool(bountydetails[4] == 'true') bountydetails[5] = bool(bountydetails[5] == 'true') bountydetails[6] = str(bountydetails[6]) bountydetails[7] = int(bountydetails[7]) bountydetails[8] = str(bountydetails[8]) bountydetails[9] = int(bountydetails[9]) bountydetails[10] = str(bountydetails[10]) print(bountydetails) contract_address = request.POST.get('contract_address') network = request.POST.get('network') did_change, old_bounty, new_bounty = process_bounty_details( bountydetails, issue_url, contract_address, network) print(f"LEGACY: {did_change} changed, {issue_url}") if did_change: print("- processing changes") process_bounty_changes(old_bounty, new_bounty) BountySyncRequest.objects.create(github_url=issue_url, processed=False) return JsonResponse(result)
def sync_web3(request): """ Sync up web3 with the database. This function has a few different uses. It is typically called from the front end using the javascript `sync_web3` function. The `issueURL` is passed in first, followed optionally by a `bountydetails` argument. """ # setup result = {} issueURL = request.POST.get('issueURL', False) bountydetails = json.loads(request.POST.get('bountydetails', "{}")) if issueURL: issueURL = normalizeURL(issueURL) if not bountydetails: # create a bounty sync request result['status'] = 'OK' for existing_bsr in BountySyncRequest.objects.filter( github_url=issueURL, processed=False): existing_bsr.processed = True existing_bsr.save() else: contract_address = request.POST.get('contract_address') network = request.POST.get('network') didChange, old_bounty, new_bounty = process_bounty_details( bountydetails, issueURL, contract_address, network) print("{} changed, {}".format(didChange, issueURL)) if didChange: print("- processing changes") process_bounty_changes(old_bounty, new_bounty, None) BountySyncRequest.objects.create( github_url=issueURL, processed=False, ) return JsonResponse(result)
def process_bounty_details(bountydetails, url, contract_address, network): """Process legacy bounty details.""" url = normalizeURL(url) # extract json metadata = None try: metadata = json.loads(bountydetails[8]) except Exception as e: print(e) metadata = {} # create new bounty (but only if things have changed) did_change = False old_bounties = Bounty.objects.none() try: old_bounties = Bounty.objects.current().filter( github_url=url, title=metadata.get('issueTitle'), ).order_by('-created_on') did_change = (bountydetails != old_bounties.first().raw_data) if not did_change: return (did_change, old_bounties.first(), old_bounties.first()) except Exception as e: print(e) did_change = True with transaction.atomic(): new_bounty = Bounty.objects.create( title=metadata.get('issueTitle', ''), web3_type='legacy_gitcoin', web3_created=timezone.datetime.fromtimestamp(bountydetails[7]), value_in_token=bountydetails[0], token_name=metadata.get('tokenName'), token_address=bountydetails[1], bounty_type=metadata.get('bountyType'), project_length=metadata.get('projectLength'), experience_level=metadata.get('experienceLevel'), github_url=url, bounty_owner_address=bountydetails[2], bounty_owner_email=metadata.get('notificationEmail', ''), bounty_owner_github_username=metadata.get('githubUsername', ''), is_open=bountydetails[4], expires_date=timezone.datetime.fromtimestamp(bountydetails[9]), raw_data=bountydetails, metadata=metadata, current_bounty=True, contract_address=contract_address, network=network, issue_description='', ) for old_bounty in old_bounties: if old_bounty.current_bounty: old_num_fulfillments = old_bounty.fulfillments.count() old_bounty.num_fulfillments = old_num_fulfillments old_bounty.fulfillments.update(bounty=new_bounty) if new_bounty.num_fulfillments < old_num_fulfillments or \ new_bounty.num_fulfillments < new_bounty.fulfillments.count(): new_bounty.num_fulfillments = new_bounty.fulfillments.count( ) new_bounty.save() old_bounty.current_bounty = False old_bounty.save() new_bounty.fetch_issue_item() if not new_bounty.avatar_url: new_bounty.avatar_url = new_bounty.get_avatar_url() new_bounty.save() return (did_change, old_bounties.first(), new_bounty)
def test_normalize_url(self): """Test the dashboard helper normalizeURL method.""" assert normalizeURL('https://gitcoin.co/') == 'https://gitcoin.co'