hardhat/types#Network TypeScript Examples

The following examples show how to use hardhat/types#Network. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts    From hardhat-deploy with MIT License 7 votes vote down vote up
export function processNamedAccounts(
  network: Network,
  namedAccounts: {
    [name: string]:
      | string
      | number
      | {[network: string]: null | number | string};
  },
  accounts: string[],
  chainIdGiven: string
): {
  namedAccounts: {[name: string]: string};
  unnamedAccounts: string[];
  unknownAccounts: string[];
  addressesToProtocol: {[address: string]: string};
} {
  if (namedAccounts) {
    return transformNamedAccounts(
      namedAccounts,
      chainIdGiven,
      accounts,
      process.env.HARDHAT_DEPLOY_ACCOUNTS_NETWORK || getNetworkName(network)
    );
  } else {
    return {
      namedAccounts: {},
      unnamedAccounts: accounts,
      unknownAccounts: [],
      addressesToProtocol: {},
    };
  }
}
Example #2
Source File: utils.ts    From hardhat-deploy with MIT License 7 votes vote down vote up
export function getNetworkName(network: Network): string {
  if (process.env['HARDHAT_DEPLOY_FORK']) {
    return process.env['HARDHAT_DEPLOY_FORK'];
  }
  if ('forking' in network.config && (network.config.forking as any)?.network) {
    return (network.config.forking as any)?.network;
  }
  return network.name;
}
Example #3
Source File: utils.ts    From hardhat-deploy with MIT License 6 votes vote down vote up
export function getDeployPaths(network: Network): string[] {
  const networkName = getNetworkName(network);
  if (networkName === network.name) {
    return network.deploy || store.networks[networkName]?.deploy; // fallback to global store
  } else {
    return store.networks[networkName]?.deploy; // skip network.deploy
  }
}
Example #4
Source File: verifier.ts    From balancer-v2-monorepo with GNU General Public License v3.0 5 votes vote down vote up
constructor(_network: Network, _apiKey: string) {
    this.network = _network;
    this.apiKey = _apiKey;
  }
