From e123717ee803be0c4626f004538b9f05db65d1f1 Mon Sep 17 00:00:00 2001 From: irubido Date: Tue, 30 Jan 2024 13:56:11 +0100 Subject: [PATCH] log formating, interval promise --- packages/plugin/package.json | 2 + ...ultichainHardhatRuntimeEnvironmentField.ts | 30 ++++++++++---- packages/plugin/src/utils.ts | 39 +++++++++++-------- 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/packages/plugin/package.json b/packages/plugin/package.json index 741bae2..e5f2e5f 100644 --- a/packages/plugin/package.json +++ b/packages/plugin/package.json @@ -37,9 +37,11 @@ "@types/node": "^18", "chai": "^4.2.0", "chai-as-promised": "^7.1.1", + "chalk": "^5.3.0", "eslint": "^8", "hardhat": "^2.0.0", "mocha": "^10", + "terminal-link": "^3.0.0", "ts-node": "^10", "typescript": "^5" }, diff --git a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts index e8951c0..e7ee490 100644 --- a/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts +++ b/packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts @@ -7,6 +7,8 @@ import Web3, { PayableCallOptions, } from "web3"; import { vars } from "hardhat/config"; +import chalk from "chalk"; +import terminalLink from "terminal-link"; import { getConfigEnvironmentVariable, getNetworkChainId, @@ -138,10 +140,17 @@ export class MultichainHardhatRuntimeEnvironmentField { fees ) .send(payableTxOptions); - - const { from, to, transactionHash } = receipt; + const networkNames = Object.keys(networkArgs); + const { transactionHash } = receipt; console.log( - `Transacion sent from ${from} to ${to}, transaction hash: ${transactionHash}` + `Multichain deployment initiated, transaction hash: ${chalk.bold( + transactionHash + )} + + ` + + "\n" + + "Destinaton networks:" + + networkNames.join("\r\n") ); const [deployer] = await this.web3.eth.getAccounts(); @@ -153,8 +162,6 @@ export class MultichainHardhatRuntimeEnvironmentField { return deployDomain.chainId; }); - const networkNames = Object.keys(networkArgs); - const deploymentInfo: DeploymentInfo[] = await Promise.all( destinationDomainChainIDs.map(async (domainChainID, index) => { const network = networkNames[index]; @@ -168,15 +175,22 @@ export class MultichainHardhatRuntimeEnvironmentField { ) .call(); console.log( - `Contract address for ${network.toUpperCase()}: ${contractAddress}` + `Contract deployed on ${chalk.bold( + network.toUpperCase() + )}: ${chalk.bold(contractAddress)}` ); - const explorerUrl = transferStatusInterval( + const explorerUrl = await transferStatusInterval( this.hre.config.multichain.environment, transactionHash, domainChainID ); - console.log(`Bridge transfer executed, explorer url: ${explorerUrl}`); + const explorerLink = terminalLink(explorerUrl, explorerUrl); + console.log( + `Bridge transfer executed. More details: ${chalk.underline( + explorerLink + )}` + ); return { network, diff --git a/packages/plugin/src/utils.ts b/packages/plugin/src/utils.ts index d08aa57..bbec2aa 100644 --- a/packages/plugin/src/utils.ts +++ b/packages/plugin/src/utils.ts @@ -155,28 +155,35 @@ export function mapNetworkArgs( }; } -export function transferStatusInterval( +export async function transferStatusInterval( environment: Environment, txHash: string, domainID: number -): string { - let controller: AbortController; +): Promise { let explorerUrl: string = ""; - const interval = setInterval(() => { - controller = new AbortController(); - void getTransferStatusData(environment, txHash, domainID.toString()).then( - (transferStatus) => { - explorerUrl = transferStatus.explorerUrl; - - if (transferStatus.status === "executed") { - clearInterval(interval); - controller.abort(); - return; + await new Promise((resolve) => { + let controller: AbortController; + setInterval(() => { + controller = new AbortController(); + void getTransferStatusData(environment, txHash, domainID.toString()).then( + (transferStatus) => { + explorerUrl = transferStatus.explorerUrl; + + if (transferStatus.status === "executed") { + controller.abort(); + resolve(explorerUrl); + } + if (transferStatus.status === "failed") { + throw new HardhatPluginError( + "@chainsafe/hardhat-plugin-multichain-deploy", + `Bridge transfer failed` + ); + } } - } - ); - }, 1000) as unknown as number; + ); + }, 1000); + }); return explorerUrl; }