Skip to content

Commit

Permalink
Process pegout rejection reasons
Browse files Browse the repository at this point in the history
In order to show a more descriptive error in the app.
  • Loading branch information
lserra-iov authored and annipi committed Nov 18, 2024
1 parent 8ae9380 commit 75886fe
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/__tests__/unit/services/pegout-data.processor.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,6 @@ describe('Service: PegoutDataProcessor', () => {
valueRequestedInSatoshis: 0,
valueInSatoshisToBeReceived: 0,
feeInSatoshisToBePaid: 0,
reason: '',
btcRawTxInputsHash: '',
getId: function (): string {
return '';
Expand Down
7 changes: 6 additions & 1 deletion src/models/pegout-status.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Model, model, property} from '@loopback/repository';
import { PegoutStatuses } from './rsk/pegout-status-data-model';
import { PegoutStatuses, RejectedPegoutReason } from './rsk/pegout-status-data-model';

@model()
export class PegoutStatus extends Model {
Expand Down Expand Up @@ -51,6 +51,11 @@ export class PegoutStatus extends Model {
})
btcTxId?: string;

@property({
type: 'string',
})
reason?: RejectedPegoutReason;


constructor(data: Partial<PegoutStatus> = {}) {
super();
Expand Down
14 changes: 13 additions & 1 deletion src/models/rsk/pegout-status-data-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export enum PegoutStatuses {
RELEASE_BTC = 'RELEASE_BTC'
}

export const RejectedPegoutReasons = {
'1': 'LOW_AMOUNT',
'2': 'CALLER_CONTRACT',
'3': 'FEE_ABOVE_VALUE',
} as const;

export type RejectedPegoutReason = (typeof RejectedPegoutReasons)[keyof typeof RejectedPegoutReasons];

export interface PegoutStatusDataModel {
originatingRskTxHash: string;
rskTxHash: string;
Expand All @@ -23,6 +31,7 @@ export interface PegoutStatusDataModel {
feeInSatoshisToBePaid: number;
status: PegoutStatuses;
btcRawTransaction: string;
reason?: RejectedPegoutReason;
}

export class PegoutStatusAppDataModel implements PegoutStatusDataModel{
Expand Down Expand Up @@ -61,13 +70,15 @@ export class PegoutStatusAppDataModel implements PegoutStatusDataModel{
rskSenderAddress,
valueRequestedInSatoshis,
status,
reason,
} = model;
return new PegoutStatusAppDataModel({
originatingRskTxHash,
rskTxHash,
rskSenderAddress,
valueRequestedInSatoshis,
status,
reason,
});
}
rskTxHash: string;
Expand All @@ -80,6 +91,7 @@ export class PegoutStatusAppDataModel implements PegoutStatusDataModel{
btcRawTransaction: string;
originatingRskTxHash: string;
createdOn: Date;
reason?: RejectedPegoutReason;
}

export class PegoutStatusDbDataModel implements SearchableModel, PegoutStatusDataModel {
Expand All @@ -99,7 +111,7 @@ export class PegoutStatusDbDataModel implements SearchableModel, PegoutStatusDat
valueRequestedInSatoshis: number;
valueInSatoshisToBeReceived: number;
feeInSatoshisToBePaid: number;
reason: string;
reason?: RejectedPegoutReason;
btcRawTxInputsHash: string;
batchPegoutIndex: number;
batchPegoutRskTxHash: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const PegoutStatusSchema = new mongoose.Schema({
batchPegoutRskTxHash: {type: String},
changedByEvent: {type: String},
updatedAt: {type: Date},
reason: {type: String},
});

const PegoutStatusConnector = mongoose.model<PegoutStatusMongoModel>("PegoutStatus", PegoutStatusSchema);
Expand Down
11 changes: 6 additions & 5 deletions src/services/pegout-status/pegout-status-builder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import ExtendedBridgeTx from '../extended-bridge-tx';
import { PegoutStatuses, PegoutStatusDbDataModel } from '../../models/rsk/pegout-status-data-model';
import { PegoutStatuses, PegoutStatusDbDataModel, RejectedPegoutReasons } from '../../models/rsk/pegout-status-data-model';
import { BRIDGE_EVENTS } from '../../utils/bridge-utils';
import { BtcAddressUtils } from '../../utils/btc-utils';
import {ExtendedBridgeEvent} from "../../models/types/bridge-transaction-parser";
import { isAvailable } from '../../utils/ts-utils';

export class PegoutStatusBuilder {

Expand Down Expand Up @@ -36,9 +37,7 @@ export class PegoutStatusBuilder {
const events: ExtendedBridgeEvent[] = extendedBridgeTx.events as ExtendedBridgeEvent[];
const releaseRequestRejectedEvent: ExtendedBridgeEvent = events.find(event => event.name === BRIDGE_EVENTS.RELEASE_REQUEST_REJECTED)!;

const rskSenderAddress = <string> releaseRequestRejectedEvent!.arguments.sender;
const amount = <number> releaseRequestRejectedEvent.arguments!.amount;
const reason = <string> releaseRequestRejectedEvent.arguments!.reason;
const {sender: rskSenderAddress, amount, reason} = releaseRequestRejectedEvent.arguments;

const status: PegoutStatusDbDataModel = new PegoutStatusDbDataModel();

Expand All @@ -48,7 +47,9 @@ export class PegoutStatusBuilder {
status.rskBlockHeight = extendedBridgeTx.blockNumber;
status.rskSenderAddress = rskSenderAddress;
status.valueRequestedInSatoshis = amount;
status.reason = reason;
if (isAvailable(RejectedPegoutReasons, reason)) {
status.reason = RejectedPegoutReasons[reason];
}
status.originatingRskBlockHeight = extendedBridgeTx.blockNumber;
status.status = PegoutStatuses.REJECTED;
status.isNewestStatus = true;
Expand Down
2 changes: 2 additions & 0 deletions src/services/pegout-status/pegout-status.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export class PegoutStatusService {
feeInSatoshisToBePaid,
status,
btcRawTransaction,
reason,
} = model;
return new PegoutStatus({
originatingRskTxHash,
Expand All @@ -136,6 +137,7 @@ export class PegoutStatusService {
feeInSatoshisToBePaid,
status,
btcTxId: btcRawTransaction ? this.btcUtils.getBtcTxIdFromRawTransaction(btcRawTransaction) : undefined,
reason,
});
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils/ts-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function isAvailable<Obj extends object>(
obj: Obj,
key: PropertyKey,
): key is keyof Obj {
return key in obj;
}

0 comments on commit 75886fe

Please sign in to comment.