Skip to content

Commit

Permalink
feature/952 Implement ML Number Library (#149)
Browse files Browse the repository at this point in the history
* Implement MlNumber. Adjust tests. Fix integration-runner == issue. Upgrade deps. Bump up version
* Refactor to uppercase L in MLNumber
* Upgrade to latest ml-number
  • Loading branch information
ggrg authored Oct 3, 2019
1 parent 847b09a commit 9f83aab
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 32 deletions.
111 changes: 97 additions & 14 deletions package-lock.json

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

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "central-settlement",
"description": "Central settlements hosted by a scheme to record and make settlements",
"version": "8.1.0",
"version": "8.2.0",
"license": "Apache-2.0",
"private": false,
"author": "ModusBox",
Expand Down Expand Up @@ -30,12 +30,13 @@
"@mojaloop/central-ledger": "8.1.1",
"@mojaloop/central-services-auth": "5.2.1",
"@mojaloop/central-services-database": "7.4.0",
"@mojaloop/central-services-error-handling": "7.5.0",
"@mojaloop/central-services-error-handling": "8.2.0",
"@mojaloop/central-services-health": "8.1.1",
"@mojaloop/central-services-logger": "8.1.2",
"@mojaloop/central-services-shared": "8.1.5",
"@mojaloop/central-services-stream": "8.1.1",
"@mojaloop/forensic-logging-client": "5.2.0",
"@mojaloop/ml-number": "8.2.0",
"@now-ims/hapi-now-auth": "2.0.0",
"blipp": "4.0.1",
"hapi-auth-bearer-token": "6.2.1",
Expand All @@ -46,9 +47,9 @@
"uuid4": "1.1.4"
},
"devDependencies": {
"@hapi/joi": "16.1.4",
"bluebird": "3.5.5",
"eslint": "6.5.0",
"@hapi/joi": "16.1.5",
"bluebird": "3.7.0",
"eslint": "6.5.1",
"faucet": "0.0.1",
"node-fetch": "2.6.0",
"nodemon": "1.19.3",
Expand Down
23 changes: 12 additions & 11 deletions src/models/settlement/facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
'use strict'

const ErrorHandler = require('@mojaloop/central-services-error-handling')
const MLNumber = require('@mojaloop/ml-number')
const Db = require('../../lib/db')
const Uuid = require('uuid4')
const Crypto = require('crypto')
Expand Down Expand Up @@ -282,13 +283,13 @@ const settlementTransfersReserve = async function (settlementId, transactionTime
// TODO: insert new limit with correct value for startAfterParticipantPositionChangeId
await ParticipantFacade.adjustLimits(dfspAccountId, {
type: 'NET_DEBIT_CAP',
value: netDebitCap + dfspAmount
value: new MLNumber(netDebitCap).add(dfspAmount).toNumber()
}, trx)
}

// Persist dfsp latestPosition
await knex('participantPosition')
.update('value', dfspPositionValue + dfspAmount)
.update('value', new MLNumber(dfspPositionValue).add(dfspAmount).toNumber())
.where('participantPositionId', dfspPositionId)
.transacting(trx)

Expand All @@ -297,7 +298,7 @@ const settlementTransfersReserve = async function (settlementId, transactionTime
.insert({
participantPositionId: dfspPositionId,
transferStateChangeId: transferStateChangeId,
value: dfspPositionValue + dfspAmount,
value: new MLNumber(dfspPositionValue).add(dfspAmount).toNumber(),
reservedValue: dfspReservedValue,
createdDate: transactionTimestamp
})
Expand All @@ -308,7 +309,7 @@ const settlementTransfersReserve = async function (settlementId, transactionTime
const destination = dfspName
const payload = {
currency: currencyId,
value: dfspPositionValue + dfspAmount,
value: new MLNumber(dfspPositionValue).add(dfspAmount).toNumber(),
changedDate: new Date().toISOString()
}
const message = Facade.getNotificationMessage(action, destination, payload)
Expand All @@ -324,7 +325,7 @@ const settlementTransfersReserve = async function (settlementId, transactionTime

// Persist hub latestPosition
await knex('participantPosition')
.update('value', hubPositionValue + hubAmount)
.update('value', new MLNumber(hubPositionValue).add(hubAmount).toNumber())
.where('participantPositionId', hubPositionId)
.transacting(trx)

Expand All @@ -333,7 +334,7 @@ const settlementTransfersReserve = async function (settlementId, transactionTime
.insert({
participantPositionId: hubPositionId,
transferStateChangeId: transferStateChangeId,
value: hubPositionValue + hubAmount,
value: new MLNumber(hubPositionValue).add(hubAmount).toNumber(),
reservedValue: 0,
createdDate: transactionTimestamp
})
Expand Down Expand Up @@ -616,7 +617,7 @@ const settlementTransfersCommit = async function (settlementId, transactionTimes

// Persist dfsp latestPosition
await knex('participantPosition')
.update('value', dfspPositionValue + dfspAmount)
.update('value', new MLNumber(dfspPositionValue).add(dfspAmount).toNumber())
.where('participantPositionId', dfspPositionId)
.transacting(trx)

Expand All @@ -625,7 +626,7 @@ const settlementTransfersCommit = async function (settlementId, transactionTimes
.insert({
participantPositionId: dfspPositionId,
transferStateChangeId: transferStateChangeId,
value: dfspPositionValue + dfspAmount,
value: new MLNumber(dfspPositionValue).add(dfspAmount).toNumber(),
reservedValue: dfspReservedValue,
createdDate: transactionTimestamp
})
Expand All @@ -641,7 +642,7 @@ const settlementTransfersCommit = async function (settlementId, transactionTimes

// Persist hub latestPosition
await knex('participantPosition')
.update('value', hubPositionValue + hubAmount)
.update('value', new MLNumber(hubPositionValue).add(hubAmount).toNumber())
.where('participantPositionId', hubPositionId)
.transacting(trx)

Expand All @@ -650,7 +651,7 @@ const settlementTransfersCommit = async function (settlementId, transactionTimes
.insert({
participantPositionId: hubPositionId,
transferStateChangeId: transferStateChangeId,
value: hubPositionValue + hubAmount,
value: new MLNumber(hubPositionValue).add(hubAmount).toNumber(),
reservedValue: 0,
createdDate: transactionTimestamp
})
Expand All @@ -661,7 +662,7 @@ const settlementTransfersCommit = async function (settlementId, transactionTimes
const destination = dfspName
const payload = {
currency: currencyId,
value: dfspPositionValue + dfspAmount,
value: new MLNumber(dfspPositionValue).add(dfspAmount).toNumber(),
changedDate: new Date().toISOString()
}
const message = Facade.getNotificationMessage(action, destination, payload)
Expand Down
2 changes: 1 addition & 1 deletion test/integration-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ docker logs $APP_HOST
>&1 echo "Copy results to local directory"
docker cp $APP_HOST:$DOCKER_WORKING_DIR/$APP_DIR_TEST_RESULTS $TEST_DIR

if [ "$test_exit_code" == 0 ]
if [ "$test_exit_code" = "0" ]
then
>&1 echo "Showing results..."
cat $APP_DIR_TEST_RESULTS/$TEST_RESULTS_FILE
Expand Down
3 changes: 2 additions & 1 deletion test/integration/settlementTransfer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
const Test = require('tapes')(require('tape'))
const Sinon = require('sinon')
const Logger = require('@mojaloop/central-services-logger')
const MLNumber = require('@mojaloop/ml-number')
const PrepareTransferData = require('./helpers/transferData')
const Models = require('./helpers/models')
const Config = require('../../src/lib/config')
Expand Down Expand Up @@ -307,7 +308,7 @@ Test('SettlementTransfer should', async settlementTransferTest => {
test.equal(currentPayerPosition, initialPayerPosition, 'position for NET_SETTLEMENT_SENDER is not changed')

const currentPayeePosition = (await ParticipantPositionModel.getPositionByCurrencyId(netRecipientAccountId)).value
test.equal(currentPayeePosition, initialPayeePosition + netSettlementAmount, 'position for NET_SETTLEMENT_RECIPIENT is adjusted')
test.equal(currentPayeePosition, new MLNumber(initialPayeePosition).add(netSettlementAmount).toNumber(), 'position for NET_SETTLEMENT_RECIPIENT is adjusted')

test.end()
} catch (err) {
Expand Down

0 comments on commit 9f83aab

Please sign in to comment.