Skip to content

Commit

Permalink
feat: find and list user transactions from solowallet
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Nov 30, 2024
1 parent fc71ef4 commit c094d53
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 38 deletions.
14 changes: 13 additions & 1 deletion apps/api/src/solowallet/solowallet.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { DepositFundsRequestDto } from '@bitsacco/common';
import {
DepositFundsRequestDto,
FindUserTxsRequestDto,
} from '@bitsacco/common';
import { Body, Controller, Logger, Post } from '@nestjs/common';
import { ApiOperation, ApiBody } from '@nestjs/swagger';
import { SolowalletService } from './solowallet.service';
Expand All @@ -19,4 +22,13 @@ export class SolowalletController {
depositFunds(@Body() req: DepositFundsRequestDto) {
return this.walletService.depositFunds(req);
}

@Post('user-deposits')
@ApiOperation({ summary: 'Find Solowallet user deposits' })
@ApiBody({
type: FindUserTxsRequestDto,
})
findUserDeposits(@Body() req: FindUserTxsRequestDto) {
return this.walletService.findUserDeposits(req);
}
}
5 changes: 5 additions & 0 deletions apps/api/src/solowallet/solowallet.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
DepositFundsRequestDto,
FindUserTxsRequestDto,
SOLOWALLET_SERVICE_NAME,
SolowalletServiceClient,
} from '@bitsacco/common';
Expand All @@ -23,4 +24,8 @@ export class SolowalletService implements OnModuleInit {
depositFunds(req: DepositFundsRequestDto) {
return this.client.depositFunds(req);
}

findUserDeposits(req: FindUserTxsRequestDto) {
return this.client.findUserDeposits(req);
}
}
2 changes: 1 addition & 1 deletion apps/shares/.env.manual
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NODE_ENV='development'
SHARES_GRPC_URL='0.0.0.0:4070'
SHARES_ISSUED='10000'
DATABASE_URL=mongodb://bs:password@mongodb:27017
DATABASE_URL=mongodb://bs:password@0.0.0.0:27017
6 changes: 6 additions & 0 deletions apps/solowallet/src/solowallet.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GrpcMethod } from '@nestjs/microservices';
import {
SolowalletServiceControllerMethods,
DepositFundsRequestDto,
FindUserTxsRequestDto,
} from '@bitsacco/common';
import { SolowalletService } from './solowallet.service';

