Skip to content

Commit

Permalink
get time frame module address
Browse files Browse the repository at this point in the history
  • Loading branch information
yu23ki14 committed Oct 3, 2024
1 parent 57f4131 commit 5be8d9c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
36 changes: 21 additions & 15 deletions pkgs/cli/src/commands/hats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ hatsCommands
.description("Show all of the Hats that are associated with the tree ID")
.option("-id, --treeId <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);
});
Expand All @@ -47,8 +48,9 @@ hatsCommands
.description("Show all of the wears that are associated with the hat ID")
.option("-id, --hatId <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);
});
Expand All @@ -75,32 +77,34 @@ hatsCommands
hatsCommands
.command("createHat")
.description("Create Hat")
.requiredOption("-hid", "--hatId <hatId>", "Hat ID")
.requiredOption("-img", "--imageUri <imageURI>", "Image URI")
.option("-det", "--details <details>", "Details")
.option("-max", "--maxSupply <maxSupply>", "Max Supply")
.option("-el", "--eligibility <eligibility>", "Eligibility Address")
.option("-tgl", "--toggle <toggle>", "Toggle")
.option("-mut", "--mutable <mutable>", "Mutable")
.requiredOption("-phid, --parentHatId <parentHatId>", "Parent Hat ID")
.requiredOption("-img, --imageURI <imageURI>", "Image URI")
.option("-det , --details <details>", "Details")
.option("-max, --maxSupply <maxSupply>", "Max Supply")
.option("-el, --eligibility <eligibility>", "Eligibility Address")
.option("-tgl, --toggle <toggle>", "Toggle")
.option("-mut, --mutable <mutable>", "Mutable")
.action(
async ({
hatId,
parentHatId,
details,
maxSupply,
eligibility,
toggle,
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);
}
);

Expand All @@ -110,8 +114,10 @@ hatsCommands
hatsCommands
.command("mintHat")
.description("Mint Hat")
.requiredOption("-hid", "--hatId <hatId>", "Hat ID")
.requiredOption("-hid, --hatId <hatId>", "Hat ID")
.requiredOption("--wearer <wearer>", "Wearer address")
.action(async ({ hatId, wearer }) => {
await mintHat({ hatId, wearer });
const transactionHash = await mintHat({ hatId, wearer });

console.log("Transaction hash: ", transactionHash);
});
1 change: 0 additions & 1 deletion pkgs/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const hatsContractBaseConfig = {
};

export const hatsTimeFrameContractBaseConfig = {
address: "0xd4a66507ea8c8382fa8474ed6cae4163676a434a" as Address,
abi: HATS_TIME_FRAME_MODULE_ABI,
};

Expand Down
33 changes: 31 additions & 2 deletions pkgs/cli/src/modules/hatsProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
hatsTimeFrameContractBaseConfig,
} from "../config";
import { publicClient, walletClient } from "..";
import { hatIdToTreeId } from "@hatsprotocol/sdk-v1-core";

// ###############################################################
// Read with subgraph
Expand Down Expand Up @@ -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
// ###############################################################
Expand Down Expand Up @@ -133,7 +154,9 @@ export const createHat = async (args: {
args.imageURI,
],
});
walletClient.writeContract(request);
const transactionHash = walletClient.writeContract(request);

return transactionHash;
};

/**
Expand All @@ -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;
};

0 comments on commit 5be8d9c

Please sign in to comment.