diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c2a69ffbe16..2f2aa090055 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,12 +13,12 @@ repos: language: system entry: sh -c 'npm run stylelint:fix' files: .css$ -# - repo: git://github.com/pre-commit/mirrors-isort -# sha: 'v4.3.4' -# hooks: -# - id: isort -# args: -# - --recursive -# - --settings-path -# - ./setup.cfg -# - . +- repo: git://github.com/pre-commit/mirrors-isort + sha: 'v4.3.4' + hooks: + - id: isort + args: + - --recursive + - --settings-path + - ./setup.cfg + - . diff --git a/app/app/local.env b/app/app/local.env index a65a28c6e53..130b9e42ce0 100644 --- a/app/app/local.env +++ b/app/app/local.env @@ -65,6 +65,7 @@ GIPHY_KEY= YOUTUBE_API_KEY= VIEW_BLOCK_API_KEY= ETHERSCAN_API_KEY= +POLYGON_API_KEY= FORTMATIC_LIVE_KEY= FORTMATIC_TEST_KEY= XINFIN_API_KEY= diff --git a/app/app/settings.py b/app/app/settings.py index f0f7fdb9851..eb24a801cf3 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -52,6 +52,7 @@ GIPHY_KEY = env('GIPHY_KEY', default='LtaY19ToaBSckiLU4QjW0kV9nIP75NFy') YOUTUBE_API_KEY = env('YOUTUBE_API_KEY', default='YOUR-SupEr-SecRet-YOUTUBE-KeY') ETHERSCAN_API_KEY = env('ETHERSCAN_API_KEY', default='YOUR-ETHERSCAN-KEY') +POLYGON_API_KEY = env('POLYGON_API_KEY', default='YOUR-POLYGON-KEY') VIEW_BLOCK_API_KEY = env('VIEW_BLOCK_API_KEY', default='YOUR-VIEW-BLOCK-KEY') FORTMATIC_LIVE_KEY = env('FORTMATIC_LIVE_KEY', default='YOUR-SupEr-SecRet-LiVe-FoRtMaTiC-KeY') FORTMATIC_TEST_KEY = env('FORTMATIC_TEST_KEY', default='YOUR-SupEr-SecRet-TeSt-FoRtMaTiC-KeY') diff --git a/app/assets/v2/js/cart-ethereum-polygon.js b/app/assets/v2/js/cart-ethereum-polygon.js index faf41bd425e..7f933b6997b 100644 --- a/app/assets/v2/js/cart-ethereum-polygon.js +++ b/app/assets/v2/js/cart-ethereum-polygon.js @@ -19,8 +19,7 @@ Vue.component('grantsCartEthereumPolygon', { polygon: { showModal: false, // true to show modal to user, false to hide checkoutStatus: 'not-started', // options are 'not-started', 'pending', and 'complete' - estimatedGasCost: 65000, - gasPrices: null + estimatedGasCost: 65000 }, cart: { @@ -382,14 +381,6 @@ Vue.component('grantsCartEthereumPolygon', { let gasLimit = 0; - // fetch gas prices from polygon gas tracker - if (!this.polygon.gasPrices) { - const priceUrl = 'https://api.polygonscan.com/api?module=gastracker&action=gasoracle&apikey=I28K1DVQAWAISBSI146I71YQDBK6N1C9GJ'; - const priceResponse = await fetch(priceUrl); - - this.polygon.gasPrices = (await priceResponse.json()).result; - } - // If user has enough balance within Polygon, cost equals the minimum amount let { isBalanceSufficient, requiredAmounts } = await this.hasEnoughBalanceInPolygon(); @@ -494,7 +485,7 @@ Vue.component('grantsCartEthereumPolygon', { // Check if user has enough MATIC to cover gas costs if (this.polygon.estimatedGasCost) { const gasFeeInWei = web3.utils.toWei( - (this.polygon.estimatedGasCost * Number(this.polygon.gasPrices.SafeGasPrice)).toString(), 'gwei' // using safe gas price + (this.polygon.estimatedGasCost * Number(document.polygonGasPrice)).toString(), 'gwei' // using safe gas price ); if (userMaticBalance.lt(gasFeeInWei)) { diff --git a/app/grants/management/commands/fetch_gas_prices.py b/app/grants/management/commands/fetch_gas_prices.py new file mode 100644 index 00000000000..bc09e4f0a5a --- /dev/null +++ b/app/grants/management/commands/fetch_gas_prices.py @@ -0,0 +1,56 @@ +""" + Copyright (C) 2021 Gitcoin Core + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +""" + +import json + +from django.conf import settings +from django.core.management.base import BaseCommand + +import requests +from economy.models import EncodeAnything +from perftools.models import JSONStore + + +def polygon(): + res = requests.get( + f"https://api.polygonscan.com/api?module=gastracker&action=gasoracle&apikey={settings.POLYGON_API_KEY}" + ) + data = res.json()['result'] + print(data) + view = "gas_prices" + keyword = "polygon" + + JSONStore.objects.filter(view=view, key=keyword).all().delete() + data = json.loads(json.dumps(data, cls=EncodeAnything)) + JSONStore.objects.create( + view=view, + key=keyword, + data=data, + ) + + +class Command(BaseCommand): + + help = "get gas prices for networks" + + def handle(self, *args, **options): + try: + print("Polygon") + polygon() + except Exception as e: + print(e) diff --git a/app/grants/templates/grants/cart-vue.html b/app/grants/templates/grants/cart-vue.html index 89893c0efe3..4cf81642b3b 100644 --- a/app/grants/templates/grants/cart-vue.html +++ b/app/grants/templates/grants/cart-vue.html @@ -14,7 +14,7 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see . {% endcomment %} -{% load static i18n bundle tz %} +{% load static i18n bundle tz get_item %} @@ -218,6 +218,7 @@

