Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for L2 networks [zksync] #229

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion packages/sysweb3-keyring/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"satoshi-bitcoin": "^1.0.5",
"syscoinjs-lib": "^1.0.218",
"syscointx-js": "^1.0.106",
"web3": "^1.7.1"
"web3": "^1.7.1",
"zksync-ethers": "5"
},
"devDependencies": {
"@types/crypto-js": "^4.1.3",
Expand Down
39 changes: 38 additions & 1 deletion packages/sysweb3-keyring/src/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { deepCopy } from '@ethersproject/properties';
import { fetchJson } from '@ethersproject/web';
import { BigNumber, ethers, logger } from 'ethers';
import { ConnectionInfo, Logger, shallowCopy } from 'ethers/lib/utils';
import { Provider } from 'zksync-ethers';

import { handleStatusCodeError } from './errorUtils';
import { checkError } from './utils';
export class CustomJsonRpcProvider extends ethers.providers.JsonRpcProvider {

class BaseProvider extends ethers.providers.JsonRpcProvider {
private timeoutCounter = 0;
private isPossibleGetChainId = true;
private cooldownTime = 120 * 1000;
Expand Down Expand Up @@ -356,3 +358,38 @@ export class CustomJsonRpcProvider extends ethers.providers.JsonRpcProvider {
return promise;
}
}

export class CustomJsonRpcProvider extends BaseProvider {
constructor(
signal: AbortSignal,
url?: ConnectionInfo | string,
network?: Networkish
) {
super(signal, url, network);
}
}

export class CustomL2JsonRpcProvider extends Provider {
private baseProvider: BaseProvider;

constructor(
signal: AbortSignal,
url?: ConnectionInfo | string,
network?: ethers.providers.Networkish
) {
super(url, network);
this.baseProvider = new BaseProvider(signal, url, network);
}

perform(method: string, params: any) {
return this.baseProvider.perform(method, params);
}

send(method: string, params: any[]) {
return this.baseProvider.send(method, params);
}

sendBatch(method: string, params: any[]) {
return this.baseProvider.sendBatch(method, params);
}
}
19 changes: 14 additions & 5 deletions packages/sysweb3-keyring/src/transactions/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import floor from 'lodash/floor';
import omit from 'lodash/omit';

import { LedgerKeyring } from '../ledger';
import { CustomJsonRpcProvider } from '../providers';
import { CustomJsonRpcProvider, CustomL2JsonRpcProvider } from '../providers';
import { SyscoinHDSigner } from '../signers';
import { TrezorKeyring } from '../trezor';
import {
Expand All @@ -48,8 +48,10 @@ import {
} from '@pollum-io/sysweb3-utils';

export class EthereumTransactions implements IEthereumTransactions {
public web3Provider: CustomJsonRpcProvider;
public contentScriptWeb3Provider: CustomJsonRpcProvider;
public web3Provider: CustomJsonRpcProvider | CustomL2JsonRpcProvider;
public contentScriptWeb3Provider:
| CustomJsonRpcProvider
| CustomL2JsonRpcProvider;
public trezorSigner: TrezorKeyring;
public ledgerSigner: LedgerKeyring;
private getNetwork: () => INetwork;
Expand Down Expand Up @@ -1775,11 +1777,18 @@ export class EthereumTransactions implements IEthereumTransactions {
public setWeb3Provider(network: INetwork) {
this.abortController.abort();
this.abortController = new AbortController();
this.web3Provider = new CustomJsonRpcProvider(
const L2Networks = [324, 300];
const isL2Network = L2Networks.includes(network.chainId);

const CurrentProvider = isL2Network
? CustomL2JsonRpcProvider
: CustomJsonRpcProvider;

this.web3Provider = new CurrentProvider(
this.abortController.signal,
network.url
);
this.contentScriptWeb3Provider = new CustomJsonRpcProvider(
this.contentScriptWeb3Provider = new CurrentProvider(
this.abortController.signal,
network.url
);
Expand Down
6 changes: 3 additions & 3 deletions packages/sysweb3-keyring/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TransactionResponse } from '@ethersproject/abstract-provider';
import { TypedData, TypedMessage } from 'eth-sig-util';
import { ethers, BigNumber, BigNumberish } from 'ethers';
import { CustomJsonRpcProvider } from 'providers';
import { CustomJsonRpcProvider, CustomL2JsonRpcProvider } from 'providers';
import {
EncryptedKeystoreV3Json,
Sign,
Expand Down Expand Up @@ -134,8 +134,8 @@ export interface IEthereumTransactions {
) => Promise<any[]>;
setWeb3Provider: (network: INetwork) => void;
importAccount: (mnemonicOrPrivKey: string) => ethers.Wallet;
web3Provider: CustomJsonRpcProvider;
contentScriptWeb3Provider: CustomJsonRpcProvider;
web3Provider: CustomJsonRpcProvider | CustomL2JsonRpcProvider;
contentScriptWeb3Provider: CustomJsonRpcProvider | CustomL2JsonRpcProvider;
}

export interface ISyscoinTransactions {
Expand Down
9 changes: 8 additions & 1 deletion packages/sysweb3-keyring/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2760,7 +2760,7 @@ ethereumjs-wallet@^1.0.2:
utf8 "^3.0.0"
uuid "^8.3.2"

[email protected], ethers@^5.6.4, ethers@^5.6.9:
[email protected], ethers@^5.6.4, ethers@^5.6.9, ethers@~5.7.0:
version "5.7.2"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.7.2.tgz#3a7deeabbb8c030d4126b24f84e525466145872e"
integrity sha512-wswUsmWo1aOK8rR7DIKiWSw9DbLWe6x98Jrn8wcTflTVvaXhAMaB5zGAXy0GYQEQp9iO1iSHWVyARQm11zUtyg==
Expand Down Expand Up @@ -5349,3 +5349,10 @@ yallist@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==

zksync-ethers@5:
version "5.10.0"
resolved "https://registry.yarnpkg.com/zksync-ethers/-/zksync-ethers-5.10.0.tgz#3caf25478539a3fd6e170c13555cec6a1dae32e2"
integrity sha512-OAjTGAHF9wbdkRGkj7XZuF/a1Sk/FVbwH4pmLjAKlR7mJ7sQtQhBhrPU2dCc67xLaNvEESPfwil19ES5wooYFg==
dependencies:
ethers "~5.7.0"