From 23909b208a7f253e174b95404468b07198768562 Mon Sep 17 00:00:00 2001 From: Azorlogh Date: Mon, 8 Dec 2025 14:52:14 +0100 Subject: [PATCH] fix saveall when balance is negative --- internal/interpreter/interpreter.go | 4 +++- ...save-from-account__save-all-negative-balance.num | 6 ++++++ ...ccount__save-all-negative-balance.num.specs.json | 13 +++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 internal/interpreter/testdata/script-tests/save/save-from-account__save-all-negative-balance.num create mode 100644 internal/interpreter/testdata/script-tests/save/save-from-account__save-all-negative-balance.num.specs.json diff --git a/internal/interpreter/interpreter.go b/internal/interpreter/interpreter.go index 383b36ef..14ba6cfd 100644 --- a/internal/interpreter/interpreter.go +++ b/internal/interpreter/interpreter.go @@ -395,7 +395,9 @@ func (st *programState) runSaveStatement(saveStatement parser.SaveStatement) Int balance := st.CachedBalances.fetchBalance(*account, *asset, "") if amt == nil { - balance.Set(big.NewInt(0)) + if balance.Sign() > 0 { + balance.Set(big.NewInt(0)) + } } else { // Do not allow negative saves if amt.Cmp(big.NewInt(0)) == -1 { diff --git a/internal/interpreter/testdata/script-tests/save/save-from-account__save-all-negative-balance.num b/internal/interpreter/testdata/script-tests/save/save-from-account__save-all-negative-balance.num new file mode 100644 index 00000000..0a85a69f --- /dev/null +++ b/internal/interpreter/testdata/script-tests/save/save-from-account__save-all-negative-balance.num @@ -0,0 +1,6 @@ +save [USD *] from @alice + +send [USD 10] ( + source = @alice allowing overdraft up to [USD 30] + destination = @world +) diff --git a/internal/interpreter/testdata/script-tests/save/save-from-account__save-all-negative-balance.num.specs.json b/internal/interpreter/testdata/script-tests/save/save-from-account__save-all-negative-balance.num.specs.json new file mode 100644 index 00000000..6d9c6bd9 --- /dev/null +++ b/internal/interpreter/testdata/script-tests/save/save-from-account__save-all-negative-balance.num.specs.json @@ -0,0 +1,13 @@ +{ + "testCases": [ + { + "it": "-", + "balances": { + "alice": { + "USD": -100 + } + }, + "expect.error.missingFunds": true + } + ] +}