Skip to content

Commit

Permalink
Add fixes to support web3.js 4x version
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Aug 7, 2023
1 parent a7466f5 commit d1d0894
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/prover/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
"@types/http-proxy": "^1.17.10",
"@types/yargs": "^17.0.24",
"axios": "^1.3.4",
"ethers": "^6.4.2",
"web3": "^1.10.0"
"ethers": "^6.7.0",
"web3": "^4.0.3"
},
"keywords": [
"ethereum",
Expand Down
12 changes: 11 additions & 1 deletion packages/prover/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export interface EIP1193Provider {
request: (payload: JsonRpcRequestOrBatch) => Promise<JsonRpcResponseOrBatch>;
}

export interface Web3jsProvider {
request: (payload: JsonRpcRequest) => Promise<JsonRpcResponse>;
}

// Some providers uses `request` instead of the `send`. e.g. Ganache
export interface RequestProvider {
request(
Expand All @@ -59,7 +63,13 @@ export interface SendAsyncProvider {
sendAsync(payload: JsonRpcRequestOrBatch): Promise<JsonRpcResponseOrBatch>;
}

export type Web3Provider = SendProvider | EthersProvider | SendAsyncProvider | RequestProvider | EIP1193Provider;
export type Web3Provider =
| SendProvider
| EthersProvider
| SendAsyncProvider
| RequestProvider
| EIP1193Provider
| Web3jsProvider;

export type ELVerifiedRequestHandlerOpts<Params = unknown[]> = {
payload: JsonRpcRequest<Params>;
Expand Down
20 changes: 19 additions & 1 deletion packages/prover/src/utils/assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ export function assertLightClient(client?: Lightclient): asserts client is Light
}
}

/**
* Checks if the provider is a web3.js version 4x.
*/
export function isWeb3jsProvider(provider: Web3Provider): provider is EIP1193Provider {
return (
"isWeb3Provider" in provider.constructor &&
(provider.constructor as {isWeb3Provider: (provider: Web3Provider) => boolean}).isWeb3Provider(provider)
);
}

export function isSendProvider(provider: Web3Provider): provider is SendProvider {
return (
!isWeb3jsProvider(provider) &&
"send" in provider &&
typeof provider.send === "function" &&
provider.send.length > 1 &&
Expand All @@ -25,6 +36,7 @@ export function isSendProvider(provider: Web3Provider): provider is SendProvider

export function isEthersProvider(provider: Web3Provider): provider is EthersProvider {
return (
!isWeb3jsProvider(provider) &&
"send" in provider &&
typeof provider.send === "function" &&
provider.send.length > 1 &&
Expand All @@ -33,7 +45,12 @@ export function isEthersProvider(provider: Web3Provider): provider is EthersProv
}

export function isRequestProvider(provider: Web3Provider): provider is RequestProvider {
return "request" in provider && typeof provider.request === "function" && provider.request.length > 1;
return (
!isWeb3jsProvider(provider) &&
"request" in provider &&
typeof provider.request === "function" &&
provider.request.length > 1
);
}

export function isSendAsyncProvider(provider: Web3Provider): provider is SendAsyncProvider {
Expand All @@ -46,6 +63,7 @@ export function isSendAsyncProvider(provider: Web3Provider): provider is SendAsy

export function isEIP1193Provider(provider: Web3Provider): provider is EIP1193Provider {
return (
!isWeb3jsProvider(provider) &&
"request" in provider &&
typeof provider.request === "function" &&
provider.request.constructor.name === "AsyncFunction"
Expand Down
33 changes: 24 additions & 9 deletions packages/prover/src/web3_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
isRequestProvider,
isSendAsyncProvider,
isSendProvider,
isWeb3jsProvider,
} from "./utils/assertion.js";
import {processAndVerifyRequest} from "./utils/process.js";
import {isBatchRequest} from "./utils/json_rpc.js";
Expand Down Expand Up @@ -184,21 +185,40 @@ function handleEthersProvider(

return {provider: Object.assign(provider, {send: newSend}), rpc};
}

/**
*
*
* @export
* @template T
* @param {T} provider
* @param {Logger} logger
* @return {*} {Web3ProviderTypeHandler<T>}
*/
export function getProviderTypeHandler<T extends Web3Provider>(
provider: T,
logger: Logger
): Web3ProviderTypeHandler<T> {
if (isSendProvider(provider)) {
logger.debug("Provider is recognized as legacy provider with 'send' method.");
return handleSendProvider as unknown as Web3ProviderTypeHandler<T>;
if (isWeb3jsProvider(provider)) {
logger.debug("Provider is recognized as 'web3.js' provider.");
// EIP-1193 provider is fully compatible with web3.js#4x provider interface
return handleEIP1193Provider as unknown as Web3ProviderTypeHandler<T>;
}

if (isEthersProvider(provider)) {
logger.debug("Provider is recognized as 'ethers' provider.");
return handleEthersProvider as unknown as Web3ProviderTypeHandler<T>;
}

if (isEIP1193Provider(provider)) {
logger.debug("Provider is recognized as 'EIP1193' provider.");
return handleEIP1193Provider as unknown as Web3ProviderTypeHandler<T>;
}

if (isSendProvider(provider)) {
logger.debug("Provider is recognized as legacy provider with 'send' method.");
return handleSendProvider as unknown as Web3ProviderTypeHandler<T>;
}

if (isRequestProvider(provider)) {
logger.debug("Provider is recognized as legacy provider with 'request' method.");
return handleRequestProvider as unknown as Web3ProviderTypeHandler<T>;
Expand All @@ -209,10 +229,5 @@ export function getProviderTypeHandler<T extends Web3Provider>(
return handleSendAsyncProvider as unknown as Web3ProviderTypeHandler<T>;
}

if (isEIP1193Provider(provider)) {
logger.debug("Provider is recognized as 'EIP1193' provider.");
return handleEIP1193Provider as unknown as Web3ProviderTypeHandler<T>;
}

throw new Error("Unsupported provider type");
}
35 changes: 30 additions & 5 deletions packages/prover/test/unit/utils/assertion.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {expect} from "chai";
import {ethers} from "ethers";
import Web3 from "web3";
import {isSendProvider} from "../../../src/utils/assertion.js";
import {isSendProvider, isWeb3jsProvider, isEthersProvider} from "../../../src/utils/assertion.js";
import {Web3Provider} from "../../../src/interfaces.js";

describe("utils/assertion", () => {
describe("isSendProvider", () => {
Expand All @@ -15,13 +16,37 @@ describe("utils/assertion", () => {
});

it("should return false for ethers provider", () => {
const provider = new ethers.JsonRpcProvider("");
const provider = new ethers.JsonRpcProvider("https://lodestar-sepoliarpc.chainsafe.io");
expect(isSendProvider(provider)).to.be.false;
});

it("should return true for web3 provider", () => {
const provider = new Web3.providers.HttpProvider("");
expect(isSendProvider(provider)).to.be.true;
it("should return false for web3 provider", () => {
const provider = new Web3.providers.HttpProvider("https://lodestar-sepoliarpc.chainsafe.io");
expect(isSendProvider(provider as Web3Provider)).to.be.false;
});
});

describe("isWeb3jsProvider", () => {
it("should return true if provider is web3.js provider", () => {
const provider = new Web3.providers.HttpProvider("https://lodestar-sepoliarpc.chainsafe.io");
expect(isWeb3jsProvider(provider as Web3Provider)).to.be.true;
});

it("should return false if provider is not web3.js provider", () => {
const provider = new ethers.JsonRpcProvider("https://lodestar-sepoliarpc.chainsafe.io");
expect(isWeb3jsProvider(provider as Web3Provider)).to.be.false;
});
});

describe("isEthersProvider", () => {
it("should return false if provider is not ethers provider", () => {
const provider = new Web3.providers.HttpProvider("https://lodestar-sepoliarpc.chainsafe.io");
expect(isEthersProvider(provider as Web3Provider)).to.be.false;
});

it("should return true if provider is ethers provider", () => {
const provider = new ethers.JsonRpcProvider("https://lodestar-sepoliarpc.chainsafe.io");
expect(isEthersProvider(provider as Web3Provider)).to.be.true;
});
});
});
4 changes: 2 additions & 2 deletions packages/prover/test/unit/web3_provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {expect} from "chai";
import Web3 from "web3";
import {ethers} from "ethers";
import sinon from "sinon";
import {createVerifiedExecutionProvider, ProofProvider, LCTransport} from "@lodestar/prover/browser";
import {createVerifiedExecutionProvider, ProofProvider, LCTransport, Web3Provider} from "@lodestar/prover/browser";
import {ELRpc} from "../../src/utils/rpc.js";

describe("web3_provider", () => {
Expand All @@ -19,7 +19,7 @@ describe("web3_provider", () => {
sandbox.stub(ELRpc.prototype, "verifyCompatibility").resolves();

const {provider, proofProvider} = createVerifiedExecutionProvider(
new Web3.providers.HttpProvider("https://lodestar-sepoliarpc.chainsafe.io"),
new Web3.providers.HttpProvider("https://lodestar-sepoliarpc.chainsafe.io") as Web3Provider,
{
transport: LCTransport.Rest,
urls: ["https://lodestar-sepolia.chainsafe.io"],
Expand Down

0 comments on commit d1d0894

Please sign in to comment.