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";
|
2023-03-27 04:50:59 -07:00
|
|
|
import * as fs from 'fs';
|
2023-01-07 01:55:12 -08:00
|
|
|
import * as os from 'os';
|
|
|
|
|
|
|
|
// number of users
|
2023-03-29 14:01:15 -07:00
|
|
|
const nbUsers = process.argv.length > 2 ? +process.argv[2] : 10;
|
2023-01-07 01:55:12 -08:00
|
|
|
// url
|
2023-01-04 05:33:36 -08:00
|
|
|
const url = process.argv.length > 3 ? process.argv[3] : "http://0.0.0.0:8899";
|
2023-03-29 14:01:15 -07:00
|
|
|
// tokens to transfer to new accounts 0.5 sol
|
|
|
|
const fee_payer_balance = process.argv.length > 4 ? +process.argv[4] : (LAMPORTS_PER_SOL / 2);
|
|
|
|
// tokens to transfer to new accounts 0.5 sol
|
|
|
|
const number_of_fee_payers = process.argv.length > 4 ? +process.argv[4] : 10;
|
2023-01-07 01:55:12 -08:00
|
|
|
// outfile
|
|
|
|
const outFile = process.argv.length > 4 ? process.argv[4] : "out.json";
|
2023-03-27 04:50:59 -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();
|
|
|
|
}
|
|
|
|
|
2023-03-27 04:50:59 -07:00
|
|
|
(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}`);
|
2023-03-27 04:50:59 -07:00
|
|
|
console.time('Time taken');
|
|
|
|
|
2023-01-07 01:55:12 -08:00
|
|
|
const connection = new Connection(url, 'confirmed');
|
2023-03-27 04:50:59 -07:00
|
|
|
|
|
|
|
const authority = Keypair.fromSecretKey(
|
2023-01-07 01:55:12 -08:00
|
|
|
Uint8Array.from(
|
2023-03-27 04:50:59 -07:00
|
|
|
JSON.parse(
|
|
|
|
process.env.KEYPAIR ||
|
2023-01-07 01:55:12 -08:00
|
|
|
fs.readFileSync(os.homedir() + '/.config/solana/id.json', 'utf-8'),
|
2023-03-27 04:50:59 -07:00
|
|
|
),
|
2023-01-07 01:55:12 -08:00
|
|
|
),
|
2023-03-27 04:50:59 -07: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}`);
|
|
|
|
}));
|
|
|
|
|
2023-03-27 04:50:59 -07:00
|
|
|
const userKps = Array(nbUsers).fill(0).map(() => Keypair.generate());
|
2023-01-07 01:55:12 -08:00
|
|
|
|
2023-03-27 04:50:59 -07:00
|
|
|
const mint = await splToken.createMint(
|
2023-01-07 01:55:12 -08:00
|
|
|
connection,
|
|
|
|
authority,
|
|
|
|
authority.publicKey,
|
|
|
|
null,
|
|
|
|
6,
|
|
|
|
);
|
2023-03-27 04:50:59 -07:00
|
|
|
|
|
|
|
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,
|
2023-02-06 07:15:26 -08:00
|
|
|
user.publicKey,
|
2023-03-27 04:50:59 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
console.log("Account created");
|
2023-01-07 01:55:12 -08:00
|
|
|
|
2023-02-06 07:15:26 -08:00
|
|
|
await splToken.mintTo(
|
2023-01-07 01:55:12 -08:00
|
|
|
connection,
|
|
|
|
authority,
|
|
|
|
mint,
|
2023-02-06 07:15:26 -08:00
|
|
|
account,
|
2023-01-07 01:55:12 -08:00
|
|
|
authority,
|
|
|
|
1_000_000_000_000,
|
|
|
|
)
|
|
|
|
|
2023-03-27 04:50:59 -07:00
|
|
|
console.log("Account minted");
|
|
|
|
|
|
|
|
return account;
|
|
|
|
}));
|
|
|
|
|
|
|
|
console.timeLog('Time taken');
|
|
|
|
|
|
|
|
const users = userKps.map(x => ({
|
|
|
|
'publicKey': x.publicKey.toBase58(),
|
|
|
|
'secretKey': Array.from(x.secretKey)
|
|
|
|
}));
|
2023-01-07 01:55:12 -08:00
|
|
|
|
|
|
|
const data = {
|
2023-03-29 14:01:15 -07:00
|
|
|
'fee_payers': fee_payers,
|
2023-03-27 04:50:59 -07:00
|
|
|
'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);
|
|
|
|
|
2023-03-27 04:50:59 -07:00
|
|
|
fs.writeFileSync(outFile, JSON.stringify(data));
|
|
|
|
})()
|