Skip to content

Commit 2ce8e89

Browse files
authored
GITC-616: fetch gas price from polygon gas tracker (#9733)
* fetch gas price from polygon gas tracker * pre-commit linting * revamp * rename env
1 parent 69018dc commit 2ce8e89

File tree

9 files changed

+99
-2
lines changed

9 files changed

+99
-2
lines changed

app/app/local.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ GIPHY_KEY=
6565
YOUTUBE_API_KEY=
6666
VIEW_BLOCK_API_KEY=
6767
ETHERSCAN_API_KEY=
68+
POLYGONSCAN_API_KEY=
6869
FORTMATIC_LIVE_KEY=
6970
FORTMATIC_TEST_KEY=
7071
XINFIN_API_KEY=

app/app/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
GIPHY_KEY = env('GIPHY_KEY', default='LtaY19ToaBSckiLU4QjW0kV9nIP75NFy')
5353
YOUTUBE_API_KEY = env('YOUTUBE_API_KEY', default='YOUR-SupEr-SecRet-YOUTUBE-KeY')
5454
ETHERSCAN_API_KEY = env('ETHERSCAN_API_KEY', default='YOUR-ETHERSCAN-KEY')
55+
POLYGONSCAN_API_KEY = env('POLYGONSCAN_API_KEY', default='YOUR-POLYGONSCAN-KEY')
5556
VIEW_BLOCK_API_KEY = env('VIEW_BLOCK_API_KEY', default='YOUR-VIEW-BLOCK-KEY')
5657
FORTMATIC_LIVE_KEY = env('FORTMATIC_LIVE_KEY', default='YOUR-SupEr-SecRet-LiVe-FoRtMaTiC-KeY')
5758
FORTMATIC_TEST_KEY = env('FORTMATIC_TEST_KEY', default='YOUR-SupEr-SecRet-TeSt-FoRtMaTiC-KeY')

app/assets/v2/js/cart-ethereum-polygon.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ Vue.component('grantsCartEthereumPolygon', {
483483
// Check if user has enough MATIC to cover gas costs
484484
if (this.polygon.estimatedGasCost) {
485485
const gasFeeInWei = web3.utils.toWei(
486-
(this.polygon.estimatedGasCost * 2).toString(), 'gwei' // using 2 gwei as gas price
486+
(this.polygon.estimatedGasCost * Number(document.polygonGasPrice)).toString(), 'gwei' // using safe gas price
487487
);
488488

489489
if (userMaticBalance.lt(gasFeeInWei)) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Copyright (C) 2021 Gitcoin Core
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU Affero General Public License as published
6+
by the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU Affero General Public License for more details.
13+
14+
You should have received a copy of the GNU Affero General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
"""
18+
19+
import json
20+
21+
from django.conf import settings
22+
from django.core.management.base import BaseCommand
23+
24+
import requests
25+
from economy.models import EncodeAnything
26+
from perftools.models import JSONStore
27+
28+
29+
def polygon():
30+
res = requests.get(
31+
f"https://api.polygonscan.com/api?module=gastracker&action=gasoracle&apikey={settings.POLYGONSCAN_API_KEY}"
32+
)
33+
data = res.json()['result']
34+
print(data)
35+
view = "gas_prices"
36+
keyword = "polygon"
37+
38+
JSONStore.objects.filter(view=view, key=keyword).all().delete()
39+
data = json.loads(json.dumps(data, cls=EncodeAnything))
40+
JSONStore.objects.create(
41+
view=view,
42+
key=keyword,
43+
data=data,
44+
)
45+
46+
47+
class Command(BaseCommand):
48+
49+
help = "get gas prices for networks"
50+
51+
def handle(self, *args, **options):
52+
try:
53+
print("Polygon")
54+
polygon()
55+
except Exception as e:
56+
print(e)

app/grants/templates/grants/cart-vue.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
You should have received a copy of the GNU Affero General Public License
1515
along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
{% endcomment %}
17-
{% load static i18n bundle tz %}
17+
{% load static i18n bundle tz get_item %}
1818
<!DOCTYPE html>
1919
<html lang="en">
2020

@@ -218,6 +218,7 @@ <h4 class="font-weight-bold mb-5 my-md-0">Discover Grants</h4>
218218
<script>
219219
document.authenticated = {{ authenticated | yesno:"true,false" }};
220220
document.isFullyVerified = '{{is_fully_verified}}' === 'True';
221+
document.polygonGasPrice = "{{ gas_prices|get_item:'polygon' }}"
221222
</script>
222223
{% bundle merge_js file qrcode %}
223224
<script src="qrcode.min.js" base-dir="/node_modules/qrcodejs/"></script>

app/grants/templatetags/grants_extra.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,17 @@ def modulo(num, val):
6161
return num % val
6262
return 0
6363

64+
6465
@register.simple_tag
6566
def is_team_member(grant, profile):
6667
return is_grant_team_member(grant, profile)
6768

69+
6870
@register.simple_tag
6971
def is_grants_path(path):
7072
return path.lower().startswith('/grants')
7173

74+
7275
@register.simple_tag
7376
def is_favorite(grant, profile):
7477
if profile:

app/grants/views.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,6 +2433,9 @@ def grants_cart_view(request):
24332433
context['is_fully_verified'] = (is_brightid_verified and profile.sms_verification and \
24342434
profile.is_poap_verified and profile.is_twitter_verified and \
24352435
profile.is_google_verified and profile.is_poh_verified)
2436+
context['gas_prices'] = {
2437+
'polygon': JSONStore.objects.get(view='gas_prices', key='polygon').data['SafeGasPrice']
2438+
}
24362439
else:
24372440
return redirect('/login/github/?next=' + request.get_full_path())
24382441

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
"""Define the get_item template tag to allow fetching dict or tuple in templates.
3+
4+
Copyright (C) 2021 Gitcoin Core
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License as published
8+
by the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU Affero General Public License for more details.
15+
16+
You should have received a copy of the GNU Affero General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
19+
"""
20+
from django import template
21+
22+
register = template.Library()
23+
24+
25+
@register.filter
26+
def get_item(container, key):
27+
if type(container) is dict:
28+
return container.get(key)
29+
elif type(container) in (list, tuple):
30+
return container[key] if len(container) > key else None
31+
return None

scripts/crontab

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/us
6161
## GRANTS
6262
*/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
6363
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
64+
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
6465

6566
## GITCOIN MARKETING
6667
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

0 commit comments

Comments
 (0)