Skip to content

Commit

Permalink
Get gas price from ethgasAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriy committed Oct 19, 2020
1 parent 0c22155 commit c42505a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/blockchain-bridge/eth/EthMethods.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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));
};
Expand All @@ -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));

Expand Down
15 changes: 13 additions & 2 deletions src/blockchain-bridge/eth/EthMethodsERC20.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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));
};
Expand All @@ -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));

Expand Down

0 comments on commit c42505a

Please sign in to comment.