-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.js
More file actions
197 lines (169 loc) Β· 6.35 KB
/
init.js
File metadata and controls
197 lines (169 loc) Β· 6.35 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import * as ethers from "ethers";
import * as starknet from "starknet";
// Configuration object replacing CLI arguments
const CONFIG = {
eth_rpc_url: "http://localhost:8545", // Replace with your Ethereum RPC URL
starknet_rpc_url: "http://localhost:9944", // Replace with your StarkNet RPC URL
bridge_address: "0x2279b7a0a67db372996a5fab50d91eaa73d2ebe6", // Replace with your bridge contract address
eth_token_address: "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", // Replace with your ETH token address
num_accounts: 30, // Number of accounts to create
eth_private_key : "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", // Replace with your Ethereum private key
oz_account_class_hash : "0x5c478ee27f2112411f86f207605b2e2c58cdb647bac0df27f660ef2252359c6", // Replace with your OZ account class hash
};
class AccountManager {
constructor(eth_rpc_url, starknet_rpc_url) {
// Initialize providers
this.eth_provider = new ethers.JsonRpcProvider(eth_rpc_url);
this.wallet = new ethers.Wallet(
CONFIG.eth_private_key,
this.eth_provider
);
this.starknet_provider = new starknet.RpcProvider({
nodeUrl: starknet_rpc_url,
});
}
async getAppChainBalance(address, eth_token_address) {
const abi = [
{
name: "balanceOf",
type: "function",
inputs: [{ name: "account", type: "felt" }],
outputs: [{ name: "balance", type: "Uint256" }],
stateMutability: "view",
},
];
const ethContract = new starknet.Contract(
abi,
eth_token_address,
this.starknet_provider
);
const balance = await ethContract.balanceOf(address);
return balance.balance;
}
async bridgeToChain(bridge_address, starknet_account_address, eth_token_address) {
console.log(`π Bridging funds to ${starknet_account_address}...`);
const contract = new ethers.Contract(
bridge_address,
["function deposit(uint256, uint256)"],
this.wallet
);
const initial_balance = await this.getAppChainBalance(
starknet_account_address,
eth_token_address
);
const tx = await contract.deposit(
ethers.parseEther("1"),
starknet_account_address,
{ value: ethers.parseEther("1.01") }
);
await tx.wait();
console.log("β
Successfully sent 1 ETH on L1 bridge");
// Wait for funds to arrive on Starknet
let counter = 10;
while (counter--) {
const final_balance = await this.getAppChainBalance(
starknet_account_address,
eth_token_address
);
if (final_balance > initial_balance) {
console.log(
"π° Account balance:",
(final_balance / 10n ** 18n).toString(),
"ETH"
);
return true;
}
console.log("π Waiting for funds to arrive on Starknet...");
await new Promise((resolve) => setTimeout(resolve, 5000));
}
throw new Error("Failed to bridge funds to Starknet");
}
generateAccountKeys() {
const privateKey = starknet.stark.randomAddress();
const publicKey = starknet.ec.starkCurve.getStarkKey(privateKey);
// Calculate the account address
const accountConstructorCallData = starknet.CallData.compile({
publicKey: publicKey,
});
const accountAddress = starknet.hash.calculateContractAddressFromHash(
publicKey,
CONFIG.oz_account_class_hash,
accountConstructorCallData,
0
);
return {
address: accountAddress,
privateKey: privateKey,
publicKey: publicKey,
};
}
async deployAccount(accountKeys) {
console.log(`βοΈ Deploying account at ${accountKeys.address}...`);
const account = new starknet.Account(
this.starknet_provider,
accountKeys.address,
accountKeys.privateKey,
"1"
);
const { transaction_hash } = await account.deployAccount({
classHash: CONFIG.oz_account_class_hash,
constructorCalldata: [accountKeys.publicKey],
addressSalt: accountKeys.publicKey,
});
// Wait for deployment
const receipt = await this.starknet_provider.waitForTransaction(transaction_hash);
if (!receipt.isSuccess()) {
throw new Error(`Failed to deploy account - ${transaction_hash}`);
}
console.log(`β
Account deployed successfully - ${transaction_hash}`);
return receipt;
}
}
async function main() {
// Validate configuration
if (!CONFIG.eth_rpc_url || !CONFIG.starknet_rpc_url || !CONFIG.bridge_address ||
!CONFIG.eth_token_address || !CONFIG.num_accounts) {
console.log("Error: Missing required configuration parameters");
process.exit(1);
}
if (isNaN(CONFIG.num_accounts) || CONFIG.num_accounts <= 0) {
console.log("Error: Number of accounts must be a positive integer");
process.exit(1);
}
const manager = new AccountManager(CONFIG.eth_rpc_url, CONFIG.starknet_rpc_url);
const accounts = [];
console.log(`π Creating and funding ${CONFIG.num_accounts} accounts...\n`);
for (let i = 0; i < CONFIG.num_accounts; i++) {
console.log(`\nπ Processing account ${i + 1}/${CONFIG.num_accounts}`);
try {
// Generate account keys
const accountKeys = manager.generateAccountKeys();
// Bridge funds to the account
await manager.bridgeToChain(CONFIG.bridge_address, accountKeys.address, CONFIG.eth_token_address);
// Deploy the account
await manager.deployAccount(accountKeys);
// Get final balance
const balance = await manager.getAppChainBalance(accountKeys.address, CONFIG.eth_token_address);
accounts.push({
...accountKeys,
balance: balance.toString(),
});
console.log("\n⨠Account creation successful!");
console.log("Address:", accountKeys.address);
console.log("Private Key:", accountKeys.privateKey);
console.log("Balance:", (BigInt(balance) / 10n ** 18n).toString(), "ETH");
console.log("-".repeat(50));
} catch (error) {
console.error(`β Error processing account ${i + 1}:`, error.message);
}
}
console.log(accounts);
console.log("\nπ Summary of created accounts:");
accounts.forEach((account, index) => {
console.log(`\nAccount ${index + 1}:`);
console.log("Address:", account.address);
console.log("Private Key:", account.privateKey);
console.log("Balance:", (BigInt(account.balance) / 10n ** 18n).toString(), "ETH");
});
}
main().catch(console.error);