Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/wrapper-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ jobs:
with:
node-version: '${{ steps.nvm.outputs.NVMRC }}'

- name: Install dependencies of provider
run: yarn install --nonInteractive --frozen-lockfile --prefer-offline
working-directory: ./provider/implementations/js

- name: Build provider
run: yarn build
working-directory: ./provider/implementations/js

- name: Install dependencies
run: yarn install --nonInteractive --frozen-lockfile --prefer-offline
working-directory: ./wrapper
Expand Down
13 changes: 6 additions & 7 deletions provider/implementations/js/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@polywrap/ethereum-provider-js",
"description": "Ethereum Provider JS Plugin",
"version": "0.2.4",
"version": "0.3.0",
"license": "MIT",
"main": "build/index.js",
"files": [
Expand All @@ -18,19 +18,19 @@
"dependencies": {
"@ethersproject/address": "5.7.0",
"@ethersproject/providers": "5.7.0",
"@polywrap/core-js": "0.10.0-pre.10",
"@polywrap/plugin-js": "0.10.0-pre.10",
"@polywrap/core-js": "0.10.0-pre.12",
"@polywrap/plugin-js": "0.10.0-pre.12",
"ethers": "5.7.0"
},
"peerDependencies": {
"@polywrap/core-js": "0.10.x",
"@polywrap/plugin-js": "0.10.x"
},
"devDependencies": {
"polywrap": "0.10.0-pre.10",
"polywrap": "0.10.0-pre.12",
"@types/jest": "26.0.8",
"@polywrap/client-js": "0.10.0-pre.10",
"@polywrap/test-env-js": "0.10.0-pre.10",
"@polywrap/client-js": "0.10.0-pre.12",
"@polywrap/cli-js": "0.10.0-pre.12",
"eth-ens-namehash": "2.0.8",
"jest": "26.6.3",
"js-sha3": "0.8.0",
Expand All @@ -43,4 +43,3 @@
"access": "public"
}
}

3 changes: 3 additions & 0 deletions provider/implementations/js/polywrap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ project:
source:
module: ./src/index.ts
schema: ./src/schema.graphql
import_abis:
- uri: wrap://ens/wraps.eth:ethereum-provider@2.0.0
abi: ../../interface/src/schema.graphql
2 changes: 1 addition & 1 deletion provider/implementations/js/src/Connections.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Connection, EthereumProvider } from "./Connection";
import { IProvider_Connection as SchemaConnection } from "./wrap";
import { Connection as SchemaConnection } from "./wrap";

