Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GITC-616: fetch gas price from polygon gas tracker #9733

Merged
merged 4 commits into from
Nov 30, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions app/assets/v2/js/cart-ethereum-polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ 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
estimatedGasCost: 65000,
gasPrices: null
},

cart: {
Expand Down Expand Up @@ -381,6 +382,14 @@ 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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to switch this out for a paid account or move this to the backend and set up a cron to store and update the safeGasPrice periodically.

This is going to get hammered if we call it every time a user hits the checkout.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say set a high default price and wrap this in a try catch for now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we load the api key from the settings?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that is a nice catch ! @chibie i agree with nutrina here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we should really be protecting that key - I would recommend moving this to the backend and returning the safeGasPrice as part of the context.

Copy link
Contributor Author

@chibie chibie Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gdixon pushed a commit c2cfb70

we should set POLYGONSCAN_API_KEY env and run ./manage.py fetch_gas_prices after deploy

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 @@ -485,7 +494,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(this.polygon.gasPrices.SafeGasPrice)).toString(), 'gwei' // using safe gas price
);

if (userMaticBalance.lt(gasFeeInWei)) {
Expand Down