Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Cache chain ID to avoid extra network requests
Browse files Browse the repository at this point in the history
  • Loading branch information
haltman-at committed Mar 24, 2021
1 parent e6f61f1 commit b626080
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/contract/lib/contract/constructorMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ module.exports = Contract => ({
// save properties
this.currentProvider = provider;
this.networkType = networkType;

//invalidate cached chain ID
this._chainId = undefined;
},

setProvider(provider) {
Expand Down
13 changes: 11 additions & 2 deletions packages/contract/lib/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,17 @@ const execute = {
sendTransaction: async function (web3, params, promiEvent, context) {
// get the chainId to be compliant with EIP-155
// Geth wants this value in base 16 prefixed by "0x"
const chainId = await web3.eth.net.getId();
params.chainId = `0x${chainId.toString(16)}`;
let chainId;
if (context.contract._chainId) {
chainId = context.contract._chainId;
} else {
chainId = await web3.eth.net.getId();
chainId = `0x${chainId.toString(16)}`;
//cache it in a field on the contract
//(I'd do this in setProvider instead of here, but that's not async)
context.contract._chainId = chainId;
}
params.chainId = chainId;

//if we don't need the debugger, let's not risk any errors on our part,
//and just have web3 do everything
Expand Down

0 comments on commit b626080

Please sign in to comment.