new-account-with-lamports.js 576 B

12345678910111213141516171819202122232425
  1. // @flow
  2. import {Account, Connection} from '@solana/web3.js';
  3. import {sleep} from './sleep';
  4. export async function newAccountWithLamports(
  5. connection: Connection,
  6. lamports: number = 1000000,
  7. ): Promise<Account> {
  8. const account = new Account();
  9. let retries = 30;
  10. await connection.requestAirdrop(account.publicKey, lamports);
  11. for (;;) {
  12. await sleep(500);
  13. if (lamports == (await connection.getBalance(account.publicKey))) {
  14. return account;
  15. }
  16. if (--retries <= 0) {
  17. break;
  18. }
  19. }
  20. throw new Error(`Airdrop of ${lamports} failed`);
  21. }