Example #5
Source File: verifier.ts    From balancer-v2-monorepo with GNU General Public License v3.0 5 votes vote down vote up
network: Network;
Example #6
Source File: DeploymentsManager.ts    From hardhat-deploy with MIT License 5 votes vote down vote up
private network: Network;
Example #7
Source File: globalStore.ts    From hardhat-deploy with MIT License 5 votes vote down vote up
store: {
  networks: {[name: string]: Network};
} = {
  networks: {},
}
Example #8
Source File: DeploymentsManager.ts    From hardhat-deploy with MIT License 4 votes vote down vote up
constructor(env: HardhatRuntimeEnvironment, network: Network) {
    log('constructing DeploymentsManager');

    this.network = network;

    this.impersonateUnknownAccounts = true;
    this.impersonatedAccounts = [];
    this.db = {
      gasUsed: BigNumber.from(0),
      accountsLoaded: false,
      namedAccounts: {},
      unnamedAccounts: [],
      deploymentsLoaded: false,
      deployments: {},
      migrations: {},
      writeDeploymentsToFiles: true, // should default to true ? so we can run scripts that use `deploy` by default
      fixtureCounter: 0,
      snapshotCounter: 0,
      pastFixtures: {},
      logEnabled: process.env['HARDHAT_DEPLOY_LOG'] ? true : false,
      pendingTransactions: {},
      savePendingTx: false,
      gasPrice: undefined,
      runAsNode: false,
    };
    this.env = env;
    this.deploymentsPath = env.config.paths.deployments;

    // TODO
    // this.env.artifacts = new HardhatDeployArtifacts(this.env.artifacts);

    this.partialExtension = {
      readDotFile: async (name: string): Promise<string> =>
        this.readDotFile(name),
      saveDotFile: async (name: string, content: string): Promise<void> =>
        this.saveDotFile(name, content),
      deleteDotFile: async (name: string): Promise<void> =>
        this.deleteDotFile(name),
      save: async (
        name: string,
        deployment: DeploymentSubmission
      ): Promise<void> => {
        this.saveDeployment(name, deployment);
      },
      delete: async (name: string): Promise<void> =>
        this.deleteDeployment(name),
      get: async (name: string) => {
        await this.setup(false);
        const deployment = this.db.deployments[name];
        if (deployment === undefined) {
          throw new Error(`No deployment found for: ${name}`);
        }
        return deployment;
      },
      getOrNull: async (name: string) => {
        await this.setup(false);
        return this.db.deployments[name];
      },
      getDeploymentsFromAddress: async (address: string) => {
        const deployments = [];
        for (const deployment of Object.values(this.db.deployments)) {
          if (deployment.address === address) {
            deployments.push(deployment);
          }
        }
        return deployments;
      },
      all: async () => {
        await this.setup(false);
        return this.db.deployments; // TODO copy
      },
      getArtifact: async (contractName: string): Promise<Artifact> => {
        if (this.db.onlyArtifacts) {
          const artifactFromFolder = await getArtifactFromFolders(
            contractName,
            this.db.onlyArtifacts
          );
          if (!artifactFromFolder) {
            throw new Error(
              `cannot find artifact "${contractName}" from folder ${this.db.onlyArtifacts}`
            );
          }
          return artifactFromFolder as Artifact;
        }
        let artifact: Artifact | ExtendedArtifact | undefined =
          await getArtifactFromFolders(contractName, [
            this.env.config.paths.artifacts,
          ]);
        if (artifact) {
          return artifact as Artifact;
        }
        const importPaths = this.getImportPaths();
        artifact = await getArtifactFromFolders(contractName, importPaths);

        if (!artifact) {
          throw new Error(`cannot find artifact "${contractName}"`);
        }
        return artifact as Artifact;
      },
      getExtendedArtifact: async (
        contractName: string
      ): Promise<ExtendedArtifact> => {
        if (this.db.onlyArtifacts) {
          const artifactFromFolder = await getExtendedArtifactFromFolders(
            contractName,
            this.db.onlyArtifacts
          );
          if (!artifactFromFolder) {
            throw new Error(
              `cannot find artifact "${contractName}" from folder ${this.db.onlyArtifacts}`
            );
          }
          return artifactFromFolder as ExtendedArtifact;
        }
        let artifact: ExtendedArtifact | undefined =
          await getExtendedArtifactFromFolders(contractName, [
            this.env.config.paths.artifacts,
          ]);
        if (artifact) {
          return artifact;
        }
        const importPaths = this.getImportPaths();
        artifact = await getExtendedArtifactFromFolders(
          contractName,
          importPaths
        );
        if (artifact) {
          return artifact;
        }

        if (!artifact) {
          throw new Error(`cannot find artifact "${contractName}"`);
        }
        return artifact;
      },
      run: (
        tags?: string | string[],
        options: {
          resetMemory?: boolean;
          deletePreviousDeployments?: boolean;
          writeDeploymentsToFiles?: boolean;
          export?: string;
          exportAll?: string;
        } = {
          resetMemory: true,
          writeDeploymentsToFiles: false,
          deletePreviousDeployments: false,
        }
      ) => {
        return this.runDeploy(tags, {
          resetMemory:
            options.resetMemory === undefined ? true : options.resetMemory,
          deletePreviousDeployments:
            options.deletePreviousDeployments === undefined
              ? false
              : options.deletePreviousDeployments,
          writeDeploymentsToFiles:
            options.writeDeploymentsToFiles === undefined
              ? false
              : options.writeDeploymentsToFiles,
          export: options.export,
          exportAll: options.exportAll,
          log: false,
          savePendingTx: false,
        });
      },
      fixture: async (
        tags?: string | string[],
        options?: {
          fallbackToGlobal?: boolean;
          keepExistingDeployments?: boolean;
        }
      ) => {
        await this.setup(tags === undefined);
        options = {fallbackToGlobal: true, ...options};
        if (typeof tags === 'string') {
          tags = [tags];
        }
        const globalKey = '::global';
        const globalFixture = this.db.pastFixtures[globalKey];

        let fixtureKey = globalKey;
        if (tags !== undefined) {
          fixtureKey = '::' + tags.join('.');
        }

        if (this.db.pastFixtures[fixtureKey]) {
          const pastFixture = this.db.pastFixtures[fixtureKey];
          const success = await this.revertSnapshot(pastFixture);
          if (success) {
            return this.db.deployments;
          } else {
            delete this.db.pastFixtures[fixtureKey];
          }
        }
        if (globalFixture && options.fallbackToGlobal) {
          const success = await this.revertSnapshot(globalFixture);
          if (success) {
            return this.db.deployments;
          } else {
            delete this.db.pastFixtures[globalKey];
          }
        }
        await this.runDeploy(tags, {
          resetMemory: !options.keepExistingDeployments,
          writeDeploymentsToFiles: false,
          deletePreviousDeployments: false,
          log: false,
          savePendingTx: false,
        });

        await this.saveSnapshot(fixtureKey);
        return this.db.deployments;
      },
      createFixture: <T, O>(func: FixtureFunc<T, O>) => {
        const baseId = '' + ++this.db.fixtureCounter + '::';
        return async (options?: O) => {
          let id = baseId;
          if (options !== undefined) {
            id = id + JSON.stringify(options);
          }
          const saved = this.db.pastFixtures[id];
          if (saved) {
            const success = await this.revertSnapshot(saved);
            if (success) {
              return saved.data;
            }
          }
          const data = await func(this.env, options);
          await this.saveSnapshot(id, data);
          return data;
        };
      },
      log: (...args: any[]) => {
        if (this.db.logEnabled) {
          console.log(...args);
        }
      },
      getNetworkName: () => this.getNetworkName(),
      getGasUsed: () => this.db.gasUsed.toNumber(),
    } as PartialExtension;

    const print = (msg: string) => {
      if (this.db.logEnabled) {
        process.stdout.write(msg);
      }
    };

    log('adding helpers');
    const helpers = addHelpers(
      this,
      this.partialExtension,
      this.network,
      this.partialExtension.getArtifact,
      async (
        name: string,
        deployment: DeploymentSubmission,
        artifactName?: string
      ): Promise<void> => {
        if (
          artifactName &&
          this.db.writeDeploymentsToFiles &&
          this.network.saveDeployments
        ) {
          // toSave (see deployments.save function)
          const extendedArtifact =
            await this.partialExtension.getExtendedArtifact(artifactName);
          deployment = {
            ...deployment,
            ...extendedArtifact,
          };
        }
        await this.partialExtension.save(name, deployment);
      },
      () => {
        return this.db.writeDeploymentsToFiles && this.network.saveDeployments;
      },
      this.onPendingTx.bind(this),
      async () => {
        // TODO extraGasPrice ?
        let gasPrice: BigNumber | undefined;
        let maxFeePerGas: BigNumber | undefined;
        let maxPriorityFeePerGas: BigNumber | undefined;
        if (this.db.gasPrice) {
          gasPrice = BigNumber.from(this.db.gasPrice);
        } else {
          if (this.db.maxFeePerGas) {
            maxFeePerGas = BigNumber.from(this.db.maxFeePerGas);
          }
          if (this.db.maxPriorityFeePerGas) {
            maxPriorityFeePerGas = BigNumber.from(this.db.maxPriorityFeePerGas);
          }
        }
        return {gasPrice, maxFeePerGas, maxPriorityFeePerGas};
      },
      this.partialExtension.log,
      print
    );

    this.deploymentsExtension = helpers.extension;
    this.utils = helpers.utils;
  }