Skip to content

Commit

Permalink
feat: aggregate solowallet deposits meta
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Dec 1, 2024
1 parent 71c10ef commit 5295d1d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
43 changes: 41 additions & 2 deletions apps/solowallet/src/solowallet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
Currency,
DepositFundsRequestDto,
DepositFundsResponse,
DepositsMeta,
fedimint_receive_failure,
fedimint_receive_success,
FedimintService,
fiatToBtc,
FindUserDepositTxsResponse,
FindUserTxsRequestDto,
FmInvoice,
PaginatedSolowalletTxsResponse,
Expand Down Expand Up @@ -183,6 +185,31 @@ export class SolowalletService {
};
}

private async getDepositsMeta(): Promise<DepositsMeta | undefined> {
let meta: DepositsMeta = undefined;
try {
meta = await this.wallet
.aggregate([
{
$group: {
_id: null,
totalMsats: { $sum: '$amountMsats' },
count: { $sum: 1 },
avgMsats: { $avg: '$amountMsats' },
avgFiat: { $avg: '$amountFiat' },
},
},
])
.then((result) => {
return result[0];
});
} catch (e) {
this.logger.error('Error getting deposits meta', e);
}

return meta;
}

async depositFunds({
userId,
amountFiat,
Expand Down Expand Up @@ -232,17 +259,29 @@ export class SolowalletService {
pagination: { page: 0, size: 10 },
});

const meta = await this.getDepositsMeta();

return {
txId: deposit._id,
deposits,
meta,
};
}

async findUserDeposits({
userId,
pagination,
}: FindUserTxsRequestDto): Promise<PaginatedSolowalletTxsResponse> {
return this.getPaginatedUserDeposits({ userId, pagination });
}: FindUserTxsRequestDto): Promise<FindUserDepositTxsResponse> {
const deposits = await this.getPaginatedUserDeposits({
userId,
pagination,
});
const meta = await this.getDepositsMeta();

return {
deposits,
meta,
};
}

@OnEvent(fedimint_receive_success)
Expand Down
25 changes: 19 additions & 6 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.

15 changes: 14 additions & 1 deletion proto/solowallet.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package solowallet;
service SolowalletService {
rpc DepositFunds(DepositFundsRequest) returns (DepositFundsResponse){}

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

message DepositFundsRequest {
Expand All @@ -27,6 +27,7 @@ message DepositFundsRequest {
message DepositFundsResponse {
string tx_id = 1;
PaginatedSolowalletTxsResponse deposits = 2;
optional DepositsMeta meta = 3;
}

message SolowalletTx {
Expand Down Expand Up @@ -56,6 +57,11 @@ message FindUserTxsRequest {
lib.PaginatedRequest pagination = 2;
}

message FindUserDepositTxsResponse {
PaginatedSolowalletTxsResponse deposits = 1;
optional DepositsMeta meta = 2;
}

message PaginatedSolowalletTxsResponse {
// List of onramp swaps
repeated SolowalletTx transactions = 1;
Expand All @@ -66,3 +72,10 @@ message PaginatedSolowalletTxsResponse {
// Number of pages given the current page size
int32 pages = 4;
}

message DepositsMeta {
int32 total_msats = 1;
int32 count = 2;
float avg_msats = 3;
float avg_fiat = 4;
}

0 comments on commit 5295d1d

Please sign in to comment.