Skip to content

Commit

Permalink
unify and enhance the URLs assignment used for local testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad-Altabba committed Sep 10, 2024
1 parent 383de0f commit 67d170f
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 67 deletions.
13 changes: 7 additions & 6 deletions test/local/account-abstraction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import Storage from './files/Storage.json';
import Token from './files/Token.json';
import Paymaster from './files/Paymaster.json';
import MultisigAccount from './files/TwoUserMultisig.json';
import { getAccounts, L2Provider, PAYMASTER, APPROVAL_TOKEN } from './fixtures';
import { getAccounts, PAYMASTER, APPROVAL_TOKEN } from './fixtures';
import { getPaymasterParams, ContractFactory } from '../../src';
import { Address } from 'web3';
import { Eip712TxData } from '../../src/types';
import { Transaction } from 'web3-types';
import { L2_CHAIN_URL } from 'test/utils';
const { ETH_ADDRESS } = constants;
const accounts = getAccounts();

jest.setTimeout(300000);
describe('Account Abstraction', () => {
const l2Provider = new Web3ZKsyncL2(L2Provider);
const l2Provider = new Web3ZKsyncL2(L2_CHAIN_URL);
const PRIVATE_KEY1 = accounts[0].privateKey;
const ADDRESS1 = accounts[0].address;
const wallet = new ZKsyncWallet(PRIVATE_KEY1, l2Provider);
Expand Down Expand Up @@ -119,7 +120,7 @@ describe('Account Abstraction', () => {
);
const owner1 = new ZKsyncWallet(
'0x63bbd75f81a3b889bf4faf581162dfe52bbd09d0149f4987d9a8cb654684441c',
new Web3ZKsyncL2(L2Provider),
new Web3ZKsyncL2(L2_CHAIN_URL),
);
// refill owner1 for gas estimation
await (
Expand All @@ -130,7 +131,7 @@ describe('Account Abstraction', () => {
).wait();
const owner2 = new ZKsyncWallet(
'0x2553cbc2c37248d0058a736f79ce1dac13e24dbecb0d371df6f4f53fa97ef789',
new Web3ZKsyncL2(L2Provider),
new Web3ZKsyncL2(L2_CHAIN_URL),
);
const args = [owner1.getAddress(), owner2.getAddress()];
const multisigContract = await factory.deploy(args);
Expand Down Expand Up @@ -190,7 +191,7 @@ describe('Account Abstraction', () => {
);
const owner1 = new ZKsyncWallet(
'0x63bbd75f81a3b889bf4faf581162dfe52bbd09d0149f4987d9a8cb654684441c',
new Web3ZKsyncL2(L2Provider),
new Web3ZKsyncL2(L2_CHAIN_URL),
);
// refill owner1 for gas estimation
await (
Expand All @@ -201,7 +202,7 @@ describe('Account Abstraction', () => {
).wait();
const owner2 = new ZKsyncWallet(
'0x2553cbc2c37248d0058a736f79ce1dac13e24dbecb0d371df6f4f53fa97ef789',
new Web3ZKsyncL2(L2Provider),
new Web3ZKsyncL2(L2_CHAIN_URL),
);
const args = [owner1.getAddress(), owner2.getAddress()];
const multisigContract = await factory.deploy(args);
Expand Down
7 changes: 4 additions & 3 deletions test/local/deposit-withdraw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { TransactionFactory } from 'web3-eth-accounts';
import { Web3ZKsyncL2, Web3ZKsyncL1, ZKsyncWallet } from '../../src';
import { EIP712_TX_TYPE, ETH_ADDRESS } from '../../src/constants';
import * as utils from '../../src/utils';
import { getAccounts, L1Provider, L2Provider } from './fixtures';
import { getAccounts } from './fixtures';
import { L1_CHAIN_URL, L2_CHAIN_URL } from 'test/utils';

// TODO: This test needs to setup local dev nodes for L1 and L2
// and also needs to have a private key with funds in the L1
Expand All @@ -13,9 +14,9 @@ jest.setTimeout(10000);
describe('wallet', () => {
// @ts-ignore
TransactionFactory.registerTransactionType(EIP712_TX_TYPE, utils.EIP712Transaction);
const l1Provider = new Web3ZKsyncL1(L1Provider);
const l1Provider = new Web3ZKsyncL1(L1_CHAIN_URL);
const accounts = getAccounts();
const l2Provider = new Web3ZKsyncL2(L2Provider);
const l2Provider = new Web3ZKsyncL2(L2_CHAIN_URL);
const PRIVATE_KEY = accounts[0].privateKey;
const wallet = new ZKsyncWallet(PRIVATE_KEY, l2Provider, l1Provider);
it('should deposit', async () => {
Expand Down
14 changes: 8 additions & 6 deletions test/local/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ export const DAI_L2 = '0xf5F299B7A29f08b533BbDD19C2Bb2b3e1D975bD2';

import accountsData from './rich-wallets.json';
import { abi as TokenAbi } from './files/Token.json';
import { L2_CHAIN_URL, L1_CHAIN_URL } from 'test/utils';
export const getAccounts = () => accountsData;

export const L1Provider = 'http://127.0.0.1:8545';
export const L2Provider = 'http://127.0.0.1:3050';

export const prepareAccount = async (privateKey: string) => {
const provider = new Web3ZKsyncL2(L2Provider);
const l1Provider = new Web3ZKsyncL1(L1Provider);
const provider = new Web3ZKsyncL2(L2_CHAIN_URL);
const l1Provider = new Web3ZKsyncL1(L1_CHAIN_URL);
const wallet = new ZKsyncWallet(privateKey, provider, l1Provider);
const wallet0 = new ZKsyncWallet(accountsData[0].privateKey, provider, l1Provider);
const crownContract = new l1Provider.eth.Contract(TokenAbi, ERC20_CROWN);
Expand Down Expand Up @@ -44,5 +42,9 @@ export const prepareAccount = async (privateKey: string) => {

export const getPreparedWallet = async (privateKey: string) => {
await prepareAccount(privateKey);
return new ZKsyncWallet(privateKey, new Web3ZKsyncL2(L2Provider), new Web3ZKsyncL1(L1Provider));
return new ZKsyncWallet(
privateKey,
new Web3ZKsyncL2(L2_CHAIN_URL),
new Web3ZKsyncL1(L1_CHAIN_URL),
);
};
7 changes: 3 additions & 4 deletions test/local/provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Web3ZKsyncL2, ZKsyncWallet, SmartAccount } from '../../src';
import { L2Provider } from './fixtures';
import { ADDRESS2, PRIVATE_KEY1, PRIVATE_KEY2 } from '../utils';
import { ADDRESS2, L2_CHAIN_URL, PRIVATE_KEY1, PRIVATE_KEY2 } from '../utils';
import { EIP712_TX_TYPE } from '../../src/constants';
import { privateKeyToAccount } from 'web3-eth-accounts';
describe('EIP712Signer', () => {
it('should different wallets be able to use different accounts, even when using the same EIP712Signer', async () => {
const l2Provider = new Web3ZKsyncL2(L2Provider);
const l2Provider = new Web3ZKsyncL2(L2_CHAIN_URL);
const w1 = new ZKsyncWallet(PRIVATE_KEY1, l2Provider);

const s1 = await w1.provider?.signTransaction({
Expand Down Expand Up @@ -36,7 +35,7 @@ describe('EIP712Signer', () => {
});

it('should different smart accounts be able to use different accounts, even when using the same EIP712Signer', async () => {
const l2Provider = new Web3ZKsyncL2(L2Provider);
const l2Provider = new Web3ZKsyncL2(L2_CHAIN_URL);
const acc = privateKeyToAccount(PRIVATE_KEY1);
const acc2 = privateKeyToAccount(PRIVATE_KEY2);
const sa1 = new SmartAccount(
Expand Down
25 changes: 12 additions & 13 deletions test/local/smart-account.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { IS_ETH_BASED, deepEqualExcluding, PRIVATE_KEY2, ADDRESS2 } from '../utils';
import {
PAYMASTER,
APPROVAL_TOKEN,
DAI_L1,
getAccounts,
L2Provider,
L1Provider,
prepareAccount,
} from './fixtures';
IS_ETH_BASED,
deepEqualExcluding,
PRIVATE_KEY2,
ADDRESS2,
L1_CHAIN_URL,
L2_CHAIN_URL,
} from '../utils';
import { PAYMASTER, APPROVAL_TOKEN, DAI_L1, getAccounts, prepareAccount } from './fixtures';
import MultisigAccount from './files/TwoUserMultisig.json';
import { EIP712_TX_TYPE, ETH_ADDRESS, ETH_ADDRESS_IN_CONTRACTS } from '../../src/constants';
import { toBigInt, toWei } from 'web3-utils';
Expand All @@ -28,8 +27,8 @@ jest.setTimeout(50000);
const accounts = getAccounts();
const mainAccount = accounts[0];
const PRIVATE_KEY = mainAccount.privateKey;
const provider = new Web3ZKsyncL2(L2Provider);
const l1Provider = new Web3ZKsyncL1(L1Provider);
const provider = new Web3ZKsyncL2(L2_CHAIN_URL);
const l1Provider = new Web3ZKsyncL1(L1_CHAIN_URL);
const wallet = new ZKsyncWallet(PRIVATE_KEY, provider, l1Provider);
const account = new SmartAccount(
{ address: mainAccount.address, secret: mainAccount.privateKey },
Expand Down Expand Up @@ -548,8 +547,8 @@ describe('SmartAccount', () => {
});
});
describe('MultisigECDSASmartAccount', () => {
const provider = new Web3ZKsyncL2(L2Provider);
const ethProvider = new Web3ZKsyncL1(L1Provider);
const provider = new Web3ZKsyncL2(L2_CHAIN_URL);
const ethProvider = new Web3ZKsyncL1(L1_CHAIN_URL);
const wallet = new ZKsyncWallet(mainAccount.privateKey, provider, ethProvider);
let account: SmartAccount;

Expand Down
4 changes: 2 additions & 2 deletions test/local/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as web3Accounts from 'web3-eth-accounts';
import { Web3 } from 'web3';
import * as constants from '../../src/constants';
import * as utils from '../../src/utils';
import { L2Provider } from './fixtures';
import { L2_CHAIN_URL } from 'test/utils';

describe('utils', () => {
describe('#isTypedDataSignatureCorrect()', () => {
Expand All @@ -22,7 +22,7 @@ describe('utils', () => {
270,
);
const signature = await eip712Signer.sign(tx);
const web3 = new Web3(L2Provider);
const web3 = new Web3(L2_CHAIN_URL);
expect(signature).toBe(
'0x5ea12f3d54a1624d7e7f5161dbf6ab746c3335e643b2966264e740cf8e10e9b64b0251fb79d9a5b11730387085a0d58f105926f72e20242ecb274639991939ca1b',
);
Expand Down
8 changes: 3 additions & 5 deletions test/local/wallet-send.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Web3ZKsyncL2, ZKsyncWallet, Web3ZKsyncL1, getPaymasterParams } from '../../src';
import { IS_ETH_BASED, ADDRESS2 } from '../utils';
import { IS_ETH_BASED, ADDRESS2, L1_CHAIN_URL, L2_CHAIN_URL } from '../utils';
import { ETH_ADDRESS_IN_CONTRACTS, LEGACY_ETH_ADDRESS } from '../../src/constants';
import {
getAccounts,
L1Provider,
L2Provider,
PAYMASTER,
DAI_L1,
APPROVAL_TOKEN,
Expand All @@ -17,8 +15,8 @@ jest.setTimeout(600000);
const accounts = getAccounts();
const mainAccount = accounts[0];
const PRIVATE_KEY = mainAccount.privateKey;
const provider = new Web3ZKsyncL2(L2Provider);
const ethProvider = new Web3ZKsyncL1(L1Provider);
const provider = new Web3ZKsyncL2(L2_CHAIN_URL);
const ethProvider = new Web3ZKsyncL1(L1_CHAIN_URL);
const wallet = new ZKsyncWallet(PRIVATE_KEY, provider, ethProvider);

describe('Wallet', () => {
Expand Down
8 changes: 3 additions & 5 deletions test/local/wallet-transfer.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Web3ZKsyncL2, ZKsyncWallet, Web3ZKsyncL1, getPaymasterParams } from '../../src';
import { IS_ETH_BASED, ADDRESS2 } from '../utils';
import { IS_ETH_BASED, ADDRESS2, L2_CHAIN_URL, L1_CHAIN_URL } from '../utils';
import { ETH_ADDRESS_IN_CONTRACTS, LEGACY_ETH_ADDRESS } from '../../src/constants';
import {
getAccounts,
L1Provider,
L2Provider,
PAYMASTER,
DAI_L1,
APPROVAL_TOKEN,
Expand All @@ -17,8 +15,8 @@ jest.setTimeout(600000);
const accounts = getAccounts();
const mainAccount = accounts[0];
const PRIVATE_KEY = mainAccount.privateKey;
const provider = new Web3ZKsyncL2(L2Provider);
const ethProvider = new Web3ZKsyncL1(L1Provider);
const provider = new Web3ZKsyncL2(L2_CHAIN_URL);
const ethProvider = new Web3ZKsyncL1(L1_CHAIN_URL);
const wallet = new ZKsyncWallet(PRIVATE_KEY, provider, ethProvider);

describe('Wallet', () => {
Expand Down
8 changes: 3 additions & 5 deletions test/local/wallet-withdraw.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Web3ZKsyncL2, ZKsyncWallet, Web3ZKsyncL1, getPaymasterParams } from '../../src';
import { IS_ETH_BASED } from '../utils';
import { IS_ETH_BASED, L1_CHAIN_URL, L2_CHAIN_URL } from '../utils';
import { ETH_ADDRESS_IN_CONTRACTS, LEGACY_ETH_ADDRESS } from '../../src/constants';
import {
getAccounts,
L1Provider,
L2Provider,
ERC20_CROWN,
PAYMASTER,
DAI_L1,
Expand All @@ -17,8 +15,8 @@ jest.setTimeout(600000);
const accounts = getAccounts();
const mainAccount = accounts[0];
const PRIVATE_KEY = mainAccount.privateKey;
const provider = new Web3ZKsyncL2(L2Provider);
const ethProvider = new Web3ZKsyncL1(L1Provider);
const provider = new Web3ZKsyncL2(L2_CHAIN_URL);
const ethProvider = new Web3ZKsyncL1(L1_CHAIN_URL);
const wallet = new ZKsyncWallet(PRIVATE_KEY, provider, ethProvider);

describe('Wallet', () => {
Expand Down
15 changes: 10 additions & 5 deletions test/local/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import * as ethAccounts from 'web3-eth-accounts';
import * as web3Utils from 'web3-utils';
import type { Transaction } from 'web3-types';
import { utils, Web3ZKsyncL2, ZKsyncWallet, Web3ZKsyncL1, getPaymasterParams } from '../../src';
import { IS_ETH_BASED, ADDRESS1, ADDRESS2, deepEqualExcluding } from '../utils';
import {
IS_ETH_BASED,
ADDRESS1,
ADDRESS2,
deepEqualExcluding,
L1_CHAIN_URL,
L2_CHAIN_URL,
} from '../utils';
import {
ETH_ADDRESS,
ETH_ADDRESS_IN_CONTRACTS,
Expand All @@ -14,8 +21,6 @@ import {
import { IERC20ABI } from '../../src/contracts/IERC20';
import {
getAccounts,
L1Provider,
L2Provider,
PAYMASTER,
DAI_L1,
APPROVAL_TOKEN,
Expand All @@ -29,8 +34,8 @@ jest.setTimeout(600000);
const accounts = getAccounts();
const mainAccount = accounts[0];
const PRIVATE_KEY = mainAccount.privateKey;
const provider = new Web3ZKsyncL2(L2Provider);
const ethProvider = new Web3ZKsyncL1(L1Provider);
const provider = new Web3ZKsyncL2(L2_CHAIN_URL);
const ethProvider = new Web3ZKsyncL1(L1_CHAIN_URL);
const wallet = new ZKsyncWallet(PRIVATE_KEY, provider, ethProvider);
const walletAddress = wallet.getAddress();

Expand Down
3 changes: 3 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,7 @@ main()
.catch(error => {
console.log(`Error: ${error}`);
console.warn(error.stack);
console.warn(
'Test Setup is verified with node 18. If you use nvm, you can execute this before running the script: `nvm use 18`.',
);
});
36 changes: 23 additions & 13 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ export const ERC20_CROWN = '0x927488F48ffbc32112F1fF721759649A89721F8F';
export const APPROVAL_TOKEN = ERC20_CROWN; // Crown token
export const PAYMASTER = '0x13D0D8550769f59aa241a41897D4859c87f7Dd46'; // Crown token paymaster

export const IS_ETH_BASED = true;
// To setup a local test network refer to: https://github.com/matter-labs/local-setup/blob/main/README.md
// Set the following variable to `true` when the test network is bootstrapped with `./start.sh`.
// But set it to `false` when `./start-zk-chains.sh` is used.
const IS_BOOTSTRAPPED_NETWORK = true;

const stringify = (value: any) =>
JSON.stringify(value, (_, v) => (typeof v === 'bigint' ? v.toString() : v) as unknown);
const convert = (value: any) => {
if (typeof value === 'bigint') {
return value.toString();
}
return typeof value === 'object' ? JSON.parse(stringify(value)) : value;
};
export const IS_ETH_BASED = false;

export const L2_CHAIN_URL = IS_BOOTSTRAPPED_NETWORK
? 'http://127.0.0.1:3050'
: IS_ETH_BASED
? 'http://127.0.0.1:15100'
: 'http://127.0.0.1:15200';

export const L2_CHAIN_URL = IS_ETH_BASED
? 'http://127.0.0.1:3050' // probably need to use the port 15100 after some investigation (change and run the tests to see the error)
: 'http://127.0.0.1:15200';
export const L1_CHAIN_URL = 'http://127.0.0.1:15045';
export const L1_CHAIN_URL = IS_BOOTSTRAPPED_NETWORK
? 'http://127.0.0.1:8545'
: 'http://127.0.0.1:15045';

export function deepEqualExcluding(
obj1: Record<string, any>,
Expand All @@ -39,3 +40,12 @@ export function deepEqualExcluding(
}
}
}

const stringify = (value: any) =>
JSON.stringify(value, (_, v) => (typeof v === 'bigint' ? v.toString() : v) as unknown);
const convert = (value: any) => {
if (typeof value === 'bigint') {
return value.toString();
}
return typeof value === 'object' ? JSON.parse(stringify(value)) : value;
};

0 comments on commit 67d170f

Please sign in to comment.