Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Romsters committed Oct 12, 2023
1 parent 7671dfd commit 0067dcb
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 22 deletions.
37 changes: 17 additions & 20 deletions packages/api/src/transfer/transfer.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,26 +228,24 @@ describe("TransferService", () => {
});

describe("when token type is ERC20", () => {
it("adds ERC20 filter", async () => {
it("adds tokenAddress filter", async () => {
await service.findTokenTransfers(filterOptions);
expect(queryBuilderMock.where).toBeCalledTimes(1);
expect(queryBuilderMock.where).toHaveBeenCalledWith({
tokenAddress: filterOptions.tokenAddress,
fields: typeorm.IsNull(),
});
});
});

describe("when token type is ERC721", () => {
it("adds ERC721 filter", async () => {
it("adds tokenAddress filter", async () => {
await service.findTokenTransfers({
...filterOptions,
tokenType: TokenType.ERC721,
});
expect(queryBuilderMock.where).toBeCalledTimes(1);
expect(queryBuilderMock.where).toHaveBeenCalledWith({
tokenAddress: "tokenAddress",
fields: typeorm.Not(typeorm.IsNull()),
});
});
});
Expand Down Expand Up @@ -345,13 +343,19 @@ describe("TransferService", () => {
});

describe("when token type is ERC20", () => {
it("adds ERC20 filter", async () => {
it("adds address and ERC20 filter", async () => {
await service.findTokenTransfers(filterOptions);
expect(addressTransfersQueryBuilderMock.where).toBeCalledTimes(1);
expect(addressTransfersQueryBuilderMock.where).toHaveBeenCalledWith({
address: filterOptions.address,
fields: typeorm.IsNull(),
});
expect(addressTransfersQueryBuilderMock.andWhere).toBeCalledTimes(1);
expect(addressTransfersQueryBuilderMock.andWhere).toHaveBeenCalledWith(
`"addressTransfer"."tokenType" = :tokenType`,
{
tokenType: TokenType.ERC20,
}
);
});
});

Expand All @@ -364,8 +368,14 @@ describe("TransferService", () => {
expect(addressTransfersQueryBuilderMock.where).toBeCalledTimes(1);
expect(addressTransfersQueryBuilderMock.where).toHaveBeenCalledWith({
address: filterOptions.address,
fields: typeorm.Not(typeorm.IsNull()),
});
expect(addressTransfersQueryBuilderMock.andWhere).toBeCalledTimes(1);
expect(addressTransfersQueryBuilderMock.andWhere).toHaveBeenCalledWith(
`"addressTransfer"."tokenType" = :tokenType`,
{
tokenType: TokenType.ERC721,
}
);
});
});

Expand All @@ -385,19 +395,6 @@ describe("TransferService", () => {
});
});

describe("when token address is not specified", () => {
it("adds filter to exclude ETH token", async () => {
await service.findTokenTransfers(filterOptions);
expect(addressTransfersQueryBuilderMock.andWhere).toBeCalledTimes(1);
expect(addressTransfersQueryBuilderMock.andWhere).toHaveBeenCalledWith(
`"addressTransfer"."tokenAddress" != :tokenAddress`,
{
tokenAddress: normalizeAddressTransformer.to(L2_ETH_TOKEN_ADDRESS),
}
);
});
});

it("joins transfers and tokens records to the address transfers", async () => {
await service.findTokenTransfers(filterOptions);
expect(addressTransfersQueryBuilderMock.leftJoinAndSelect).toBeCalledTimes(2);
Expand Down
3 changes: 1 addition & 2 deletions packages/api/src/transfer/transfer.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { BadRequestException, Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository, FindOperator, MoreThanOrEqual, LessThanOrEqual, IsNull, Not } from "typeorm";
import { Repository, FindOperator, MoreThanOrEqual, LessThanOrEqual } from "typeorm";
import { Pagination } from "nestjs-typeorm-paginate";
import { paginate } from "../common/utils";
import { L2_ETH_TOKEN_ADDRESS } from "../common/constants";
import { IPaginationOptions, SortingOrder } from "../common/types";
import { Transfer } from "./transfer.entity";
import { TokenType } from "../token/token.entity";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { types, utils } from "zksync-web3";
import { mock } from "jest-mock-extended";
import { ZERO_HASH_64 } from "../../../constants";
import { TransferType } from "../../../entities/transfer.entity";
import { TokenType } from "../../../entities/token.entity";
import { defaultFinalizeDepositHandler } from "./default.handler";

describe("defaultFinalizeDepositHandler", () => {
Expand Down Expand Up @@ -66,11 +67,13 @@ describe("defaultFinalizeDepositHandler", () => {
log.topics[3] = ZERO_HASH_64;
const result = defaultFinalizeDepositHandler.extract(log, blockDetails);
expect(result.tokenAddress).toBe(utils.L2_ETH_TOKEN_ADDRESS);
expect(result.tokenType).toBe(TokenType.ETH);
});

it("extracts transfer with tokenAddress field populated with lower cased l2Token", () => {
const result = defaultFinalizeDepositHandler.extract(log, blockDetails);
expect(result.tokenAddress).toBe("0xdc187378edd8ed1585fb47549cc5fe633295d571");
expect(result.tokenType).toBe(TokenType.ERC20);
});

it("extracts transfer of deposit type", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BigNumber } from "ethers";
import { types, utils } from "zksync-web3";
import { mock } from "jest-mock-extended";
import { TransferType } from "../../../entities/transfer.entity";
import { TokenType } from "../../../entities/token.entity";
import { ethMintFromL1Handler } from "./ethMintFromL1.handler";

describe("ethMintFromL1Handler", () => {
Expand Down Expand Up @@ -60,6 +61,11 @@ describe("ethMintFromL1Handler", () => {
expect(result.blockNumber).toBe(215276);
});

it("extracts transfer with tokenType as ETH", () => {
const result = ethMintFromL1Handler.extract(log, blockDetails);
expect(result.tokenType).toBe(TokenType.ETH);
});

it("extracts transfer with populated amount", () => {
const result = ethMintFromL1Handler.extract(log, blockDetails);
expect(result.amount).toStrictEqual(BigNumber.from("0x6f05b59d3b20000"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BigNumber } from "ethers";
import { types } from "zksync-web3";
import { mock } from "jest-mock-extended";
import { TransferType } from "../../../entities/transfer.entity";
import { TokenType } from "../../../entities/token.entity";
import { contractDeployerTransferHandler } from "./contractDeployerTransfer.handler";

describe("contractDeployerTransferHandler", () => {
Expand Down Expand Up @@ -117,6 +118,11 @@ describe("contractDeployerTransferHandler", () => {
expect(result.type).toBe(TransferType.Mint);
});

it("extracts transfer with ERC20 token type", () => {
const result = contractDeployerTransferHandler.extract(log, blockDetails);
expect(result.tokenType).toBe(TokenType.ERC20);
});

it("adds isFeeOrRefund as false", () => {
const result = contractDeployerTransferHandler.extract(log, blockDetails);
expect(result.isFeeOrRefund).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BigNumber } from "ethers";
import { types, utils } from "zksync-web3";
import { mock } from "jest-mock-extended";
import { TransferType } from "../../../entities/transfer.entity";
import { TokenType } from "../../../entities/token.entity";
import { defaultTransferHandler } from "./default.handler";

describe("defaultTransferHandler", () => {
Expand Down Expand Up @@ -101,11 +102,13 @@ describe("defaultTransferHandler", () => {
log.address = utils.L2_ETH_TOKEN_ADDRESS;
const result = defaultTransferHandler.extract(log, blockDetails);
expect(result.tokenAddress).toBe(utils.L2_ETH_TOKEN_ADDRESS);
expect(result.tokenType).toBe(TokenType.ETH);
});

it("extracts transfer with tokenAddress field populated with lower cased log address", () => {
const result = defaultTransferHandler.extract(log, blockDetails);
expect(result.tokenAddress).toBe("0xc7e0220d02d549c4846a6ec31d89c3b670ebe35c");
expect(result.tokenType).toBe(TokenType.ERC20);
});

it("extracts transfer of fee type if to address is a bootloader address", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { types } from "zksync-web3";
import { mock } from "jest-mock-extended";
import { ZERO_HASH_64 } from "../../../constants";
import { TransferType } from "../../../entities/transfer.entity";
import { TokenType } from "../../../entities/token.entity";
import { erc721TransferHandler } from "./erc721Transfer.handler";

describe("erc721TransferHandler", () => {
Expand Down Expand Up @@ -90,6 +91,11 @@ describe("erc721TransferHandler", () => {
expect(result.tokenAddress).toBe("0x89bcb56033920b8a654109faeb1f87e0c3358cad");
});

it("extracts transfer of ERC721 token type", () => {
const result = erc721TransferHandler.extract(log, blockDetails);
expect(result.tokenType).toBe(TokenType.ERC721);
});

it("extracts transfer of transfer type if from address is not a zero address", () => {
const result = erc721TransferHandler.extract(log, blockDetails);
expect(result.type).toBe(TransferType.Transfer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BigNumber } from "ethers";
import { types, utils } from "zksync-web3";
import { mock } from "jest-mock-extended";
import { TransferType } from "../../../entities/transfer.entity";
import { TokenType } from "../../../entities/token.entity";
import { ethWithdrawalToL1Handler } from "./ethWithdrawalToL1.handler";

describe("ethWithdrawalToL1Handler", () => {
Expand Down Expand Up @@ -76,6 +77,11 @@ describe("ethWithdrawalToL1Handler", () => {
expect(result.type).toBe(TransferType.Withdrawal);
});

it("extracts transfer of ETH token type", () => {
const result = ethWithdrawalToL1Handler.extract(log, blockDetails);
expect(result.tokenType).toBe(TokenType.ETH);
});

it("adds isFeeOrRefund as false", () => {
const result = ethWithdrawalToL1Handler.extract(log, blockDetails);
expect(result.isFeeOrRefund).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { types, utils } from "zksync-web3";
import { mock } from "jest-mock-extended";
import { ZERO_HASH_64 } from "../../../constants";
import { TransferType } from "../../../entities/transfer.entity";
import { TokenType } from "../../../entities/token.entity";
import { defaultWithdrawalInitiatedHandler } from "./default.handler";

describe("defaultWithdrawalInitiatedHandler", () => {
Expand Down Expand Up @@ -66,11 +67,13 @@ describe("defaultWithdrawalInitiatedHandler", () => {
log.topics[3] = ZERO_HASH_64;
const result = defaultWithdrawalInitiatedHandler.extract(log, blockDetails);
expect(result.tokenAddress).toBe(utils.L2_ETH_TOKEN_ADDRESS);
expect(result.tokenType).toBe(TokenType.ETH);
});

it("extracts transfer with tokenAddress field populated with lower cased l2Token", () => {
const result = defaultWithdrawalInitiatedHandler.extract(log, blockDetails);
expect(result.tokenAddress).toBe("0xdc187378edd8ed1585fb47549cc5fe633295d571");
expect(result.tokenType).toBe(TokenType.ERC20);
});

it("extracts transfer of deposit type", () => {
Expand Down
Loading

0 comments on commit 0067dcb

Please sign in to comment.