From a3b141ca1eebeb7df8f592a574e92302377c007a Mon Sep 17 00:00:00 2001 From: yu23ki14 Date: Thu, 3 Oct 2024 23:06:32 +0900 Subject: [PATCH] add loading --- pkgs/cli/src/modules/fractiontoken.ts | 21 ++++++++++++++++++++- pkgs/cli/src/services/loading.ts | 14 ++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 pkgs/cli/src/services/loading.ts diff --git a/pkgs/cli/src/modules/fractiontoken.ts b/pkgs/cli/src/modules/fractiontoken.ts index 9c12409..7b0ff6a 100644 --- a/pkgs/cli/src/modules/fractiontoken.ts +++ b/pkgs/cli/src/modules/fractiontoken.ts @@ -1,6 +1,7 @@ -import { Address } from "viem"; +import { Address, decodeEventLog } from "viem"; import { publicClient, walletClient } from ".."; import { fractionTokenBaseConfig } from "../config"; +import { startLoading } from "../services/loading"; export const getTokenId = async (hatId: bigint, account: Address) => { const res = await publicClient.readContract({ @@ -29,6 +30,7 @@ export const sendFractionToken = async ( hatId: bigint, amount: bigint ) => { + const stop = startLoading(); const tokenId = await getTokenId(hatId, walletClient.account?.address!); const { request } = await publicClient.simulateContract({ @@ -39,5 +41,22 @@ export const sendFractionToken = async ( }); const transactionHash = await walletClient.writeContract(request); + const receipt = await publicClient.waitForTransactionReceipt({ + hash: transactionHash, + }); + + const log = receipt.logs.find((log) => { + const decodedLog = decodeEventLog({ + abi: fractionTokenBaseConfig.abi, + data: log.data, + topics: log.topics, + }); + return decodedLog.eventName === "TransferSingle"; + }); + + stop(); + + console.log(log); + return transactionHash; }; diff --git a/pkgs/cli/src/services/loading.ts b/pkgs/cli/src/services/loading.ts new file mode 100644 index 0000000..c782f5a --- /dev/null +++ b/pkgs/cli/src/services/loading.ts @@ -0,0 +1,14 @@ +export const startLoading = () => { + const brailleChars = ["⠁", "⠃", "⠇", "⡇", "⡏", "⡟", "⡿", "⡿", "⣿"]; + + let index = 0; + const interval = setInterval(() => { + process.stdout.write(`\rLoading ${brailleChars[index]} `); + index = (index + 1) % brailleChars.length; + }, 200); + + return () => { + clearInterval(interval); + console.log("Done!\n"); + }; +};