Skip to content

Commit

Permalink
FIx precision loss with numeric send
Browse files Browse the repository at this point in the history
bbedward committed Jul 1, 2024
1 parent e9a5849 commit 260d55c
Showing 2 changed files with 15 additions and 26 deletions.
37 changes: 13 additions & 24 deletions apps/server/models/requests/send_request.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,35 @@
package requests

import (
"bytes"
"encoding/json"
"fmt"
)

type SendRequest struct {
BaseRequest `mapstructure:",squash"`
Source string `json:"source" mapstructure:"source"`
Destination string `json:"destination" mapstructure:"destination"`
Amount string `json:"-" mapstructure:"amount"` // Exclude from automatic unmarshalling
RawAmount json.RawMessage `json:"amount" mapstructure:"-"`
ID *string `json:"id,omitempty" mapstructure:"id,omitempty"`
Work *string `json:"work,omitempty" mapstructure:"work,omitempty"`
Source string `json:"source" mapstructure:"source"`
Destination string `json:"destination" mapstructure:"destination"`
Amount string `json:"amount" mapstructure:"amount"`
ID *string `json:"id,omitempty" mapstructure:"id,omitempty"`
Work *string `json:"work,omitempty" mapstructure:"work,omitempty"`
}

func (r *SendRequest) UnmarshalJSON(data []byte) error {
type Alias SendRequest
aux := &struct {
aux := struct {
*Alias
Amount json.Number `json:"amount"`
}{
Alias: (*Alias)(r),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}

var temp interface{}
if err := json.Unmarshal(r.RawAmount, &temp); err != nil {
return err
}
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.UseNumber()

switch v := temp.(type) {
case float64:
r.Amount = fmt.Sprintf("%.0f", v) // Safely convert to string without scientific notation
case int:
r.Amount = fmt.Sprintf("%d", v)
case string:
r.Amount = v
default:
return fmt.Errorf("amount is of a type I don't know how to handle")
if err := decoder.Decode(&aux); err != nil {
return err
}

r.Amount = aux.Amount.String()
return nil
}
4 changes: 2 additions & 2 deletions apps/server/models/requests/send_request_test.go
Original file line number Diff line number Diff line change
@@ -22,15 +22,15 @@ func TestDecodeSendRequest(t *testing.T) {
}

func TestDecodeSendRequestNumericAmount(t *testing.T) {
encoded := `{"action":"send","wallet":"1234","source":"nano_1","destination":"nano_2","bpow_key":"abc","amount":1234}`
encoded := `{"action":"send","wallet":"1234","source":"nano_1","destination":"nano_2","bpow_key":"abc","amount":12340000000000000000000000000}`
var decoded SendRequest
json.Unmarshal([]byte(encoded), &decoded)
assert.Equal(t, "send", decoded.Action)
assert.Equal(t, "1234", decoded.Wallet)
assert.Equal(t, "nano_1", decoded.Source)
assert.Equal(t, "nano_2", decoded.Destination)
assert.Equal(t, "abc", *decoded.BpowKey)
assert.Equal(t, "1234", decoded.Amount)
assert.Equal(t, "12340000000000000000000000000", decoded.Amount)
assert.Nil(t, decoded.Work)
}

0 comments on commit 260d55c

Please sign in to comment.