From caccd7b4391479f5cedc1de052074fdf526776fd Mon Sep 17 00:00:00 2001 From: Paul <108695806+pxrl@users.noreply.github.com> Date: Mon, 4 Sep 2023 16:11:38 +0200 Subject: [PATCH] improve: Overhaul print formatting This addresses a few nits in the code, but also makes it simpler. --- scripts/spokepool.ts | 69 ++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 48 deletions(-) diff --git a/scripts/spokepool.ts b/scripts/spokepool.ts index fcf999243..f9ebd0188 100644 --- a/scripts/spokepool.ts +++ b/scripts/spokepool.ts @@ -22,17 +22,6 @@ const chains = [1, 10, 137, 324, 8453, 42161]; const padLeft = 20; const padRight = 25; -// @todo: Use SDK-v2 createShortHexString() after bumping version. -function formatAddress(address: string, maxWidth = 18): string { - const separator = "..."; - const textLen = maxWidth - separator.length; - return ( - address.substring(0, Math.ceil(textLen / 2)) + - "..." + - address.substring(address.length - Math.floor(textLen / 2), address.length) - ); -} - function validateChainIds(chainIds: number[]): boolean { const knownChainIds = [...chains, ...testChains]; return chainIds.every((chainId) => { @@ -45,49 +34,33 @@ function validateChainIds(chainIds: number[]): boolean { } function printDeposit(log: LogDescription): void { - const originChainId = log.args.originChainId as number; - const destinationChainId = log.args.destinationChainId as number; - const depositor = log.args.depositor as string; - const recipient = log.args.depositor as string; - const originToken = log.args.originToken; - const amount = log.args.amount as BigNumber; - - const tokenSymbol = resolveTokenSymbols([originToken], originChainId)[0]; + const { originChainId, originToken } = log.args; + const eventArgs = Object.keys(log.args).filter((key) => isNaN(Number(key))); + const padLeft = eventArgs.reduce((acc, cur) => cur.length > acc ? cur.length : acc, 0); + + const fields = { + tokenSymbol: resolveTokenSymbols([originToken], originChainId)[0], + ...Object.fromEntries(eventArgs.map((key) => [key, log.args[key]])), + }; console.log( - `Fill for ${getNetworkName(originChainId)} deposit # ${log.args.depositId}\n` + - `\t${"Depositor".padEnd(padLeft)}: ${formatAddress(depositor, padRight)}\n` + - `\t${"Recipient".padEnd(padLeft)}: ${formatAddress(recipient, padRight)}\n` + - `\t${"Origin chain".padEnd(padLeft)}: ${getNetworkName(originChainId).toString().padStart(padRight)}\n` + - `\t${"Destination chain".padEnd(padLeft)}: ${getNetworkName(destinationChainId).padStart(padRight)}\n` + - `\t${"Token".padEnd(padLeft)}: ${tokenSymbol.padStart(padRight)}\n` + - `\t${"Amount".padEnd(padLeft)}: ${amount.toString().padStart(padRight)}\n` + - `\t${"Relayer Fee".padEnd(padLeft)}: ${log.args.relayerFeePct.toString().padStart(padRight)}\n` + `Deposit # ${log.args.depositId} on ${getNetworkName(originChainId)}:\n` + + Object.entries(fields).map(([k,v]) => `\t${k.padEnd(padLeft)} : ${v}`).join("\n") + "\n" ); } function printFill(log: LogDescription): void { - const originChainId = log.args.originChainId as number; - const destinationChainId = log.args.destinationChainId as number; - const depositor = log.args.depositor as string; - const recipient = log.args.depositor as string; - const destinationToken = log.args.destinationToken; - const amount = log.args.amount as BigNumber; - const totalFilledAmount = log.args.totalFilledAmount as BigNumber; - - const tokenSymbol = resolveTokenSymbols([destinationToken], destinationChainId)[0]; - const totalFilledPct = `${totalFilledAmount.mul(100).div(amount)} %`; + const { originChainId, destinationChainId, destinationToken, amount, totalFilledAmount } = log.args; + const eventArgs = Object.keys(log.args).filter((key) => isNaN(Number(key))); + const padLeft = eventArgs.reduce((acc, cur) => cur.length > acc ? cur.length : acc, 0); + + const fields = { + tokenSymbol: resolveTokenSymbols([destinationToken], destinationChainId)[0], + totalFilledPct: `${totalFilledAmount.mul(100).div(amount)} %`, + ...Object.fromEntries(eventArgs.map((key) => [key, log.args[key]])), + }; console.log( - `Fill for ${getNetworkName(originChainId)} deposit # ${log.args.depositId}\n` + - `\t${"Depositor".padEnd(padLeft)}: ${formatAddress(depositor, padRight)}\n` + - `\t${"Recipient".padEnd(padLeft)}: ${formatAddress(recipient, padRight)}\n` + - `\t${"Origin chain".padEnd(padLeft)}: ${getNetworkName(originChainId).toString().padStart(padRight)}\n` + - `\t${"Destination chain".padEnd(padLeft)}: ${getNetworkName(destinationChainId).padStart(padRight)}\n` + - `\t${"Token".padEnd(padLeft)}: ${tokenSymbol.padStart(padRight)}\n` + - `\t${"Amount".padEnd(padLeft)}: ${amount.toString().padStart(padRight)}\n` + - `\t${"Fill Amount".padEnd(padLeft)}: ${log.args.fillAmount.toString().padStart(padRight)}\n` + - `\t${"Relayer Fee".padEnd(padLeft)}: ${log.args.relayerFeePct.toString().padStart(padRight)}\n` + - `\t${"LP Fee".padEnd(padLeft)}: ${log.args.realizedLpFeePct.toString().padStart(padRight)}\n` + - `\t${"Filled".padEnd(padLeft)}: ${totalFilledPct.padStart(padRight)}\n` + `Fill for ${getNetworkName(originChainId)} deposit # ${log.args.depositId}:\n` + + Object.entries(fields).map(([k,v]) => `\t${k.padEnd(padLeft)} : ${v}`).join("\n") + "\n" ); }