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 return res, nil
} }
// Format the amount with 2 decimal places before saving // Format the amount to 2 decimal places before saving (truncated)
formattedAmount := fmt.Sprintf("%.2f", inputAmount) 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)) err = store.WriteEntry(ctx, sessionId, storedb.DATA_AMOUNT, []byte(formattedAmount))
if err != nil { if err != nil {
logg.ErrorCtxf(ctx, "failed to write amount entry with", "key", storedb.DATA_AMOUNT, "value", formattedAmount, "error", err) 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", 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 { 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)) multiplier := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(tokenDecimal)), nil))
finalAmount := new(big.Float).Mul(amount, multiplier) finalAmount := new(big.Float).Mul(amount, multiplier)
// Convert finalAmount to a string // Return finalAmount as a string with 0 decimal places (rounded)
finalAmountStr := new(big.Int) return finalAmount.Text('f', 0), nil
finalAmount.Int(finalAmountStr)
return finalAmountStr.String(), nil
} }
func ReadTransactionData(ctx context.Context, store DataStore, sessionId string) (TransactionData, error) { func ReadTransactionData(ctx context.Context, store DataStore, sessionId string) (TransactionData, error) {