Skip to content

Commit

Permalink
refactor: fedimint service has single, simple responsibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Nov 2, 2024
1 parent 52edb3a commit c0fe38c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
38 changes: 11 additions & 27 deletions apps/swap/src/fedimint/fedimint.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ConfigService } from '@nestjs/config';
import { Injectable, Logger } from '@nestjs/common';
import { MpesaOnrampSwap, SwapTransactionState } from '../../prisma/client';
import { FedimintClient, LightningPayResponse } from './fmts';

@Injectable()
Expand Down Expand Up @@ -34,34 +33,19 @@ export class FedimintService {
this.logger.log('FedimintService initialized');
}

async swapToBtc(
swap: MpesaOnrampSwap,
): Promise<{ state: SwapTransactionState; operationId: string }> {
this.logger.log('Swapping to BTC');
this.logger.log('Swap', swap);
async pay(invoice: string): Promise<{ operationId: string; fee: number }> {
this.logger.log('Paying invoice');
this.logger.log('Invoice', invoice);

if (
swap.state === SwapTransactionState.COMPLETE ||
swap.state === SwapTransactionState.FAILED
) {
throw new Error('Swap transaction alread finalized');
}

if (swap.state === SwapTransactionState.PROCESSING) {
this.logger.log(`Attempting to pay : ${swap.lightning}`);

const resp: LightningPayResponse = await this.fedimint.lightning.pay({
paymentInfo: swap.lightning,
const { operationId, fee }: LightningPayResponse =
await this.fedimint.lightning.pay({
paymentInfo: invoice,
});

this.logger.log('Lightning payment response', resp);

return {
state: SwapTransactionState.COMPLETE,
operationId: resp.operationId,
};
}

throw new Error('Attempted swap to btc while mpesa is still pending');
this.logger.log('Paid Invoice : ', operationId);
return {
operationId,
fee,
};
}
}
32 changes: 30 additions & 2 deletions apps/swap/src/swap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { v4 as uuidv4 } from 'uuid';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Inject, Injectable, Logger } from '@nestjs/common';
import { SwapTransactionState } from '../prisma/client';
import { MpesaOnrampSwap, SwapTransactionState } from '../prisma/client';
import { FxService } from './fx/fx.service';
import { PrismaService } from './prisma.service';
import { IntasendService } from './intasend/intasend.service';
Expand Down Expand Up @@ -259,7 +259,7 @@ export class SwapService {
let updates: { state: SwapTransactionState };
switch (mpesa.state) {
case MpesaTractactionState.Complete:
const { state } = await this.fedimintService.swapToBtc(swap);
const { state } = await this.swapToBtc(swap);
updates = { state };
break;
case MpesaTractactionState.Processing:
Expand All @@ -284,6 +284,34 @@ export class SwapService {
this.logger.log('Swap Updated');
return;
}

private async swapToBtc(
swap: MpesaOnrampSwap,
): Promise<{ state: SwapTransactionState; operationId: string }> {
this.logger.log('Swapping to BTC');
this.logger.log('Swap', swap);

if (
swap.state === SwapTransactionState.COMPLETE ||
swap.state === SwapTransactionState.FAILED
) {
throw new Error('Swap transaction alread finalized');
}

if (swap.state === SwapTransactionState.PROCESSING) {
this.logger.log(`Attempting to pay : ${swap.lightning}`);

const { operationId } = await this.fedimintService.pay(swap.lightning);
this.logger.log('Completed Onramp Swap', swap.id, operationId);

return {
state: SwapTransactionState.COMPLETE,
operationId,
};
}

throw new Error('Attempted swap to btc while mpesa is still pending');
}
}

function mapMpesaTxStateToSwapStatus(state: MpesaTractactionState): SwapStatus {
Expand Down

0 comments on commit c0fe38c

Please sign in to comment.