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

use hardhat config from hardhat runtime environment #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
37 changes: 9 additions & 28 deletions src/MetamaskClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {BaseProvider} from '@ethersproject/providers';
import hre, { ethers } from "hardhat";
import "@nomicfoundation/hardhat-ethers";
import {SignerWithAddress} from "@nomicfoundation/hardhat-ethers/signers";
import "@typechain/hardhat";
Expand All @@ -22,38 +23,14 @@ export class TransactionWrapper {
}
}

export type ClientConfig = {
hardhatConfig?: any,
networkName?: any,
network?: any,
ethers: any,
}

export class MetamaskClient {
private filledTemplate: string = '';
private readonly port: number;
private id = 1;
private txHashMap = new Map<number, string>();
network: any;
server: any;
ethers: any;

constructor(config: ClientConfig, private estimateGas: boolean = false, defaultServerPort: number = 8989) {
this.ethers = config.ethers;

if (config.hardhatConfig == null && config.network == null)
throw new Error("Invalid configuration");

if (config.network == null) {
if (config.hardhatConfig.networks == null || config.hardhatConfig.networks![config.networkName] == null)
throw new Error("Requested network is not configured: " + config.networkName);
this.network = config.hardhatConfig.networks![config.networkName];
this.network.name = config.networkName;
} else {
this.network = config.network.config;
this.network.name = config.network.name;
}

constructor(private estimateGas: boolean = false, defaultServerPort: number = 8989) {
const app: Express = express();
this.port = process.env.PORT ? Number(process.env.PORT) : defaultServerPort;
app.use(bodyParser.urlencoded({extended: false}));
Expand All @@ -79,7 +56,8 @@ export class MetamaskClient {

public async getSigner(): Promise<SignerWithAddress> {
return new Promise<SignerWithAddress>((resolve, reject) => {
this.ethers.getSigners().then((signers: any[]) => {
// hook hardhat's `ethers` object
ethers.getSigners().then((signers: any[]) => {
let signer = signers[0];
// let f = signer.sendTransaction;
let x = async (transaction: any) => {
Expand Down Expand Up @@ -121,8 +99,11 @@ export class MetamaskClient {
this.filledTemplate = Eta.render(
syncReadFile('template.html'), {
transactions: transactions,
network: this.network.name,
chainId: "0x" + (this.network.chainId).toString(16),
network: hre.network.name,
// `31337` is the chainId of default `localhost` network. Reference: https://hardhat.org/hardhat-network/docs/reference#chainid
// `localhost` network's chainId is not explicitly configured in `hardhat.config.ts`
// so we use `31337` as default value
chainId: "0x" + (hre.network.config.chainId ?? 31337).toString(16),
serverPort: this.port
})! as string;
open(`http://localhost:${this.port}/send-transactions`);
Expand Down