Skip to content

Commit

Permalink
add loading
Browse files Browse the repository at this point in the history
  • Loading branch information
yu23ki14 committed Oct 3, 2024
1 parent 7e92e2b commit a3b141c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
21 changes: 20 additions & 1 deletion pkgs/cli/src/modules/fractiontoken.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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({
Expand All @@ -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;
};
14 changes: 14 additions & 0 deletions pkgs/cli/src/services/loading.ts
Original file line number Diff line number Diff line change
@@ -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");
};
};

0 comments on commit a3b141c

Please sign in to comment.