Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature add gas estimation type 2 #31

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@
### 1.0.8 (2023-11-30)

- Updated gasFee() method for type 1 transactions and added its test.
- Changed license to MIT.
- Changed license to MIT.

### 1.0.9 (2023-12-12)

- Added gas estimation for EIP-1559 transaction on arbitrum.
52 changes: 50 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-arbitrum-controller",
"version": "1.0.8",
"version": "1.0.9",
"description": "arbitrum controller for safle vault",
"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 All @@ -43,6 +45,7 @@
"nyc": "^15.0.0"
},
"dependencies": {
"axios": "^1.6.2",
"bip39": "^3.0.4",
"browser-passworder": "^2.0.3",
"crypto-js": "^4.1.1",
Expand Down
54 changes: 40 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const { normalize: normalizeAddress } = require('eth-sig-util')
const SimpleKeyring = require('eth-simple-keyring')
const HdKeyring = require('eth-hd-keyring')

const axios = require('axios')
let chainId;

const keyringTypes = [
SimpleKeyring,
HdKeyring,
Expand Down Expand Up @@ -230,7 +233,7 @@ class KeyringController extends EventEmitter {

importWallet(_privateKey) {
try {

if (_privateKey.startsWith('0x')) {
_privateKey = _privateKey.slice(2)
}
Expand Down Expand Up @@ -505,23 +508,46 @@ class KeyringController extends EventEmitter {
return { transactionDetails: receipt.transactionHash }
}

async getFees(arbitrumTx, web3) {
const { from, to, value, data, manualLimit } = arbitrumTx
const gasLimit = manualLimit ? manualLimit : await web3.eth.estimateGas({ to, from, value, data })
const gasPrice = parseInt(await web3.eth.getGasPrice());
const fees = {
"slow":{
"gasPrice": parseInt(gasPrice)
/**
* get Fees method to get the fees for Optimism Chain
*
* @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 } = rawTx
const gasLimit = await web3.eth.estimateGas({ to, from, value, data });
chainId = await web3.eth.getChainId();

const response = await axios({
url: `https://gas-api.metaswap.codefi.network/networks/42161/suggestedGasFees`,
method: 'GET',
});

let fees = {
slow: {
maxPriorityFeePerGas: parseInt(parseFloat(response.data.low.suggestedMaxPriorityFeePerGas) * Math.pow(10, 9)),
maxFeePerGas: parseInt(parseFloat(response.data.low.suggestedMaxFeePerGas) * Math.pow(10, 9)),

},
"standard":{
"gasPrice": gasPrice + parseInt(gasPrice * 0.05)
medium: {
maxPriorityFeePerGas: parseInt(parseFloat(response.data.medium.suggestedMaxPriorityFeePerGas) * Math.pow(10, 9)),
maxFeePerGas: parseInt(parseFloat(response.data.medium.suggestedMaxFeePerGas) * Math.pow(10, 9)),

},
"fast":{
"gasPrice": gasPrice + parseInt(gasPrice * 0.1)
fast: {
maxPriorityFeePerGas: parseInt(parseFloat(response.data.high.suggestedMaxPriorityFeePerGas) * Math.pow(10, 9)),
maxFeePerGas: parseInt(parseFloat(response.data.high.suggestedMaxFeePerGas) * Math.pow(10, 9)),

},
baseFee: 0
baseFee: parseInt(parseFloat(response.data.estimatedBaseFee) * Math.pow(10, 9)),
};

return {
gasLimit: gasLimit,
fees: fees
}
return { gasLimit: gasLimit, fees: fees}
}
}

Expand Down
42 changes: 0 additions & 42 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,46 +121,4 @@ describe('Initialize wallet ', () => {
const getEstimate = await arbitrumKeyring.getFees(tx, web3)
console.log(" get gas estimate ", getEstimate)
})

it("sign Transaction ", async () => {

const accounts = await arbitrumKeyring.getAccounts()
const from = accounts[0]
const web3 = new Web3(TESTNET.URL);

const count = await web3.eth.getTransactionCount(from);

const defaultNonce = await web3.utils.toHex(count);

const rawTx = {
to: '0x9E1447ea3F6abA7a5D344B360B95Fd9BAE049448',
from,
value: web3.utils.numberToHex(web3.utils.toWei('0.01', 'ether')),
gasLimit: web3.utils.numberToHex(25000),
maxPriorityFeePerGas: web3.utils.numberToHex(web3.utils.toWei('55', 'gwei')),
maxFeePerGas: web3.utils.numberToHex(web3.utils.toWei('56', 'gwei')),
nonce: defaultNonce,
data: '0x00',
type: '0x2',
chainId: 421613,
};
/*
const rawTx2 = {
type: 2,
nonce: 0,
to: "0xacde0f575d8caf7bdba417326797c1a1d1b21f88", // Address to send to
maxPriorityFeePerGas: 10000, // Recommended maxPriorityFeePerGas
maxFeePerGas: 10000, // Recommended maxFeePerGas
value: web3.utils.toHex(1000000), // .01 ETH
gasLimit: "21000", // basic transaction costs exactly 21000
chainId: 421613,
};
*/
const privateKey = await arbitrumKeyring.exportAccount(accounts[0])
const signedTX = await arbitrumKeyring.signTransaction(rawTx, privateKey)
console.log("signedTX ", signedTX)

// const sentTX = await arbitrumKeyring.sendTransaction(signedTX, web3)
// console.log("sentTX ", sentTX)
})
})
Loading