-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGiveRightToVote.ts
74 lines (61 loc) · 2.58 KB
/
GiveRightToVote.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { sepolia } from "viem/chains";
import * as dotenv from "dotenv";
import { abi, bytecode } from "../artifacts/contracts/Ballot.sol/Ballot.json";
dotenv.config();
import { createPublicClient, http, createWalletClient, formatEther, toHex, hexToString, Address } from "viem";
import { privateKeyToAccount } from "viem/accounts";
const deployerPrivateKey = process.env.PRIVATE_KEY || "";
const providerApiKey = process.env.ALCHEMY_API_KEY || "";
async function main() {
// npx ts-node --files ./scripts/GiveRightToVote.ts [contractAddress] [wallet1] [wallet2]...
// Receiving parameters - Get contractAddress from args
const parameters = process.argv.slice(2);
if (!parameters || parameters.length < 2)
throw new Error("Parameters not provided");
const contractAddress = parameters[0] as `0x${string}`;
if (!contractAddress) throw new Error("Contract address not provided");
if (!/^0x[a-fA-F0-9]{40}$/.test(contractAddress))
throw new Error("Invalid contract address");
// Get the wallet from args
const wallets = parameters.slice(1);
// Create public client to connect with sepolia using Alchemy
const publicClient = createPublicClient({
chain: sepolia,
transport: http(`https://eth-sepolia.g.alchemy.com/v2/${providerApiKey}`),
});
// Interact with the contract as the chairperson/deployer
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,
});
// For each wallet address in wallets, give them rights to vote IF the address is correct
for (const walletAddress of wallets) {
const wallet_ = walletAddress as `0x${string}`;
if (!wallet_) return;
if (!/^0x[a-fA-F0-9]{40}$/.test(walletAddress)) {
console.log(`\nInvalid wallet address arg: ${wallet_}`);
return;
}
console.log(`\nGiving ${walletAddress} right to vote...`);
const hash = await deployer.writeContract({
address: contractAddress,
abi,
functionName: "giveRightToVote",
args: [wallet_],
});
console.log("Transaction hash:", hash);
console.log("Waiting for confirmations...");
const receipt = await publicClient.waitForTransactionReceipt({ hash });
console.log(`Wallet ${walletAddress} has been given a right to vote`);
};
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});