solana-web3.js
solana-web3.js copied to clipboard
[feature] encourage local keypair reuse (via easy loading local keypairs)
Motivation
Sadly, it has become a fairly common practice for many to generate a new keypair and airdrop tokens for a single use. Especially for tests. This adds additional strain on the devnet/testnet faucet due to throwing away tokens. Especially when this is performed in a test that airdrops 1 SOL only to perform a few transactions.
One way to help combat this is to add built-in support for developers to more easily reuse keypairs (and their balances). Having a small number of helper functions built in would make it a more common practice to reuse keypairs and tokens during tests and not drain the faucet as much from tests.
This would also remove common boilerplate that some devs end up writing to do all this themselves.
Example use case
- developer (and/or CI platform) would have a private key saved on their local file system (or in a pre-defined ENV variable)
- helper function would load in the private key string or u8 array, converting it into a valid keypair
- if no keypair was found, generate a new one and save to file system
- auto check the balance (unless it was freshly generated)
- auto airdrop on low balance
- developer would perform their regular test and execute their code
This process would then repeat for the next test, except the subsequent tests would not need to generate a new keypair and airdrop more tokens.
For newly generated keypairs, like might be common within stateless CI/CD pipelines: at the end of tests, have a smart way to encourage newly generated keypairs to send the remaining funds back to the faucet. Reducing waste of tokens and faucet leaks. Although, this would likely require the dev to add a transfer function at the end of their test. But if it was handled auto-magically, then that would be the goal.
Details
These helper functions would work in a few steps:
- load a private key string or u8 array from the file system by (checking a few default locations):
- check a user supplied file system path
- check a pre-defined ENV variable
- check locally within the repo (at a pre-defined location)
- check the solana cli configured keypair path (might be trickier)
- if no keypair was located, generate a new one and save it to the file system in the project's directory (at the pre-defined location)
- auto check the current balance
- this step could be disabled via a function argument
- option to request an airdrop on low balance (airdrop on low balance would likely be a default
true
setting)
Here are some example helper functions I wrote for the solana-developers/compressed-nfts
demo. These functions largely demonstrate the things above.
Note: This functionality could live in a separate package than @solana/keys
, since I would expect it to only be used in local/testing/development environments. Allowing for it to be installed as a dev dependency.