Skip to content

Commit

Permalink
Merge pull request #6 from solana-developers/add-request-and-confirm-…
Browse files Browse the repository at this point in the history
…airdrop

add: requestAndConfirmAirdrop()
  • Loading branch information
mikemaccana authored Jan 9, 2024
2 parents b45cdb0 + 31136a2 commit f8d46af
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 12 deletions.
22 changes: 12 additions & 10 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ jobs:

steps:
- uses: actions/checkout@v3
# Install Solana

# From https://docs.solana.com/cli/install-solana-cli-tools
- run: sh -c "$(curl -sSfL https://release.solana.com/stable/install)"
- name: Install Solana
run: sh -c "$(curl -sSfL https://release.solana.com/stable/install)"

- name: Add to PATH
shell: bash
run: |
echo "/home/runner/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH
run: echo "/home/runner/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH

- name: Run Solana validator (and background it)
run: solana-test-validator &

# Install everything
- run: npm ci
- name: Install everything
run: npm ci

- name: Check Solana keygen is installed
run: which solana-keygen
run: echo $PATH; which solana-keygen

# Run tests
- run: npm test
- name: Run tests
run: npm test
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.3

- Now just `helpers`
- Added `requestAndConfirmAirdrop`
- Added `getCustomErrorMessage`

## 1.2

- Added `addKeypairToEnvFile()`
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ And `errorMessage` will now be:
"This token mint cannot freeze accounts";
```

## requestAndConfirmAirdrop()

Request and confirm an airdrop in one step. This is built the next future version of web3.js, but we've added it here now for your convenience.

```typescript
const balance = await requestAndConfirmAirdrop(
connection,
keypair.publicKey,
lamportsToAirdrop,
);
```

As soon as the `await` returns, the airdropped tokens will be ready in the address, and the new balance of tokens is returned by requestAndConfirmAirdrop(). This makes `requestAndConfirmAirdrop()` very handy in testing scripts.

# node.js specific helpers

## getKeypairFromFile()
Expand Down Expand Up @@ -118,6 +132,20 @@ await addKeypairToEnvFile(testKeypair, "SECRET_KEY", ".env.local");

This will also reload the env file

## requestAndConfirmAirdrop()

Request and confirm an airdrop in one step. This is built into the next version of web3.js, but we've added it here now for your convenience.

```typescript
await requestAndConfirmAirdrop(
connection,
keypair.publicKey,
lamportsToAirdrop,
);
```

As soon as the `await` returns, the airdropped tokens will be ready in the address. This makes `requestAndConfirmAirdrop()` very handy in testing scripts. Note you may want to check the balance (`const balance = await connection.getBalance(keypair.publicKey);`) to ensure you only use your airdrops when you need them.

### Secret key format

Secret keys can be read in either the more compact base58 format (`base58.encode(randomKeypair.secretKey);`), like:
Expand Down
20 changes: 19 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {
getKeypairFromFile,
addKeypairToEnvFile,
getCustomErrorMessage,
requestAndConfirmAirdrop,
} from "./index";
import { Keypair } from "@solana/web3.js";
import { Connection, Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js";
import assert from "node:assert/strict";
import base58 from "bs58";
// See https://m.media-amazon.com/images/I/51TJeGHxyTL._SY445_SX342_.jpg
Expand Down Expand Up @@ -168,3 +169,20 @@ describe("addKeypairToEnvFile", () => {
);
});
});

describe("requestAndConfirmAirdrop", () => {
test("Checking the balance after requestAndConfirmAirdrop", async () => {
const keypair = Keypair.generate();
const connection = new Connection("http://127.0.0.1:8899");
const originalBalance = await connection.getBalance(keypair.publicKey);
const lamportsToAirdrop = 1 * LAMPORTS_PER_SOL;
assert.equal(originalBalance, 0);
const newBalance = await requestAndConfirmAirdrop(
connection,
keypair.publicKey,
lamportsToAirdrop,
);

assert.equal(newBalance, lamportsToAirdrop);
});
});
24 changes: 23 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Keypair } from "@solana/web3.js";
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
import base58 from "bs58";
import path from "path";
import { readFile, appendFile } from "fs/promises";
Expand Down Expand Up @@ -109,3 +109,25 @@ export const addKeypairToEnvFile = async (
const secretKeyString = keypairToSecretKeyJSON(keypair);
await appendFile(fileName, `\n${variableName}=${secretKeyString}`);
};

export const requestAndConfirmAirdrop = async (
connection: Connection,
publicKey: PublicKey,
amount: number,
) => {
let airdropTransactionSignature = await connection.requestAirdrop(
publicKey,
amount,
);
// Wait for airdrop confirmation
const latestBlockHash = await connection.getLatestBlockhash();
await connection.confirmTransaction(
{
blockhash: latestBlockHash.blockhash,
lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
signature: airdropTransactionSignature,
},
"confirmed",
);
return connection.getBalance(publicKey, "confirmed");
};

0 comments on commit f8d46af

Please sign in to comment.