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 2, 2024
1 parent 71c10ef commit 965d33f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 29 deletions.
6 changes: 6 additions & 0 deletions apps/solowallet/src/db/solowallet.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ export class SolowalletDocument extends AbstractDocument {
@Prop({ type: String, required: true })
lightning: string;

@Prop({ type: String, required: true, unique: true })
paymentTracker?: string;

@Prop({ type: String, required: true })
reference: string;
}

export const SolowalletSchema =
SchemaFactory.createForClass(SolowalletDocument);

// Ensure uniqueness only when paymentTracker is not null
SolowalletSchema.index({ paymentTracker: 1 }, { unique: true, sparse: true });
77 changes: 57 additions & 20 deletions apps/solowallet/src/solowallet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import {
Currency,
DepositFundsRequestDto,
DepositFundsResponse,
DepositsMeta,
fedimint_receive_failure,
fedimint_receive_success,
FedimintService,
fiatToBtc,
FindUserDepositTxsResponse,
FindUserTxsRequestDto,
FmInvoice,
PaginatedSolowalletTxsResponse,
QuoteDto,
QuoteRequestDto,
Expand Down Expand Up @@ -154,20 +155,8 @@ export class SolowalletService {
const deposits = allDeposits
.slice(selectPage * size, (selectPage + 1) * size + size)
.map((deposit) => {
let lightning: FmInvoice;
try {
lightning = JSON.parse(deposit.lightning);
} catch (error) {
this.logger.warn('Error parsing lightning invoice', error);
lightning = {
invoice: '',
operationId: '',
};
}

return {
...deposit,
lightning,
id: deposit._id,
status: deposit.status,
createdAt: deposit.createdAt.toDateString(),
Expand All @@ -183,6 +172,38 @@ export class SolowalletService {
};
}

private async getDepositsMeta(
userId: string,
): Promise<DepositsMeta | undefined> {
let meta: DepositsMeta = undefined;
try {
meta = await this.wallet
.aggregate([
{
$match: {
userId: userId,
status: TransactionStatus.COMPLETE,
},
},
{
$group: {
_id: '$userId',
totalMsats: { $sum: '$amountMsats' },
avgMsats: { $avg: '$amountMsats' },
count: { $sum: 1 },
},
},
])
.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 @@ -219,30 +240,46 @@ export class SolowalletService {
userId,
amountMsats,
amountFiat,
lightning: JSON.stringify(lightning),
lightning: lightning.invoice,
paymentTracker: lightning.operationId,
status,
reference,
});

// listen for payment
this.fedimintService.receive(ReceiveContext.SOLOWALLET, deposit._id);
this.fedimintService.receive(
ReceiveContext.SOLOWALLET,
lightning.operationId,
);

const deposits = await this.getPaginatedUserDeposits({
userId,
pagination: { page: 0, size: 10 },
});

const meta = await this.getDepositsMeta(userId);

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(userId);

return {
deposits,
meta,
};
}

@OnEvent(fedimint_receive_success)
Expand All @@ -251,7 +288,7 @@ export class SolowalletService {
operationId,
}: ReceivePaymentSuccessEvent) {
await this.wallet.findOneAndUpdate(
{ _id: operationId },
{ paymentTracker: operationId },
{
status: TransactionStatus.COMPLETE,
},
Expand All @@ -268,11 +305,11 @@ export class SolowalletService {
operationId,
}: ReceivePaymentFailureEvent) {
this.logger.log(
`Failed to eceive lightning payment for ${context} : ${operationId}`,
`Failed to receive lightning payment for ${context} : ${operationId}`,
);

await this.wallet.findOneAndUpdate(
{ _id: operationId },
{ paymentTracker: operationId },
{
state: TransactionStatus.FAILED,
},
Expand Down
2 changes: 1 addition & 1 deletion apps/swap/src/swap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export class SwapService {
operationId,
}: ReceivePaymentFailureEvent) {
this.logger.log(
`Failed to eceive lightning payment for ${context} : ${operationId}`,
`Failed to receive lightning payment for ${context} : ${operationId}`,
);

await this.offramp.findOneAndUpdate(
Expand Down
23 changes: 17 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.

16 changes: 14 additions & 2 deletions 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 All @@ -40,7 +41,7 @@ message SolowalletTx {

optional int32 amount_fiat = 5;

lightning.Bolt11 lightning = 6;
string lightning = 6;

reserved 7, 8, 9;

Expand All @@ -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,9 @@ message PaginatedSolowalletTxsResponse {
// Number of pages given the current page size
int32 pages = 4;
}

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

0 comments on commit 965d33f

Please sign in to comment.