Skip to content

Commit

Permalink
Merge pull request #30 from solana-developers/initialize-without-airdrop
Browse files Browse the repository at this point in the history
feat: skipping airdrop if desired
  • Loading branch information
mikemaccana authored Mar 27, 2024
2 parents 0e36801 + 10d5446 commit e49a730
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ src/temp
dist
node_modules
test-ledger
.env*
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,18 +332,20 @@ How the keypair is initialized is dependant on the `initializeKeypairOptions`:
interface initializeKeypairOptions {
envFileName?: string;
envVariableName?: string;
airdropAmount?: number;
airdropAmount?: number | null;
minimumBalance?: number;
keypairPath?: string;
}
```

By default, the keypair will be retrieved from the `.env` file. If a `.env` file does not exist, this function will create one with a new keypair under the optional `envVariableName`.

To load the keypair from the filesystem, pass in the `keypairPath`.
To load the keypair from the filesystem, pass in the `keypairPath`. When set, loading a keypair from the filesystem will take precedence over loading from the `.env` file.

After the keypair has been loaded, it will check the account's balance. If the balance is below the `minimumBalance`, it will airdrop the account `airdropAmount`.

After the keypair has been loaded, you can skip the airdrop on low balance by setting `airdropAmount` to `0` or `null`.

To initialize a keypair from the `.env` file, and airdrop it 1 sol if it's beneath 0.5 sol:

```typescript
Expand Down
25 changes: 13 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export const addKeypairToEnvFile = async (
export interface InitializeKeypairOptions {
envFileName?: string;
envVariableName?: string;
airdropAmount?: number;
airdropAmount?: number | null;
minimumBalance?: number;
keypairPath?: string;
}
Expand All @@ -219,15 +219,14 @@ export const initializeKeypair = async (
options?: InitializeKeypairOptions,
): Promise<Keypair> => {
let {
envFileName,
envVariableName,
airdropAmount,
minimumBalance,
keypairPath,
envFileName,
envVariableName = DEFAULT_ENV_KEYPAIR_VARIABLE_NAME,
airdropAmount = DEFAULT_AIRDROP_AMOUNT,
minimumBalance = DEFAULT_MINIMUM_BALANCE,
} = options || {};

let keypair: Keypair;
envVariableName = envVariableName || DEFAULT_ENV_KEYPAIR_VARIABLE_NAME;

if (keypairPath) {
keypair = await getKeypairFromFile(keypairPath);
Expand All @@ -238,12 +237,14 @@ export const initializeKeypair = async (
await addKeypairToEnvFile(keypair, envVariableName, envFileName);
}

await airdropIfRequired(
connection,
keypair.publicKey,
airdropAmount || DEFAULT_AIRDROP_AMOUNT,
minimumBalance || DEFAULT_MINIMUM_BALANCE,
);
if (!!airdropAmount) {
await airdropIfRequired(
connection,
keypair.publicKey,
airdropAmount,
minimumBalance,
);
}

return keypair;
};
Expand Down

0 comments on commit e49a730

Please sign in to comment.