-
Notifications
You must be signed in to change notification settings - Fork 17
/
estimateGas.js
31 lines (29 loc) · 1.11 KB
/
estimateGas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const fs = require('fs');
const path = require('path');
const Web3API = require('web3');
export default async () => {
const web3 = new Web3API(new Web3API.providers.HttpProvider('https://mainnet.infura.io'));
const tokenAddress = "0x..";
const fromAddress = "0x..";
const toAddress = "0x..";
// ABI to transfer ERC20 Token
let abi = JSON.parse(fs.readFileSync(path.join(__dirname, '.', 'main_contract.json'), 'utf-8'));
// Get ERC20 Token contract instance
let contract = new web3.eth.Contract(abi, tokenAddress, {
from: fromAddress
});
// calculate ERC20 token amount
let amount = 1;
let tokenAmount = web3.utils.toWei(amount.toString(), 'ether')
// Will call estimate the gas a method execution will take when executed in the EVM without.
let estimateGas = await web3.eth.estimateGas({
"value": '0x0', // Only tokens
"data": contract.methods.transfer(toAddress, tokenAmount).encodeABI(),
"from": fromAddress,
"to": toAddress
});
console.log({
tokenAmount: tokenAmount,
estimateGas: estimateGas * 1.10
});
}