diff --git a/pkgs/cli/src/commands/hats.ts b/pkgs/cli/src/commands/hats.ts index 583ee1f..ac34246 100644 --- a/pkgs/cli/src/commands/hats.ts +++ b/pkgs/cli/src/commands/hats.ts @@ -33,8 +33,9 @@ hatsCommands .description("Show all of the Hats that are associated with the tree ID") .option("-id, --treeId ", "Tree ID") .action(async (options) => { + const { chain } = rootProgram.opts(); // ツリー情報を全て取得する。 - const tree = await getTreeInfo(Number(options.treeId), options.chainId); + const tree = await getTreeInfo(Number(options.treeId), chain); console.log(tree); }); @@ -47,8 +48,9 @@ hatsCommands .description("Show all of the wears that are associated with the hat ID") .option("-id, --hatId ", "Hat ID") .action(async (options) => { + const { chain } = rootProgram.opts(); // ツリー情報を全て取得する。 - const wearers = await getWearersInfo(options.hatId, options.chainId); + const wearers = await getWearersInfo(options.hatId, chain); console.log(wearers); }); @@ -75,16 +77,16 @@ hatsCommands hatsCommands .command("createHat") .description("Create Hat") - .requiredOption("-hid", "--hatId ", "Hat ID") - .requiredOption("-img", "--imageUri ", "Image URI") - .option("-det", "--details
", "Details") - .option("-max", "--maxSupply ", "Max Supply") - .option("-el", "--eligibility ", "Eligibility Address") - .option("-tgl", "--toggle ", "Toggle") - .option("-mut", "--mutable ", "Mutable") + .requiredOption("-phid, --parentHatId ", "Parent Hat ID") + .requiredOption("-img, --imageURI ", "Image URI") + .option("-det , --details
", "Details") + .option("-max, --maxSupply ", "Max Supply") + .option("-el, --eligibility ", "Eligibility Address") + .option("-tgl, --toggle ", "Toggle") + .option("-mut, --mutable ", "Mutable") .action( async ({ - hatId, + parentHatId, details, maxSupply, eligibility, @@ -92,15 +94,17 @@ hatsCommands mutable, imageURI, }) => { - await createHat({ - parentHatId: BigInt(hatId), + const transactionHash = await createHat({ + parentHatId: BigInt(parentHatId), details, maxSupply, eligibility: eligibility as Address, toggle: toggle as Address, - mutable: mutable, + mutable: mutable == "true", imageURI, }); + + console.log("Transaction hash: ", transactionHash); } ); @@ -110,8 +114,10 @@ hatsCommands hatsCommands .command("mintHat") .description("Mint Hat") - .requiredOption("-hid", "--hatId ", "Hat ID") + .requiredOption("-hid, --hatId ", "Hat ID") .requiredOption("--wearer ", "Wearer address") .action(async ({ hatId, wearer }) => { - await mintHat({ hatId, wearer }); + const transactionHash = await mintHat({ hatId, wearer }); + + console.log("Transaction hash: ", transactionHash); }); diff --git a/pkgs/cli/src/config.ts b/pkgs/cli/src/config.ts index 4c9ef54..0dbbe57 100644 --- a/pkgs/cli/src/config.ts +++ b/pkgs/cli/src/config.ts @@ -11,7 +11,6 @@ export const hatsContractBaseConfig = { }; export const hatsTimeFrameContractBaseConfig = { - address: "0xd4a66507ea8c8382fa8474ed6cae4163676a434a" as Address, abi: HATS_TIME_FRAME_MODULE_ABI, }; diff --git a/pkgs/cli/src/modules/hatsProtocol.ts b/pkgs/cli/src/modules/hatsProtocol.ts index 8428332..8097cff 100644 --- a/pkgs/cli/src/modules/hatsProtocol.ts +++ b/pkgs/cli/src/modules/hatsProtocol.ts @@ -6,6 +6,7 @@ import { hatsTimeFrameContractBaseConfig, } from "../config"; import { publicClient, walletClient } from ".."; +import { hatIdToTreeId } from "@hatsprotocol/sdk-v1-core"; // ############################################################### // Read with subgraph @@ -103,6 +104,26 @@ export const getWearerInfo = async (walletAddress: string, chainId: number) => { return wearer; }; +export const getHatsTimeframeModuleAddress = async ( + hatId: string, + chainId: number +) => { + const treeId = hatIdToTreeId(BigInt(hatId)); + const { hats } = await getTreeInfo(treeId, chainId); + const hatterHat = hats?.find((hat) => hat.levelAtLocalTree === 1); + if (!hatterHat) { + throw new Error("Hatter hat not found"); + } + + const wearers = await getWearersInfo(hatterHat.id, chainId); + + if (wearers.length === 0) { + throw new Error("No wearers found for hatter hat"); + } + + return wearers[0].id; +}; + // ############################################################### // Write with viem // ############################################################### @@ -133,7 +154,9 @@ export const createHat = async (args: { args.imageURI, ], }); - walletClient.writeContract(request); + const transactionHash = walletClient.writeContract(request); + + return transactionHash; }; /** @@ -142,9 +165,15 @@ export const createHat = async (args: { export const mintHat = async (args: { hatId: bigint; wearer: Address }) => { const { request } = await publicClient.simulateContract({ ...hatsTimeFrameContractBaseConfig, + address: await getHatsTimeframeModuleAddress( + args.hatId.toString(), + Number(publicClient.chain?.id) + ), account: walletClient.account, functionName: "mintHat", args: [args.hatId, args.wearer], }); - walletClient.writeContract(request); + const transactionHash = await walletClient.writeContract(request); + + return transactionHash; };