Skip to content

Commit

Permalink
test: refactoring path and variables
Browse files Browse the repository at this point in the history
  • Loading branch information
pcheremu committed Mar 7, 2024
1 parent b0a38a0 commit a82496a
Show file tree
Hide file tree
Showing 24 changed files with 68 additions and 76 deletions.
12 changes: 11 additions & 1 deletion packages/integration-tests/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class Helper {
exec(script, { encoding: "utf-8" }, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing script "${script}":`, error);
console.error(`stderr executing script "${script}":`, stderr);
reject(error);
} else {
console.log(`> Run NPM Script "${script}":\n`, stdout);
Expand All @@ -35,8 +36,17 @@ export class Helper {
});
}

async writeFile(filePath: string, fileName: string, data: string) {
const absoluteRoute = path.join(filePath, fileName);
try {
await fs.writeFile(absoluteRoute, data);
} catch {
console.log(`Cannot write: ${fileName} to ${filePath}`);
}
}

async readFile(filePath: string, fileName: string) {
const absoluteRoute = path.join(filePath + fileName);
const absoluteRoute = path.join(filePath, fileName);

try {
return await fs.readFile(absoluteRoute, { encoding: "utf-8" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Deployer } from "@matterlabs/hardhat-zksync-deploy";
import { promises as fs } from "fs";

import { localConfig } from "../../config";
import { Buffer } from "../../constants";
import { Buffer, Path } from "../../constants";
import { Helper } from "../../helper";
import getWallet from "../utils/getWallet";

import type { HardhatRuntimeEnvironment } from "hardhat/types";
const helper = new Helper();

export default async function (hre: HardhatRuntimeEnvironment) {
const wallet = await getWallet(hre);
Expand All @@ -15,5 +17,5 @@ export default async function (hre: HardhatRuntimeEnvironment) {
const contract = await deployer.deploy(artifact, [], localConfig.gasLimit);
const contractAddress = contract.address;
console.log(`${artifact.contractName} was deployed to ${contractAddress}`);
await fs.writeFile(Buffer.greeterL2, contractAddress);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.greeterL2, contractAddress);
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
import { Deployer } from "@matterlabs/hardhat-zksync-deploy";
import * as ethers from "ethers";
import { promises as fs } from "fs";
import { Wallet } from "zksync-web3";

import { Buffer, Wallets } from "../../constants";
import { Buffer, Path, Wallets } from "../../constants";
import { Helper } from "../../helper";

import type { HardhatRuntimeEnvironment } from "hardhat/types";

const helper = new Helper();
export default async function (hre: HardhatRuntimeEnvironment) {
const wallet = new Wallet(Wallets.richWalletPrivateKey);
// The wallet that will receive ERC20 tokens
const emptyWallet = Wallet.createRandom();
console.log(`Empty wallet's address: ${emptyWallet.address}`);
await fs.writeFile(Buffer.emptyWalletAddress, emptyWallet.address);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.emptyWalletAddress, emptyWallet.address);
console.log(`Empty wallet's private key: ${emptyWallet.privateKey}`);
await fs.writeFile(Buffer.emptyWalletPrivateKey, emptyWallet.privateKey);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.emptyWalletPrivateKey, emptyWallet.privateKey);

const deployer = new Deployer(hre, wallet);

// Deploying the ERC20 token
const erc20Artifact = await deployer.loadArtifact("MyERC20");
const erc20 = await deployer.deploy(erc20Artifact, ["MyToken", "MyToken", 18]);
console.log(`ERC20 address: ${erc20.address}`);
await fs.writeFile(Buffer.customToken, erc20.address);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.customToken, erc20.address);

const paymasterArtifact = await deployer.loadArtifact("MyPaymaster");
const paymaster = await deployer.deploy(paymasterArtifact, [erc20.address]);
console.log(`Paymaster address: ${paymaster.address}`);
await fs.writeFile(Buffer.paymaster, paymaster.address);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.paymaster, paymaster.address);

const deployTransaction = await paymaster.deployTransaction;
console.log(`Paymaster deploy transaction: ${deployTransaction.hash}`);
await fs.writeFile(Buffer.paymasterDeployTx, deployTransaction.hash);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.paymasterDeployTx, deployTransaction.hash);

