Skip to content

Commit 6bf5fb0

Browse files
authored
Merge pull request #16 from getsafle/feature-calculate-tx-fees
Function to get the tx fees
2 parents bf620af + 4d4fa42 commit 6bf5fb0

6 files changed

Lines changed: 30 additions & 3 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
node_modules
1+
node_modules
2+
.vscode
3+
test.js

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@
3939
##### Implement transaction broadcast functionality
4040

4141
- Added `sendTransaction()` function to send a signed transaction to the blockchain.
42+
43+
### 1.4.0 (2022-04-12)
44+
45+
##### Function to get the transaction fees
46+
47+
- Added `getFees()` function to get the transaction fees for a raw transaction object.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,9 @@ const balance = await getBalance(address, web3);
8484
```
8585
const receipt = await ethController.sendTransaction(signedTx, web3);
8686
```
87+
88+
### Calculate Tx Fees
89+
90+
```
91+
const fees = await ethController.getFees(rawTx, web3);
92+
```

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@getsafle/vault-eth-controller",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "Ethereum controller for safle vault.",
55
"main": "src/index.js",
66
"scripts": {

src/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,19 @@ class KeyringController extends EventEmitter {
532532
const receipt = await web3.eth.sendSignedTransaction(signedTx);
533533
return { transactionDetails: receipt.transactionHash }
534534
}
535+
536+
async getFees(rawTx, web3) {
537+
const { from, to, value, data, gasLimit, maxFeePerGas } = rawTx
538+
const estimate = gasLimit ? gasLimit : await web3.eth.estimateGas({ to, from, value, data });
539+
540+
const re = /[0-9A-Fa-f]{6}/g;
541+
542+
const maxFee = (re.test(maxFeePerGas)) ? parseInt(maxFeePerGas, 16) : maxFeePerGas;
543+
544+
const gas = (re.test(estimate)) ? parseInt(estimate, 16) : estimate
545+
546+
return { transactionFees: web3.utils.fromWei((gas * maxFee).toString(), 'ether') }
547+
}
535548
}
536549

537550
const getBalance = async (address, web3) => {

0 commit comments

Comments
 (0)