Skip to content

Commit

Permalink
revamp
Browse files Browse the repository at this point in the history
  • Loading branch information
chibie committed Nov 29, 2021
1 parent a14998f commit c2cfb70
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 23 deletions.
18 changes: 9 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
- .
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=
POLYGON_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')
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')
Expand Down
13 changes: 2 additions & 11 deletions app/assets/v2/js/cart-ethereum-polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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)) {
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.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)
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
6 changes: 4 additions & 2 deletions app/grants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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())

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 c2cfb70

Please sign in to comment.