Skip to content

Commit

Permalink
fix passing in ethers provider or web3 provider
Browse files Browse the repository at this point in the history
  • Loading branch information
joshstevens19 committed Jul 1, 2021
1 parent 1b65d64 commit a382d68
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export enum ErrorCodes {
canNotFindProviderUrl = 16,
wrongEthersProviderContext = 17,
chainIdNotSupported = 18,
chainIdCanNotBeFound = 19,
}
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-uniswap-sdk",
"version": "2.4.0",
"version": "2.4.1",
"description": "Simple easy to understand SDK for uniswap which looks over best v2 and v3 to find you the best swap quote",
"main": "dist/index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/common/errors/error-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export enum ErrorCodes {
canNotFindProviderUrl = 16,
wrongEthersProviderContext = 17,
chainIdNotSupported = 18,
chainIdCanNotBeFound = 19,
}
2 changes: 1 addition & 1 deletion src/ethers-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('EthersProvider', () => {
});
}).toThrowError(
new UniswapError(
'Can not find chain name for 10293',
'Can not find chain name for 10293. This lib only supports mainnet(1), ropsten(4), kovan(42), rinkeby(4), görli(5) and ropsten(3)',
ErrorCodes.canNotFindChainId
)
);
Expand Down
38 changes: 35 additions & 3 deletions src/ethers-provider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BigNumber from 'bignumber.js';
import { Contract, ContractInterface, providers } from 'ethers';
import { ErrorCodes } from './common/errors/error-codes';
import { UniswapError } from './common/errors/uniswap-error';
Expand All @@ -13,7 +14,11 @@ export interface EthereumProvider {
}

export class EthersProvider {
private _ethersProvider: providers.BaseProvider;
private _ethersProvider:
| providers.StaticJsonRpcProvider
| providers.JsonRpcProvider
| providers.InfuraProvider
| providers.Web3Provider;
constructor(private _providerContext: ChainIdAndProvider | EthereumProvider) {
const chainId = (<ChainIdAndProvider>this._providerContext).chainId;
if (chainId) {
Expand Down Expand Up @@ -51,7 +56,11 @@ export class EthersProvider {
);
}

this._ethersProvider = new providers.Web3Provider(ethereumProvider);
if (ethereumProvider._isProvider) {
this._ethersProvider = ethereumProvider;
} else {
this._ethersProvider = new providers.Web3Provider(ethereumProvider);
}
}
}

Expand All @@ -73,7 +82,30 @@ export class EthersProvider {
* Get the network
*/
public network(): providers.Network {
return this._ethersProvider.network;
if (this._ethersProvider.network) {
return this._ethersProvider.network;
}

// @ts-ignore
if (this._ethersProvider.provider) {
// @ts-ignore
const chainId = this._ethersProvider.provider.chainId;
if (chainId) {
const chainIdNumber = new BigNumber(chainId).toNumber();
const chainName = ChainNames.get(chainIdNumber);
if (chainName) {
return {
chainId: chainIdNumber,
name: chainName,
};
}
}
}

throw new UniswapError(
'chainId can not be found on the provider',
ErrorCodes.chainIdCanNotBeFound
);
}

/**
Expand Down

0 comments on commit a382d68

Please sign in to comment.