Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: viem client and sendEth #122

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions pkgs/cli/src/commands/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Command } from "commander";
import { getEthAddress, sendEth } from "../modules/viem";
import { listProfiles, saveProfile, deleteProfile } from "../services/wallet";
import {
getWallet,
listProfiles,
saveProfile,
deleteProfile,
} 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")
Expand Down Expand Up @@ -50,11 +53,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 <name>", "Wallet name")
.requiredOption("--receiver <receiver>", "Receiver address")
.requiredOption("--amount <amount>", "Amount")
.option("--chainId <chainId>", "chainId")
.action(async ({ name, receiver, amount }) => {
const wallet = await getWallet(name);
await sendEth(wallet, receiver, amount);
});
69 changes: 54 additions & 15 deletions pkgs/cli/src/modules/viem.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,67 @@
import { createWalletClient, http, parseEther } from "viem";
import {
Chain,
createWalletClient,
http,
parseEther,
PrivateKeyAccount,
WalletClient,
} 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`);
}

const client = createWalletClient({
return chain;
}

export const setWallet = async (
account: PrivateKeyAccount,
chainId?: number | undefined
) => {
const chain = chainId ? getChainById(chainId) : holesky;

const wallet = createWalletClient({
account,
chain: hardhat,
chain,
transport: http(),
});

const hash = await client.sendTransaction({
return wallet;
};

export const sendEth = async (
wallet: WalletClient,
to: `0x${string}`,
amount: string
) => {
const account = wallet.account;

if (!account) {
throw new Error("Client account is not defined");
}

const hash = await wallet.sendTransaction({
account,
to: to,
value: parseEther("0.001"),
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: ${wallet.chain?.id}`);

return hash;
};

Expand Down
11 changes: 9 additions & 2 deletions pkgs/cli/src/services/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];

Expand All @@ -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.");
Expand Down Expand Up @@ -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();
Expand Down
Loading