Skip to content

Commit

Permalink
merged main, refactored temp keypair path to parent
Browse files Browse the repository at this point in the history
  • Loading branch information
heyAyushh committed Sep 5, 2024
2 parents 181ce43 + a78c452 commit 5d2b2e6
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.5

- Add `makeTokenMint()`
- 2.5.4 includes a few fixes to build system and TS types that were missing in earlier 2.5.x releases

## 2.4

Expand Down
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solana-developers/helpers",
"version": "2.5.2",
"version": "2.5.4",
"description": "Solana helper functions",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
9 changes: 6 additions & 3 deletions src/lib/airdrop.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Connection, Keypair, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";
import { InitializeKeypairOptions } from "../types";
import { type Connection, Keypair, LAMPORTS_PER_SOL, type PublicKey } from "@solana/web3.js";
import type { InitializeKeypairOptions } from "../types";
import { addKeypairToEnvFile, getKeypairFromEnvironment, getKeypairFromFile } from "./keypair";

const DEFAULT_AIRDROP_AMOUNT = 1 * LAMPORTS_PER_SOL;
const DEFAULT_MINIMUM_BALANCE = 0.5 * LAMPORTS_PER_SOL;
const DEFAULT_ENV_KEYPAIR_VARIABLE_NAME = "PRIVATE_KEY";

// TODO: honestly initializeKeypair is a bit vague
// we can probably give this a better name,
// just not sure what yet
export const initializeKeypair = async (
connection: Connection,
options?: InitializeKeypairOptions,
): Promise<Keypair> => {
let {
const {
keypairPath,
envFileName,
envVariableName = DEFAULT_ENV_KEYPAIR_VARIABLE_NAME,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/keypair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const keypairToSecretKeyJSON = (keypair: Keypair): string => {
};

export const getKeypairFromFile = async (filepath?: string) => {
const path = await import("path");
const path = await import("node:path");
// Work out correct file name
if (!filepath) {
filepath = DEFAULT_FILEPATH;
Expand All @@ -24,7 +24,7 @@ export const getKeypairFromFile = async (filepath?: string) => {
// Get contents of file
let fileContents: string;
try {
const { readFile } = await import("fs/promises");
const { readFile } = await import("node:fs/promises");
const fileContentsBuffer = await readFile(filepath);
fileContents = fileContentsBuffer.toString();
} catch (error) {
Expand Down Expand Up @@ -81,7 +81,7 @@ export const addKeypairToEnvFile = async (
variableName: string,
envFileName?: string,
) => {
const { appendFile } = await import("fs/promises");
const { appendFile } = await import("node:fs/promises");
if (!envFileName) {
envFileName = ".env";
}
Expand Down
22 changes: 9 additions & 13 deletions tests/src/airdrop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@ describe("initializeKeypair", () => {

test("generates a new keypair and airdrops needed amount", async () => {
// We need to use a specific file name to avoid conflicts with other tests
const envFileName = ".env-unittest-initkeypair";
const envFileName = "../.env-unittest-initkeypair";
const options: InitializeKeypairOptions = {
envFileName,
envVariableName: keypairVariableName,
};

const signerFirstLoad = await initializeKeypair(connection, options);
const userBefore = await initializeKeypair(connection, options);

// Check balance
const firstBalanceLoad = await connection.getBalance(
signerFirstLoad.publicKey,
);
assert.ok(firstBalanceLoad > 0);
const balanceBefore = await connection.getBalance(userBefore.publicKey);
assert.ok(balanceBefore > 0);

// Check that the environment variable was created
dotenv.config({ path: envFileName });
Expand All @@ -40,19 +38,17 @@ describe("initializeKeypair", () => {
}

// Now reload the environment and check it matches our test keypair
const signerSecondLoad = await initializeKeypair(connection, options);
const userAfter = await initializeKeypair(connection, options);

// Check the keypair is the same
assert.ok(signerFirstLoad.publicKey.equals(signerSecondLoad.publicKey));
assert.ok(userBefore.publicKey.equals(userAfter.publicKey));

// Check balance has not changed
const secondBalanceLoad = await connection.getBalance(
signerSecondLoad.publicKey,
);
assert.equal(firstBalanceLoad, secondBalanceLoad);
const balanceAfter = await connection.getBalance(userAfter.publicKey);
assert.equal(balanceBefore, balanceAfter);

// Check there is a secret key
assert.ok(signerSecondLoad.secretKey);
assert.ok(userAfter.secretKey);

await deleteFile(envFileName);
});
Expand Down
2 changes: 1 addition & 1 deletion tests/src/keypair.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("addKeypairToEnvFile", () => {

test("generates new keypair and writes to env if variable doesn't exist", async () => {
// We need to use a specific file name to avoid conflicts with other tests
const envFileName = ".env-unittest-addkeypairtoenvfile";
const envFileName = "../.env-unittest-addkeypairtoenvfile";
await addKeypairToEnvFile(testKeypair, "TEMP_KEYPAIR", envFileName);

// Now reload the environment and check it matches our test keypair
Expand Down

0 comments on commit 5d2b2e6

Please sign in to comment.