Skip to content

Commit

Permalink
GITC-616: fetch gas price from polygon gas tracker (#9733)
Browse files Browse the repository at this point in the history
* fetch gas price from polygon gas tracker

* pre-commit linting

* revamp

* rename env
  • Loading branch information
chibie authored Nov 30, 2021
1 parent 69018dc commit 2ce8e89
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/app/local.env
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ GIPHY_KEY=
YOUTUBE_API_KEY=
VIEW_BLOCK_API_KEY=
ETHERSCAN_API_KEY=
POLYGONSCAN_API_KEY=
FORTMATIC_LIVE_KEY=
FORTMATIC_TEST_KEY=
XINFIN_API_KEY=
Expand Down
1 change: 1 addition & 0 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
POLYGONSCAN_API_KEY = env('POLYGONSCAN_API_KEY', default='YOUR-POLYGONSCAN-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')
Expand Down
2 changes: 1 addition & 1 deletion app/assets/v2/js/cart-ethereum-polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,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 * 2).toString(), 'gwei' // using 2 gwei as gas price
(this.polygon.estimatedGasCost * Number(document.polygonGasPrice)).toString(), 'gwei' // using safe gas price
);

if (userMaticBalance.lt(gasFeeInWei)) {
Expand Down
56 changes: 56 additions & 0 deletions app/grants/management/commands/fetch_gas_prices.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
"""

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.POLYGONSCAN_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)
3 changes: 2 additions & 1 deletion app/grants/templates/grants/cart-vue.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
{% endcomment %}
{% load static i18n bundle tz %}
{% load static i18n bundle tz get_item %}
<!DOCTYPE html>
<html lang="en">

Expand Down Expand Up @@ -218,6 +218,7 @@ <h4 class="font-weight-bold mb-5 my-md-0">Discover Grants</h4>
<script>
document.authenticated = {{ authenticated | yesno:"true,false" }};
document.isFullyVerified = '{{is_fully_verified}}' === 'True';
document.polygonGasPrice = "{{ gas_prices|get_item:'polygon' }}"
</script>
{% bundle merge_js file qrcode %}
<script src="qrcode.min.js" base-dir="/node_modules/qrcodejs/"></script>
Expand Down
3 changes: 3 additions & 0 deletions app/grants/templatetags/grants_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ def modulo(num, val):
return num % val
return 0


@register.simple_tag
def is_team_member(grant, profile):
return is_grant_team_member(grant, profile)


@register.simple_tag
def is_grants_path(path):
return path.lower().startswith('/grants')


@register.simple_tag
def is_favorite(grant, profile):
if profile:
Expand Down
3 changes: 3 additions & 0 deletions app/grants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2433,6 +2433,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())

Expand Down
31 changes: 31 additions & 0 deletions app/retail/templatetags/get_item.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
"""
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
1 change: 1 addition & 0 deletions scripts/crontab
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2ce8e89

Please sign in to comment.