Skip to content

Commit

Permalink
Add assertBigNumberEqual(), assertPublicKeyEqual() and getRandomBigNu…
Browse files Browse the repository at this point in the history
…mber()
  • Loading branch information
mikemaccana committed Jun 5, 2024
1 parent f76d0cd commit 376c790
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 2.3

Mainly changes to help unit testing:

- Add `assertBigNumberEqual`
- Add `assertPublicKeyEqual`
- Add `getRandomBigNumber`

## 2.2

- Add `getSimulationComputeUnits()`
Expand Down
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Eventually, most of these will end up in `@solana/web3.js`.

## What can I do with this module?

[Assert whether two public keys have the same value](#assert-whether-two-public-keys-have-the-same-value)

[Assert whether two BigNumbers (BNs) have the same value](<#assert-whether-two-bignumbers-(bns)-have-the-same-value>)

[Get a random BigNumber](#get-a-random-bignumber)

[Make multiple keypairs at once](#make-multiple-keypairs-at-once)

[Resolve a custom error message](#resolve-a-custom-error-message)
Expand Down Expand Up @@ -40,6 +46,38 @@ PRs are very much welcome! Read the [CONTRIBUTING guidelines for the Solana cour

## helpers for the browser and node.js

### Assert whether two public keys have the same value

Mainly helpful in unit testing. Will throw if the Keypairs don't have the same value.

```typescript
assertPublicKeyEqual(one.publicKey, two.publicKey);
```

### Assert whether two BigNumbers (BNs) have the same value

Mainly helpful in unit testing. Will throw if the BigNumbers don't have the same value.

You can provie this with either BNs / BigNumbers (from `bn.js`) or strings formatted using the same output as `bn.js` uses. They'll be compared as if they were BNs. This is handy for comparing fetch account values where these numbers are returned as strings.

```typescript
assertBigNumberEqual(one, alsoOne);
```

const one = new BigNumber(1);
const alsoOne = new BigNumber(1);

### Get a random BigNumber

Mainly helpful in unit testing. Will throw if the Keypairs don't have the same value.

An optional `size` can be given with a size in bytes. Default is `8`.

```typescript
const randomBigNumber = getRandomBigNumber();
console.log(randomBigNumber);
```

### Make multiple keypairs at once

Usage:
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

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

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solana-developers/helpers",
"version": "2.2.0",
"version": "2.3.0",
"description": "Solana helper functions",
"main": "dist/index.js",
"private": false,
Expand Down Expand Up @@ -33,11 +33,12 @@
},
"devDependencies": {
"@digitak/esrun": "^3.2.24",
"@types/bn.js": "^5.1.5",
"@types/node": "^20.5.7",
"tsup": "^7.2.0",
"esrun": "^3.2.26",
"typescript": "^5.2.2",
"prettier": "^3.0.3"
"prettier": "^3.0.3",
"tsup": "^7.2.0",
"typescript": "^5.2.2"
},
"publishConfig": {
"access": "public",
Expand Down
34 changes: 34 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
initializeKeypair,
getLogs,
getSimulationComputeUnits,
assertBigNumberEqual,
getRandomBigNumber,
} from "./index";
import {
Connection,
Expand All @@ -28,6 +30,7 @@ import base58 from "bs58";
import { exec as execNoPromises } from "child_process";
import { promisify } from "util";
import { writeFile, unlink as deleteFile } from "node:fs/promises";
import BigNumber from "bn.js";
import dotenv from "dotenv";

const exec = promisify(execNoPromises);
Expand Down Expand Up @@ -499,3 +502,34 @@ describe("getSimulationComputeUnits", () => {
assert.equal(computeUnitsSendSolAndSayThanks, 3888);
});
});

describe("getRandomBigNumber", () => {
test("getRandomBigNumber works", () => {
const randomBigNumber = getRandomBigNumber();
assert.ok(randomBigNumber);
});
});

describe("assertBigNumberEqual", () => {
test("assertBigNumberEqual works", () => {
const one = new BigNumber(1);
const alsoOne = new BigNumber(1);
assertBigNumberEqual(one, alsoOne);
});

test("assertBigNumberEqual throws an error if the numbers are not equal", () => {
const one = new BigNumber(1);
const two = new BigNumber(2);
assert.throws(() => assertBigNumberEqual(one, two), {
message: "assertion failed",
});
});
});

describe("assertPublicKeyEqual", () => {
test("assertPublicKeyEqual works", () => {
const one = Keypair.generate();
const two = Keypair.fromSecretKey(one.secretKey);
assert.ok(one.publicKey.equals(two.publicKey));
});
});
29 changes: 29 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ import {
SimulatedTransactionResponse,
} from "@solana/web3.js";
import base58 from "bs58";
import BigNumber from "bn.js";
import path from "path";
import { readFile, appendFile } from "fs/promises";
import assert from "node:assert/strict";
import { randomBytes } from "crypto";

// Default value from Solana CLI
const DEFAULT_FILEPATH = "~/.config/solana/id.json";

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";
Expand Down Expand Up @@ -359,3 +363,28 @@ export const getSimulationComputeUnits = async (
getErrorFromRPCResponse(rpcResponse);
return rpcResponse.value.unitsConsumed || null;
};

export const getRandomBigNumber = (size: number = 8) => {
return new BigNumber(randomBytes(size));
};

export const assertBigNumberEqual = (
actual: BigNumber | string,
expected: BigNumber | string,
) => {
if (typeof actual === "string") {
actual = new BigNumber(actual);
}
if (typeof expected === "string") {
expected = new BigNumber(expected);
}
// See https://github.com/indutny/bn.js/?tab=readme-ov-file#instructions
assert(actual.cmp(expected) === 0);
};

export const assertPublicKeyEqual = (
actual: PublicKey,
expected: PublicKey,
) => {
assert.equal(actual.toBase58(), expected.toBase58());
};

0 comments on commit 376c790

Please sign in to comment.