Skip to content

Commit

Permalink
add: code and tests for requesting negative SOL per PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemaccana committed Jan 23, 2024
1 parent 375f9c4 commit 4ce6f3f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { validate } from "./validate";

const WALLET_ADDRESS_FOR_TESTS = "dDCQNnDmNbFVi8cQhKAgXhyhXeJ625tvwsunRyRc7c8";
const PDA_ADDRESS_FOR_TESTS = "4MD31b2GFAWVDYQT8KG7E5GcZiFyy4MpDUt4BcyEdJRP";

// Write some tests for the validate function
describe("validate", () => {
test("allows reasonable usage", () => {
Expand All @@ -8,7 +11,7 @@ describe("validate", () => {

test("throws when wallet address is a PDA", () => {
expect(() => {
validate("4MD31b2GFAWVDYQT8KG7E5GcZiFyy4MpDUt4BcyEdJRP", 1);
validate(PDA_ADDRESS_FOR_TESTS, 1);
}).toThrow("Please enter valid wallet address.");
});

Expand All @@ -20,13 +23,19 @@ describe("validate", () => {

test("throws when amount is 0", () => {
expect(() => {
validate("abcdef", 0);
validate(WALLET_ADDRESS_FOR_TESTS, 0);
}).toThrow("Missing SOL amount.");
});

test("throws when amount is negative", () => {
expect(() => {
validate(WALLET_ADDRESS_FOR_TESTS, -3);
}).toThrow("Requested SOL amount cannot be negative.");
});

test("throws when amount is too large", () => {
expect(() => {
validate("abcdef", 6);
validate(WALLET_ADDRESS_FOR_TESTS, 6);
}).toThrow("Requested SOL amount too large.");
});

Expand Down
4 changes: 4 additions & 0 deletions lib/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export const validate = (walletAddress: string, amount: number): void => {
throw new Error("Missing SOL amount.");
}

if (amount < 0) {
throw new Error("Requested SOL amount cannot be negative.");
}

if (amount > MAX_SOL_AMOUNT) {
throw new Error("Requested SOL amount too large.");
}
Expand Down

0 comments on commit 4ce6f3f

Please sign in to comment.