Discover Grants

{% bundle merge_js file qrcode %} diff --git a/app/grants/views.py b/app/grants/views.py index 8c8b9609180..565beff408f 100644 --- a/app/grants/views.py +++ b/app/grants/views.py @@ -90,13 +90,12 @@ from marketing.mails import grant_cancellation, new_grant_flag_admin from marketing.models import Keyword, Stat from perftools.models import JSONStore, StaticJsonEnv +from PIL import Image from ratelimit.decorators import ratelimit from retail.helpers import get_ip from townsquare.models import Announcement, Favorite, PinnedPost from townsquare.utils import can_pin from web3 import HTTPProvider, Web3 -from PIL import Image - logger = logging.getLogger(__name__) w3 = Web3(HTTPProvider(settings.WEB3_HTTP_PROVIDER)) @@ -2419,6 +2418,9 @@ def grants_cart_view(request): context['is_fully_verified'] = (is_brightid_verified and profile.sms_verification and \ profile.is_poap_verified and profile.is_twitter_verified and \ profile.is_google_verified and profile.is_poh_verified) + context['gas_prices'] = { + 'polygon': JSONStore.objects.get(view='gas_prices', key='polygon').data['SafeGasPrice'] + } else: return redirect('/login/github/?next=' + request.get_full_path()) diff --git a/app/retail/templatetags/get_item.py b/app/retail/templatetags/get_item.py new file mode 100644 index 00000000000..17d29f63aaa --- /dev/null +++ b/app/retail/templatetags/get_item.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +"""Define the get_item template tag to allow fetching dict or tuple in templates. + +Copyright (C) 2021 Gitcoin Core + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published +by the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +""" +from django import template + +register = template.Library() + + +@register.filter +def get_item(container, key): + if type(container) is dict: + return container.get(key) + elif type(container) in (list, tuple): + return container[key] if len(container) > key else None + return None diff --git a/scripts/crontab b/scripts/crontab index b4211a58391..05ccbca69bf 100644 --- a/scripts/crontab +++ b/scripts/crontab @@ -61,6 +61,7 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/us ## GRANTS */3 * * * * cd gitcoin/coin; bash scripts/run_management_command_if_not_already_running.bash sync_pending_contributions >> /var/log/gitcoin/sync_pending_contributions.log 2>&1 1 */12 * * * cd gitcoin/coin; bash scripts/run_management_command_if_not_already_running.bash send_grants_contributions_emails >> /var/log/gitcoin/send_grants_contributions_emails.log 2>&1 +5 */12 * * * cd gitcoin/coin; bash scripts/run_management_command_if_not_already_running.bash fetch_gas_prices >> /var/log/gitcoin/fetch_gas_prices.log 2>&1 ## GITCOIN MARKETING 30 */12 * * * cd gitcoin/coin; bash scripts/run_management_command_if_not_already_running.bash sync_mail >> /var/log/gitcoin/sync_mail.log 2>&1