Skip to content

Commit

Permalink
Fixing controller conection on the register flyover scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldsg20 committed Nov 18, 2024
1 parent 21f484d commit c39f548
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 31 deletions.
38 changes: 23 additions & 15 deletions src/__tests__/unit/register.controller.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@ describe('RegisterController', () => {
let registerFlyover: sinon.SinonStub;
let get: sinon.SinonStub;
let payload = new RegisterPayload({
sessionId: '43ef33c59294d5033d96cb25b8f94723',
txHash: '0x',
type: constants.TX_TYPE_PEGIN,
value: 0.005,
wallet: 'liquality',
fee: 0.000001,
});
let pegoutPayload = new RegisterPayload({
sessionId: '',
txHash: '0xb',
type: constants.TX_TYPE_PEGOUT,
value: 0.004,
wallet: 'liquality',
fee: 0.000002,
});
let flyoverPayload = new RegisterPayload({
sessionId: '',
txHash: '0xc',
type: constants.TX_TYPE_PEGOUT,
value: 0.005,
Expand All @@ -42,6 +39,27 @@ describe('RegisterController', () => {
details: {
blocksToCompleteTransaction: 2,
},
quote: {
callFeeOnWei: 800n,
depositAddr: "testDeposit",
depositConfirmations: 200,
depositDateLimit: 49800,
expireBlocks: 100,
expireDate: 49850,
productFeeAmountOnWei: 6300n,
transferConfirmations: 80,
transferTime: 50600,
valueOnWei: 18450n,
agreementTimestamp: 1620000000,
gasFeeOnWei: 1000000000000000000n,
nonce: 1n,
penaltyFeeOnWei: 1000000000000000000n,
btcRefundAddress: "testBtcRefund",
lbcAddress: "testLbc",
lpBtcAddress: "testLpBtc",
rskRefundAddress: "testRskRefund",
liquidityProviderRskAddress: "testLiquidityProviderRsk",
},
});
beforeEach(reset);
function reset() {
Expand All @@ -60,26 +78,16 @@ describe('RegisterController', () => {
);
}

it('should register a pegin with a valid sessionId', async () => {
it('should register a pegin', async () => {
register.resolves(true);
get.resolves('43ef33c59294d5033d96cb25b8f94723');
await registerController.register(payload);
const result = await context.result;
sinon.assert.called(sessionRepository.stubs.get);
sinon.assert.called(register);
expect(result.statusCode).to.equal(200);
});

it('should not register a pegin with an invalid sessionId', async () => {
get.resolves(null);
await registerController.register(payload);
const result = await context.result;
sinon.assert.called(sessionRepository.stubs.get);
sinon.assert.notCalled(register);
expect(result.statusCode).to.equal(200);
});

it('should register a pegout with any sessionId', async () => {
it('should register a pegout', async () => {
await registerController.register(pegoutPayload);
const result = await context.result;
sinon.assert.called(register);
Expand Down
12 changes: 3 additions & 9 deletions src/controllers/register.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {ServicesBindings} from '../dependency-injection-bindings';
import {RegisterPayload} from '../models';
import {RegisterService, FlyoverService} from '../services';
import {SessionRepository} from '../repositories';
import * as constants from '../constants';

export class RegisterController {
logger: Logger;
Expand Down Expand Up @@ -38,16 +37,11 @@ export class RegisterController {
})
payload: RegisterPayload,
): Promise<Response> {
const {sessionId, type, provider} = payload;
let session;
if (sessionId) {
session = await this.sessionRepository.get(sessionId);
}
if (session != null || type === constants.TX_TYPE_PEGOUT) {
await this.registerService.register(payload);
}
const {provider} = payload;
if (provider) {
await this.flyoverService.register(payload);
} else {
await this.registerService.register(payload);
}
return this.response.status(200).send();
}
Expand Down
12 changes: 6 additions & 6 deletions src/models/register-payload.model.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import {Model, model, property} from '@loopback/repository';
import { QuoteDbModel } from './quote-db.model';

@model()
export class RegisterPayload extends Model {
@property({
type: 'string',
required: true,
})
sessionId: string;

@property({
type: 'string',
required: true,
Expand Down Expand Up @@ -66,6 +61,11 @@ export class RegisterPayload extends Model {
type: 'string',
})
quoteHash?: string;

@property({
type: 'object',
})
quote?: QuoteDbModel;

constructor(data?: Partial<RegisterPayload>) { //NOSONAR
super(data);
Expand Down
3 changes: 2 additions & 1 deletion src/services/flyover.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mongoose, { Schema } from 'mongoose';
import {RegisterPayload} from '../models';
import {QuoteDbModel, RegisterPayload} from '../models';
import {FlyoverStatusModel} from '../models/flyover-status.model';
import {MongoDbDataService} from './mongodb-data.service';
import { RskNodeService } from './rsk-node.service';
Expand Down Expand Up @@ -87,6 +87,7 @@ export class FlyoverService extends MongoDbDataService<FlyoverStatusModel, Flyov
flyoverStatus.recipientAddress = payload?.details?.recipientAddress ?? '';
flyoverStatus.blockToBeFinished = currentBlock + Number(payload?.details?.blocksToCompleteTransaction ?? 0);
flyoverStatus.quoteHash = payload?.quoteHash ?? '';
flyoverStatus.quote = payload?.quote ?? {} as QuoteDbModel;
return this.set(flyoverStatus);
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "@loopback/build/config/tsconfig.common.json",
"compilerOptions": {
"target": "es2020",
"esModuleInterop": true,
"experimentalDecorators": true,
"outDir": "dist",
Expand Down

0 comments on commit c39f548

Please sign in to comment.