From 8a402c0c13f4a5703c67ac7fc7d96445f52984de Mon Sep 17 00:00:00 2001 From: nickfrosty <75431177+nickfrosty@users.noreply.github.com> Date: Fri, 22 Mar 2024 11:40:46 -0400 Subject: [PATCH 1/3] feat: skipping airdrop if desired --- README.md | 5 ++++- src/index.ts | 19 +++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a6180e3..cacf405 100644 --- a/README.md +++ b/README.md @@ -335,15 +335,18 @@ interface initializeKeypairOptions { airdropAmount?: number; minimumBalance?: number; keypairPath?: string; + requestAirdropIfRequired?: boolean; } ``` 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 `requestAirdropIfRequired` to `false`. + To initialize a keypair from the `.env` file, and airdrop it 1 sol if it's beneath 0.5 sol: ```typescript diff --git a/src/index.ts b/src/index.ts index c002c39..99a48ca 100644 --- a/src/index.ts +++ b/src/index.ts @@ -209,6 +209,7 @@ export interface InitializeKeypairOptions { airdropAmount?: number; minimumBalance?: number; keypairPath?: string; + requestAirdropIfRequired?: boolean; } export const initializeKeypair = async ( @@ -217,14 +218,14 @@ export const initializeKeypair = async ( ): Promise => { let { envFileName, - envVariableName, + envVariableName = DEFAULT_ENV_KEYPAIR_VARIABLE_NAME, airdropAmount, minimumBalance, keypairPath, + requestAirdropIfRequired = true, } = options || {}; let keypair: Keypair; - envVariableName = envVariableName || DEFAULT_ENV_KEYPAIR_VARIABLE_NAME; if (keypairPath) { keypair = await getKeypairFromFile(keypairPath); @@ -235,12 +236,14 @@ export const initializeKeypair = async ( await addKeypairToEnvFile(keypair, envVariableName, envFileName); } - await airdropIfRequired( - connection, - keypair.publicKey, - airdropAmount || DEFAULT_AIRDROP_AMOUNT, - minimumBalance || DEFAULT_MINIMUM_BALANCE, - ); + if (requestAirdropIfRequired) { + await airdropIfRequired( + connection, + keypair.publicKey, + airdropAmount || DEFAULT_AIRDROP_AMOUNT, + minimumBalance || DEFAULT_MINIMUM_BALANCE, + ); + } return keypair; }; From 56da2584fa1d27f0b43ac8d6aed4149ab8c913dd Mon Sep 17 00:00:00 2001 From: nickfrosty <75431177+nickfrosty@users.noreply.github.com> Date: Tue, 26 Mar 2024 09:07:41 -0400 Subject: [PATCH 2/3] refactor: truthy --- README.md | 5 ++--- src/index.ts | 6 ++---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index cacf405..751b945 100644 --- a/README.md +++ b/README.md @@ -332,10 +332,9 @@ 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; - requestAirdropIfRequired?: boolean; } ``` @@ -345,7 +344,7 @@ To load the keypair from the filesystem, pass in the `keypairPath`. When set, lo 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 `requestAirdropIfRequired` to `false`. +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: diff --git a/src/index.ts b/src/index.ts index 99a48ca..8281108 100644 --- a/src/index.ts +++ b/src/index.ts @@ -206,10 +206,9 @@ export const addKeypairToEnvFile = async ( export interface InitializeKeypairOptions { envFileName?: string; envVariableName?: string; - airdropAmount?: number; + airdropAmount?: number | null; minimumBalance?: number; keypairPath?: string; - requestAirdropIfRequired?: boolean; } export const initializeKeypair = async ( @@ -222,7 +221,6 @@ export const initializeKeypair = async ( airdropAmount, minimumBalance, keypairPath, - requestAirdropIfRequired = true, } = options || {}; let keypair: Keypair; @@ -236,7 +234,7 @@ export const initializeKeypair = async ( await addKeypairToEnvFile(keypair, envVariableName, envFileName); } - if (requestAirdropIfRequired) { + if (!!airdropAmount) { await airdropIfRequired( connection, keypair.publicKey, From 10d5446240b706aad9be4bac558388022edd4ff8 Mon Sep 17 00:00:00 2001 From: nickfrosty <75431177+nickfrosty@users.noreply.github.com> Date: Tue, 26 Mar 2024 09:30:50 -0400 Subject: [PATCH 3/3] fix: tests and env ignore --- .gitignore | 1 + src/index.ts | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 48da102..c509a27 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ src/temp dist node_modules test-ledger +.env* \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 8281108..a7b663d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -216,11 +216,11 @@ export const initializeKeypair = async ( options?: InitializeKeypairOptions, ): Promise => { let { + keypairPath, envFileName, envVariableName = DEFAULT_ENV_KEYPAIR_VARIABLE_NAME, - airdropAmount, - minimumBalance, - keypairPath, + airdropAmount = DEFAULT_AIRDROP_AMOUNT, + minimumBalance = DEFAULT_MINIMUM_BALANCE, } = options || {}; let keypair: Keypair; @@ -238,8 +238,8 @@ export const initializeKeypair = async ( await airdropIfRequired( connection, keypair.publicKey, - airdropAmount || DEFAULT_AIRDROP_AMOUNT, - minimumBalance || DEFAULT_MINIMUM_BALANCE, + airdropAmount, + minimumBalance, ); }