From b438f126a6bfdfe0776ef28a92b7e2443df3366f Mon Sep 17 00:00:00 2001 From: Nigel Christian Date: Thu, 8 Apr 2021 11:19:53 -0400 Subject: [PATCH] fix payment threshold bug --- package.json | 2 +- src/noops.ts | 3 +-- test/test.spec.ts | 17 ++++++++++++++++- test/util/test-util.ts | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 72b2e59..975e20a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gitpayd", - "version": "0.1.8", + "version": "0.1.9", "description": "Github Workflows + BTC / LND, gitpayd watches your repo for new commits and sends payments to contributors", "scripts": { "clean": "rm -rf dist/", diff --git a/src/noops.ts b/src/noops.ts index 01ed293..e070689 100644 --- a/src/noops.ts +++ b/src/noops.ts @@ -27,8 +27,7 @@ const isValidPayment = (issueAmount: string, balance: number): boolean => { return ( NUM_AMT > 0 && NUM_AMT < MAX_PAYMENT && - balance >= NUM_AMT && - NUM_AMT < PAYMENT_THRESHOLD + (balance - PAYMENT_THRESHOLD) > NUM_AMT ); }; diff --git a/test/test.spec.ts b/test/test.spec.ts index c5123d7..776d31c 100644 --- a/test/test.spec.ts +++ b/test/test.spec.ts @@ -1,4 +1,4 @@ -import { AuthorizedRoles, splitter, validateCollaborators } from './util/test-util'; +import { AuthorizedRoles, isValidPayment, splitter, validateCollaborators } from './util/test-util'; import { strict as assert } from 'assert'; const BODY: string = "Bounty: 100000"; @@ -26,4 +26,19 @@ describe('Test helper function for author validation', () => { const check = validateCollaborators(AuthorizedRoles.COLLABORATOR); assert.strictEqual(check, true); }); +}); + +describe('Test helper function for payment validation', () => { + it('should return true valid payment', () => { + const check = isValidPayment("10000", 400000); + assert.strictEqual(check, true); + }); + it('should return false if above maximum allowable payment', () => { + const check = isValidPayment("100001", 400000); + assert.strictEqual(check, false); + }); + it('should return false if it breaks threshold', () => { + const check = isValidPayment("10000", 209000); + assert.strictEqual(check, false); + }); }); \ No newline at end of file diff --git a/test/util/test-util.ts b/test/util/test-util.ts index 45805a0..c51c777 100644 --- a/test/util/test-util.ts +++ b/test/util/test-util.ts @@ -1,6 +1,9 @@ // TODO: fix yargs breaking test. // This is duplicated form ../util/util.ts +const TEST_MAX_PAYMENT = 100000; +const TEST_PAYMENT_THRESHOLD = 200000; + /** * Authorized roles */ @@ -29,4 +32,19 @@ export const validateCollaborators = (role: AuthorizedRoles): boolean => { return ( role === AuthorizedRoles.COLLABORATOR || role === AuthorizedRoles.OWNER ); +}; + +/** + * Run payment validity logic + * @param issueAmount - issue bounty + * @param balance - balance of the lightning node gitpayd connects to + * @returns + */ + export const isValidPayment = (issueAmount: string, balance: number): boolean => { + const NUM_AMT = parseInt(issueAmount, 10); + return ( + NUM_AMT > 0 && + NUM_AMT < TEST_MAX_PAYMENT && + (balance - TEST_PAYMENT_THRESHOLD) > NUM_AMT + ); }; \ No newline at end of file