From 90f330d6c2c97d65a616eefd4c31e7770d3914c0 Mon Sep 17 00:00:00 2001 From: ascandone Date: Wed, 4 Dec 2024 11:09:42 +0100 Subject: [PATCH] feat: trimmed zero postings --- internal/interpreter/interpreter.go | 4 ++++ internal/interpreter/interpreter_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/internal/interpreter/interpreter.go b/internal/interpreter/interpreter.go index aaa32b2..69350ac 100644 --- a/internal/interpreter/interpreter.go +++ b/internal/interpreter/interpreter.go @@ -618,6 +618,10 @@ func (s *programState) receiveFrom(destination parser.Destination, amount *big.I remainingAmount := new(big.Int).Set(amount) handler := func(keptOrDest parser.KeptOrDestination, amountToReceive *big.Int) InterpreterError { + if amountToReceive.Cmp(big.NewInt(0)) == 0 { + return nil + } + err := s.receiveFromKeptOrDest(keptOrDest, amountToReceive) if err != nil { return err diff --git a/internal/interpreter/interpreter_test.go b/internal/interpreter/interpreter_test.go index 6913efc..e1b7cb1 100644 --- a/internal/interpreter/interpreter_test.go +++ b/internal/interpreter/interpreter_test.go @@ -2659,6 +2659,30 @@ func TestZeroPostings(t *testing.T) { test(t, tc) } +func TestZeroPostingsDestination(t *testing.T) { + tc := NewTestCase() + tc.compile(t, ` + send [COIN 100] ( + source = @world + destination = { + max [COIN 0] to @d1 + remaining to @d2 + } + ) + `) + tc.expected = CaseResult{ + Postings: []machine.Posting{ + { + "world", + "d2", + big.NewInt(100), + "COIN", + }, + }, + } + test(t, tc) +} + func TestUnboundedOverdraftWhenNotEnoughFunds(t *testing.T) { tc := NewTestCase() tc.setBalance("users:2345:main", "USD/2", 8000)