-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGrantMinterRole.ts
75 lines (62 loc) · 2.49 KB
/
GrantMinterRole.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
75
// npx ts-node --files ./scripts/GrantMinterRole.ts TOKEN_ADDRESS MINTER_ADDRESS
import { createPublicClient, http, createWalletClient, keccak256, toHex } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { sepolia } from "viem/chains";
import { abi } from "../artifacts/contracts/MyToken.sol/MyToken.json";
import * as dotenv from "dotenv";
dotenv.config();
const providerApiKey = process.env.ALCHEMY_API_KEY || "";
const delegatorPrivateKey = process.env.PRIVATE_KEY || "";
const MINTER_ROLE = keccak256(toHex("MINTER_ROLE"));;
function validateParameters(parameters: string[]) {
if (!parameters || parameters.length < 2)
throw new Error("Parameters not provided");
const tokenAddress = parameters[0] as `0x${string}`;
if (!tokenAddress) throw new Error("Token address not provided");
if (!/^0x[a-fA-F0-9]{40}$/.test(tokenAddress))
throw new Error("Invalid token address");
const minterAddress = parameters[1] as `0x${string}`;
if (!minterAddress) throw new Error("Minter address not provided");
if (!/^0x[a-fA-F0-9]{40}$/.test(minterAddress))
throw new Error("Invalid minter address");
return { tokenAddress, minterAddress }
}
async function main() {
console.log("\n");
const { tokenAddress, minterAddress } = validateParameters(process.argv.slice(2));
const account = privateKeyToAccount(`0x${delegatorPrivateKey}`);
const sender = createWalletClient({
account,
chain: sepolia,
transport: http(`https://eth-sepolia.g.alchemy.com/v2/${providerApiKey}`),
});
console.log(`Granting minter role to ${minterAddress}`);
console.log("Confirm? (Y/n)");
const stdin = process.stdin;
stdin.on("data", async function (d) {
if (d.toString().trim() == "Y") {
const hash = await sender.writeContract({
address: tokenAddress,
abi,
functionName: "grantRole",
args: [MINTER_ROLE, minterAddress],
});
console.log("Transaction hash:", hash);
console.log("Waiting for confirmations...");
const publicClient = createPublicClient({
chain: sepolia,
transport: http(`https://eth-sepolia.g.alchemy.com/v2/${providerApiKey}`),
});
const receipt = await publicClient.waitForTransactionReceipt({ hash });
console.log(`Transaction confirmed: ${receipt.status}`);
console.log(`Block: ${receipt.blockNumber}`)
} else {
console.log("Operation cancelled");
}
process.exit();
});
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});