From b298be543620c977a79cc951b607e6abd68735d7 Mon Sep 17 00:00:00 2001 From: Dulana Peiris Date: Fri, 26 Dec 2025 10:42:02 +0530 Subject: [PATCH 1/2] Refactor refund logic to use UtilHelper for formatting XRPL amounts --- mb-xrpl/lib/message-board.js | 8 ++++---- mb-xrpl/lib/setup.js | 3 ++- mb-xrpl/lib/util-helper.js | 9 +++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/mb-xrpl/lib/message-board.js b/mb-xrpl/lib/message-board.js index 8eab5d39..7accdc01 100644 --- a/mb-xrpl/lib/message-board.js +++ b/mb-xrpl/lib/message-board.js @@ -904,7 +904,7 @@ class MessageBoard { } } console.log(`Refunding tenant ${uriToken.Owner}...`); - await this.hostClient.refundTenant(lease.tx_hash, uriToken.Owner, uriInfo.leaseAmount.toString(), { submissionRef: submissionRefs?.refs[0] }); + await this.hostClient.refundTenant(lease.tx_hash, uriToken.Owner, UtilHelper.formatXrplAmount(uriInfo.leaseAmount), { submissionRef: submissionRefs?.refs[0] }); }); } else { @@ -970,7 +970,7 @@ class MessageBoard { // If lease is in ACQUIRING status acquire response is not received by the tenant and lease is not in expiry list. if (lease.status === LeaseStatus.ACQUIRING) { console.log(`Refunding tenant ${uriToken.Owner}...`); - await this.hostClient.refundTenant(lease.tx_hash, uriToken.Owner, uriInfo.leaseAmount.toString(), { submissionRef: submissionRefs?.refs[0] }); + await this.hostClient.refundTenant(lease.tx_hash, uriToken.Owner, UtilHelper.formatXrplAmount(uriInfo.leaseAmount), { submissionRef: submissionRefs?.refs[0] }); } }); } @@ -1101,7 +1101,7 @@ class MessageBoard { } } console.log(`Refunding tenant ${eventInfo.data.tenant} for acquire...`); - await this.hostClient.refundTenant(trx.hash, eventInfo.data.tenant, uriInfo.leaseAmount.toString(), { submissionRef: submissionRefs?.refs[0] }); + await this.hostClient.refundTenant(trx.hash, eventInfo.data.tenant, UtilHelper.formatXrplAmount(uriInfo.leaseAmount), { submissionRef: submissionRefs?.refs[0] }); }); } } @@ -1128,7 +1128,7 @@ class MessageBoard { } // The refund for the extension, if tenant still own the URIToken. console.log(`Refunding tenant ${eventInfo.data.tenant} for extend...`); - await this.hostClient.refundTenant(trx.hash, eventInfo.data.tenant, eventInfo.data.payment.toString(), { submissionRef: submissionRefs?.refs[0] }); + await this.hostClient.refundTenant(trx.hash, eventInfo.data.tenant, UtilHelper.formatXrplAmount(eventInfo.data.payment), { submissionRef: submissionRefs?.refs[0] }); }); } else { console.log(`No such URIToken (${eventInfo.data.uriTokenId}) was found.`); diff --git a/mb-xrpl/lib/setup.js b/mb-xrpl/lib/setup.js index 784dd69a..49c9a7dd 100644 --- a/mb-xrpl/lib/setup.js +++ b/mb-xrpl/lib/setup.js @@ -908,7 +908,8 @@ class Setup { const remainingMoments = lease.life_moments - spentMoments; if (remainingMoments > 0) { console.log(`Refunding tenant ${lease.tenant_xrp_address}...`); - await hostClient.refundTenant(lease.tx_hash, lease.tenant_xrp_address, (uriInfo.leaseAmount * remainingMoments).toString(), { retryOptions: { maxRetryAttempts: MAX_TX_RETRY_ATTEMPTS, feeUplift: Math.floor(acc.affordableExtraFee / MAX_TX_RETRY_ATTEMPTS) } }); + const refundAmount = UtilHelper.formatXrplAmount(uriInfo.leaseAmount, remainingMoments); + await hostClient.refundTenant(lease.tx_hash, lease.tenant_xrp_address, refundAmount, { retryOptions: { maxRetryAttempts: MAX_TX_RETRY_ATTEMPTS, feeUplift: Math.floor(acc.affordableExtraFee / MAX_TX_RETRY_ATTEMPTS) } }); } } diff --git a/mb-xrpl/lib/util-helper.js b/mb-xrpl/lib/util-helper.js index 8d0ba1ec..de8eca3f 100644 --- a/mb-xrpl/lib/util-helper.js +++ b/mb-xrpl/lib/util-helper.js @@ -1,4 +1,5 @@ const ip6addr = require('ip6addr'); +const Decimal = require('decimal.js'); class UtilHelper { @@ -38,6 +39,14 @@ class UtilHelper { return false; } } + + static formatXrplAmount(amount, multiplier = 1) { + Decimal.set({ precision: 15, rounding: Decimal.ROUND_DOWN, toExpNeg: -15, toExpPos: 15 }); + return new Decimal(amount) + .times(multiplier) + .toSignificantDigits(15) + .toFixed(); + } } module.exports = { From e2b5f2726e072e04e80dc131d281174bd7616f60 Mon Sep 17 00:00:00 2001 From: Dulana Peiris Date: Fri, 26 Dec 2025 10:49:54 +0530 Subject: [PATCH 2/2] Add decimal.js dependency --- mb-xrpl/package-lock.json | 7 ++++--- mb-xrpl/package.json | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/mb-xrpl/package-lock.json b/mb-xrpl/package-lock.json index 495abfa8..e57e119c 100644 --- a/mb-xrpl/package-lock.json +++ b/mb-xrpl/package-lock.json @@ -6,6 +6,7 @@ "": { "name": "mb-xrpl", "dependencies": { + "decimal.js": "10.6.0", "evernode-js-client": "0.6.60", "ip6addr": "0.2.5", "sqlite3": "5.1.7" @@ -699,9 +700,9 @@ } }, "node_modules/decimal.js": { - "version": "10.4.3", - "resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.4.3.tgz", - "integrity": "sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==" + "version": "10.6.0", + "resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.6.0.tgz", + "integrity": "sha512-YpgQiITW3JXGntzdUmyUR1V812Hn8T1YVXhCu+wO3OpS4eU9l4YdD3qjyiKdV6mvV29zapkMeD390UVEf2lkUg==" }, "node_modules/decompress-response": { "version": "6.0.0", diff --git a/mb-xrpl/package.json b/mb-xrpl/package.json index 18c9e21d..7159ccca 100644 --- a/mb-xrpl/package.json +++ b/mb-xrpl/package.json @@ -5,6 +5,7 @@ "build": "npm run lint && ncc build app.js --minify -o dist" }, "dependencies": { + "decimal.js": "10.6.0", "evernode-js-client": "0.6.60", "ip6addr": "0.2.5", "sqlite3": "5.1.7"