Replies: 1 comment
-
When you pass a network address to ethers.getDefaultProvider, it gives you a JsonRpcProvider, both are the same thing. Also if you already have an ethers provider, but want a provider with a different network address, you have to create a new provider. This should be fine but if that causes a performance issue to you, you may have to use a lower level API through ethers.utils.fetchJson for making JSON RPC calls to a network address. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I was wondering what the best practice is in terms of using the
ethers
provider
as either a singleton (or persistent instance) in a module or to create a new instance every time a request is made. For example, for a provider that is listening for block events with something likeprovider.on('block', async (blockNumber) => {
it makes sense to use a single instance like the following:But let's say that our application also has an
express
endpoint that can query balances through something likeawait provider.getBalance(address);
or sign and send a transaction with a signer like:Is it easier/harder to have create a new instance of the provider for these operations with
const provider = new ethers.providers.JsonRpcProvider(NETWORK_ADDRESS);
or use theethers.getDefaultProvider(NETWORK_ADDRESS);
each time?It seems like that in situations where using you're performing event monitoring in the blockchain for blocks, transactions and contracts, the single persistent connection via one provider is the right answer. So, for the other types of queries should I be creating
new
instance of a provider each time? Or is there some magic inside the provider that is occurring where what I'm describing doesn't matter and everything can be done through one provider regardless?Also, does the
ethers.getDefaultProvider(NETWORK_ADDRESS)
function return anew
instance provider each time it is called or a shared instance? e.g.Beta Was this translation helpful? Give feedback.
All reactions