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/README.md b/README.md index a6180e3..751b945 100644 --- a/README.md +++ b/README.md @@ -332,7 +332,7 @@ 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; } @@ -340,10 +340,12 @@ interface initializeKeypairOptions { 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 diff --git a/src/index.ts b/src/index.ts index 08af69f..35098b6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -209,7 +209,7 @@ export const addKeypairToEnvFile = async ( export interface InitializeKeypairOptions { envFileName?: string; envVariableName?: string; - airdropAmount?: number; + airdropAmount?: number | null; minimumBalance?: number; keypairPath?: string; } @@ -219,15 +219,14 @@ export const initializeKeypair = async ( options?: InitializeKeypairOptions, ): Promise => { 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); @@ -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; };