Synthetixio / synthetix-js

[deprecated] Please use https://github.com/Synthetixio/js-monorepo/tree/master/packages/contracts-interface
63 stars 44 forks source link

have an easy way to inject a custom provider #38

Open oleiba opened 4 years ago

oleiba commented 4 years ago

Currently, it seems there's no straight forward way to inject a provider when initializing SynthetixJS.

One can inject a signer, which can accept a provider in its constructor, but in order for the provider to implement the needed interface - it seems we must use either of ethers' provider classes. The problem is that the only field exported by SynthetixJS from ethers is utils. I suggest to also export providers from ethers, so one can easily inject its own custom provider:

const provider = new SynthetixJs.providers.InfuraProvider('ropsten', projectId);
const signer = new SynthetixJs.signers.PrivateKey(
    provider,
    networkId,  // Ropsten
    privateKey
);
const snxjs = new SynthetixJs({ networkId, signer });
jjgonecrypto commented 4 years ago

Actually the provider should additionally be passed as a property itself in the constructor argument:

const snxjs = new SynthetixJs({ networkId, provider, signer })

See here: https://github.com/Synthetixio/synthetix-js/blob/master/src/contractSettings.js#L22

But your point still stands - we can export the providers from Infura to make them more useful. This is especially important after the change from Infura that no longer allows access to archive nodes by default - see https://github.com/ethers-io/ethers.js/issues/715.

Thanks for filing, I'll keep this open until we can prioritize and address it. cc @clementbalestrat