Expand All @@ -15,4 +16,9 @@ export class SolowalletController {
depositFunds(request: DepositFundsRequestDto) {
return this.solowalletService.depositFunds(request);
}

@GrpcMethod()
findUserDeposits(request: FindUserTxsRequestDto) {
return this.solowalletService.findUserDeposits(request);
}
}
37 changes: 34 additions & 3 deletions apps/solowallet/src/solowallet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import {
CreateOnrampSwapDto,
DepositFundsRequestDto,
fiatToBtc,
SolowalletDepositTransaction,
FindUserTxsRequest,
PaginatedRequestDto,
PaginatedSolowalletTxsResponse,
SolowalletTxs,
SWAP_SERVICE_NAME,
SwapResponse,
SwapServiceClient,
Expand Down Expand Up @@ -74,7 +77,7 @@ export class SolowalletService {
async depositFunds({
userId,
fiatDeposit,
}: DepositFundsRequestDto): Promise<SolowalletDepositTransaction> {
}: DepositFundsRequestDto): Promise<SolowalletTxs> {
const { status, reference, amountMsats, amountFiat } = fiatDeposit
? await this.initiateSwap(fiatDeposit)
: {
Expand All @@ -95,10 +98,38 @@ export class SolowalletService {

return {
...deposit,
status,
id: deposit._id,
createdAt: deposit.createdAt.toDateString(),
updatedAt: deposit.updatedAt.toDateString(),
};
}

async findUserDeposits({
userId,
pagination,
}: FindUserTxsRequest): Promise<PaginatedSolowalletTxsResponse> {
const allDeposits = await this.wallet.find({ userId });

const { page, size } = pagination;
const pages = Math.ceil(allDeposits.length / size);

// select the last page if requested page exceeds total pages possible
const selectPage = page > pages ? pages - 1 : page;

const deposits = allDeposits
.slice(selectPage * size, (selectPage + 1) * size + size)
.map((deposit) => ({
...deposit,
id: deposit._id,
createdAt: deposit.createdAt.toDateString(),
updatedAt: deposit.updatedAt.toDateString(),
}));

return {
transactions: deposits,
page: selectPage,
size,
pages,
};
}
}
2 changes: 1 addition & 1 deletion apps/swap/.env.manual
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ REDIS_HOST='0.0.0.0'
REDIS_PORT='6379'
MOCK_BTC_KES_RATE='8708520.117232416'
# CURRENCY_API_KEY='foo-bar-baz'
DATABASE_URL=mongodb://bs:password@mongodb:27017
DATABASE_URL=mongodb://bs:password@0.0.0.0:27017
INTASEND_PUBLIC_KEY=ISPubKey_test_925ab885-f06d-4ace-8507-4186413a59a4
INTASEND_PRIVATE_KEY=ISSecretKey_test_3d887e44-33c4-4455-978e-d2ae7b10907d
FEDIMINT_CLIENTD_BASE_URL=http://0.0.0.0:7070
Expand Down
6 changes: 3 additions & 3 deletions apps/swap/src/swap.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
Currency,
PaginatedRequest,
QuoteRequest,
QuoteResponse,
TransactionStatus,
Expand All @@ -17,6 +16,7 @@ import {
fiatToBtc,
btcToFiat,
SupportedCurrencyType,
PaginatedRequestDto,
} from '@bitsacco/common';
import { v4 as uuidv4 } from 'uuid';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
Expand Down Expand Up @@ -205,7 +205,7 @@ export class SwapService {
async listOnrampSwaps({
page,
size,
}: PaginatedRequest): Promise<PaginatedSwapResponse> {
}: PaginatedRequestDto): Promise<PaginatedSwapResponse> {
const onramps = await this.onramp.find({});
const pages = Math.ceil(onramps.length / size);

Expand Down Expand Up @@ -297,7 +297,7 @@ export class SwapService {
async listOfframpSwaps({
page,
size,
}: PaginatedRequest): Promise<PaginatedSwapResponse> {
}: PaginatedRequestDto): Promise<PaginatedSwapResponse> {
const offramps = await this.offramp.find({});
const pages = Math.ceil(offramps.length / size);

Expand Down
1 change: 1 addition & 0 deletions libs/common/src/dto/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './lib.dto';
export * from './swap.dto';
export * from './nostr.dto';
export * from './sms.dto';
Expand Down
16 changes: 16 additions & 0 deletions libs/common/src/dto/lib.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Type } from 'class-transformer';
import { IsNumber } from 'class-validator';
import { PaginatedRequest } from '../types';
import { ApiProperty } from '@nestjs/swagger';

export class PaginatedRequestDto implements PaginatedRequest {
@IsNumber()
@Type(() => Number)
@ApiProperty({ type: Number })
page: number;

@IsNumber()
@Type(() => Number)
@ApiProperty({ type: Number })
size: number;
}
16 changes: 15 additions & 1 deletion libs/common/src/dto/solowallet.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Type } from 'class-transformer';
import { IsNotEmpty, IsString, ValidateNested } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { CreateOnrampSwapDto } from './swap.dto';
import { DepositFundsRequest } from '../types';
import { DepositFundsRequest, FindUserTxsRequest } from '../types';
import { PaginatedRequestDto } from './lib.dto';

export class DepositFundsRequestDto implements DepositFundsRequest {
@IsNotEmpty()
Expand All @@ -16,3 +17,16 @@ export class DepositFundsRequestDto implements DepositFundsRequest {
@ApiProperty({ type: CreateOnrampSwapDto })
fiatDeposit?: CreateOnrampSwapDto;
}

export class FindUserTxsRequestDto implements FindUserTxsRequest {
@IsNotEmpty()
@IsString()
@Type(() => String)
@ApiProperty()
userId: string;

@ValidateNested()
@Type(() => PaginatedRequestDto)
@ApiProperty({ type: PaginatedRequestDto })
pagination: PaginatedRequestDto;
}
7 changes: 7 additions & 0 deletions libs/common/src/types/proto/lib.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 31 additions & 9 deletions libs/common/src/types/proto/solowallet.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions libs/common/src/types/proto/swap.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions proto/lib.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ enum TransactionStatus {
FAILED = 2;
COMPLETE = 3;
}

message PaginatedRequest {
// Page offset to start from
int32 page = 2;
// Number of items to be return per page
int32 size = 1;
}
22 changes: 20 additions & 2 deletions proto/solowallet.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import "swap.proto";
package solowallet;

service SolowalletService {
rpc DepositFunds(DepositFundsRequest) returns (SolowalletDepositTransaction){}
rpc DepositFunds(DepositFundsRequest) returns (SolowalletTxs){}

rpc FindUserDeposits(FindUserTxsRequest) returns (PaginatedSolowalletTxsResponse){}
}

message DepositFundsRequest {
Expand All @@ -17,7 +19,7 @@ message DepositFundsRequest {
// Add more otional funding sources, like direct lightning deposit
}

message SolowalletDepositTransaction {
message SolowalletTxs {
string id = 1;

string user_id = 2;
Expand All @@ -36,3 +38,19 @@ message SolowalletDepositTransaction {

optional string updatedAt = 12;
}

message FindUserTxsRequest {
string user_id = 1;
lib.PaginatedRequest pagination = 2;
}

message PaginatedSolowalletTxsResponse {
// List of onramp swaps
repeated SolowalletTxs transactions = 1;
// Current page offset
int32 page = 2;
// Number of items return per page
int32 size = 3;
// Number of pages given the current page size
int32 pages = 4;
}
Loading

0 comments on commit c094d53

Please sign in to comment.