await (
await deployer.zkWallet.sendTransaction({
Expand Down
7 changes: 4 additions & 3 deletions packages/integration-tests/src/playbook/deploy/erc20toL1.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { promises as fs } from "fs";
import { ethers } from "hardhat";

import { localConfig } from "../../config";
import { Buffer, Wallets } from "../../constants";
import { Buffer, Path, Wallets } from "../../constants";
import { Helper } from "../../helper";

const helper = new Helper();
async function main() {
const [deployer] = await ethers.getSigners();

Expand All @@ -16,7 +17,7 @@ async function main() {
const contract = await ethers.getContractFactory("L1");
const token = await contract.deploy(Wallets.richWalletAddress, localConfig.gasLimit);

await fs.writeFile(Buffer.L1, token.address);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.L1, token.address);
}

main()
Expand Down
7 changes: 4 additions & 3 deletions packages/integration-tests/src/playbook/deploy/erc20toL2.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Deployer } from "@matterlabs/hardhat-zksync-deploy";
import { promises as fs } from "fs";

import { localConfig } from "../../config";
import { Buffer } from "../../constants";
import { Buffer, Path } from "../../constants";
import { Helper } from "../../helper";
import getWallet from "../utils/getWallet";

import type { HardhatRuntimeEnvironment } from "hardhat/types";
const helper = new Helper();

export default async function (hre: HardhatRuntimeEnvironment) {
const wallet = await getWallet(hre);
Expand All @@ -15,5 +16,5 @@ export default async function (hre: HardhatRuntimeEnvironment) {
const contract = await deployer.deploy(artifact, [], localConfig.gasLimit);
const contractAddress = contract.address;
console.log(`${artifact.contractName} was deployed to ${contractAddress}`);
await fs.writeFile(Buffer.L2, contractAddress);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.L2, contractAddress);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Deployer } from "@matterlabs/hardhat-zksync-deploy";
import { promises as fs } from "fs";

import { Buffer, Wallets } from "../../constants";
import { Buffer, Path, Wallets } from "../../constants";
import { Helper } from "../../helper";
import verify from "../utils/displayVerificationInfo";
import getWallet from "../utils/getWallet";

import type { HardhatRuntimeEnvironment } from "hardhat/types";
const helper = new Helper();

const contract_name = "TokenF2L2"; // insert the name of the contract you want to deploy
const constructor_arguments = [Wallets.richWalletAddress]; // insert the constructor arguments of the contract you want to deploy
Expand All @@ -27,7 +28,7 @@ export default async function (hre: HardhatRuntimeEnvironment) {

// Show the contract info.
console.log(`Contract "${artifact.contractName}" was deployed to ${deployedContract.address}`);
await fs.writeFile(Buffer.addressMultiTransferETH, deployedContract.address);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.addressMultiTransferETH, deployedContract.address);

await verify({ hre, contract: deployedContract, contractConstructorArguments: constructor_arguments, artifact });

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Deployer } from "@matterlabs/hardhat-zksync-deploy";
import { promises as fs } from "fs";

import { Buffer, Path } from "../../constants";
import { Helper } from "../../helper";
Expand Down Expand Up @@ -33,8 +32,8 @@ export default async function (hre: HardhatRuntimeEnvironment) {
const address = deployedContract.address;
const txHash = deployedContract.deployTransaction.hash;

await fs.writeFile(Buffer.addressMultiCallCaller, address);
await fs.writeFile(Buffer.txMultiCallCaller, txHash);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.addressMultiCallCaller, address);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.txMultiCallCaller, txHash);

return [address, txHash];
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export default async function (hre: HardhatRuntimeEnvironment) {
const address = deployedContract.address;
const txHash = deployedContract.deployTransaction.hash;

await fs.writeFile(Buffer.addressMultiCallMiddle, address);
await fs.writeFile(Buffer.txMultiCallMiddle, txHash);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.addressMultiCallMiddle, address);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.txMultiCallMiddle, txHash);

return [address, txHash];
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Deployer } from "@matterlabs/hardhat-zksync-deploy";
import { promises as fs } from "fs";

import { Buffer } from "../../constants";
import { Buffer, Path } from "../../constants";
import { Helper } from "../../helper";
import getWallet from "../utils/getWallet";

