Skip to content

Commit

Permalink
Added proper support for gas limit, changed Web3 HTTP provider to sup…
Browse files Browse the repository at this point in the history
…port keepalive
  • Loading branch information
talkol committed Jul 26, 2020
1 parent 2d87716 commit def22bc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export function getCurrentClockTime() {
return Math.round(new Date().getTime() / 1000);
}

export function toNumber(val: number | string) {
if (typeof val == 'string') {
return parseInt(val);
} else return val;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type JsonResponse = any;

Expand Down
20 changes: 17 additions & 3 deletions src/write/ethereum-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as Logger from '../logger';
import { State, EthereumTxStatus, GasPriceStrategy } from '../model/state';
import { getCurrentClockTime, jsonStringifyComplexTypes } from '../helpers';
import { getCurrentClockTime, jsonStringifyComplexTypes, toNumber } from '../helpers';
import { TransactionConfig } from 'web3-core';

const GAS_LIMIT_ESTIMATE_EXTRA = 100000;
const GAS_LIMIT_HARD_LIMIT = 2000000;

export type Type = 'ready-to-sync' | 'ready-for-committee';

Expand Down Expand Up @@ -50,14 +54,24 @@ export async function signAndSendTransaction(

const nonce = await state.web3.eth.getTransactionCount(senderAddress, 'latest'); // ignore pending pool

const txObject = {
const txObject: TransactionConfig = {
from: senderAddress,
to: contractAddress,
gasPrice: gasPrice,
gas: 10000000, // TODO: fix with real value
data: encodedAbi,
nonce: nonce,
};

let gasLimit = toNumber(await state.web3.eth.estimateGas(txObject));
if (gasLimit <= 0) throw new Error(`Cannot estimate gas for tx with data ${encodedAbi}`);
gasLimit += GAS_LIMIT_ESTIMATE_EXTRA;
if (gasLimit > GAS_LIMIT_HARD_LIMIT) {
throw new Error(`Gas limit estimate ${gasLimit} over hard limit ${GAS_LIMIT_HARD_LIMIT}`);
}
txObject.gas = gasLimit;

Logger.log(`About to sign and send tx object: ${jsonStringifyComplexTypes(txObject)}.`);

const { rawTransaction, transactionHash } = await state.signer.sign(txObject);
if (!rawTransaction || !transactionHash) {
throw new Error(`Could not sign tx object: ${jsonStringifyComplexTypes(txObject)}.`);
Expand Down
4 changes: 4 additions & 0 deletions src/write/ethereum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ function getMockWeb3Client(behavior: 'success' | 'badsend' | 'pending' | 'revert
if (behavior == 'overmax') return '999000000000';
return '40000000000';
},
estimateGas: async () => {
await sleep(0);
return 500000;
},
sendSignedTransaction: async () => {
await sleep(0);
if (behavior == 'badsend') throw new Error('send error');
Expand Down
6 changes: 5 additions & 1 deletion src/write/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import { compiledContracts } from '@orbs-network/orbs-ethereum-contracts-v2/rele

export function initWeb3Client(ethereumEndpoint: string, electionsContractAddress: string, state: State) {
// init web3
state.web3 = new Web3(ethereumEndpoint);
state.web3 = new Web3(
new Web3.providers.HttpProvider(ethereumEndpoint, {
keepAlive: true,
})
);
// TODO: state.web3.eth.transactionPollingTimeout = 0.01;
// TODO: do we need to disable web3 receipt polling explicitly?
// init contracts
Expand Down

0 comments on commit def22bc

Please sign in to comment.