Request money sends `incomingAmount` in whole units, not minor units
Problem
On the Request page, the user types the amount in whole units, for example 10.50 for 10.50 EUR. The frontend sends this number unchanged (transfers.ts:43, amount: z.coerce.number()).
IncomingPaymentService.create also sends the number unchanged (incomingPayment/service.ts:86):
const response = await this.rhyzaAdminClient.createPaymentIntent({
walletAddress: existingWalletAddress.url,
incomingAmount: amount,
...
})
Rhyza reads incomingAmount in minor units of the wallet address's asset. For EUR at scale 2, 1000 is 10.00 EUR. Rhyza converts the value with BigInt() (aggregate.ts:462) and stores it as a uint64.
The Rafiki code before interledger/interledger-pay#101 (testnet#2257) scaled the amount:
amount: BigInt(amount * 10 ** asset.scale)
The read path in the same file divides by the scale. getPaymentDetailsByUrl calls transformAmount(value, scale) (incomingPayment/service.ts:104).
Impact
- A request for 10 EUR creates a payment intent for 0.10 EUR. The payment intent completes after it receives 10 cents. The payer sees 0.10 EUR on the payment page.
- A request for 10.50 EUR sends
10.5. This value is not a whole number of minor units.BigInt(10.5)throws an error in Rhyza. The request then probably fails with500. This comes from the code; I did not run it.
The unit tests do not find this error. For an input of 100, they expect incomingAmount: 100 (service.test.ts:123, test "should create a payment intent and return its url").
Fix
- Multiply the amount by
10 ** scale, with the scale of the account's asset. - Round the result to an integer, for example
Math.round(amount * 10 ** scale). Rhyza's spec typesincomingAmountas a number. - Change the tests to expect the scaled value.
- Add a test with a fraction, for example
10.29.
Note: Do not use transformBalance (helpers.ts:24) for step 2. It rounds down a floating-point result. For example, 10.29 * 100 is 1028.9999…, and transformBalance gives 1028.
Found at
testnet v1 @ 71e6f304, Rhyza main @ 5e206fcf.
Hey can I take this up?