Skip to content

Commit

Permalink
update: getWallet and sendEth
Browse files Browse the repository at this point in the history
  • Loading branch information
yawn-c111 committed Sep 30, 2024
1 parent ccefc2e commit 015c0b9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
6 changes: 3 additions & 3 deletions pkgs/cli/src/commands/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ walletCommands
.requiredOption("--receiver <receiver>", "Receiver address")
.requiredOption("--amount <amount>", "Amount")
.option("--chainId <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);
});
25 changes: 15 additions & 10 deletions pkgs/cli/src/modules/viem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
http,
parseEther,
PrivateKeyAccount,
WalletClient,
} from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { hardhat, sepolia, holesky } from "viem/chains";
Expand All @@ -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;
};
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

0 comments on commit 015c0b9

Please sign in to comment.