Skip to content

Commit

Permalink
dded gas limit and gas estimation in getFees()
Browse files Browse the repository at this point in the history
  • Loading branch information
SDargarh committed Nov 30, 2023
1 parent 1a822b6 commit f517340
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 41 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
.nyc_output
jscoverage
jscoverage
test.js
.vscode/launch.json
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@
- Added coverage report for the test cases
- Added nyc dependency for the coverage report
- Updated node version
- Added dev and test branches in CI
- Added dev and test branches in CI

### 1.2.8 (2023-11-30)

- Added gas limit and gas estimation in getFees()
84 changes: 82 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@getsafle/vault-polygon-controller",
"version": "1.2.7",
"version": "1.2.8",
"description": "",
"engines": {
"node": ">= 10"
Expand All @@ -17,7 +17,9 @@
"cover:reporthtml": "nyc report --reporter=html --report-dir='./jscoverage'"
},
"nyc": {
"exclude": ["test/**.js"]
"exclude": [
"test/**.js"
]
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -45,6 +47,7 @@
"dependencies": {
"@ethereumjs/common": "^2.6.0",
"@ethereumjs/tx": "^3.4.0",
"axios": "^1.6.2",
"bip39": "^3.0.4",
"browser-passworder": "^2.0.3",
"crypto-js": "^4.1.1",
Expand Down
3 changes: 3 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
GAS_FEE_API: 'https://gasstation.polygon.technology/v2/'
}
58 changes: 47 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const { normalize: normalizeAddress } = require('eth-sig-util')

const SimpleKeyring = require('eth-simple-keyring')
const HdKeyring = require('eth-hd-keyring')
const axios = require("axios");
const { GAS_FEE_API } = require('./constants/index')

const keyringTypes = [
SimpleKeyring,
Expand Down Expand Up @@ -511,18 +513,52 @@ class KeyringController extends EventEmitter {
return { transactionDetails: receipt.transactionHash }
}

async getFees(rawTx, web3) {
const { from, to, value, data, gasLimit, maxFeePerGas } = rawTx
const estimate = gasLimit ? gasLimit : await web3.eth.estimateGas({ to, from, value, data });

const re = /[0-9A-Fa-f]{6}/g;

const maxFee = (re.test(maxFeePerGas)) ? parseInt(maxFeePerGas, 16) : maxFeePerGas;

const gas = (re.test(estimate)) ? parseInt(estimate, 16) : estimate

return { transactionFees: web3.utils.fromWei((gas * maxFee).toString(), 'ether') }
/**
* get Fees method to get the fees for reqiured chainId for avalanche
*
* returns the object having gasLimit and fees for the block
*
* @param {Object} rawTx - Rawtransaction - {from,to,value,data, chainId}
* @param {Object} web3 - web3 object.
* @returns {Object} - gasLimit for the transaction and fees for the transaction
*/
async getFees(rawTx, web3) {
const { from, to, value, data, chainId } = rawTx
const gasLimit = await web3.eth.estimateGas({ to, from, value, data });

let URL = GAS_FEE_API

const response = await axios({
url : `${URL}`,
method: 'GET',
});


let fees = {
"slow" : {
"maxPriorityFeePerGas" : parseInt(web3.utils.toWei(response.data.safeLow.maxPriorityFee.toString(), 'gwei')),
"maxFeePerGas" : parseInt(web3.utils.toWei(response.data.safeLow.maxFee.toString(), 'gwei')),

},
"standard" : {
"maxPriorityFeePerGas" : parseInt(web3.utils.toWei(response.data.standard.maxPriorityFee.toString(), 'gwei')),
"maxFeePerGas" : parseInt(web3.utils.toWei(response.data.standard.maxFee.toString(), 'gwei')),

},
"fast" : {
"maxPriorityFeePerGas" : parseInt(web3.utils.toWei(response.data.fast.maxPriorityFee.toString(), 'gwei')),
"maxFeePerGas" : parseInt(web3.utils.toWei(response.data.fast.maxFee.toString(), 'gwei')),

},
"baseFee" : parseInt(web3.utils.toWei(response.data.estimatedBaseFee.toString(), 'gwei')),
};

return {
gasLimit: gasLimit,
fees: fees
}
}

}

const getBalance = async (address, web3) => {
Expand Down
6 changes: 4 additions & 2 deletions test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module.exports = {
EXTERNAL_ACCOUNT_WRONG_PRIVATE_KEY_2: "0xbcb7a8680126610ca94440b020280f9ef829ad26637bfb5cc",
EXTERNAL_ACCOUNT_WRONG_PRIVATE_KEY_3: "QUWL7cmUp9Cj9DF3gLFqqSipopXyzuF4QXmDNV3ZTZ28GB6Ug98Z",

EXTERNAL_ACCOUNT_ADDRESS_TO_GET_FEE: "0x8dC847Af872947Ac18d5d63fA646EB65d4D99560",

TRANSACTION_TYPE: {
NATIVE_TRANSFER: "NATIVE_TRANSFER",
CONTRACT_TRANSACTION: "CONTRACT_TRANSACTION",
Expand All @@ -30,12 +32,12 @@ module.exports = {
MAINNET: {
NETWORK: "MAINNET",
CHAIN_ID: 137,
URL: 'https://matic-mainnet.chainstacklabs.com'
URL: 'https://polygon-rpc.com/'
},
TESTNET: {
NETWORK: "TESTNET",
CHAIN_ID: 80001,
URL: 'https://matic-mumbai.chainstacklabs.com'
URL: 'https://polygon-mumbai.infura.io/v3/'
}
}
}
25 changes: 3 additions & 22 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
EXTERNAL_ACCOUNT_WRONG_PRIVATE_KEY_1,
EXTERNAL_ACCOUNT_WRONG_PRIVATE_KEY_2,
EXTERNAL_ACCOUNT_WRONG_PRIVATE_KEY_3,
EXTERNAL_ACCOUNT_ADDRESS_TO_GET_FEE,
POLYGON_NETWORK: {
TESTNET,
MAINNET
Expand Down Expand Up @@ -102,37 +103,17 @@ describe('Initialize wallet ', () => {
const data = txData.encodeABI();

const tx = {
from: accounts[0],
from: EXTERNAL_ACCOUNT_ADDRESS_TO_GET_FEE,
to: POLYGON_CONTRACT,
value: POLYGON_AMOUNT_TO_CONTRACT,
data,
maxFeePerGas: 2500000002,
maxPriorityFeePerGas: 2500000000
chainID: 80001
}

const fees = await polygonKeyring.getFees(tx, web3)
console.log("fees ", fees)

const privateKey = await polygonKeyring.exportAccount(accounts[0])
const tx2 = await polygonKeyring.sign(tx, privateKey, web3)
console.log("tx2 ", tx2)
const tx3 = await polygonKeyring.sign(TESTING_MESSAGE_1, privateKey, web3)
console.log("tx3 ", tx3)


})

it("Get fees with manual gasLimit", async () => {
const web3 = new Web3(TESTNET.URL);
const tx = {
gasLimit: 2100,
maxFeePerGas: 2500000002,
maxPriorityFeePerGas: 2500000000
}
const fees = await polygonKeyring.getFees(tx, web3)
console.log(" with manual gasLimit ", fees)

})

it("Should import correct account ", async () => {
const address = await polygonKeyring.importWallet(EXTERNAL_ACCOUNT_PRIVATE_KEY)
Expand Down

0 comments on commit f517340

Please sign in to comment.