-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeployMyToken.ts
53 lines (46 loc) · 1.72 KB
/
DeployMyToken.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// npx ts-node --files ./scripts/DeployMyToken.ts
import { createPublicClient, http, createWalletClient, formatEther } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { sepolia } from "viem/chains";
import { abi, bytecode } from "../artifacts/contracts/MyToken.sol/MyToken.json";
import * as dotenv from "dotenv";
dotenv.config();
const providerApiKey = process.env.ALCHEMY_API_KEY || "";
const deployerPrivateKey = process.env.PRIVATE_KEY || "";
async function main() {
const publicClient = createPublicClient({
chain: sepolia,
transport: http(`https://eth-sepolia.g.alchemy.com/v2/${providerApiKey}`),
});
const blockNumber = await publicClient.getBlockNumber();
console.log("Last block number:", blockNumber);
const account = privateKeyToAccount(`0x${deployerPrivateKey}`);
const deployer = createWalletClient({
account,
chain: sepolia,
transport: http(`https://eth-sepolia.g.alchemy.com/v2/${providerApiKey}`),
});
console.log("Deployer address:", deployer.account.address);
const balance = await publicClient.getBalance({
address: deployer.account.address,
});
console.log(
"Deployer balance:",
formatEther(balance),
deployer.chain.nativeCurrency.symbol
);
console.log("\nDeploying Token contract");
const hash = await deployer.deployContract({
abi,
bytecode: bytecode as `0x${string}`,
});
console.log("Transaction hash:", hash);
console.log("Waiting for confirmations...");
const receipt = await publicClient.waitForTransactionReceipt({ hash });
const contractAddress = receipt.contractAddress;
console.log("Token contract deployed to:", contractAddress);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});