-
Notifications
You must be signed in to change notification settings - Fork 0
/
fund-accounts.ts
61 lines (52 loc) · 2.19 KB
/
fund-accounts.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
import { readFileSync } from "fs";
import { Account, Call, Contract, uint256 } from "starknet";
import { deployer, ethContract, provider, strkContract } from "../lib";
// Update theses
const ethFundingAmount = BigInt(1e16);
const strkFundingAmount = BigInt(5e17);
// Parse file of accounts
const v3Accounts: Account[] = JSON.parse(readFileSync("./.env.v3Accounts.json", "utf-8"));
const v2Accounts: Account[] = JSON.parse(readFileSync("./.env.v2Accounts.json", "utf-8"));
const calls: Call[] = [];
// This will query too much in short amount of time, run at your own risks
// await Promise.all(v3Accounts.map(async (acc) => await checkBalance(strkContract, acc)));
// await Promise.all(v2Accounts.map(async (acc) => await checkBalance(ethContract, acc)));
for (const account of v3Accounts) {
await checkBalance(strkContract, account, strkFundingAmount);
}
for (const account of v2Accounts) {
await checkBalance(ethContract, account, ethFundingAmount);
}
if (calls.length > 0) {
console.log(`About to fund ${calls.length} accounts`);
await processCallsInChunks(calls, 500);
}
console.log(
`All accounts should have at least ${Number(ethFundingAmount) / 1e18} ETH or ${Number(strkFundingAmount) / 1e18} STRK`,
);
async function checkBalance(contract: Contract, account: Account, fundingAmount: bigint) {
const balance = await safeBalanceOf(contract, account.address);
if (balance < fundingAmount) {
calls.push(contract.populateTransaction.transfer(account.address, uint256.bnToUint256(fundingAmount - balance)));
}
}
async function safeBalanceOf(contract: Contract, address: string, retry = 0) {
try {
return await contract.balanceOf(address);
} catch (e) {
console.log(`Failing to get balance, retrying... (${retry})`);
if (retry >= 5) {
console.log(e);
return;
}
return safeBalanceOf(contract, address, retry + 1);
}
}
async function processCallsInChunks(calls: Call[], chunkSize: number) {
for (let i = 0; i < calls.length; i += chunkSize) {
console.log(`Funding from ${i} to ${i + chunkSize}`);
const chunk = calls.slice(i, i + chunkSize);
const { transaction_hash } = await deployer.execute(chunk);
await provider.waitForTransaction(transaction_hash);
}
}