import { getNetwork } from "@ethersproject/providers";
import {KnownNetwork, KnownNetworkId} from "./networks";
Expand Down
54 changes: 28 additions & 26 deletions provider/implementations/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import {
Module,
manifest,
CoreClient,
IProvider_Module_Args_request as Args_request,
IProvider_Module_Args_signMessage as Args_signMessage,
IProvider_Module_Args_signTransaction as Args_signTransaction,
IProvider_Module_Args_address as Args_address,
IProvider_Module_Args_chainId as Args_chainId,
IProvider_Module_Args_waitForTransaction as Args_waitForTransaction,
IProvider_Connection as SchemaConnection
Args_request,
Args_signMessage,
Args_signTransaction,
Args_waitForTransaction,
Connection as SchemaConnection,
Args_signerAddress,
} from "./wrap";
import { PluginFactory, PluginPackage } from "@polywrap/plugin-js";
import { Connection } from "./Connection";
Expand Down Expand Up @@ -37,6 +36,12 @@ export class EthereumProviderPlugin extends Module<ProviderConfig> {
const params = JSON.parse(args?.params ?? "[]");
const provider = connection.getProvider();

// Optimizations, utilizing the cache within ethers
if (args.method === "eth_chainId") {
const network = await provider.getNetwork();
return JSON.stringify("0x" + network.chainId.toString(16));
}

try {
const req = await provider.send(args.method, params);
return JSON.stringify(req);
Expand All @@ -46,7 +51,7 @@ export class EthereumProviderPlugin extends Module<ProviderConfig> {
* Ethers-rs defines the type of EIP 1559 tx
* as 0x02, but metamask expects it as 0x2,
* hence, the need of this workaround. Related:
* https://github.com/foundry-rs/foundry/issues/3890.
* https://github.com/MetaMask/metamask-extension/issues/18076
*
* We check if the parameters comes as array, if the error
* contains 0x2 and if the type is 0x02, then we change it
Expand Down Expand Up @@ -83,6 +88,18 @@ export class EthereumProviderPlugin extends Module<ProviderConfig> {
return true;
}

public async signerAddress(
args: Args_signerAddress,
_client: CoreClient
): Promise<string | null> {
try {
const connection = await this._getConnection(args.connection);
return await connection.getSigner().getAddress();
} catch (_error) {
return null;
}
}

public async signMessage(
args: Args_signMessage,
_client: CoreClient
Expand All @@ -102,25 +119,10 @@ export class EthereumProviderPlugin extends Module<ProviderConfig> {
return ethers.utils.joinSignature(signedTx as { r: string; s: string; v: number | undefined });
}

public async address(
args: Args_address,
_client: CoreClient
): Promise<string> {
const connection = await this._getConnection(args.connection);
return await connection.getSigner().getAddress();
}

public async chainId(
args: Args_chainId,
_client: CoreClient
): Promise<string> {
const connection = await this._getConnection(args.connection);
const network = await connection.getProvider().getNetwork();
return network.chainId.toString();
}

private async _getConnection(connection?: SchemaConnection | null): Promise<Connection> {
return this._connections.getConnection(connection ?? this.env.connection);
return this._connections.getConnection(
connection ?? this.env.connection
);
}

private _parseTransaction(rlp: Uint8Array): ethers.providers.TransactionRequest {
Expand Down
9 changes: 2 additions & 7 deletions provider/implementations/js/src/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
#import { Module, Connection } into IProvider from "wrap://ens/wraps.eth:ethereum-provider@1.1.0"

type Env {
connection: IProvider_Connection
}

type Module implements IProvider {}
#import * from "wrap://ens/wraps.eth:ethereum-provider@2.0.0"
#import { Env } from "wrap://ens/wraps.eth:ethereum-provider@2.0.0"
20 changes: 11 additions & 9 deletions provider/implementations/js/tests/connection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Connection } from "../src";
import { initTestEnvironment, stopTestEnvironment, providers } from "@polywrap/test-env-js";
import { Commands } from "@polywrap/cli-js";
import { ETH_ENS_IPFS_MODULE_CONSTANTS } from "polywrap";
import { Wallet } from "ethers";

jest.setTimeout(600000);
Expand All @@ -11,21 +12,22 @@ const getRpcUri = (network: BasicNetwork): string => {
}

describe("Connection", () => {
const ethProvider = ETH_ENS_IPFS_MODULE_CONSTANTS.ethereumProvider;
const signerAddress = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1";
let testnet: Connection;

beforeAll(async () => {
await initTestEnvironment();
await Commands.infra("up", { modules: ["eth-ens-ipfs"] });
testnet = new Connection({
provider: providers.ethereum,
provider: ethProvider,
signer: new Wallet(
"0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"
),
});
});

afterAll(async () => {
await stopTestEnvironment();
await Commands.infra("down", { modules: ["eth-ens-ipfs"] });
});

it("Constructs from Networkish", () => {
Expand All @@ -48,8 +50,8 @@ describe("Connection", () => {
const goerliUri = getRpcUri("goerli");
const connection = new Connection({ provider: goerliUri });
expect(connection.getProvider().connection.url).toEqual(goerliUri);
connection.setProvider(providers.ethereum);
expect(connection.getProvider().connection.url).toEqual(providers.ethereum);
connection.setProvider(ethProvider);
expect(connection.getProvider().connection.url).toEqual(ethProvider);
expect(connection.getSigner()).toBeDefined();
});

Expand All @@ -61,15 +63,15 @@ describe("Connection", () => {
});

it("gets signer from provider", async () => {
const connection = new Connection({ provider: providers.ethereum });
const connection = new Connection({ provider: ethProvider });
const signer = connection.getSigner();
expect(signer).toBeDefined();
});
});

describe("setSigner", () => {
it ("sets signer from ethers Signer", async () => {
const connection = new Connection({ provider: providers.ethereum });
const connection = new Connection({ provider: ethProvider });
connection.setSigner(new Wallet(
"0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"
));
Expand All @@ -87,7 +89,7 @@ describe("Connection", () => {
});

it("sets signer from address", async () => {
const connection = new Connection({ provider: providers.ethereum });
const connection = new Connection({ provider: ethProvider });
connection.setSigner(signerAddress);
const signer = connection.getSigner();
expect(signer).toBeDefined();
Expand Down
Loading