Skip to content

Commit

Permalink
fix: incompatibility between v1 and v2 api format when using numbers …
Browse files Browse the repository at this point in the history
…with more than 7 digits (#696)
  • Loading branch information
gfyrag authored Feb 20, 2025
1 parent b9703f3 commit 6d9ae35
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ func (s ScriptV1) ToCore() Script {
case string:
s.Script.Vars[k] = v
case map[string]any:
s.Script.Vars[k] = fmt.Sprintf("%s %v", v["asset"], v["amount"])
switch amount := v["amount"].(type) {
case string:
s.Script.Vars[k] = fmt.Sprintf("%s %s", v["asset"], amount)
case float64:
s.Script.Vars[k] = fmt.Sprintf("%s %d", v["asset"], int(amount))
}
default:
s.Script.Vars[k] = fmt.Sprint(v)
}
Expand Down
64 changes: 64 additions & 0 deletions internal/script_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ledger

import (
"math/big"
"testing"

"github.com/stretchr/testify/require"
)

func TestConvertScriptV1(t *testing.T) {
t.Parallel()

type testCase struct {
name string
inputVars map[string]any
expected map[string]string
}

testCases := []testCase{
{
name: "float64 conversion",
inputVars: map[string]any{
"amount": map[string]any{
"asset": "USD",
"amount": float64(999999999999999),
},
},
expected: map[string]string{
"amount": "USD 999999999999999",
},
},
{
name: "big int conversion",
inputVars: map[string]any{
"amount": map[string]any{
"asset": "USD",
"amount": func() string {
ret, _ := big.NewInt(0).SetString("9999999999999999999999999999999999999999", 10)
return ret.String()
}(),
},
},
expected: map[string]string{
"amount": "USD 9999999999999999999999999999999999999999",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

script := ScriptV1{
Script: Script{
Plain: ``,
},
Vars: tc.inputVars,
}

converted := script.ToCore()
require.Equal(t, tc.expected, converted.Vars)
})
}
}

0 comments on commit 6d9ae35

Please sign in to comment.