Skip to content

Commit

Permalink
initial price estimate for approve & lock tx are too low #57
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriy committed Oct 20, 2020
1 parent bc00aa3 commit 0060424
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
12 changes: 11 additions & 1 deletion src/blockchain-bridge/eth/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as agent from 'superagent';
import Web3 from 'web3';
import { mulDecimals } from '../../utils';
import { divDecimals, mulDecimals } from '../../utils';
import { web3 } from './index';
const BN = require('bn.js');

export const getGasPrice = async (web3: Web3) => {
Expand All @@ -22,3 +23,12 @@ export const getGasPrice = async (web3: Web3) => {

return res;
};

export const getNetworkFee = async () => {
const gasPrice = await getGasPrice(web3);
const gasLimit = new BN(process.env.ETH_GAS_LIMIT);

const fee = gasLimit.mul(gasPrice).mul(new BN(2));

return Number(divDecimals(fee, 18));
};
14 changes: 9 additions & 5 deletions src/pages/Exchange/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,15 @@ export const Details = observer<{ showTotal?: boolean; children?: any }>(
style={{ borderTop: '1px solid #dedede' }}
>
<AssetRow label="Network Fee" value="">
<Price
value={exchange.networkFee}
isEth={exchange.mode === EXCHANGE_MODE.ETH_TO_ONE}
boxProps={{ pad: {} }}
/>
{!exchange.isFeeLoading ? (
<Price
value={exchange.networkFee}
isEth={exchange.mode === EXCHANGE_MODE.ETH_TO_ONE}
boxProps={{ pad: {} }}
/>
) : (
<Text>...loading</Text>
)}
</AssetRow>

{/*<AssetRow*/}
Expand Down
18 changes: 14 additions & 4 deletions src/stores/Exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as operationService from 'services';

import * as contract from '../blockchain-bridge';
import { sleep, uuid } from '../utils';
import { getNetworkFee } from '../blockchain-bridge/eth/helpers';

export enum EXCHANGE_STEPS {
GET_TOKEN_ADDRESS = 'GET_TOKEN_ADDRESS',
Expand All @@ -37,6 +38,7 @@ export class Exchange extends StoreConstructor {
@observable txHash = '';
@observable actionStatus: statusFetching = 'init';
@observable stepNumber = 0;
@observable isFeeLoading = false;

defaultTransaction = {
oneAddress: '',
Expand Down Expand Up @@ -71,9 +73,13 @@ export class Exchange extends StoreConstructor {
return this.stepsConfig[this.stepNumber];
}

@observable ethNetworkFee = 0;

@computed
get networkFee() {
return this.mode === EXCHANGE_MODE.ETH_TO_ONE ? 0.000845586 : 0.0134438;
return this.mode === EXCHANGE_MODE.ETH_TO_ONE
? this.ethNetworkFee
: 0.0134438;
}

stepsConfig: Array<IStepConfig> = [
Expand All @@ -82,14 +88,18 @@ export class Exchange extends StoreConstructor {
buttons: [
{
title: 'Continue',
onClick: () => {
onClick: async () => {
this.stepNumber = this.stepNumber + 1;
// this.transaction.oneAddress = this.stores.user.address;
this.transaction.erc20Address = this.stores.userMetamask.erc20Address;

switch (this.mode) {
case EXCHANGE_MODE.ETH_TO_ONE:
this.transaction.ethAddress = this.stores.userMetamask.ethAddress;

this.isFeeLoading = true;
this.ethNetworkFee = await getNetworkFee();
this.isFeeLoading = false;
break;
case EXCHANGE_MODE.ONE_TO_ETH:
this.transaction.oneAddress = this.stores.user.address;
Expand Down Expand Up @@ -261,8 +271,8 @@ export class Exchange extends StoreConstructor {

let ethMethods, hmyMethods;

if(!this.stores.user.address || !this.stores.userMetamask.ethAddress) {
await sleep(3000)
if (!this.stores.user.address || !this.stores.userMetamask.ethAddress) {
await sleep(3000);
}

if (this.operation.oneAddress !== this.stores.user.address) {
Expand Down

0 comments on commit 0060424

Please sign in to comment.