Skip to content

Commit

Permalink
feat: added getRemainingBudget() getter
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>
  • Loading branch information
quiet-node committed Sep 30, 2024
1 parent 955111f commit fd9e119
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/relay/src/lib/services/hbarLimitService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ export class HbarLimitService implements IHbarLimitService {
);
}

/**
* Retrieves the remaining budget.
*
* @public
* @returns {number} The remaining budget value.
*/
public getRemainingBudget(): number {
return this.remainingBudget;
}

/**
* Checks if the total daily budget has been exceeded.
* @param {string} mode - The mode of the transaction or request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('HbarLimitService', function () {
const register = new Registry();
const totalBudget = 100_000;
const mode = constants.EXECUTION_MODE.TRANSACTION;
const mockTxCost = 100;
const methodName = 'testMethod';
const mockEthAddress = '0x123';
const mockIpAddress = 'x.x.x';
Expand Down Expand Up @@ -668,6 +669,17 @@ describe('HbarLimitService', function () {
});
});

describe('getRemainingBudget', () => {
it('Should correctly get the remaining budget', async () => {
const originalRemainingBudget = hbarLimitService.getRemainingBudget();
expect(originalRemainingBudget).to.eq(totalBudget);

await hbarLimitService.addExpense(mockTxCost);
const updatedRemainingBudget = hbarLimitService.getRemainingBudget();
expect(updatedRemainingBudget).to.eq(totalBudget - mockTxCost);
});
});

describe('isDailyBudgetExceeded', function () {
const testIsDailyBudgetExceeded = async (remainingBudget: number, expected: boolean) => {
// @ts-ignore
Expand Down

0 comments on commit fd9e119

Please sign in to comment.