import type { HardhatRuntimeEnvironment } from "hardhat/types";

export default async function (hre: HardhatRuntimeEnvironment) {
const helper = new Helper();
console.log(`Running deploy script for the Greeter contract`);

const wallet = await getWallet(hre);
Expand Down Expand Up @@ -48,8 +49,8 @@ export default async function (hre: HardhatRuntimeEnvironment) {
const address = deployedContract.address;
const txHash = deployedContract.deployTransaction.hash;

await fs.writeFile(Buffer.addressMultiCallRoot, address);
await fs.writeFile(Buffer.txMultiCallRoot, txHash);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.addressMultiCallRoot, address);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.txMultiCallRoot, txHash);

return [address, txHash];
}
7 changes: 4 additions & 3 deletions packages/integration-tests/src/playbook/deploy/nftToL1.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { promises as fs } from "fs";
import { ethers } from "hardhat";
import * as hardhatConfig from "hardhat";

import { Buffer, Wallets } from "../../constants";
import { Buffer, Path, Wallets } from "../../constants";
import { Helper } from "../../helper";

import type { HardhatRuntimeEnvironment } from "hardhat/types";
const helper = new Helper();

async function main() {
const hre: HardhatRuntimeEnvironment = hardhatConfig;
Expand All @@ -22,7 +23,7 @@ async function main() {
console.error(`The NFT minting has been unsuccessful: ${mintNFT}`);
}

await fs.writeFile(Buffer.NFTtoL1, myNFT.address);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.NFTtoL1, myNFT.address);
}

main()
Expand Down
7 changes: 4 additions & 3 deletions packages/integration-tests/src/playbook/deploy/nftToL2.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Deployer } from "@matterlabs/hardhat-zksync-deploy";
import { promises as fs } from "fs";

import { Buffer, Wallets } from "../../constants";
import { Buffer, Path, Wallets } from "../../constants";
import { Helper } from "../../helper";
import displayVerificationInfo from "../utils/displayVerificationInfo";
import getWallet from "../utils/getWallet";

import type { HardhatRuntimeEnvironment } from "hardhat/types";

export default async function (hre: HardhatRuntimeEnvironment) {
const helper = new Helper();
console.log(`Running deploy script for the Greeter contract`);

const wallet = await getWallet(hre);
Expand All @@ -30,5 +31,5 @@ export default async function (hre: HardhatRuntimeEnvironment) {

displayVerificationInfo({ hre, contract: myNFT, contractConstructorArguments, artifact });

await fs.writeFile(Buffer.NFTtoL2, myNFT.address);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.NFTtoL2, myNFT.address);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default async function (hre: HardhatRuntimeEnvironment) {

console.log(`Transaction hash: ${transactionReceipt.transactionHash}`);

await fs.writeFile(Buffer.executeGreeterTx, transactionReceipt.transactionHash);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.executeGreeterTx, transactionReceipt.transactionHash);

return transactionReceipt.transactionHash;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default async function (hre: HardhatRuntimeEnvironment) {
console.log("Multicall contract caller: ", txHash);
console.log(`Contract said after new greeting: ${await attachedContract.newCallGreeter()}`);

await fs.writeFile(Buffer.txUseMultiCallContracts, txHash);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.txUseMultiCallContracts, txHash);

return txHash;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as ethers from "ethers";
import { promises as fs } from "fs";
import { Provider, Wallet } from "zksync-web3";

import { localConfig } from "../../config";
Expand Down Expand Up @@ -91,7 +90,7 @@ export default async function callMultiTransferETH(hre: HardhatRuntimeEnvironmen
)}" Custom token II`
);

await fs.writeFile(Buffer.txMultiTransferETH, ethTransfer);
await fs.writeFile(Buffer.txMultiTransferCustomTokenI, customToken1Transfer);
await fs.writeFile(Buffer.txMultiTransferCustomTokenII, customToken2Transfer);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.txMultiTransferETH, ethTransfer);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.txMultiTransferCustomTokenI, customToken1Transfer);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.txMultiTransferCustomTokenII, customToken2Transfer);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as ethers from "ethers";
import { promises as fs } from "fs";
import { Provider, utils, Wallet } from "zksync-web3";

