From c42505a3bf06ab5be3a07b6945b5ad34081a6119 Mon Sep 17 00:00:00 2001 From: yuriy Date: Tue, 20 Oct 2020 02:18:35 +0300 Subject: [PATCH] Get gas price from ethgasAPI --- src/blockchain-bridge/eth/EthMethods.ts | 13 ++++++++++--- src/blockchain-bridge/eth/EthMethodsERC20.ts | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/blockchain-bridge/eth/EthMethods.ts b/src/blockchain-bridge/eth/EthMethods.ts index 610131c9..fb028919 100644 --- a/src/blockchain-bridge/eth/EthMethods.ts +++ b/src/blockchain-bridge/eth/EthMethods.ts @@ -1,7 +1,8 @@ +import * as agent from 'superagent'; import { Contract } from 'web3-eth-contract'; import { getAddress } from '@harmony-js/crypto'; import Web3 from 'web3'; -import { mulDecimals } from '../../utils'; +import { divDecimals, mulDecimals } from '../../utils'; const BN = require('bn.js'); export interface IEthMethodsInitParams { @@ -28,12 +29,15 @@ export class EthMethods { // @ts-ignore const accounts = await ethereum.enable(); + const info = await agent.get('https://data-api.defipulse.com/api/v1/egs/api/ethgasAPI.json?api-key=babbb3d37bf13922c3c0cd578aa97b97116930b18461d1db2663059edeb1') + const gasPrice = mulDecimals(info.body.safeLow, 8); + return await this.ethTokenContract.methods .approve(this.ethManagerAddress, mulDecimals(amount, 18)) .send({ from: accounts[0], gas: process.env.ETH_GAS_LIMIT, - gasPrice: new BN(await this.web3.eth.getGasPrice()).mul(new BN(1)), + gasPrice, }) .on('transactionHash', hash => sendTxCallback(hash)); }; @@ -53,12 +57,15 @@ export class EthMethods { Number(process.env.ETH_GAS_LIMIT), ); + const info = await agent.get('https://data-api.defipulse.com/api/v1/egs/api/ethgasAPI.json?api-key=babbb3d37bf13922c3c0cd578aa97b97116930b18461d1db2663059edeb1') + const gasPrice = mulDecimals(info.body.safeLow, 8); + let transaction = await this.ethManagerContract.methods .lockToken(mulDecimals(amount, 18), hmyAddrHex) .send({ from: accounts[0], gas: new BN(gasLimit), - gasPrice: new BN(await this.web3.eth.getGasPrice()).mul(new BN(1)), + gasPrice, }) .on('transactionHash', hash => sendTxCallback(hash)); diff --git a/src/blockchain-bridge/eth/EthMethodsERC20.ts b/src/blockchain-bridge/eth/EthMethodsERC20.ts index d937272b..917c05e1 100644 --- a/src/blockchain-bridge/eth/EthMethodsERC20.ts +++ b/src/blockchain-bridge/eth/EthMethodsERC20.ts @@ -1,3 +1,4 @@ +import * as agent from 'superagent'; import { Contract } from 'web3-eth-contract'; import { getAddress } from '@harmony-js/crypto'; import Web3 from 'web3'; @@ -36,12 +37,17 @@ export class EthMethodsERC20 { erc20Address, ); + const info = await agent.get( + 'https://data-api.defipulse.com/api/v1/egs/api/ethgasAPI.json?api-key=babbb3d37bf13922c3c0cd578aa97b97116930b18461d1db2663059edeb1', + ); + const gasPrice = mulDecimals(info.body.safeLow, 8); + await erc20Contract.methods .approve(this.ethManagerAddress, mulDecimals(amount, decimals)) .send({ from: accounts[0], gas: process.env.ETH_GAS_LIMIT, - gasPrice: new BN(await this.web3.eth.getGasPrice()).mul(new BN(1)), + gasPrice, }) .on('transactionHash', hash => sendTxCallback(hash)); }; @@ -67,12 +73,17 @@ export class EthMethodsERC20 { Number(process.env.ETH_GAS_LIMIT), ); + const info = await agent.get( + 'https://data-api.defipulse.com/api/v1/egs/api/ethgasAPI.json?api-key=babbb3d37bf13922c3c0cd578aa97b97116930b18461d1db2663059edeb1', + ); + const gasPrice = mulDecimals(info.body.safeLow, 8); + let transaction = await this.ethManagerContract.methods .lockToken(erc20Address, mulDecimals(amount, decimals), hmyAddrHex) .send({ from: accounts[0], gas: new BN(gasLimit), - gasPrice: new BN(await this.web3.eth.getGasPrice()).mul(new BN(1)), + gasPrice, }) .on('transactionHash', hash => sendTxCallback(hash));