From df13a8fc787210fdb7ed4293eb7412b659c6ae25 Mon Sep 17 00:00:00 2001 From: yawn <69970183+yawn-c111@users.noreply.github.com> Date: Mon, 30 Sep 2024 21:39:25 +0900 Subject: [PATCH 1/2] feat: viem client and sendEth --- pkgs/cli/src/commands/wallet.ts | 18 +++++----- pkgs/cli/src/modules/viem.ts | 58 ++++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/pkgs/cli/src/commands/wallet.ts b/pkgs/cli/src/commands/wallet.ts index 6da93ab..1c0b2f0 100644 --- a/pkgs/cli/src/commands/wallet.ts +++ b/pkgs/cli/src/commands/wallet.ts @@ -1,11 +1,9 @@ import { Command } from "commander"; import { getEthAddress, sendEth } from "../modules/viem"; -import { listProfiles, saveProfile } from "../services/wallet"; +import { getWallet, listProfiles, saveProfile } from "../services/wallet"; export const walletCommands = new Command(); -const { TOBAN_PRIVATE_KEY } = process.env; - walletCommands .name("wallet") .description("This is a CLI function for toban project") @@ -39,11 +37,11 @@ walletCommands walletCommands .command("sendEth") .description("Send ETH") - .action(async () => { - console.log("Start send ETH"); - const address1 = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8"; - - await sendEth(TOBAN_PRIVATE_KEY as `0x${string}`, address1); - - console.log("End send ETH"); + .requiredOption("--name ", "Wallet name") + .requiredOption("--receiver ", "Receiver address") + .requiredOption("--amount ", "Amount") + .option("--chainId ", "chainId") + .action(async ({ name, receiver, amount, chainId }) => { + const account = getWallet(name); + await sendEth(account, receiver, amount, chainId); }); diff --git a/pkgs/cli/src/modules/viem.ts b/pkgs/cli/src/modules/viem.ts index 0f703c6..6387f09 100644 --- a/pkgs/cli/src/modules/viem.ts +++ b/pkgs/cli/src/modules/viem.ts @@ -1,28 +1,62 @@ -import { createWalletClient, http, parseEther } from "viem"; +import { + Chain, + createWalletClient, + http, + parseEther, + PrivateKeyAccount, +} from "viem"; import { privateKeyToAccount } from "viem/accounts"; -import { hardhat } from "viem/chains"; +import { hardhat, sepolia, holesky } from "viem/chains"; -/** - * ETHを送金するためのメソッド - * @param secretKey - * @param to - * @returns - */ -export const sendEth = async (secretKey: `0x${string}`, to: `0x${string}`) => { - const account = privateKeyToAccount(secretKey); +const chains = [hardhat, sepolia, holesky]; + +function getChainById(chainId: number | string): Chain { + const numericChainId = Number(chainId); + + const chain = chains.find((c) => c.id === numericChainId); + + if (!chain) { + throw new Error(`Chain with id ${numericChainId} not found`); + } + + return chain; +} + +export const setClient = async ( + account: PrivateKeyAccount, + chainId?: number | undefined +) => { + const chain = chainId ? getChainById(chainId) : holesky; const client = createWalletClient({ account, - chain: hardhat, + chain, transport: http(), }); + return client; +}; + +export const sendEth = async ( + account: PrivateKeyAccount, + to: `0x${string}`, + amount: string, + chainId?: number +) => { + const client = await setClient(account, chainId); + const hash = await client.sendTransaction({ account, to: to, - value: parseEther("0.001"), + value: parseEther(amount), }); + console.log(`Transaction sent: ${hash}`); + console.log(`From: ${account.address}`); + console.log(`To: ${to}`); + console.log(`Amount: ${amount} ETH`); + console.log(`Chain ID: ${client.chain.id}`); + return hash; }; From 015c0b998cb9e4dfec35635293da1b025a8db30f Mon Sep 17 00:00:00 2001 From: yawn <69970183+yawn-c111@users.noreply.github.com> Date: Mon, 30 Sep 2024 22:15:43 +0900 Subject: [PATCH 2/2] update: getWallet and sendEth --- pkgs/cli/src/commands/wallet.ts | 6 +++--- pkgs/cli/src/modules/viem.ts | 25 +++++++++++++++---------- pkgs/cli/src/services/wallet.ts | 11 +++++++++-- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/pkgs/cli/src/commands/wallet.ts b/pkgs/cli/src/commands/wallet.ts index 17d9450..9bcc2c6 100644 --- a/pkgs/cli/src/commands/wallet.ts +++ b/pkgs/cli/src/commands/wallet.ts @@ -57,7 +57,7 @@ walletCommands .requiredOption("--receiver ", "Receiver address") .requiredOption("--amount ", "Amount") .option("--chainId ", "chainId") - .action(async ({ name, receiver, amount, chainId }) => { - const account = getWallet(name); - await sendEth(account, receiver, amount, chainId); + .action(async ({ name, receiver, amount }) => { + const wallet = await getWallet(name); + await sendEth(wallet, receiver, amount); }); diff --git a/pkgs/cli/src/modules/viem.ts b/pkgs/cli/src/modules/viem.ts index 6387f09..35bab5f 100644 --- a/pkgs/cli/src/modules/viem.ts +++ b/pkgs/cli/src/modules/viem.ts @@ -4,6 +4,7 @@ import { http, parseEther, PrivateKeyAccount, + WalletClient, } from "viem"; import { privateKeyToAccount } from "viem/accounts"; import { hardhat, sepolia, holesky } from "viem/chains"; @@ -22,40 +23,44 @@ function getChainById(chainId: number | string): Chain { return chain; } -export const setClient = async ( +export const setWallet = async ( account: PrivateKeyAccount, chainId?: number | undefined ) => { const chain = chainId ? getChainById(chainId) : holesky; - const client = createWalletClient({ + const wallet = createWalletClient({ account, chain, transport: http(), }); - return client; + return wallet; }; export const sendEth = async ( - account: PrivateKeyAccount, + wallet: WalletClient, to: `0x${string}`, - amount: string, - chainId?: number + amount: string ) => { - const client = await setClient(account, chainId); + const account = wallet.account; + + if (!account) { + throw new Error("Client account is not defined"); + } - const hash = await client.sendTransaction({ + const hash = await wallet.sendTransaction({ account, - to: to, + to, value: parseEther(amount), + chain: wallet.chain, }); console.log(`Transaction sent: ${hash}`); console.log(`From: ${account.address}`); console.log(`To: ${to}`); console.log(`Amount: ${amount} ETH`); - console.log(`Chain ID: ${client.chain.id}`); + console.log(`Chain ID: ${wallet.chain?.id}`); return hash; }; diff --git a/pkgs/cli/src/services/wallet.ts b/pkgs/cli/src/services/wallet.ts index 3ed470f..b96e838 100644 --- a/pkgs/cli/src/services/wallet.ts +++ b/pkgs/cli/src/services/wallet.ts @@ -3,6 +3,7 @@ import path from "path"; import { Hex } from "viem"; import { privateKeyToAccount, privateKeyToAddress } from "viem/accounts"; const profilesPath = path.join(__dirname, "profiles.json"); +import { setWallet } from "../modules/viem"; export interface Profile { name: string; @@ -17,7 +18,7 @@ export const getProfiles = () => { return JSON.parse(data) as Profile[]; }; -export const getWallet = (name?: string) => { +export const getAccount = (name?: string) => { const profiles = getProfiles(); const profile = profiles.find((p) => p.name === name) || profiles[0]; @@ -26,6 +27,12 @@ export const getWallet = (name?: string) => { return privateKeyToAccount(profile.privateKey); }; +export const getWallet = (name?: string, chainId?: number | undefined) => { + const account = getAccount(name); + + return setWallet(account, chainId); +}; + export const saveProfile = (params: Profile) => { if (!params.privateKey.match(/^0x[0-9a-f]{64}$/)) { console.log("Invalid private key."); @@ -59,7 +66,7 @@ export const deleteProfile = (params: { name: string }) => { writeFileSync(profilesPath, JSON.stringify(profiles, null, 2)); console.log(`Profile "${params.name}" with private key has been deleted.`); -} +}; export const listProfiles = () => { const profiles = getProfiles();