@solarity/hardhat-migrate
Version:
The simplest way to deploy smart contracts
97 lines • 3.21 kB
JavaScript
import axios from "axios";
import { ethers } from "ethers";
import { ExtendedHardhatEthersSigner } from "./ExtendedHardhatEthersSigner.js";
import { createEthersProvider, ethersProvider } from "./EthersProvider.js";
import { toJSON } from "../../utils/index.js";
import { createTransactionRunner } from "../runners/TransactionRunner.js";
class StateMiddleware {
static pendingRequests = {};
static async retry(fn, args) {
const cacheKey = ethers.id(`${fn.name}:${toJSON(args)}`);
if (this.pendingRequests[cacheKey]) {
return this.pendingRequests[cacheKey];
}
const workPromise = fn(...args);
this.pendingRequests[cacheKey] = workPromise;
try {
return await workPromise;
}
finally {
delete this.pendingRequests[cacheKey];
}
}
}
class NetworkManager {
axios;
provider;
_currentFrom = undefined;
_signers = {};
constructor() {
this.axios = this.withRetry(axios);
this.provider = this.withRetry(ethersProvider);
}
getProvider() {
return this.provider;
}
async getEthersSigner(from) {
if (!from) {
return this.provider.provider.getSigner(this._currentFrom);
}
return this.provider.getSigner(await ethers.resolveAddress(from));
}
async getSigner(from) {
if (from && this._signers[from]) {
return this._signers[from];
}
const signer = await this._getSigner(from);
this._signers[await signer.getAddress()] = signer;
if (from) {
this._signers[from] = signer;
}
return signer;
}
async setSigner(from) {
this._currentFrom = from ? await ethers.resolveAddress(from) : from;
}
withRetry(instance) {
return new Proxy(instance, {
get(target, propKey, receiver) {
const origMethod = target[propKey];
if (typeof origMethod === "function") {
return (...args) => {
return StateMiddleware.retry(origMethod.bind(target), args);
};
}
return Reflect.get(target, propKey, receiver);
},
});
}
async _getSigner(from) {
if (!from) {
return ExtendedHardhatEthersSigner.fromSignerName(this._currentFrom);
}
// From specified as name. Cast Wallet branch.
if (!ethers.isAddress(from)) {
return ExtendedHardhatEthersSigner.fromSignerName(from);
}
// From specified as address. HardhatEthersProvider branch.
const address = await ethers.resolveAddress(from);
return ExtendedHardhatEthersSigner.fromSignerName(address);
}
}
export let networkManager = null;
export async function buildNetworkDeps(hre) {
await createEthersProvider(hre);
createTransactionRunner(hre);
if (networkManager) {
return;
}
networkManager = new NetworkManager();
}
/**
* Used only in test environments to ensure test atomicity
*/
export function resetNetworkManager() {
networkManager = null;
}
//# sourceMappingURL=NetworkManager.js.map