Skip to content

Commit

Permalink
Add tests for remaining functions of handler for L2RewardsManager
Browse files Browse the repository at this point in the history
  • Loading branch information
9inpachi committed Aug 27, 2024
1 parent ce1d646 commit 51e8337
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
42 changes: 37 additions & 5 deletions lib/contracts/handlers/l2-reward-manager-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { isHash } from 'viem';
import { L2_REWARD_MANAGER_ABIS } from '../abis/l2-reward-manager-abis';
import { Chain } from '../../chains/constants';
import {
setupTestPublicClient,
setupTestWalletClient,
} from '../../../test/setup-test-clients';
import { mockAccount, testingUtils } from '../../../test/setup-tests';
import { Chain } from '../../chains/constants';
import { L2_REWARD_MANAGER_ABIS } from '../abis/l2-reward-manager-abis';
import { L2RewardManagerHandler } from './l2-reward-manager-handler';
import {
ClaimOrder,
L2RewardManagerHandler,
} from './l2-reward-manager-handler';
import { generateAddress } from '../../../test/mocks/address';
import { InvalidInputError } from '../../errors/validation-errors';

describe('L2RewardManagerHandler', () => {
const contractTestingUtils = testingUtils.generateContractUtils(
Expand Down Expand Up @@ -73,14 +78,41 @@ describe('L2RewardManagerHandler', () => {
});

it('should check if the reward has been claimed', async () => {
contractTestingUtils.mockCall('isClaimed', [[true]]);
contractTestingUtils.mockCall('isClaimed', [true]);

const isClaimed = await handler.isClaimed(mockAccount, generateAddress(32));
expect(isClaimed).toEqual(true);
});

it('should check if the claiming is locked', async () => {
contractTestingUtils.mockCall('isClaimingLocked', [[true]]);
contractTestingUtils.mockCall('isClaimingLocked', [true]);
expect(await handler.isClaimingLocked(generateAddress(32))).toEqual(true);
});

it('should claim rewards through claim orders', async () => {
const mockClaimOrders: ClaimOrder[] = [
{
account: generateAddress(),
amount: 1n,
intervalId: generateAddress(32),
merkleProof: [generateAddress(32)],
},
];
contractTestingUtils.mockTransaction('claimRewards');

const { transact, estimate } = handler.claimRewards(
mockAccount,
mockClaimOrders,
);

expect(typeof (await estimate())).toBe('bigint');
expect(isHash(await transact())).toBe(true);
});

it('should throw error if claim orders are empty', async () => {
const mockClaimOrders: ClaimOrder[] = [];
expect(() => handler.claimRewards(mockAccount, mockClaimOrders)).toThrow(
InvalidInputError,
);
});
});
18 changes: 13 additions & 5 deletions lib/contracts/handlers/l2-reward-manager-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { ViemChain, VIEM_CHAINS, Chain } from '../../chains/constants';
import { CONTRACT_ADDRESSES } from '../addresses';
import { L2_REWARD_MANAGER_ABIS } from '../abis/l2-reward-manager-abis';
import { InvalidInputError } from '../../errors/validation-errors';

export type ClaimOrder = {
account: Address;
Expand Down Expand Up @@ -145,12 +146,19 @@ export class L2RewardManagerHandler {
*
* `estimate: () => Promise<bigint>` - Gas estimate of the
* transaction.
* @throws {InvalidInputError} If `claimOrders` is empty.
*/
public claimRewards(
account: Address,
// One or more claim orders.
claimOrders: [ClaimOrder, ...ClaimOrder[]],
) {
public claimRewards(account: Address, claimOrders: ClaimOrder[]) {
if (claimOrders.length === 0) {
throw new InvalidInputError(
'`claimOrders` cannot be empty and must be specified',
{
fixMessage:
'Specify at least one or more claim orders to claim rewards',
},
);
}

const transact = () =>
this.getContract().write.claimRewards([claimOrders], {
chain: this.viemChain,
Expand Down
2 changes: 2 additions & 0 deletions lib/errors/validation-errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BaseError } from './base-error';

export class AccountError extends BaseError {}

export class InvalidInputError extends BaseError {}

0 comments on commit 51e8337

Please sign in to comment.