Compare commits

...

3 Commits

3 changed files with 15 additions and 7 deletions

View File

@ -1861,8 +1861,11 @@ func (h *MenuHandlers) ValidateAmount(ctx context.Context, sym string, input []b
return res, nil
}
// Format the amount with 2 decimal places before saving
formattedAmount := fmt.Sprintf("%.2f", inputAmount)
// Format the amount to 2 decimal places before saving (truncated)
cents := int((inputAmount + 1e-9) * 100)
truncated := float64(cents) / 100
formattedAmount := fmt.Sprintf("%.2f", truncated)
err = store.WriteEntry(ctx, sessionId, storedb.DATA_AMOUNT, []byte(formattedAmount))
if err != nil {
logg.ErrorCtxf(ctx, "failed to write amount entry with", "key", storedb.DATA_AMOUNT, "value", formattedAmount, "error", err)

View File

@ -1678,6 +1678,14 @@ func TestValidateAmount(t *testing.T) {
Content: "0.02ms",
},
},
{
name: "Test with valid decimal amount",
input: []byte("0.149"),
activeBal: []byte("5"),
expectedResult: resource.Result{
Content: "0.14",
},
},
}
for _, tt := range tests {

View File

@ -38,11 +38,8 @@ func ParseAndScaleAmount(storedAmount, activeDecimal string) (string, error) {
multiplier := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(tokenDecimal)), nil))
finalAmount := new(big.Float).Mul(amount, multiplier)
// Convert finalAmount to a string
finalAmountStr := new(big.Int)
finalAmount.Int(finalAmountStr)
return finalAmountStr.String(), nil
// Return finalAmount as a string with 0 decimal places (rounded)
return finalAmount.Text('f', 0), nil
}
func ReadTransactionData(ctx context.Context, store DataStore, sessionId string) (TransactionData, error) {