Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: skipping airdrop if desired #30

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
mikemaccana marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -206,7 +206,7 @@ export const addKeypairToEnvFile = async (
export interface InitializeKeypairOptions {
envFileName?: string;
envVariableName?: string;
airdropAmount?: number;
airdropAmount?: number | null;
minimumBalance?: number;
keypairPath?: string;
}
Expand All @@ -216,15 +216,14 @@ export const initializeKeypair = async (
options?: InitializeKeypairOptions,
): Promise<Keypair> => {
let {
envFileName,
envVariableName,
airdropAmount,
minimumBalance,
keypairPath,
envFileName,
envVariableName = DEFAULT_ENV_KEYPAIR_VARIABLE_NAME,
mikemaccana marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -235,12 +234,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,
);
}
mikemaccana marked this conversation as resolved.
Show resolved Hide resolved

return keypair;
};
Expand Down
Loading