Skip to content

Commit

Permalink
Merge pull request #16 from getsafle/feature-calculate-tx-fees
Browse files Browse the repository at this point in the history
Function to get the tx fees
  • Loading branch information
apoorvq committed Apr 12, 2022
2 parents bf620af + 4d4fa42 commit 6bf5fb0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1 +1,3 @@
node_modules
node_modules
.vscode
test.js
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -39,3 +39,9 @@
##### Implement transaction broadcast functionality

- Added `sendTransaction()` function to send a signed transaction to the blockchain.

### 1.4.0 (2022-04-12)

##### Function to get the transaction fees

- Added `getFees()` function to get the transaction fees for a raw transaction object.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -84,3 +84,9 @@ const balance = await getBalance(address, web3);
```
const receipt = await ethController.sendTransaction(signedTx, web3);
```

### Calculate Tx Fees

```
const fees = await ethController.getFees(rawTx, web3);
```
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@getsafle/vault-eth-controller",
"version": "1.3.0",
"version": "1.4.0",
"description": "Ethereum controller for safle vault.",
"main": "src/index.js",
"scripts": {
Expand Down
13 changes: 13 additions & 0 deletions src/index.js
Expand Up @@ -532,6 +532,19 @@ class KeyringController extends EventEmitter {
const receipt = await web3.eth.sendSignedTransaction(signedTx);
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') }
}
}

const getBalance = async (address, web3) => {
Expand Down

0 comments on commit 6bf5fb0

Please sign in to comment.