Skip to content

Commit

Permalink
Merge pull request #102 from OriginTrail/bugfix/tx-retry
Browse files Browse the repository at this point in the history
Changed logic for increasing gas price, while retrying tx
  • Loading branch information
u-hubar authored Sep 7, 2023
2 parents 4ea85ad + dda4a16 commit 0632717
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
4 changes: 2 additions & 2 deletions 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dkg.js",
"version": "6.0.10",
"version": "6.0.11",
"description": "Javascript library for interaction with the OriginTrail Decentralized Knowledge Graph",
"main": "index.js",
"scripts": {
Expand Down
35 changes: 19 additions & 16 deletions services/blockchain-service/blockchain-service-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,39 @@ class BlockchainServiceBase {
return;

Check failure on line 49 in services/blockchain-service/blockchain-service-base.js

View workflow job for this annotation

GitHub Actions / build

Unnecessary return statement
}

async getNetworkGasPrice(blockchain) {
const web3Instance = await this.getWeb3Instance(blockchain);

try {
return await web3Instance.eth.getGasPrice();
} catch (error) {
console.warn(`Failed to fetch the gas price from the network: ${error}. Using default value: 100 Gwei.`);
return Web3.utils.toWei('100', 'Gwei');
}
}

async callContractFunction(contractName, functionName, args, blockchain) {
const contractInstance = await this.getContractInstance(contractName, blockchain);
return contractInstance.methods[functionName](...args).call();
}

async prepareTransaction(contractInstance, functionName, args, blockchain) {
const publicKey = await this.getPublicKey(blockchain);
const web3Instance = await this.getWeb3Instance(blockchain);
const gasLimit = await contractInstance.methods[functionName](...args).estimateGas({
from: blockchain.publicKey,
});

const encodedABI = await contractInstance.methods[functionName](...args).encodeABI();

let gasPrice;
if (blockchain.gasPrice === undefined) {
gasPrice = await web3Instance.eth.getGasPrice();
}

// Gas price increase for handling `Transaction not mined` error
if (blockchain.retryTx && gasPrice <= blockchain.gasPrice) {
gasPrice = Number(blockchain.gasPrice) + 1;
} else if (blockchain.gasPrice) {
// Gas price provided by developers
gasPrice = blockchain.gasPrice;
}
let gasPrice = Number(
blockchain.previousTxGasPrice ||
blockchain.gasPrice ||
await this.getNetworkGasPrice(blockchain)
);

// Fallback
if (!gasPrice) {
gasPrice = Web3.utils.toWei('100', 'Gwei');
if (blockchain.retryTx) {
// Increase gas price by 20%
gasPrice *= 1.2;
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class NodeBlockchainService extends BlockchainServiceBase {
async executeContractFunction(contractName, functionName, args, blockchain) {
const web3Instance = await this.getWeb3Instance(blockchain);
let result;
let gasPrice;
let previousTxGasPrice;
let transactionRetried = false;

while (result === undefined) {
Expand All @@ -68,7 +68,7 @@ class NodeBlockchainService extends BlockchainServiceBase {
args,
blockchain,
);
gasPrice = tx.gasPrice;
previousTxGasPrice = tx.gasPrice;
// eslint-disable-next-line no-await-in-loop
const createdTransaction = await web3Instance.eth.accounts.signTransaction(
tx,
Expand All @@ -92,7 +92,7 @@ class NodeBlockchainService extends BlockchainServiceBase {
// eslint-disable-next-line no-param-reassign
blockchain.retryTx = true;
// eslint-disable-next-line no-param-reassign
blockchain.gasPrice = gasPrice;
blockchain.previousTxGasPrice = previousTxGasPrice;
} else {
throw e;
}
Expand Down

0 comments on commit 0632717

Please sign in to comment.