Skip to content

Commit

Permalink
update: added tests for new sdk version
Browse files Browse the repository at this point in the history
  • Loading branch information
kiriyaga committed Oct 18, 2023
1 parent ac4cc97 commit b6fd783
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 163 deletions.
4 changes: 2 additions & 2 deletions packages/hardhat-zksync-zksync2js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@
"sinon": "^9.0.0",
"ts-node": "^10.9.1",
"typescript": "~5.0.0",
"zksync2-js": "^0.1.0-beta.0",
"zksync2-js": "0.2.0",
"@typechain/ethers-v6": "^0.5.0",
"rlp": "3.0.0",
"ethers": "^6.7.1"
},
"peerDependencies": {
"zksync2-js": "^0.1.0-beta.0",
"zksync2-js": "0.2.0",
"ethers": "^6.7.1"
},
"prettier": {
Expand Down
136 changes: 72 additions & 64 deletions packages/hardhat-zksync-zksync2js/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Contract, ContractFactory, Provider, Signer, Wallet } from "zksync2-js"
import {ContractFactory as cf} from "zksync-web3"

import * as ethers from "ethers";

Expand Down Expand Up @@ -37,24 +36,38 @@ function isArtifact(artifact: any): artifact is ZkSyncArtifact {
);
}

export async function getSigners(
export function getWallets(
hre: HardhatRuntimeEnvironment,
): Wallet[] {
const accounts: string[] = rich_wallets.map((wallet) => wallet.privateKey);

const wallets = accounts.map((account) => getWallet(hre, account))

return wallets;
}

export function getWallet(
hre: HardhatRuntimeEnvironment,
privateKey?: string
): Wallet {
return new Wallet(privateKey ? privateKey : rich_wallets[0].privateKey, hre.zksync2js.provider);
}

export function getSigners(
hre: HardhatRuntimeEnvironment
): Promise<Signer[]> {
): Signer[] {
const accounts: string[] = rich_wallets.map((wallet) => wallet.address);

const signersWithAddress = await Promise.all(
accounts.map((account) => getSigner(hre, account))
);
const signersWithAddress = accounts.map((account) => getSigner(hre, account))

return signersWithAddress;
}

export async function getSigner(
export function getSigner(
hre: HardhatRuntimeEnvironment,
address: string
): Promise<Signer> {

return new Signer(hre.zksync2js.provider, address);
): Signer {
return Signer.from(new Signer(hre.zksync2js.provider, address), hre.network.config.chainId);
}

export async function getImpersonatedSigner(
Expand All @@ -69,25 +82,25 @@ export async function getImpersonatedSigner(
export function getContractFactory(
hre: HardhatRuntimeEnvironment,
name: string,
signerOrOptions?: Signer | FactoryOptions,
walletOrOption?: Wallet | FactoryOptions,
): Promise<ContractFactory>;


export function getContractFactory(
hre: HardhatRuntimeEnvironment,
abi: any[],
bytecode: ethers.BytesLike,
signer?: Signer,
wallet?: Wallet,
deploymentType?: DeploymentType
): Promise<ContractFactory>;

export async function getContractFactory(
hre: HardhatRuntimeEnvironment,
nameOrAbi: string | any[],
bytecodeOrFactoryOptions?:
| (Signer | FactoryOptions)
| (Wallet | FactoryOptions)
| ethers.BytesLike,
signer?: Signer,
wallet?: Wallet,
deploymentType?: DeploymentType
): Promise<ContractFactory> {
if (typeof nameOrAbi === "string") {
Expand All @@ -96,7 +109,7 @@ export async function getContractFactory(
return getContractFactoryFromArtifact(
hre,
artifact,
bytecodeOrFactoryOptions as Signer | FactoryOptions | undefined,
bytecodeOrFactoryOptions as Wallet | FactoryOptions | undefined,
deploymentType
);
}
Expand All @@ -105,15 +118,15 @@ export async function getContractFactory(
hre,
nameOrAbi,
bytecodeOrFactoryOptions as ethers.BytesLike,
signer,
wallet,
deploymentType
);
}

function isFactoryOptions(
signerOrOptions?: Signer | FactoryOptions
): signerOrOptions is FactoryOptions {
if (signerOrOptions === undefined || "provider" in signerOrOptions) {
walletOrOptions?: Wallet | FactoryOptions
): walletOrOptions is FactoryOptions {
if (walletOrOptions === undefined || "provider" in walletOrOptions) {
return false;
}

Expand All @@ -123,21 +136,21 @@ function isFactoryOptions(
export async function getContractFactoryFromArtifact(
hre: HardhatRuntimeEnvironment,
artifact: ZkSyncArtifact,
signerOrOptions?: Signer | FactoryOptions,
walletOrOptions?: Wallet | FactoryOptions,
deploymentType?: DeploymentType
): Promise<ContractFactory> {
let signer: Signer | undefined;
let wallet: Wallet | undefined;

if (!isArtifact(artifact)) {
throw new ZkSync2JsPluginError(
`You are trying to create a contract factory from an artifact, but you have not passed a valid artifact parameter.`
);
}

if (isFactoryOptions(signerOrOptions)) {
signer = signerOrOptions.signer;
if (isFactoryOptions(walletOrOptions)) {
wallet = walletOrOptions.wallet;
} else {
signer = signerOrOptions;
wallet = walletOrOptions;
}

if (artifact.bytecode === "0x") {
Expand All @@ -151,7 +164,7 @@ If you want to call a contract using ${artifact.contractName} as its interface u
hre,
artifact.abi,
artifact.bytecode,
signer,
wallet,
deploymentType
);
}
Expand All @@ -160,62 +173,61 @@ async function getContractFactoryByAbiAndBytecode(
hre: HardhatRuntimeEnvironment,
abi: any[],
bytecode: ethers.BytesLike,
signer?: Signer,
wallet?: Wallet,
deploymentType?: DeploymentType
): Promise<ContractFactory> {

if (signer === undefined) {
const signers = await hre.zksync2js.getSigners();
signer = signers[0];
if (wallet === undefined) {
const wallets = getWallets(hre);
wallet = wallets[0];
}

return new ContractFactory(abi, bytecode, signer, deploymentType);
return new ContractFactory(abi, bytecode, wallet, deploymentType);
}

export async function getContractAt(
hre: HardhatRuntimeEnvironment,
nameOrAbi: string | any[],
address: string | Address,
signer?: Signer
wallet?: Wallet
) {
if (typeof nameOrAbi === "string") {
const artifact = await loadArtifact(hre, nameOrAbi);

return getContractAtFromArtifact(hre, artifact, address, signer);
return getContractAtFromArtifact(hre, artifact, address, wallet);
}

if (signer === undefined) {
const signers = await hre.zksync2js.getSigners();
signer = signers[0];
if (wallet === undefined) {
const wallets = getWallets(hre);
wallet = wallets[0];
}

// If there's no signer, we want to put the provider for the selected network here.
// This allows read only operations on the contract interface.
const signerOrProvider: Signer | Provider =
signer !== undefined ? signer : hre.zksync2js.provider;
const walletOrProvider: Wallet | Provider =
wallet !== undefined ? wallet : hre.zksync2js.provider;


return new Contract(address, nameOrAbi, signerOrProvider);
return new Contract(address, nameOrAbi, walletOrProvider);
}

export async function getContractAtFromArtifact(
hre: HardhatRuntimeEnvironment,
artifact: ZkSyncArtifact,
address: string | Address,
signer?: Signer
wallet?: Wallet
) {
if (!isArtifact(artifact)) {
throw new ZkSync2JsPluginError(
`You are trying to create a contract by artifact, but you have not passed a valid artifact parameter.`
);
}

if (signer === undefined) {
const signers = await hre.zksync2js.getSigners();
signer = signers[0];
if (wallet === undefined) {
wallet = hre.zksync2js.getWallet();
}

let contract = new Contract(address, artifact.abi, signer);
let contract = new Contract(address, artifact.abi, wallet);

if (contract.runner === null) {
contract = contract.connect(hre.zksync2js.provider) as Contract;
Expand All @@ -227,21 +239,17 @@ export async function getContractAtFromArtifact(
export async function deployContract(
hre: HardhatRuntimeEnvironment,
artifact: ZkSyncArtifact,
signer?: Signer | Wallet,
wallet?: Wallet,
constructorArguments: any[] = [],
overrides?: ethers.Overrides,
additionalFactoryDeps?: ethers.BytesLike[],
deploymentType?: DeploymentType
additionalFactoryDeps?: ethers.BytesLike[]
): Promise<Contract> {

if(signer === undefined) {
const signers = await hre.zksync2js.getSigners();
signer = Signer.from(signers[0], 1);
if(wallet === undefined) {
wallet = getWallet(hre);
}

let w = new Wallet(rich_wallets[0].privateKey, hre.zksync2js.provider);

const factory = new ContractFactory(artifact.abi, artifact.deployedBytecode, w, "create");
const factory = new ContractFactory(artifact.abi, artifact.bytecode, wallet);

const baseDeps = await extractFactoryDeps(hre, artifact);
const additionalDeps = additionalFactoryDeps
Expand All @@ -252,19 +260,19 @@ export async function deployContract(
const { customData, ..._overrides } = overrides ?? {};

// Encode and send the deploy transaction providing factory dependencies.
const contract = await factory.deploy(...constructorArguments, {
from: w.address,
//gasLimit: 13919861n,
..._overrides,
customData: {
...customData,
factoryDeps,
},
});

await contract.deployed();
const contract = await factory.deploy(...constructorArguments, {
..._overrides,
customData: {
...customData,
salt: ethers.ZeroHash,
factoryDeps,
},
}
);

return contract;
await contract.waitForDeployment();

return contract as Contract;
}


Expand Down
10 changes: 7 additions & 3 deletions packages/hardhat-zksync-zksync2js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import {
getContractFactory,
getContractFactoryFromArtifact,
getImpersonatedSigner,
getWallet,
getSigner,
getSigners,
loadArtifact,
deployContract
deployContract,
getWallets
} from "./helpers";
import { ZkSyncArtifact } from "./types";
import { ethers } from "ethers";
Expand All @@ -29,6 +31,8 @@ import { ethers } from "ethers";
return {
...zksync2js,
provider,
getWallet: (privateKey?: string) => getWallet(hre, privateKey),
getWallets: () => getWallets(hre),
getSigner: (address: string) => getSigner(hre, address),
getSigners: () => getSigners(hre),
getImpersonatedSigner: (address: string) => getImpersonatedSigner(hre, address),
Expand All @@ -40,10 +44,10 @@ import { ethers } from "ethers";
loadArtifact: (name: string) => loadArtifact(hre, name),
deployContract: (artifact: ZkSyncArtifact,
constructorArguments: any[],
signer?: Signer | Wallet,
wallet?: Wallet,
overrides?: ethers.Overrides,
additionalFactoryDeps?: ethers.BytesLike[]) =>
deployContract(hre, artifact, signer, constructorArguments = [], overrides, additionalFactoryDeps)
deployContract(hre, artifact, wallet, constructorArguments = [], overrides, additionalFactoryDeps)
};
});
});
Expand Down
Loading

0 comments on commit b6fd783

Please sign in to comment.