Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal/machine/vm/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can the type be an other thing ?

default:
s.Script.Vars[k] = fmt.Sprint(v)
}
Expand Down
56 changes: 56 additions & 0 deletions internal/machine/vm/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,59 @@ func TestRun(t *testing.T) {
})
}
}

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)
})
}
}
Loading