def value_in_eth(self): if self.tokenName == 'ETH': return self.amount try: return convert_amount(self.amount, self.tokenName, 'ETH') except: return None
def value_in_eth(self): if self.token_name == 'ETH': return self.value_in_token try: return convert_amount(self.value_in_token, self.token_name, 'ETH') except Exception: return None
def gas(request): _cts = conf_time_spread() recommended_gas_price = recommend_min_gas_price_to_confirm_in_time( confirm_time_minutes_target) if recommended_gas_price < 2: _cts = conf_time_spread(recommended_gas_price) context = { 'title': _('Live Ethereum (ETH) Gas Usage & Confirmation Times'), 'card_desc': _('Real-time graph of Ethereum (ETH) Live Predicted Confirmation Times (x axis) vs Gas Usage' ), 'eth_to_usd': round(convert_amount(1, 'ETH', 'USDT'), 0), 'start_gas_cost': recommended_gas_price, 'gas_advisories': gas_advisories(), 'conf_time_spread': _cts, 'hide_send_tip': True, 'is_3d': request.GET.get("is_3d", False), } return TemplateResponse(request, 'gas.html', context)
def gas_calculator(request): recommended_gas_price = recommend_min_gas_price_to_confirm_in_time( confirm_time_minutes_target) _cts = conf_time_spread() actions = [{ 'name': 'New Bounty', 'target': '/new', 'persona': 'funder', 'product': 'bounties', }, { 'name': 'Fulfill Bounty', 'target': 'issue/fulfill', 'persona': 'developer', 'product': 'bounties', }, { 'name': 'Increase Funding', 'target': 'issue/increase', 'persona': 'funder', 'product': 'bounties', }, { 'name': 'Accept Submission', 'target': 'issue/accept', 'persona': 'funder', 'product': 'bounties', }, { 'name': 'Cancel Funding', 'target': 'issue/cancel', 'persona': 'funder', 'product': 'bounties', }, { 'name': 'Send tip', 'target': 'tip/send/2/', 'persona': 'funder', 'product': 'tips', }, { 'name': 'Receive tip', 'target': 'tip/receive', 'persona': 'developer', 'product': 'tips', }] context = { 'title': _('Gas Estimator'), 'card_desc': _('See what popular Gitcoin methods cost at different Gas Prices'), 'actions': actions, 'conf_time_spread': _cts, 'eth_to_usd': round(convert_amount(1, 'ETH', 'USDT'), 0), 'start_gas_cost': recommended_gas_price, 'title': 'Gas Calculator', 'hide_send_tip': True, } return TemplateResponse(request, 'gas_calculator.html', context)
def value_in_usdt(self): decimals = 10**18 if self.token_name == 'USDT': return self.value_in_token try: return round(float(convert_amount(self.value_in_eth, 'ETH', 'USDT')) / decimals, 2) except Exception: return None
def value_in_usdt(self): decimals = 1 if self.tokenName == 'USDT': return self.amount try: return round(float(convert_amount(self.value_in_eth, 'ETH', 'USDT')) / decimals, 2) except: return None
def value_usdt(self): try: return float( convert_amount(self.amount_per_period, self.token_symbol, "USDT")) except ConversionRateNotFoundError as e: logger.info(e) return None
def value_in_usdt(self): if self.token_name == 'USDT': return self.value_in_token try: return round( float(convert_amount(self.value_in_eth, 'ETH', 'USDT')) / 10**18, 2) except: return None
def get_converted_amount(self): try: return Decimal( convert_amount(self.amount_per_period, self.token_symbol, "USDT")) except ConversionRateNotFoundError as e: logger.info(e) return None
def get_converted_amount(amount, token_symbol): try: if token_symbol == "ETH" or token_symbol == "WETH": return Decimal(float(amount) * float(eth_usd_conv_rate())) else: value_token_to_eth = Decimal( convert_amount(amount, token_symbol, "ETH")) value_eth_to_usdt = Decimal(eth_usd_conv_rate()) value_usdt = value_token_to_eth * value_eth_to_usdt return value_usdt except ConversionRateNotFoundError as e: try: return Decimal(convert_amount(amount, token_symbol, "USDT")) except ConversionRateNotFoundError as no_conversion_e: logger.info(no_conversion_e) return None
def value_in_usdt_then(self): decimals = 1 if self.tokenName == 'USDT': return float(self.amount) if self.tokenName == 'DAI': return float(self.amount / 10 ** 18) try: return round(float(convert_amount(self.amount, self.tokenName, 'USDT', self.created_on)) / decimals, 2) except ConversionRateNotFoundError: return None
def value_in_usdt_then(self): decimals = 10 ** 18 if self.token_name == 'USDT': return float(self.value_in_token) if self.token_name == 'DAI': return float(self.value_in_token / 10 ** 18) try: return round(float(convert_amount(self.value_in_token, self.token_name, 'USDT', self.web3_created)) / decimals, 2) except ConversionRateNotFoundError: return None
def amount(request): response = {} try: amount = request.GET.get('amount') denomination = request.GET.get('denomination', 'ETH') if denomination == 'ETH': amount_in_eth = float(amount) else: amount_in_eth = convert_amount(amount, denomination, 'ETH') amount_in_usdt = convert_amount(amount_in_eth, 'ETH', 'USDT') response = { 'eth': amount_in_eth, 'usdt': amount_in_usdt, } return JsonResponse(response) except Exception as e: print(e) raise Http404
def fiat_price(self): """Return the fiat price.""" if self.amount_denomination.lower() in ['usd', 'usdt']: return self.amount fiat_price = None try: fiat_price = round(convert_amount(self.amount, self.amount_denomination, 'USDT'), 2) except Exception: pass return fiat_price
def new_matching_partner(request): tx_hash = request.POST.get('hash') tx_amount = request.POST.get('amount') profile = get_profile(request) def get_json_response(message, status): return JsonResponse({ 'status': status, 'message': message }, status=status) def is_verified(tx_details, tx_hash, tx_amount, network): gitcoin_account = '0x00De4B13153673BCAE2616b67bf822500d325Fc3' return has_tx_mined(tx_hash, network) and\ tx_details.to.lower() == gitcoin_account.lower() if not request.user.is_authenticated: return get_json_response("Not Authorized", 403) if not profile: return get_json_response("Profile not found.", 404) if request.POST and tx_hash: network = 'mainnet' web3 = get_web3(network) tx = web3.eth.getTransaction(tx_hash) if not tx: raise Http404 match_pledge = MatchPledge.objects.create(profile=profile, amount=convert_amount( tx.value / 10**18, 'ETH', 'USDT'), data=json.dumps({ 'tx_hash': tx_hash, 'network': network, 'from': tx['from'], 'to': tx.to, 'tx_amount': tx.value })) match_pledge.active = is_verified(tx, tx_hash, tx_amount, network) match_pledge.save() return get_json_response( """Thank you for volunteering to match on Gitcoin Grants. You are supporting open source, and we thank you.""", 201) return get_json_response("Wrong request.", 400)
def get_converted_amount(self, ignore_gitcoin_fee=False): amount = self.amount_per_period if ignore_gitcoin_fee else self.amount_per_period_minus_gas_price try: if self.token_symbol == "ETH" or self.token_symbol == "WETH": return Decimal(float(amount) * float(eth_usd_conv_rate())) else: value_token_to_eth = Decimal( convert_amount(amount, self.token_symbol, "ETH")) value_eth_to_usdt = Decimal(eth_usd_conv_rate()) value_usdt = value_token_to_eth * value_eth_to_usdt return value_usdt except ConversionRateNotFoundError as e: try: return Decimal( convert_amount(amount, self.token_symbol, "USDT")) except ConversionRateNotFoundError as no_conversion_e: logger.info(no_conversion_e) return None
def value_in_usdt(self): decimals = 1 if self.tokenName == 'USDT': return float(self.amount) if self.tokenName == 'DAI': return float(self.amount / 10**18) try: return round( float(convert_amount(self.value_in_eth, 'ETH', 'USDT')) / decimals, 2) except Exception: return None
def amount(request): """Determine the value of the provided denomination and amount in ETH and USD. Raises: Http404: The exception is raised if any error is encountered. Returns: JsonResponse: A JSON response containing ETH and USDT values. """ response = {} try: amount = str(request.GET.get('amount')) if not amount.replace('.', '').isnumeric(): return HttpResponseBadRequest('not number') denomination = request.GET.get('denomination', 'ETH') if not denomination: denomination = 'ETH' if denomination in settings.STABLE_COINS: denomination = 'USDT' if denomination == 'ETH': amount_in_eth = float(amount) else: amount_in_eth = convert_amount(amount, denomination, 'ETH') amount_in_usdt = convert_amount(amount_in_eth, 'ETH', 'USDT') response = { 'eth': amount_in_eth, 'usdt': amount_in_usdt, } return JsonResponse(response) except ConversionRateNotFoundError as e: logger.debug(e) raise Http404 except Exception as e: logger.error(e) raise Http404
def get_converted_amount(self): try: if self.token_symbol == "ETH" or self.token_symbol == "WETH": return Decimal(self.amount_per_period * eth_usd_conv_rate()) else: value_token_to_eth = Decimal(convert_amount( self.amount_per_period, self.token_symbol, "ETH") ) value_eth_to_usdt = Decimal(eth_usd_conv_rate()) value_usdt = value_token_to_eth * value_eth_to_usdt return value_usdt except ConversionRateNotFoundError as e: try: return Decimal(convert_amount( self.amount_per_period, self.token_symbol, "USDT")) except ConversionRateNotFoundError as no_conversion_e: logger.info(no_conversion_e) return None
def format_faucet_distribution(self, fr): return { 'type': 'faucet_distribution', 'created_on': fr.created_on, 'last_activity': fr.modified_on, 'amount': fr.amount, 'denomination': 'ETH', 'amount_eth': fr.amount, 'amount_usdt': convert_amount(fr.amount, 'ETH', 'USDT'), 'from_address': '0x4331B095bC38Dc3bCE0A269682b5eBAefa252929', 'claimee_address': fr.address, 'repo': 'n/a', 'from_username': '******', 'fulfiller_github_username': fr.github_username, 'status': 'sent', 'comments': f"faucet distribution {fr.pk}", }
def gas(request): _cts = conf_time_spread() recommended_gas_price = recommend_min_gas_price_to_confirm_in_time(confirm_time_minutes_target) if recommended_gas_price < 2: _cts = conf_time_spread(recommended_gas_price) context = { 'title': _('Live Gas Tool'), 'card_desc': _('See the Live Network Conditions for the Ethereum Network'), 'eth_to_usd': round(convert_amount(1, 'ETH', 'USDT'), 0), 'start_gas_cost': recommended_gas_price, 'gas_advisories': gas_advisories(), 'conf_time_spread': _cts, 'hide_send_tip': True, 'is_3d': request.GET.get("is_3d", False), 'title': 'Live Gas Usage => Predicted Conf Times' } return TemplateResponse(request, 'gas.html', context)
def successful_contribution(self, kwargs): """Create a contribution object.""" from marketing.mails import successful_contribution self.last_contribution_date = timezone.now() self.next_contribution_date = timezone.now() + timezone.timedelta(seconds=self.real_period_seconds) self.save() contribution_kwargs = { 'tx_id': kwargs.tx_id, 'gas_price': kwargs.gas_price, 'nonce': kwargs.nonce, 'subscription': self } contribution = Contribution.objects.create(**contribution_kwargs) grant = self.grant grant.amount_received = (grant.amount_received + convert_amount(self.amount_per_period, self.token_symbol, "USDT", timezone.now())) grant.save() successful_contribution(self.grant, self) return contribution
def format_ens_reg(self, ensreg): location, bio = get_bio(ensreg.profile.handle) if ensreg.profile else "", "" amount = ensreg.gas_cost_eth return { 'type': 'ens_subdomain_registration', 'created_on': ensreg.created_on, 'last_activity': ensreg.modified_on, 'amount': amount, 'denomination': 'ETH', 'amount_eth': amount, 'amount_usdt': convert_amount(amount, 'ETH', 'USDT'), 'from_address': '0x4331B095bC38Dc3bCE0A269682b5eBAefa252929', 'claimee_address': ensreg.subdomain_wallet_address, 'repo': 'n/a', 'from_username': '******', 'fulfiller_github_username': ensreg.profile.handle if ensreg.profile else "", 'status': 'sent', 'comments': f"ENS Subdomain Registration {ensreg.pk}", 'payee_bio': bio, 'payee_location': location, }
def test_convert_amount_time_travel(self): """Test the economy util convert_amount method for historic ConversionRates.""" result = convert_amount(2, 'ETH', 'USDT', datetime(2018, 1, 1)) assert round(result, 1) == 10
def test_convert_amount(self): """Test the economy util convert_amount method.""" result = convert_amount(2, 'ETH', 'USDT') assert round(result, 1) == 6
def eth_usd_conv_rate(): from_amount = 1 from_currency = 'ETH' to_currency = 'USDT' return convert_amount(from_amount, from_currency, to_currency)