Skip to content

Commit d3bb80f

Browse files
committed
EIP1559 in send transactions
Updated secretjs Cleaned up small stuff
1 parent 0ad9ee3 commit d3bb80f

17 files changed

+855
-438
lines changed

.env.development

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
ENV=TESTNET
1+
ENV=MAINNET
22
ETH_GAS_PRICE=100000000000
33
ETH_GAS_LIMIT=75000
44

55
SWAP_FEE=500000
66

7-
CHAIN_ID='supernova-1'
8-
CHAIN_NAME="supernova-1"
9-
SECRET_POST_ADDRESS="http://20.104.20.173/"
10-
SECRET_RPC='http://20.104.20.173:26657/'
7+
CHAIN_ID='secret-4'
8+
CHAIN_NAME="secret-4"
9+
SECRET_POST_ADDRESS="https://bridge-api-manager.azure-api.net/"
10+
SECRET_RPC='https://bridge-api-manager.azure-api.net/'
1111
# SECRET_WS='wss://bootstrap.secrettestnet.io:26667/websocket'
12-
SECRET_LCD='http://20.104.20.173:1317/'
12+
SECRET_LCD='https://bridge-api-manager.azure-api.net/'
13+
1314
BACKEND_URL='https://api-bridge-mainnet.azurewebsites.net'
1415
# PLSM_BACKEND_URL='https://bridge-plasm-backend-testnet.azurewebsites.net'
1516
BSC_BACKEND_URL='https://bridge-bsc-mainnet.azurewebsites.net'

.env.mainnet

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ ETH_GAS_LIMIT=75000
44

55
SWAP_FEE=500000
66

7-
CHAIN_ID='secret-3'
8-
CHAIN_NAME="secret-3"
7+
CHAIN_ID='secret-4'
8+
CHAIN_NAME="secret-4"
99
SECRET_POST_ADDRESS="https://bridge-api-manager.azure-api.net/"
1010
SECRET_RPC='https://bridge-api-manager.azure-api.net/'
1111
# SECRET_WS='wss://bootstrap.secrettestnet.io:26667/websocket'

package-lock.json

