Skip to content

Commit

Permalink
fix command
Browse files Browse the repository at this point in the history
  • Loading branch information
yu23ki14 committed Oct 3, 2024
1 parent 9fe6f7c commit 83497c6
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 124 deletions.
70 changes: 0 additions & 70 deletions pkgs/cli/src/commands/function.ts

This file was deleted.

23 changes: 20 additions & 3 deletions pkgs/cli/src/commands/hats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import {
getTreeInfo,
getWearerInfo,
getWearersInfo,
mintHat,
} from "../modules/hatsProtocol";
import { getAccount } from "../services/wallet";
import { publicClient, rootProgram, walletClient } from "..";

export const hatsCommands = new Command();

Expand All @@ -29,7 +32,7 @@ hatsCommands
.option("-id, --treeId <treeId>", "Tree ID")
.action(async (options) => {
// ツリー情報を全て取得する。
const tree = await getTreeInfo(Number(options.treeId));
const tree = await getTreeInfo(Number(options.treeId), options.chainId);

console.log(tree);
});
Expand All @@ -43,7 +46,7 @@ hatsCommands
.option("-id, --hatId <hatId>", "Hat ID")
.action(async (options) => {
// ツリー情報を全て取得する。
const wearers = await getWearersInfo(options.hatId);
const wearers = await getWearersInfo(options.hatId, options.chainId);

console.log(wearers);
});
Expand All @@ -57,7 +60,21 @@ hatsCommands
.option("-addr, --address <address>", "Wallet Address")
.action(async (options) => {
// 特定のウォレットアドレスに紐づく情報を全て取得する。
const wearer = await getWearerInfo(options.address);
const address =
options.address || getAccount(rootProgram.opts().profile).address;
const wearer = await getWearerInfo(address, rootProgram.opts().chain);

console.log(wearer);
});

/**
* ロールを付与
*/
hatsCommands
.command("mintHat")
.description("Mint Hat")
.requiredOption("--hatId <hatId>", "Hat ID")
.requiredOption("--wearer <wearer>", "Wearer address")
.action(async ({ hatId, wearer }) => {
await mintHat({ hatId, wearer });
});
34 changes: 5 additions & 29 deletions pkgs/cli/src/commands/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Command } from "commander";
import { getPublicClient, sendEth } from "../modules/viem";
import {
getWallet,
listProfiles,
saveProfile,
deleteProfile,
} from "../services/wallet";
import { mintHat } from "../modules/hatsProtocol";
import { sendEth } from "../modules/viem";
import { listProfiles, saveProfile, deleteProfile } from "../services/wallet";
import { walletClient } from "..";

export const walletCommands = new Command();

Expand Down Expand Up @@ -54,27 +49,8 @@ walletCommands
walletCommands
.command("sendEth")
.description("Send ETH")
.requiredOption("--name <name>", "Wallet name")
.requiredOption("--receiver <receiver>", "Receiver address")
.requiredOption("--amount <amount>", "Amount")
.option("--chainId <chainId>", "chainId")
.action(async ({ name, receiver, amount, chainId }) => {
const wallet = await getWallet(name, chainId);
await sendEth(wallet, receiver, amount);
});