import { localConfig } from "../../config";
Expand Down Expand Up @@ -62,7 +61,7 @@ export default async function (hre: HardhatRuntimeEnvironment) {

console.log(`Balance of the user after mint: ${await emptyWallet.getBalance(TOKEN_ADDRESS)}`);

await fs.writeFile(Buffer.paymasterTx, receipt.transactionHash);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.paymasterTx, receipt.transactionHash);
console.log(`Transaction hash: ${receipt.transactionHash}`);

return receipt.transactionHash;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as ethers from "ethers";
import { promises as fs } from "fs";
import * as path from "path";
import * as zksync from "zksync-web3";

import { localConfig } from "../../../config";
Expand All @@ -11,8 +9,6 @@ const helper = new Helper();
const syncProvider = new zksync.Provider(localConfig.L2Network);
const ethProvider = ethers.getDefaultProvider(localConfig.L1Network);
const syncWallet = new zksync.Wallet(localConfig.privateKey, syncProvider, ethProvider);
const bufferAddressL2DepositedFile = path.join(Path.absolutePathToBufferFiles, Buffer.L2deposited);
const bufferTxErc20DepositFile = path.join(Path.absolutePathToBufferFiles, Buffer.txERC20Deposit);

export const depositERC20 = async function (sum = "0.5", tokenAddress: string, units = 18) {
const deposit = await syncWallet.deposit({
Expand All @@ -30,8 +26,8 @@ export const depositERC20 = async function (sum = "0.5", tokenAddress: string, u
console.log("L2 token address ", l2TokenAddress);
const txHash = await deposit.waitFinalize();
await helper.logTransaction(Logger.deposit, txHash.transactionHash, "ERC20 token");
await fs.writeFile(bufferAddressL2DepositedFile, l2TokenAddress);
await fs.writeFile(bufferTxErc20DepositFile, txHash.transactionHash);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.L2deposited, l2TokenAddress);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.txERC20Deposit, txHash.transactionHash);

return txHash;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as ethers from "ethers";
import { promises as fs } from "fs";
import * as path from "path";
import * as zksync from "zksync-web3";

import { localConfig } from "../../../config";
Expand All @@ -12,7 +10,6 @@ export const depositEth = async function (sum = "0.000009") {
const syncProvider = new zksync.Provider(localConfig.L2Network);
const ethProvider = ethers.getDefaultProvider(localConfig.L1Network);
const syncWallet = new zksync.Wallet(localConfig.privateKey, syncProvider, ethProvider);
const bufferFile = path.join(Path.playbookRoot, Buffer.txEthDeposit);

const deposit = await syncWallet.deposit({
token: zksync.utils.ETH_ADDRESS,
Expand All @@ -22,7 +19,7 @@ export const depositEth = async function (sum = "0.000009") {
await deposit.wait(1);
const txHash = await deposit.waitFinalize();
await helper.logTransaction(Logger.deposit, txHash.transactionHash, "ETH");
await fs.writeFile(bufferFile, txHash.transactionHash);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.txEthDeposit, txHash.transactionHash);

return txHash;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as ethers from "ethers";
import { promises as fs } from "fs";
import * as path from "path";
import * as zksync from "zksync-web3";

import { localConfig } from "../../../config";
Expand All @@ -13,7 +11,6 @@ export const transferERC20 = async function (sum: string, tokenAddress: string,
const ethProvider = ethers.getDefaultProvider(localConfig.L1Network);
const syncWallet = new zksync.Wallet(localConfig.privateKey, syncProvider, ethProvider);
const syncWallet2 = new zksync.Wallet(Wallets.secondaryWalletPrivateKey, syncProvider, ethProvider);
const bufferFile = path.join(Path.playbookRoot, Buffer.txEthTransfer);

const transfer = await syncWallet.transfer({
to: syncWallet2.address,
Expand All @@ -24,7 +21,7 @@ export const transferERC20 = async function (sum: string, tokenAddress: string,

const txHash = transfer.hash;
await helper.logTransaction(Logger.transfer, txHash, tokenName);
await fs.writeFile(bufferFile, txHash);
await helper.writeFile(Path.absolutePathToBufferFiles, Buffer.txEthTransfer, txHash);

return txHash;
};
Loading

0 comments on commit a82496a

Please sign in to comment.