Skip to content

Commit

Permalink
feat: GET /deposits/details endpoint (#233)
Browse files Browse the repository at this point in the history
This endpoint allows to retrieve a single deposit based on the
`depositTxHash` and `originChainId`.
  • Loading branch information
dohaki authored May 10, 2023
1 parent 0814abb commit e72511f
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/modules/deposit/entry-point/http/controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Controller, Get, Query } from "@nestjs/common";
import { ApiResponse, ApiTags } from "@nestjs/swagger";
import { DepositService } from "../../service";
import { GetDepositsQuery, GetDepositsStatsResponse } from "./dto";
import { GetDepositsQuery, GetDepositDetailsQuery, GetDepositsStatsResponse } from "./dto";

@Controller()
export class DepositController {
Expand All @@ -20,6 +20,12 @@ export class DepositController {
return this.depositService.getDeposits(query.status, limit, offset);
}

@Get("deposits/details")
@ApiTags("deposits")
getDepositsDetails(@Query() query: GetDepositDetailsQuery) {
return this.depositService.getDepositDetails(query.depositTxHash, parseInt(query.originChainId));
}

@Get("deposits/stats")
@ApiTags("deposits")
@ApiResponse({ type: GetDepositsStatsResponse })
Expand Down
15 changes: 14 additions & 1 deletion src/modules/deposit/entry-point/http/dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty } from "@nestjs/swagger";
import { Type } from "class-transformer";
import { IsDateString, IsEnum, IsInt, IsOptional, IsString, Max, Min } from "class-validator";
import { IsDateString, IsEnum, IsInt, IsOptional, IsString, Max, Min, IsPositive } from "class-validator";

export class GetDepositsQuery {
@IsOptional()
Expand Down Expand Up @@ -38,6 +38,19 @@ export class GetDepositsQuery {
address: string;
}

export class GetDepositDetailsQuery {
@IsString()
@Type(() => String)
@ApiProperty({ example: "0x", required: true })
depositTxHash: string;

@IsInt()
@IsPositive()
@Type(() => Number)
@ApiProperty({ example: 1, required: true })
originChainId: string;
}

export class GetDepositsStatsResponse {
@ApiProperty({ example: 100, description: "Total number of deposits" })
totalDeposits: number;
Expand Down
12 changes: 12 additions & 0 deletions src/modules/deposit/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ export class InvalidAddressException extends HttpException {
);
}
}

export class DepositNotFoundException extends HttpException {
constructor() {
super(
{
error: DepositNotFoundException.name,
message: "Deposit not found",
},
HttpStatus.NOT_FOUND,
);
}
}
12 changes: 11 additions & 1 deletion src/modules/deposit/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getTotalVolumeQuery,
} from "./adapter/db/queries";
import { AppConfig } from "../configuration/configuration.service";
import { InvalidAddressException } from "./exceptions";
import { InvalidAddressException, DepositNotFoundException } from "./exceptions";

export const DEPOSITS_STATS_CACHE_KEY = "deposits:stats";

Expand Down Expand Up @@ -132,6 +132,16 @@ export class DepositService {
};
}

public async getDepositDetails(depositTxHash: string, sourceChainId: number) {
const deposit = await this.depositRepository.findOne({ where: { depositTxHash, sourceChainId } });

if (!deposit) {
throw new DepositNotFoundException();
}

return deposit;
}

public async getEtlReferralDeposits(date: string) {
// delete time from date in case of datetime
const parsedDate = new Date(date).toISOString().split("T")[0];
Expand Down
58 changes: 57 additions & 1 deletion test/deposit.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { INestApplication } from "@nestjs/common";
import { Test } from "@nestjs/testing";
import { constants, ethers } from "ethers";

import { DepositFixture, mockManyDepositEntities } from "../src/modules/deposit/adapter/db/deposit-fixture";
import {
DepositFixture,
mockManyDepositEntities,
mockDepositEntity,
} from "../src/modules/deposit/adapter/db/deposit-fixture";
import { ValidationPipe } from "../src/validation.pipe";
import { AppModule } from "../src/app.module";
import { RunMode } from "../src/dynamic-module";
Expand Down Expand Up @@ -120,6 +124,58 @@ describe("GET /deposits", () => {
});
});

describe("GET /deposits/details", () => {
const depositorAddress = "0x9A8f92a830A5cB89a3816e3D267CB7791c16b04D";
const depositTxHash = "0x91616c035fe2b7432d1549b9a204e29fd7cf3f5d5d9170cd418e5cc7dcc4e3a0";
const sourceChainId = 1;

const FILLED_DEPOSIT = mockDepositEntity({
status: "filled",
depositorAddr: depositorAddress,
sourceChainId,
depositTxHash,
amount: "10",
filled: "10",
});

beforeAll(async () => {
await app.get(DepositFixture).insertManyDeposits([FILLED_DEPOSIT]);
});

it("200 with for correct params", async () => {
const response = await request(app.getHttpServer()).get(
`/deposits/details?depositTxHash=${depositTxHash}&originChainId=${sourceChainId}`,
);
expect(response.status).toBe(200);
expect(response.body.status).toBe("filled");
});

it("404 for non existent depositTxHash", async () => {
const response = await request(app.getHttpServer()).get(
`/deposits/details?depositTxHash=0x&originChainId=${sourceChainId}`,
);
expect(response.status).toBe(404);
});

it("404 for non existent originChainId", async () => {
const response = await request(app.getHttpServer()).get(
`/deposits/details?depositTxHash=${depositTxHash}&originChainId=10`,
);
expect(response.status).toBe(404);
});

it("400 for invalid originChainId", async () => {
const response = await request(app.getHttpServer()).get(
`/deposits/details?depositTxHash=${depositTxHash}&originChainId=invalid`,
);
expect(response.status).toBe(400);
});

afterAll(async () => {
await app.get(DepositFixture).deleteAllDeposits();
});
});

describe("GET /etl/referral-deposits", () => {
const date = "2022-01-01";

Expand Down

0 comments on commit e72511f

Please sign in to comment.