Skip to content

Commit

Permalink
feat: increased scale for test currencies (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
geka-evk authored Nov 5, 2024
1 parent 2ca21fa commit 2bb792f
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 34 deletions.
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
}
],
"dependencies": {
"@mojaloop/ml-schema-transformer-lib": "^2.3.3",
"@mojaloop/ml-schema-transformer-lib": "^2.3.4",
"base64url": "3.0.1",
"fast-safe-stringify": "^2.1.1",
"ilp-packet": "3.1.3",
Expand All @@ -61,17 +61,17 @@
}
},
"devDependencies": {
"@mojaloop/api-snippets": "^17.7.3",
"@mojaloop/api-snippets": "^17.7.4",
"@types/jest": "^29.5.14",
"@types/node": "^22.8.4",
"@types/node": "^22.9.0",
"audit-ci": "^7.1.0",
"eslint": "8.57.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-plugin-import": "2.31.0",
"jest": "^29.7.0",
"jest-junit": "^16.0.0",
"nock": "^13.5.5",
"npm-check-updates": "^17.1.9",
"npm-check-updates": "^17.1.10",
"pre-commit": "^1.2.2",
"replace": "^1.2.2",
"standard-version": "^9.5.0",
Expand Down
3 changes: 2 additions & 1 deletion src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const RESOURCES = Object.freeze({
const ERROR_MESSAGES = Object.freeze({
invalidIlpExpirationDate: 'Invalid ILP expiration date',
unsupportedIlpVersion: 'Unsupported ILP version',
invalidIlpOptions: 'Invalid ILP options'
invalidIlpOptions: 'Invalid ILP options',
invalidAdjustedAmount: 'Ilp packet `amount` after scaling should be integer',
});

const ILP_VERSIONS = Object.freeze({
Expand Down
17 changes: 14 additions & 3 deletions src/lib/ilp/IlpV4.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ class IlpV4 extends IlpBase {
if (isNaN(expiresAt.getTime())){
throw new TypeError(ERROR_MESSAGES.invalidIlpExpirationDate);
}
const amount = isFx
? ILP_AMOUNT_FOR_FX
: super._getIlpCurrencyAmount(transactionObject.amount);
const amount = this.#adjustAmount(transactionObject, isFx);
const destination = this._getIlpAddress();

this.logger.isDebugEnabled && this.logger.push({ transactionObject, amount, expiresAt, destination }).debug('ILP packet input details');
Expand Down Expand Up @@ -149,6 +147,19 @@ class IlpV4 extends IlpBase {

return super._createHmac(base64EncodedTransaction, encodedSecret);
}

#adjustAmount(transactionObject, isFx) {
const amount = isFx
? ILP_AMOUNT_FOR_FX
: super._getIlpCurrencyAmount(transactionObject.amount);

if (amount.includes('.')) {
const errMessage = ERROR_MESSAGES.invalidAdjustedAmount;
this.logger.push(transactionObject.amount).warn(errMessage);
throw new TypeError(errMessage);
}
return amount;
}
}

module.exports = IlpV4;
4 changes: 2 additions & 2 deletions src/lib/ilp/currency.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,6 @@
"YER": 2,
"ZMW": 2,
"ZWL": 2,
"XXX": 0,
"XTS": 0
"XXX": 4,
"XTS": 4
}
54 changes: 46 additions & 8 deletions test/unit/lib/ilp/IlpV4.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const IlpPacket = require('ilp-packet');
const { ilpFactory, ILP_VERSIONS } = require('#src/lib/ilp/index');
const { ILP_ADDRESS, ILP_AMOUNT_FOR_FX, ERROR_MESSAGES } = require('#src/lib/constants');
const dto = require('#src/lib/dto');
const currencyJson = require('../../../../src/lib/ilp/currency');

const mockLogger = require('#test/__mocks__/mockLogger');
const fixtures = require('#test/fixtures');
Expand Down Expand Up @@ -106,13 +107,6 @@ describe('IlpV4 Tests -->', () => {
expect(dataElement).toBeDefined();
});

test('should calculate ilp packet from transaction object and condition', () => {
const transactionObj = dto.transactionObjectDto(quoteRequest, partialResponse);
const condition = ilp._sha256('preimage');
const ilpPacket = ilp.calculateIlpPacket(transactionObj, condition);
expect(ilpPacket).toBeTruthy();
});

test('should throw error if expiration in transactionObject is undefined', () => {
const transactionObj = {
expiration: undefined,
Expand Down Expand Up @@ -142,7 +136,51 @@ describe('IlpV4 Tests -->', () => {
expect(fulfilment).toBe(ilpCombo.fulfilment);
});

describe('Ilp Packet Serialize tests -->', () => {
describe('calculateIlpPacket Method Tests', () => {
const currency = 'XXX';
const scale = currencyJson[currency];

test('should calculate ilp packet from transaction object and condition', () => {
const transactionObj = dto.transactionObjectDto(quoteRequest, partialResponse);
const condition = ilp._sha256('preimage');
const ilpPacket = ilp.calculateIlpPacket(transactionObj, condition);
expect(ilpPacket).toBeTruthy();
});

test('should have scale for test currency', () => {
expect(scale).toBeDefined();
});

test('should calculate ilp packet with amount with up to 4 decimals for test currency', () => {
const amount = fixtures.moneyPayload({
amount: `1000.${'1'.repeat(scale)}`,
currency
});
const transactionObj = {
...dto.transactionObjectDto(quoteRequest, partialResponse),
amount
};
const condition = ilp._sha256('preimage');
const ilpPacket = ilp.calculateIlpPacket(transactionObj, condition);
expect(ilpPacket).toBeTruthy();
});

test('should throw error on amount with more than defined scale decimals for test currency', () => {
const amount = fixtures.moneyPayload({
amount: `2000.${'2'.repeat(scale + 1)}`,
currency
});
const transactionObj = {
...dto.transactionObjectDto(quoteRequest, partialResponse),
amount
};
const condition = ilp._sha256('preimage');
expect(() => ilp.calculateIlpPacket(transactionObj, condition))
.toThrow(TypeError(ERROR_MESSAGES.invalidAdjustedAmount));
});
});

describe('Ilp Packet Serialize Tests -->', () => {
const createIlpJson = (amount) => ({
amount,
data: Buffer.from('data'),
Expand Down

0 comments on commit 2bb792f

Please sign in to comment.