diff --git a/package.json b/package.json index 3ef9165..8c8e1a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@maticnetwork/maticjs-ethers", - "version": "1.0.3", + "version": "1.1.0", "description": "ethers plugin for matic.js", "main": "dist/npm.export.js", "types": "dist/ts/index.d.ts", diff --git a/src/ethers/web3_client.ts b/src/ethers/web3_client.ts index d75a05b..93679d0 100644 --- a/src/ethers/web3_client.ts +++ b/src/ethers/web3_client.ts @@ -6,26 +6,28 @@ import { ethBlockToMaticBlock, ethReceiptToMaticReceipt, ethTxToMaticTx } from " type ETHER_PROVIDER = providers.JsonRpcProvider; type ETHER_SIGNER = providers.JsonRpcSigner; +type WEB3_PROVIDER = providers.Web3Provider; export class EtherWeb3Client extends BaseWeb3Client { name = 'ETHER'; provider: ETHER_PROVIDER; signer: ETHER_SIGNER; - constructor(provider: ETHER_PROVIDER | Wallet, logger) { + constructor(provider: ETHER_PROVIDER | Wallet | WEB3_PROVIDER, logger) { super(logger); - if ((provider as ETHER_PROVIDER)._isProvider) { - this.provider = provider as ETHER_PROVIDER; - this.signer = this.provider.getSigner(); - } - else { - this.signer = (provider as any); - this.provider = ((provider as Wallet).provider) as any; + + if (provider instanceof ethers.providers.Web3Provider) { + this.provider = provider; + this.signer = provider.getSigner(); + } else if (provider instanceof ethers.providers.JsonRpcProvider) { + this.provider = provider; + this.signer = provider as any; + } else { + this.signer = provider as any; + this.provider = provider.provider || provider as any; } } - - getBlock(blockHashOrBlockNumber) { return this.provider.getBlock(blockHashOrBlockNumber).then(block => { return block as any; @@ -54,7 +56,9 @@ export class EtherWeb3Client extends BaseWeb3Client { getChainId() { - return this.signer.getChainId(); + return this.provider.getNetwork().then(function (res) { + return res.chainId; + }); } getBalance(address) { @@ -93,7 +97,7 @@ export class EtherWeb3Client extends BaseWeb3Client { getTransactionReceipt(transactionHash: string) { return this.provider.getTransactionReceipt(transactionHash).then(result => { this.ensureTransactionNotNull_(result); - + return ethReceiptToMaticReceipt(result); }); } @@ -151,14 +155,14 @@ export class EtherWeb3Client extends BaseWeb3Client { hexToNumber(value) { return BigNumber.from(value).toNumber(); } - + hexToNumberString(value) { return BigNumber.from(value).toString(); } signTypedData(signer, typedData) { - const {domain, types, message: value} = typedData; - if(types.EIP712Domain) { + const { domain, types, message: value } = typedData; + if (types.EIP712Domain) { delete types.EIP712Domain; } return this.signer._signTypedData(domain, types, value);