Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: on and offramp reference #19

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/api/src/swap/swap.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('SwapController', () => {
it('should call swapService.postOnrampTransaction', () => {
const req = {
quote: undefined,
ref: 'ref',
reference: 'ref',
amountFiat: '100',
source: {
currency: Currency.KES,
Expand All @@ -80,7 +80,7 @@ describe('SwapController', () => {
},
},
target: {
invoice: {
payout: {
invoice: 'lnbc1000u1p0j7j0pp5',
},
},
Expand Down Expand Up @@ -115,11 +115,11 @@ describe('SwapController', () => {
it('should call swapService.postOfframpTransaction', () => {
const req = {
quote: undefined,
ref: 'ref',
reference: 'ref',
amountFiat: '100',
target: {
currency: SupportedCurrencies.KES,
destination: {
payout: {
phone: '07000000000',
},
},
Expand Down
8 changes: 4 additions & 4 deletions apps/api/src/swap/swap.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ export class SwapController {
@ApiOperation({ summary: 'List onramp swaps' })
@ApiQuery({
name: 'page',
example: '?page=0',
example: '0',
type: ListSwapsDto['page'],
required: false,
})
@ApiQuery({
name: 'size',
example: '?size=100',
example: '100',
type: ListSwapsDto['size'],
required: false,
})
Expand Down Expand Up @@ -139,13 +139,13 @@ export class SwapController {
@ApiOperation({ summary: 'List offramp swaps' })
@ApiQuery({
name: 'page',
example: '?page=0',
example: '0',
type: ListSwapsDto['page'],
required: false,
})
@ApiQuery({
name: 'size',
example: '?size=100',
example: '100',
type: ListSwapsDto['size'],
required: false,
})
Expand Down
8 changes: 4 additions & 4 deletions apps/api/src/swap/swap.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe('SwapService', () => {
it('can initiate an onramp swap without a quote', () => {
const swap = swapService.postOnrampTransaction({
quote: undefined,
ref: '1234',
reference: 'ref',
amountFiat: '100',
source: {
currency: Currency.KES,
Expand All @@ -167,7 +167,7 @@ describe('SwapService', () => {

it('can initiate an onramp swap with a quote', () => {
const swap = swapService.postOnrampTransaction({
ref: '1234',
reference: 'ref',
amountFiat: '100',
source: {
currency: Currency.KES,
Expand Down Expand Up @@ -229,7 +229,7 @@ describe('SwapService', () => {
it('can initiate an offramp swap without a quote', () => {
const swap = swapService.postOfframpTransaction({
quote: undefined,
ref: '1234',
reference: 'ref',
amountFiat: '100',
target: {
currency: Currency.KES,
Expand All @@ -244,7 +244,7 @@ describe('SwapService', () => {

it('can initiate an offramp swap with a quote', () => {
const swap = swapService.postOfframpTransaction({
ref: '1234',
reference: 'ref',
amountFiat: '100',
target: {
currency: Currency.KES,
Expand Down
5 changes: 2 additions & 3 deletions apps/swap/src/fedimint/fedimint.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ export class FedimintService {
}

async pay(invoice: string): Promise<{ operationId: string; fee: number }> {
this.logger.log('Paying invoice');
this.logger.log('Invoice', invoice);
this.logger.log(`Paying Invoice : ${invoice}`);

const { operationId, fee }: LightningPayResponse = await this.post<
{ paymentInfo: string } & WithFederationId & WithGatewayId,
Expand All @@ -113,7 +112,7 @@ export class FedimintService {
gatewayId: this.gatewayId,
});

this.logger.log('Paid Invoice : ', operationId);
this.logger.log(`Paid Invoice : ${invoice} : ${operationId}`);
return {
operationId,
fee,
Expand Down
33 changes: 21 additions & 12 deletions apps/swap/src/swap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class SwapService {
amountFiat,
source,
target,
ref,
reference,
}: CreateOnrampSwapDto): Promise<SwapResponse> {
const rate = await this.getRate(quote, {
from: Currency.KES,
Expand All @@ -152,7 +152,7 @@ export class SwapService {
});

const swap = await this.onramp.create({
reference: ref,
reference,
state: SwapTransactionState.PENDING,
lightning: target.payout.invoice,
amountSats: amountSats.toFixed(2),
Expand All @@ -163,7 +163,7 @@ export class SwapService {
const mpesa = await this.intasendService.sendMpesaStkPush({
amount: Number(amountFiat),
phone_number: source.origin.phone,
api_ref: ref,
api_ref: reference,
});

const updatedSwap = await this.onramp.findOneAndUpdate(
Expand Down Expand Up @@ -230,10 +230,10 @@ export class SwapService {
}

async createOfframpSwap({
ref,
quote,
amountFiat,
target,
reference,
}: CreateOfframpSwapDto): Promise<SwapResponse> {
const rate = await this.getRate(quote, {
from: Currency.BTC,
Expand All @@ -246,13 +246,13 @@ export class SwapService {
});

const { invoice: lightning, operationId: id } =
await this.fedimintService.invoice(amountMsats, ref || 'offramp');
await this.fedimintService.invoice(amountMsats, reference);

const swap = await this.offramp.create({
rate,
reference,
lightning,
retryCount: 0,
reference: ref,
phone: target.payout.phone,
amountSats: amountSats.toFixed(2),
state: SwapTransactionState.PENDING,
Expand Down Expand Up @@ -399,7 +399,7 @@ export class SwapService {
const mpesa =
await this.intasendService.getMpesaTrackerFromPaymentUpdate(update);

const swap = await this.onramp.findOne({
const swap = await this.offramp.findOne({
paymentTracker: update.file_id,
});

Expand All @@ -423,9 +423,6 @@ export class SwapService {
context,
operationId,
}: ReceivePaymentSuccessEvent) {
this.logger.log('Successfully received payment');
this.logger.log(`Context : ${context}, OperationId: ${operationId}`);

const swap = await this.offramp.findOne({ _id: operationId });

const amount = Number(swap.amountSats) * Number(swap.rate);
Expand All @@ -444,15 +441,27 @@ export class SwapService {
state: SwapTransactionState.PROCESSING,
},
);

this.logger.log(
`Received lightning payment for ${context} : ${operationId}`,
);
}

@OnEvent(fedimint_receive_failure)
private async handleFailedReceive({
context,
operationId,
}: ReceivePaymentFailureEvent) {
this.logger.log('Failed to receive payment');
this.logger.log(`Context : ${context}, OperationId: ${operationId}`);
this.logger.log(
`Failed to eceive lightning payment for ${context} : ${operationId}`,
);

await this.offramp.findOneAndUpdate(
{ _id: operationId },
{
state: SwapTransactionState.FAILED,
},
);
}
}

Expand Down
11 changes: 9 additions & 2 deletions libs/common/src/dto/create-offramp-swap.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Validate,
IsEnum,
IsDefined,
ValidateNested,
} from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import {
Expand All @@ -21,25 +22,29 @@ import { TransformToCurrency } from './transforms';

class OfframpSwapTargetDto implements OfframpSwapTarget {
@IsEnum(Currency)
@ApiProperty({ enum: SupportedCurrencies, enumName: 'SupportedCurrencyType' })
@TransformToCurrency()
@ApiProperty({ enum: SupportedCurrencies, enumName: 'SupportedCurrencyType' })
currency: Currency;

@IsDefined()
@ValidateNested()
@Type(() => MobileMoneyDto)
@ApiProperty({ type: MobileMoneyDto })
payout: MobileMoneyDto;
}

export class CreateOfframpSwapDto implements OfframpSwapRequest {
@IsOptional()
@ValidateNested()
@Type(() => QuoteDto)
@ApiProperty({ type: QuoteDto })
quote: QuoteDto;

@IsNotEmpty()
@IsString()
@Type(() => String)
@ApiProperty()
ref: string;
reference: string;

@IsNotEmpty()
@IsString()
Expand All @@ -49,6 +54,8 @@ export class CreateOfframpSwapDto implements OfframpSwapRequest {
amountFiat: string;

@IsNotEmpty()
@ValidateNested()
@Type(() => OfframpSwapTargetDto)
@ApiProperty({ type: OfframpSwapTargetDto })
target: OfframpSwapTargetDto;
}
2 changes: 1 addition & 1 deletion libs/common/src/dto/create-onramp-swap.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class CreateOnrampSwapDto implements OnrampSwapRequest {
@IsString()
@Type(() => String)
@ApiProperty()
ref: string;
reference: string;

@IsNotEmpty()
@IsString()
Expand Down
6 changes: 6 additions & 0 deletions libs/common/src/dto/quote.dto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsBoolean, IsString } from 'class-validator';

export class QuoteDto {
@IsString()
@Type(() => String)
@ApiProperty()
id: string;

@IsBoolean()
@Type(() => Boolean)
@ApiProperty()
refreshIfExpired: boolean;
}
4 changes: 3 additions & 1 deletion libs/common/src/types/proto/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export interface OnrampSwapRequest {
*/
quote?: Quote | undefined;
/** Swap initiator reference to the account this transaction is associated with. */
ref: string;
reference: string;
/**
* Amount to swap
* Any transaction fees will be deducted from this amount
Expand Down Expand Up @@ -118,6 +118,8 @@ export interface OfframpSwapRequest {
* If not specified, the service will create a new quote for the swap
*/
quote?: Quote | undefined;
/** Swap initiator reference to the account this transaction is associated with. */
reference: string;
/**
* Amount to swap
* Any transaction fees will be deducted from this amount
Expand Down
9 changes: 6 additions & 3 deletions proto/swap.proto
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ message OnrampSwapRequest {
optional Quote quote = 1;

// Swap initiator reference to the account this transaction is associated with.
string ref = 2;
string reference = 2;

// Amount to swap
// Any transaction fees will be deducted from this amount
Expand Down Expand Up @@ -117,12 +117,15 @@ message OfframpSwapRequest {
// If not specified, the service will create a new quote for the swap
optional Quote quote = 1;

// Swap initiator reference to the account this transaction is associated with.
string reference = 2;

// Amount to swap
// Any transaction fees will be deducted from this amount
string amountFiat = 2;
string amountFiat = 3;

// Target of the swap
OfframpSwapTarget target = 3;
OfframpSwapTarget target = 4;
}

message OfframpSwapTarget {
Expand Down
Loading