Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
fix: updated fetchEvmGasPrice fn (#69)
Browse files Browse the repository at this point in the history
* fix: updated fetchEvmGasPrice fn

* fix: renamed to fetchTipPrice

* fix: lint
  • Loading branch information
impelcrypto authored Dec 8, 2023
1 parent 5cf0ec0 commit 157b92b
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ Here is the list of functions and their brief explanations:
39. `getEvmGas`: Retrieves the gas price for Ethereum Virtual Machine (EVM) transactions.
40. `getEvmGasCost`: Calculates the cost of gas for a transaction.
41. `formatTip`: Converts the provided fee into ether format.
42. `fetchEvmGasPrice`: Fetches gas price from an API and formats it accordingly.
42. `fetchTipPrice`: Fetches tip price from an API and formats it accordingly.
43. `getUsdBySymbol`: Fetches the price of a token in USD.
44. `calUsdAmount`: Fetches the USD price for a given token symbol and multiplies it by an amount.
45. `mergeTvlArray`: Merges the TVL (Total Value Locked) array based on the base type.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"url": "https://github.com/AstarNetwork/astar.js.git"
},
"sideEffects": false,
"version": "0.2.7",
"version": "0.2.8",
"workspaces": [
"packages/*",
"examples/*"
Expand Down
2 changes: 1 addition & 1 deletion packages/api-derive/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@astar-network/astar-api-derive",
"version": "0.2.7",
"version": "0.2.8",
"description": "Astar JS API",
"main": "index.js",
"author": "Astar Network <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@astar-network/astar-api",
"version": "0.2.7",
"version": "0.2.8",
"description": "Astar JS API",
"main": "index.js",
"author": "Astar Network <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@astar-network/astar-sdk-core",
"version": "0.2.7",
"version": "0.2.8",
"description": "the core for astar SDK",
"main": "index.js",
"author": "Astar Network <[email protected]>",
Expand Down
4 changes: 4 additions & 0 deletions packages/sdk-core/src/modules/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export const objToArray = (obj: any): any[] => {
return array;
};

export const hasProperty = (obj: object, key: string): boolean => {
return !!obj && Object.prototype.hasOwnProperty.call(obj, key);
};

export const checkIsNullOrUndefined = (value: any): boolean => {
return value === null || value === undefined;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Perbill } from '@polkadot/types/interfaces';
import { ApiPromise } from '@polkadot/api';
import { ethers } from 'ethers';
import { Codec } from '@polkadot/types/types';
import { truncate } from '@astar-network/astar-sdk-core';
import { hasProperty, truncate } from '@astar-network/astar-sdk-core';

export interface RewardDistributionConfig extends Struct {
readonly baseTreasuryPercent: Perbill;
Expand Down Expand Up @@ -210,7 +210,7 @@ export const estimatePendingRewards = async ({ api,
api.query.system.number(),
Number(api.consts.dappsStaking.blockPerEra),
String(
Object.prototype.hasOwnProperty.call(api.consts.blockReward, 'maxBlockRewardAmount')
hasProperty(api.consts.blockReward, 'maxBlockRewardAmount')
? api.consts.blockReward.maxBlockRewardAmount
: api.consts.blockReward.rewardAmount
),
Expand Down
53 changes: 5 additions & 48 deletions packages/sdk-core/src/modules/gas-api/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,76 +62,33 @@ export const formatTip = (fee: string): string => {
return price;
};

export const fetchEvmGasPrice = async ({ network,
isEip1559,
web3 }: {
network: string;
isEip1559: boolean;
web3: Web3;
}): Promise<{ evmGasPrice: GasPrice; nativeTipPrice: GasPrice }> => {
export const fetchTipPrice = async ({ network }: { network: string }): Promise<{ nativeTipPrice: GasPrice }> => {
try {
const url = `${GAS_API_URL}/gasnow?network=${network}`;
const { data } = await axios.get<ApiGasNow>(url);
if (!data || data.code !== 200) {
throw Error('something went wrong');
}
const { tip } = data.data;
const { priorityFeePerGas } = data.data.eip1559;
const nativeTipPrice = {
slow: formatTip(tip.slow),
average: formatTip(tip.average),
fast: formatTip(tip.fast)
};

if (isEip1559) {
const evmGasPrice = {
slow: priorityFeePerGas.slow,
average: priorityFeePerGas.average,
fast: priorityFeePerGas.fast,
baseFeePerGas: data.data.eip1559.baseFeePerGas
};
return {
evmGasPrice,
nativeTipPrice
};
} else {
const { slow, average, fast } = data.data;
const evmGasPrice = {
slow,
average,
fast,
baseFeePerGas: '0'
};
return {
evmGasPrice,
nativeTipPrice
};
}
return {
nativeTipPrice
};
} catch (error) {
if (network.toLowerCase() !== astarChain.DEVELOPMENT.toLowerCase()) {
console.error(error);
}
const fallbackGasPrice = Number(
await web3.eth.getGasPrice().catch(() => {
const oneGwei = '1000000000';
return oneGwei;
})
);

const slow = fallbackGasPrice;
const average = Math.floor(fallbackGasPrice * 9);
const fast = Math.floor(fallbackGasPrice * 56);
const evmGasPrice = {
slow: String(slow),
average: String(average),
fast: String(fast),
baseFeePerGas: '0'
};
const nativeTipPrice = {
slow: formatTip('10000000000000'),
average: formatTip('50000000000000'),
fast: formatTip('5000000000000000')
};
return { evmGasPrice, nativeTipPrice };
return { nativeTipPrice };
}
};
2 changes: 1 addition & 1 deletion packages/type-definitions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@astar-network/astar-type-definitions",
"version": "0.2.7",
"version": "0.2.8",
"description": "Polkadot.js type definitions for astar Network",
"main": "index.js",
"author": "Astar Network <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@astar-network/astar-types",
"version": "0.2.7",
"version": "0.2.8",
"description": "Polkadot.js type definitions for Astar Network",
"main": "index.js",
"author": "Astar Network <[email protected]>",
Expand Down

0 comments on commit 157b92b

Please sign in to comment.