/**
* ロールを付与
*/
walletCommands
.command("mintHat")
.description("Mint Hat")
.requiredOption("--name <name>", "Wallet name")
.requiredOption("--hatId <hatId>", "Hat ID")
.requiredOption("--wearer <wearer>", "Wearer address")
.option("--chainId <chainId>", "chainId")
.action(async ({ name, hatId, wearer, chainId }) => {
const publicClient = await getPublicClient(chainId);
const wallet = await getWallet(name, chainId);
await mintHat(publicClient, wallet, { hatId, wearer });
.action(async ({ receiver, amount }) => {
await sendEth(walletClient, receiver, amount);
});
2 changes: 2 additions & 0 deletions pkgs/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Address } from "viem";
import { HATS_ABI } from "./abi/hats";
import { HATS_TIME_FRAME_MODULE_ABI } from "./abi/hatsTimeFrameModule";

export const skipPreActionCommands = ["wallet>add", "wallet>list"];

export const hatsContractBaseConfig = {
address: "0x3bc1A0Ad72417f2d411118085256fC53CBdDd137" as Address,
abi: HATS_ABI,
Expand Down
35 changes: 29 additions & 6 deletions pkgs/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
#!/usr/bin/env node

import { program } from "commander";
import { functionCommands } from "./commands/function";
import { Command } from "commander";
import { hatsCommands } from "./commands/hats";
import { walletCommands } from "./commands/wallet";
import { PublicClient, WalletClient } from "viem";
import { getPublicClient } from "./modules/viem";
import { getWalletClient } from "./services/wallet";
import { skipPreActionCommands } from "./config";

program.addCommand(functionCommands);
program.addCommand(hatsCommands);
program.addCommand(walletCommands);
export const rootProgram = new Command();

program.parse(process.argv);
export let publicClient!: PublicClient;

export let walletClient!: WalletClient;

rootProgram
.version("0.0.1")
.option("--chain <chain>", "chain id", "11155111")
.option("--profile <profile>", "Wallet profile")
.hook("preAction", async (_, actionCommand) => {
const { chain, profile } = rootProgram.opts();
const parentName = actionCommand.parent?.name();
const commandName = actionCommand.name();

if (!skipPreActionCommands.includes(`${parentName}>${commandName}`)) {
publicClient = await getPublicClient(chain);
walletClient = await getWalletClient(profile, chain);
}
});

rootProgram.addCommand(hatsCommands);
rootProgram.addCommand(walletCommands);

rootProgram.parse(process.argv);
22 changes: 8 additions & 14 deletions pkgs/cli/src/modules/hatsProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
hatsContractBaseConfig,
hatsTimeFrameContractBaseConfig,
} from "../config";
import { publicClient, walletClient } from "..";

// ###############################################################
// Read with subgraph
Expand All @@ -31,9 +32,9 @@ export const hatsSubgraphClient = new HatsSubgraphClient({
/**
* ツリー情報を取得するメソッド
*/
export const getTreeInfo = async (treeId: number) => {
export const getTreeInfo = async (treeId: number, chainId: number) => {
const tree = await hatsSubgraphClient.getTree({
chainId: optimism.id,
chainId,
treeId: treeId,
props: {
hats: {
Expand All @@ -59,10 +60,10 @@ export const getTreeInfo = async (treeId: number) => {
/**
* 帽子の着用者のウォレットアドレスを一覧を取得するメソッド
*/
export const getWearersInfo = async (hatId: string) => {
export const getWearersInfo = async (hatId: string, chainId: number) => {
// get the first 10 wearers of a given hat
const wearers = await hatsSubgraphClient.getWearersOfHatPaginated({
chainId: optimism.id,
chainId,
props: {},
hatId: BigInt(hatId),
page: 0,
Expand All @@ -75,10 +76,10 @@ export const getWearersInfo = async (hatId: string) => {
/**
* 特定のウォレットアドレスが着用している全てのHats情報を取得するメソッド
*/
export const getWearerInfo = async (walletAddress: string) => {
export const getWearerInfo = async (walletAddress: string, chainId: number) => {
// get the wearer of a given hat
const wearer = await hatsSubgraphClient.getWearer({
chainId: optimism.id,
chainId,
wearerAddress: walletAddress as `0x${string}`,
props: {
currentHats: {
Expand Down Expand Up @@ -142,14 +143,7 @@ export const createHat = async (
/**
* ロール付与
*/
export const mintHat = async (
publicClient: PublicClient,
walletClient: WalletClient,
args: {
hatId: bigint;
wearer: Address;
}
) => {
export const mintHat = async (args: { hatId: bigint; wearer: Address }) => {
const { request } = await publicClient.simulateContract({
...hatsTimeFrameContractBaseConfig,
account: walletClient.account,
Expand Down
8 changes: 6 additions & 2 deletions pkgs/cli/src/services/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ export const getAccount = (name?: string) => {
const profiles = getProfiles();
const profile = profiles.find((p) => p.name === name) || profiles[0];

if (!profile) throw "Profile not found.";
if (!profile)
throw "Profile not found. Please add a profile with wallet add command.";

return privateKeyToAccount(profile.privateKey);
};

export const getWallet = (name?: string, chainId?: number | undefined) => {
export const getWalletClient = (
name?: string,
chainId?: number | undefined
) => {
const account = getAccount(name);

return setWallet(account, chainId);
Expand Down

0 comments on commit 83497c6

Please sign in to comment.