Skip to content

Commit

Permalink
refactor: two step persistence of onramp
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Nov 7, 2024
1 parent b999354 commit af2edd8
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 175 deletions.
6 changes: 3 additions & 3 deletions apps/swap/prisma/client/edge.js

Large diffs are not rendered by default.

221 changes: 118 additions & 103 deletions apps/swap/prisma/client/index.d.ts

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions apps/swap/prisma/client/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/swap/prisma/client/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "prisma-client-8249a439b47caffdc401079e727cadaab3d3608e2625a88d0e4834c1de075f21",
"name": "prisma-client-329939b91e664d538106e283479a0108be9d53a88e58de489d4dc5c579143dd3",
"main": "index.js",
"types": "index.d.ts",
"browser": "index-browser.js",
Expand Down
2 changes: 1 addition & 1 deletion apps/swap/prisma/client/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ model MpesaOnrampSwap {
/// Lightning invoice to pay.
lightning String
/// References the fiat onramp collection ID
collectionTracker String @unique
collectionTracker String? @unique
/// Fx Rate from
rate String
/// Amount in satoshi
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "MpesaOnrampSwap" ALTER COLUMN "collectionTracker" DROP NOT NULL;
2 changes: 1 addition & 1 deletion apps/swap/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ model MpesaOnrampSwap {
/// Lightning invoice to pay.
lightning String
/// References the fiat onramp collection ID
collectionTracker String @unique
collectionTracker String? @unique
/// Fx Rate from
rate String
/// Amount in satoshi
Expand Down
53 changes: 1 addition & 52 deletions apps/swap/src/swap.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ describe('SwapService', () => {
const swap = await swapService.createOnrampSwap(req);

expect(swap).toBeDefined();
expect(swap.rate).toEqual(mock_rate.toString());
expect(swap.status).toEqual(SwapStatus.PENDING);
});
});
Expand Down Expand Up @@ -271,61 +270,11 @@ describe('SwapService', () => {
state: SwapTransactionState.PENDING,
};

it.skip('creates a new swap tx if there was none recorded before', async () => {
(mockCacheManager.get as jest.Mock).mockImplementation(
(_key: string) => ({
lightning: 'lnbtcexampleinvoicee',
phone: '0700000000',
amount: '100',
rate: mock_rate.toString(),
ref: 'test-onramp-swap',
state: MpesaTransactionState.Pending,
}),
);

(
mockIntasendService.getMpesaTrackerFromCollectionUpdate as jest.Mock
).mockImplementation(() => ({
id: '123456789',
state: MpesaTransactionState.Pending,
}));

(mockPrismaService.mpesaOnrampSwap.create as jest.Mock).mockResolvedValue(
swap,
);
(
mockPrismaService.mpesaOnrampSwap.findUniqueOrThrow as jest.Mock
).mockImplementation(() => {
throw new Error('Not found');
});
(
mockPrismaService.mpesaOnrampSwap.update as jest.Mock
).mockImplementation((u) => {
return {
...swap,
state: u.data.state,
};
});

await swapService.processSwapUpdate({
...req,
state: MpesaTransactionState.Processing,
});
expect(
mockIntasendService.getMpesaTrackerFromCollectionUpdate,
).toHaveBeenCalled();
expect(
mockPrismaService.mpesaOnrampSwap.findUniqueOrThrow,
).toHaveBeenCalled();
expect(mockPrismaService.mpesaOnrampSwap.create).toHaveBeenCalled();
expect(mockPrismaService.mpesaOnrampSwap.update).toHaveBeenCalled();
});

it('should update swap tx from PENDING to PROCESSING', async () => {
(
mockIntasendService.getMpesaTrackerFromCollectionUpdate as jest.Mock
).mockImplementation(() => ({
id: '123456789',
id: 'MPSA56789',
state: MpesaTransactionState.Processing,
}));

Expand Down
27 changes: 16 additions & 11 deletions apps/swap/src/swap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,32 +141,38 @@ export class SwapService {
to: Currency.BTC,
});

const { id, state } = await this.intasendService.sendMpesaStkPush({
amount: Number(amountFiat),
phone_number: source.origin.phone,
api_ref: ref,
});

const { amountSats } = fiatToBtc({
amountFiat: Number(amountFiat),
btcToFiatRate: Number(rate),
});

const swap = await this.prismaService.mpesaOnrampSwap.create({
data: {
id,
reference: ref,
collectionTracker: id,
state: mapMpesaTxStateToSwapTxState(state),
state: SwapTransactionState.PENDING,
lightning: target.payout.invoice,
amountSats: amountSats.toFixed(2),
retryCount: 0,
rate,
},
});

const mpesa = await this.intasendService.sendMpesaStkPush({
amount: Number(amountFiat),
phone_number: source.origin.phone,
api_ref: ref,
});

const updatedSwap = await this.prismaService.mpesaOnrampSwap.update({
where: { id: swap.id },
data: {
collectionTracker: mpesa.id,
state: mapMpesaTxStateToSwapTxState(mpesa.state),
},
});

return {
...swap,
...updatedSwap,
status: mapSwapTxStateToSwapStatus(swap.state),
createdAt: swap.createdAt.toDateString(),
updatedAt: swap.updatedAt.toDateString(),
Expand Down Expand Up @@ -333,7 +339,6 @@ export class SwapService {

const swap = await this.prismaService.mpesaOnrampSwap.findUniqueOrThrow({
where: {
id: update.invoice_id,
collectionTracker: update.invoice_id,
},
});
Expand Down

0 comments on commit af2edd8

Please sign in to comment.