lite-rpc/benches/create_n_users.bench.ts

120 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-03-29 14:01:15 -07:00
import { Connection, Keypair, LAMPORTS_PER_SOL, sendAndConfirmTransaction, SystemProgram, Transaction } from '@solana/web3.js';
2023-01-07 01:55:12 -08:00
import * as splToken from "@solana/spl-token";
import * as fs from 'fs';
2023-01-07 01:55:12 -08:00
import * as os from 'os';
2023-03-29 19:34:58 -07:00
import { get_postional_arg, serialized_keypair } from './util';
2023-01-07 01:55:12 -08:00
// number of users
2023-03-29 19:34:58 -07:00
const nbUsers = +get_postional_arg(2, 10);
2023-01-07 01:55:12 -08:00
// url
2023-03-29 19:34:58 -07:00
const url = get_postional_arg(3, "http://0.0.0.0:8899");
2023-03-29 14:01:15 -07:00
// tokens to transfer to new accounts 0.5 sol
2023-03-29 19:34:58 -07:00
const fee_payer_balance = +get_postional_arg(4, (LAMPORTS_PER_SOL / 2));
2023-03-29 14:01:15 -07:00
// tokens to transfer to new accounts 0.5 sol
2023-03-29 19:34:58 -07:00
const number_of_fee_payers = +get_postional_arg(5, 10);
2023-01-07 01:55:12 -08:00
// outfile
2023-03-29 19:34:58 -07:00
const outFile = get_postional_arg(6, "out.json");
2023-03-29 19:26:24 -07:00
2023-03-29 14:01:15 -07:00
function check_if_out_file_exists() {
if (!fs.existsSync(outFile))
return;
console.warn(`{outFile} already exists. Potential loss of funds. Remove and re run`);
process.exit();
}
(async function main() {
2023-03-29 14:01:15 -07:00
check_if_out_file_exists();
console.log(`Creating ${nbUsers} users on ${url} with ${number_of_fee_payers} with balance ${fee_payer_balance} and output file ${outFile}`);
console.time('Time taken');
2023-01-07 01:55:12 -08:00
const connection = new Connection(url, 'confirmed');
const authority = Keypair.fromSecretKey(
2023-01-07 01:55:12 -08:00
Uint8Array.from(
JSON.parse(
process.env.KEYPAIR ||
2023-01-07 01:55:12 -08:00
fs.readFileSync(os.homedir() + '/.config/solana/id.json', 'utf-8'),
),
2023-01-07 01:55:12 -08:00
),
);
2023-03-29 14:01:15 -07:00
const authority_balance = await connection.getBalance(authority.publicKey);
const required_balance = number_of_fee_payers * fee_payer_balance;
if (authority_balance < required_balance) {
console.warn(`Authority doesn't have enough balance. Required at least ${required_balance} Lamport`);
process.exit();
}
const fee_payers = Array(nbUsers).fill(0).map(() => Keypair.generate());
console.log(`Sending ${fee_payer_balance} to each of ${number_of_fee_payers} fee payers`);
await Promise.all(fee_payers.map(async fee_payer => {
const ix = SystemProgram.transfer({
fromPubkey: authority.publicKey,
toPubkey: fee_payer.publicKey,
lamports: fee_payer_balance
});
const tx = new Transaction().add(ix);
const tx_sig = await sendAndConfirmTransaction(connection, tx, [authority]);
console.log(`Sent ${tx_sig}`);
}));
const userKps = Array(nbUsers).fill(0).map(() => Keypair.generate());
2023-01-07 01:55:12 -08:00
const mint = await splToken.createMint(
2023-01-07 01:55:12 -08:00
connection,
authority,
authority.publicKey,
null,
6,
);
const accounts = await Promise.all(userKps.map(async user => {
const account = await splToken.createAccount(
2023-01-07 01:55:12 -08:00
connection,
authority,
mint,
user.publicKey,
);
console.log("Account created");
2023-01-07 01:55:12 -08:00
await splToken.mintTo(
2023-01-07 01:55:12 -08:00
connection,
authority,
mint,
account,
2023-01-07 01:55:12 -08:00
authority,
1_000_000_000_000,
)
console.log("Account minted");
return account;
}));
console.timeLog('Time taken');
2023-03-29 19:26:24 -07:00
const users = userKps.map(serialized_keypair);
const fee_payers_serialized = fee_payers.map(serialized_keypair);
2023-01-07 01:55:12 -08:00
const data = {
2023-03-29 19:26:24 -07:00
'fee_payers': fee_payers_serialized,
'users': users,
'tokenAccounts': accounts,
'mint': mint,
'minted_amount': 1_000_000_000_000
2023-01-07 01:55:12 -08:00
};
console.log('created ' + nbUsers + ' Users and minted 10^12 tokens for mint ' + mint);
fs.writeFileSync(outFile, JSON.stringify(data));
})()