+694-355
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@
5252
"react-scrollbars-custom": "^4.0.21",
5353
"react-select": "^3.2.0",
5454
"react-switch": "^5.0.1",
55-
"secretjs": "0.17.4-beta1",
55+
"secretjs": "0.17.2",
5656
"semantic-ui-css": "^2.4.1",
5757
"semantic-ui-react": "^2.0.3",
5858
"style-it": "2.1.4",
5959
"superagent": "^6.0.0",
6060
"tslib": "^1.10.0",
6161
"uuid": "8.3.1",
62-
"web3": "1.2.11"
62+
"web3": "1.6.0"
6363
},
6464
"devDependencies": {
6565
"@types/classnames": "^2.2.9",

src/blockchain-bridge/eth/EthMethods.ts

+53-23
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import { Contract } from 'web3-eth-contract';
22
import Web3 from 'web3';
3-
import { ethToWei, getEIP1559Prices, getGasPrice, GWeiToWei } from './helpers';
4-
const BN = require('bn.js');
3+
import { EIP1559Gas, ethToWei, getEIP1559Prices } from './helpers';
4+
import { BigNumber } from 'bignumber.js';
55

66
export interface IEthMethodsInitParams {
77
web3: Web3;
88
ethManagerContract: Contract;
99
}
1010

1111
export class EthMethods {
12-
private web3: Web3;
12+
public web3: Web3;
1313
private ethManagerContract: Contract;
1414

1515
constructor(params: IEthMethodsInitParams) {
1616
this.web3 = params.web3;
1717
this.ethManagerContract = params.ethManagerContract;
1818
}
1919

20+
getGasPrice = async (): Promise<EIP1559Gas | BigNumber> => {
21+
return await getEIP1559Prices();
22+
};
23+
2024
swapEth = async (userAddr, amount, sendTxCallback?) => {
2125
// @ts-ignore
2226
const accounts = await ethereum.enable();
@@ -29,30 +33,56 @@ export class EthMethods {
2933
from: accounts[0],
3034
});
3135

32-
const gasLimit = Math.max(estimateGas + estimateGas * 0.3, Number(process.env.ETH_GAS_LIMIT));
36+
const gasLimit = Math.max(estimateGas + estimateGas * 0.2, Number(process.env.ETH_GAS_LIMIT));
3337
//let eip1559gas = await getEIP1559Prices();
3438

3539
//console.log(`${JSON.stringify(eip1559gas)}`);
3640

37-
this.ethManagerContract.methods
38-
.swap(secretAddrHex)
39-
.send({
40-
value: ethToWei(amount),
41-
from: accounts[0],
42-
gas: new BN(gasLimit),
43-
// maxFeePerGas: GWeiToWei(eip1559gas.maxFeePerGas),
44-
// maxPriorityFeePerGas: GWeiToWei(eip1559gas.maxPriorityFeePerGas),
45-
gasPrice: await getGasPrice(this.web3),
46-
})
47-
.on('transactionHash', function(hash) {
48-
sendTxCallback({ hash });
49-
})
50-
.then(function(receipt) {
51-
sendTxCallback({ receipt });
52-
})
53-
.catch(function(error) {
54-
sendTxCallback({ error });
55-
});
41+
const gasPrices = await this.getGasPrice();
42+
43+
if (gasPrices instanceof EIP1559Gas) {
44+
this.ethManagerContract.methods
45+
.swap(secretAddrHex)
46+
.send({
47+
value: ethToWei(amount),
48+
from: accounts[0],
49+
gas: gasLimit.toString(),
50+
//maxFeePerGas: GWeiToWei(eip1559gas.maxFeePerGas),
51+
//maxPriorityFeePerGas: GWeiToWei(eip1559gas.maxPriorityFeePerGas),
52+
// maxFeePerGas: gasPrices.maxFeePerGas.toString(),
53+
// maxPriorityFeePerGas: gasPrices.maxPriorityFeePerGas.toString(),
54+
//gasPrice: await getGasPrice(this.web3),
55+
})
56+
.on('transactionHash', function(hash) {
57+
sendTxCallback({ hash });
58+
})
59+
.then(function(receipt) {
60+
sendTxCallback({ receipt });
61+
})
62+
.catch(function(error) {
63+
sendTxCallback({ error });
64+
});
65+
} else {
66+
this.ethManagerContract.methods
67+
.swap(secretAddrHex)
68+
.send({
69+
value: ethToWei(amount),
70+
from: accounts[0],
71+
gas: gasLimit.toString(),
72+
// maxFeePerGas: GWeiToWei(eip1559gas.maxFeePerGas),
73+
// maxPriorityFeePerGas: GWeiToWei(eip1559gas.maxPriorityFeePerGas),
74+
gasPrice: gasPrices.toString(),
75+
})
76+
.on('transactionHash', function(hash) {
77+
sendTxCallback({ hash });
78+
})
79+
.then(function(receipt) {
80+
sendTxCallback({ receipt });
81+
})
82+
.catch(function(error) {
83+
sendTxCallback({ error });
84+
});
85+
}
5686
};
5787

5888
checkEthBalance = async addr => {

src/blockchain-bridge/eth/EthMethodsERC20.ts

+39-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Contract } from 'web3-eth-contract';
22
import Web3 from 'web3';
33
import { mulDecimals } from '../../utils';
4-
import { getEIP1559Prices, getGasPrice } from './helpers';
4+
import { EIP1559Gas, getEIP1559Prices, getGasPrice } from './helpers';
5+
import { BigNumber } from 'bignumber.js';
56

6-
const BN = require('bn.js');
77
const MAX_UINT = Web3.utils
88
.toBN(2)
99
.pow(Web3.utils.toBN(256))
@@ -16,7 +16,7 @@ export interface IEthMethodsInitParams {
1616
}
1717

1818
export class EthMethodsERC20 {
19-
private readonly web3: Web3;
19+
public readonly web3: Web3;
2020
private ethManagerContract: Contract;
2121
private ethManagerAddress: string;
2222

@@ -26,6 +26,10 @@ export class EthMethodsERC20 {
2626
this.ethManagerAddress = params.ethManagerAddress;
2727
}
2828

29+
getGasPrice = async (): Promise<EIP1559Gas | BigNumber> => {
30+
return await getEIP1559Prices();
31+
};
32+
2933
sendHandler = async (method: any, args: Object, callback: Function) => {
3034
method
3135
.send(args)
@@ -60,22 +64,40 @@ export class EthMethodsERC20 {
6064
amount = Number(mulDecimals(amount, decimals));
6165

6266
const allowance = await this.getAllowance(erc20Address);
67+
const gasLimit = await erc20Contract.methods
68+
.approve(this.ethManagerAddress, MAX_UINT)
69+
.estimateGas({ from: accounts[0] });
6370

64-
let eip1559gas = await getEIP1559Prices();
71+
const limit2 = (gasLimit * 1.2).toFixed(0);
6572

6673
if (Number(allowance) < Number(amount)) {
67-
this.sendHandler(
68-
erc20Contract.methods.approve(this.ethManagerAddress, MAX_UINT),
69-
{
70-
from: accounts[0],
71-
gas: process.env.ETH_GAS_LIMIT,
72-
gasPrice: await getGasPrice(this.web3),
73-
// maxFeePerGas: eip1559gas.maxFeePerGas,
74-
//maxPriorityFeePerGas: eip1559gas.maxPriorityFeePerGas,
75-
amount: amount,
76-
},
77-
callback,
78-
);
74+
let gasPrices = await this.getGasPrice();
75+
76+
if (gasPrices instanceof EIP1559Gas) {
77+
this.sendHandler(
78+
erc20Contract.methods.approve(this.ethManagerAddress, MAX_UINT),
79+
{
80+
from: accounts[0],
81+
gas: (gasLimit * 1.2).toFixed(0),
82+
//gasPrice: await getGasPrice(this.web3),
83+
// maxFeePerGas: gasPrices.maxFeePerGas.toString(),
84+
// maxPriorityFeePerGas: gasPrices.maxPriorityFeePerGas.toString(),
85+
},
86+
callback,
87+
);
88+
} else {
89+
this.sendHandler(
90+
erc20Contract.methods.approve(this.ethManagerAddress, MAX_UINT),
91+
{
92+
from: accounts[0],
93+
gas: (gasLimit * 1.2).toFixed(0),
94+
gasPrice: gasPrices.toString(),
95+
// maxFeePerGas: eip1559gas.maxFeePerGas,
96+
//maxPriorityFeePerGas: eip1559gas.maxPriorityFeePerGas,
97+
},
98+
callback,
99+
);
100+
}
79101
}
80102
};
81103

@@ -98,7 +120,7 @@ export class EthMethodsERC20 {
98120
this.ethManagerContract.methods.swapToken(secretAddrHex, mulDecimals(amount, decimals), erc20Address),
99121
{
100122
from: accounts[0],
101-
gas: new BN(gasLimit),
123+
gas: new BigNumber(gasLimit),
102124
gasPrice: await getGasPrice(this.web3),
103125
// maxFeePerGas: GWeiToWei(eip1559gas.maxFeePerGas),
104126
// maxPriorityFeePerGas: GWeiToWei(eip1559gas.maxPriorityFeePerGas),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { EIP1559Gas, getGasPrice } from '../helpers';
2+
import { EthMethods } from '../EthMethods';
3+
import { BigNumber } from 'bignumber.js';
4+
5+
export class BscMethods extends EthMethods {
6+
getGasPrice = async (): Promise<EIP1559Gas | BigNumber> => {
7+
return await getGasPrice(this.web3);
8+
};
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { EthMethodsERC20 } from '../EthMethodsERC20';
2+
import { EIP1559Gas, getGasPrice } from '../helpers';
3+
import { BigNumber } from 'bignumber.js';
4+
5+
export class BscMethodsERC20 extends EthMethodsERC20 {
6+
getGasPrice = async (): Promise<EIP1559Gas | BigNumber> => {
7+
return await getGasPrice(this.web3);
8+
};
9+
}

src/blockchain-bridge/eth/helpers.ts

+22-18
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { web3 } from './index';
44
import * as agent from 'superagent';
55
import { NETWORKS } from './networks';
66

7+
import { BigNumber } from 'bignumber.js';
78
const BN = require('bn.js');
89

9-
export const getGasPrice = async (web3: Web3) => {
10+
export const getGasPrice = async (web3: Web3): Promise<BigNumber> => {
1011
return new BN(await web3.eth.getGasPrice()).mul(new BN(1));
1112
};
1213

@@ -26,15 +27,15 @@ export const getBridgeGasPrice = async (web3: Web3) => {
2627
};
2728

2829
export const isGasCrazy = async (): Promise<boolean> => {
29-
try {
30-
const info = await agent.get(`https://ethgasstation.info/api/ethgasAPI.json`);
31-
32-
if (info.body.fastest > info.body.average * 2) {
33-
return true;
34-
}
35-
} catch (e) {
36-
console.error(`Error getting gas price: ${e}`);
37-
}
30+
// try {
31+
// const info = await agent.get(`https://ethgasstation.info/api/ethgasAPI.json`);
32+
//
33+
// if (info.body.fastest > info.body.average * 2) {
34+
// return true;
35+
// }
36+
// } catch (e) {
37+
// console.error(`Error getting gas price: ${e}`);
38+
// }
3839
return false;
3940
};
4041

@@ -62,20 +63,23 @@ export const ethToWei = (amount: string | number) => mulDecimals(amount, 18);
6263

6364
export const GWeiToWei = (amount: string | number) => mulDecimals(amount, 9);
6465

65-
interface EIP1559Gas {
66+
export class EIP1559Gas {
67+
constructor(fee: number | undefined, priority: number | undefined) {
68+
this.maxFeePerGas = fee;
69+
this.maxPriorityFeePerGas = priority;
70+
}
71+
6672
maxFeePerGas: number;
6773
maxPriorityFeePerGas: number;
6874
}
6975

7076
export const getEIP1559Prices = async (): Promise<EIP1559Gas> => {
7177
try {
72-
const info = await agent.get(`https://blocknative-api.herokuapp.com/data`);
73-
74-
return {
75-
maxFeePerGas: info.body.estimatedPrices[1].maxFeePerGas,
76-
maxPriorityFeePerGas: info.body.estimatedPrices[1].maxPriorityFeePerGas,
77-
};
78+
return new EIP1559Gas(undefined, undefined);
79+
// const info = await agent.get(`https://blocknative-api.herokuapp.com/data`);
80+
//
81+
// return new EIP1559Gas(info.body.estimatedPrices[1].maxFeePerGas, info.body.estimatedPrices[1].maxPriorityFeePerGas);
7882
} catch (e) {
79-
return { maxFeePerGas: undefined, maxPriorityFeePerGas: undefined };
83+
return new EIP1559Gas(undefined, undefined);
8084
}
8185
};

src/blockchain-bridge/eth/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { EthMethodsERC20 } from './EthMethodsERC20';
33
import { TOKEN } from '../../stores/interfaces';
44
import { EthMethodsSefi } from './EthMethodsSefi';
55
import { NETWORKS } from './networks';
6+
import { BscMethodsERC20 } from './bsc/BscMethodsERC20';
7+
import { BscMethods } from './bsc/BscMethods';
68

79
const Web3 = require('web3');
810

@@ -43,12 +45,12 @@ export const fromScrtMethods: Record<NETWORKS, Record<TOKEN, any>> = {
4345
[TOKEN.S20]: null,
4446
},
4547
[NETWORKS.BSC]: {
46-
[TOKEN.NATIVE]: new EthMethods({
48+
[TOKEN.NATIVE]: new BscMethods({
4749
web3: web3,
4850
ethManagerContract: bscManagerContract,
4951
}),
5052

51-
[TOKEN.ERC20]: new EthMethodsERC20({
53+
[TOKEN.ERC20]: new BscMethodsERC20({
5254
web3: web3,
5355
ethManagerContract: bscManagerContract,
5456
ethManagerAddress: process.env.BSC_MANAGER_CONTRACT,

src/blockchain-bridge/scrt/sefi.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CosmWasmClient, ExecuteResult, SigningCosmWasmClient } from 'secretjs';
1+
import { CosmWasmClient, ExecuteResult } from 'secretjs';
22
import { getScrtProof } from 'services';
33
import { AsyncSender } from './asyncSender';
44

src/blockchain-bridge/scrt/snip20.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { CosmWasmClient, ExecuteResult, SigningCosmWasmClient } from 'secretjs';
2-
import { divDecimals, unlockToken } from '../../utils';
1+
import { CosmWasmClient, ExecuteResult } from 'secretjs';
2+
import { unlockToken } from '../../utils';
33
import { StdFee } from 'secretjs/types/types';
44
import { AsyncSender } from './asyncSender';
55

src/pages/EthBridge/index.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ export const EthBridge = observer((props: any) => {
4141
}, []);
4242

4343
useEffect(() => {
44-
if (exchange.step === EXCHANGE_STEPS.CHECK_TRANSACTION && exchange.operation)
44+
if (exchange.step === EXCHANGE_STEPS.CHECK_TRANSACTION && exchange.operation) {
4545
exchange.fetchStatus(exchange.operation.id);
46+
}
4647
}, [exchange.step]);
4748

4849
// useEffect(() => {

0 commit comments

Comments
 (0)