Skip to content

Commit

Permalink
chore: speed up deploy-erc20 script (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimazhornyk authored Feb 29, 2024
1 parent 80e22f3 commit 6e14c97
Showing 1 changed file with 65 additions and 10 deletions.
75 changes: 65 additions & 10 deletions l1-contracts/scripts/deploy-erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as hardhat from "hardhat";

import "@nomiclabs/hardhat-ethers";
import { Command } from "commander";
import type { Contract } from "ethers";
import { Wallet } from "ethers";
import { parseEther } from "ethers/lib/utils";
import { web3Provider } from "./utils";
Expand All @@ -25,8 +26,64 @@ type Token = {

type TokenDescription = Token & {
implementation?: string;
contract?: Contract;
};

async function deployContracts(tokens: TokenDescription[], wallet: Wallet): Promise<number> {
let nonce = await wallet.getTransactionCount("pending");

for (const token of tokens) {
token.implementation = token.implementation || DEFAULT_ERC20;
const tokenFactory = await hardhat.ethers.getContractFactory(token.implementation, wallet);
const args = token.implementation !== "WETH9" ? [token.name, token.symbol, token.decimals] : [];

token.contract = await tokenFactory.deploy(...args, { gasLimit: 5000000, nonce: nonce++ });
}

await Promise.all(tokens.map(async (token) => token.contract.deployTransaction.wait()));

return nonce;
}

function getTestAddresses(): string[] {
return Array.from(
{ length: 10 },
(_, i) =>
Wallet.fromMnemonic(ethTestConfig.test_mnemonic as string, `m/44'/60'/0'/0/${i}`).connect(provider).address
);
}

function unwrapToken(token: TokenDescription): Token {
token.address = token.contract.address;

delete token.contract;
if (token.implementation) {
delete token.implementation;
}

return token;
}

async function mintTokens(tokens: TokenDescription[], wallet: Wallet, nonce: number): Promise<Token[]> {
const targetAddresses = [wallet.address, ...getTestAddresses()];

const results = [];
const promises = [];
for (const token of tokens) {
if (token.implementation !== "WETH9") {
for (const address of targetAddresses) {
const tx = await token.contract.mint(address, parseEther("3000000000"), { nonce: nonce++ });
promises.push(tx.wait());
}
}

results.push(unwrapToken(token));
}
await Promise.all(promises);

return results;
}

async function deployToken(token: TokenDescription, wallet: Wallet): Promise<Token> {
token.implementation = token.implementation || DEFAULT_ERC20;
const tokenFactory = await hardhat.ethers.getContractFactory(token.implementation, wallet);
Expand All @@ -36,12 +93,12 @@ async function deployToken(token: TokenDescription, wallet: Wallet): Promise<Tok

if (token.implementation !== "WETH9") {
await erc20.mint(wallet.address, parseEther("3000000000"));
}
for (let i = 0; i < 10; ++i) {
const testWallet = Wallet.fromMnemonic(ethTestConfig.test_mnemonic as string, "m/44'/60'/0'/0/" + i).connect(
provider
);
if (token.implementation !== "WETH9") {

for (let i = 0; i < 10; ++i) {
const testWallet = Wallet.fromMnemonic(ethTestConfig.test_mnemonic as string, "m/44'/60'/0'/0/" + i).connect(
provider
);

await erc20.mint(testWallet.address, parseEther("3000000000"));
}
}
Expand Down Expand Up @@ -90,15 +147,13 @@ async function main() {
.description("Adds a multiple tokens given in JSON format")
.action(async (tokens_json: string, cmd) => {
const tokens: Array<TokenDescription> = JSON.parse(tokens_json);
const result = [];

const wallet = cmd.privateKey
? new Wallet(cmd.privateKey, provider)
: Wallet.fromMnemonic(ethTestConfig.mnemonic, "m/44'/60'/0'/0/1").connect(provider);

for (const token of tokens) {
result.push(await deployToken(token, wallet));
}
const nonce = await deployContracts(tokens, wallet);
const result = await mintTokens(tokens, wallet, nonce);

console.log(JSON.stringify(result, null, 2));
});
Expand Down

0 comments on commit 6e14c97

Please sign in to comment.