Skip to content

Commit

Permalink
feat(machine): allow to send 0 amount (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas committed Mar 6, 2024
1 parent ad9eba2 commit 25e147c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
6 changes: 6 additions & 0 deletions pkg/core/funding.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func (f Funding) Take(amount *MonetaryInt) (Funding, Funding, error) {
remainder := Funding{
Asset: f.Asset,
}
if amount.Eq(NewMonetaryInt(0)) && len(f.Parts) > 0 {
result.Parts = append(result.Parts, FundingPart{
Account: f.Parts[0].Account,
Amount: amount,
})
}
remainingToWithdraw := amount
i := 0
for remainingToWithdraw.Gt(NewMonetaryInt(0)) && i < len(f.Parts) {
Expand Down
20 changes: 10 additions & 10 deletions pkg/ledger/execute_script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ func TestSend(t *testing.T) {
)`,
},
}
_, err := l.ExecuteScript(context.Background(), false, script)
require.Error(t, err)
require.True(t, ledger.IsValidationError(err))
require.ErrorContains(t, err, "transaction has no postings")
res, err := l.ExecuteScript(context.Background(), false, script)
require.NoError(t, err)

require.Equal(t, 1, len(res.Postings))
})

t.Run("one send with monetary all should fail", func(t *testing.T) {
Expand All @@ -123,10 +123,10 @@ func TestSend(t *testing.T) {
)`,
},
}
_, err := l.ExecuteScript(context.Background(), false, script)
require.Error(t, err)
require.True(t, ledger.IsValidationError(err))
require.ErrorContains(t, err, "transaction has no postings")
res, err := l.ExecuteScript(context.Background(), false, script)
require.NoError(t, err)

require.Equal(t, 1, len(res.Postings))
})

t.Run("one send with zero amount and another with positive amount should succeed", func(t *testing.T) {
Expand All @@ -145,7 +145,7 @@ func TestSend(t *testing.T) {
}
res, err := l.ExecuteScript(context.Background(), false, script)
require.NoError(t, err)
require.Equal(t, 1, len(res.Postings))
require.Equal(t, 2, len(res.Postings))

assertBalance(t, l, "user:001",
"USD/2", core.NewMonetaryInt(100))
Expand All @@ -167,7 +167,7 @@ func TestSend(t *testing.T) {
}
res, err := l.ExecuteScript(context.Background(), false, script)
require.NoError(t, err)
require.Equal(t, 1, len(res.Postings))
require.Equal(t, 2, len(res.Postings))

assertBalance(t, l, "user:001",
"USD/2", core.NewMonetaryInt(101))
Expand Down
3 changes: 0 additions & 3 deletions pkg/machine/vm/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,6 @@ func (m *Machine) tick() (bool, byte, error) {
for _, part := range funding.Parts {
src := part.Account
amt := part.Amount
if amt.Eq(core.NewMonetaryInt(0)) {
continue
}
m.Postings = append(m.Postings, Posting{
Source: string(src),
Destination: string(dest),
Expand Down
35 changes: 32 additions & 3 deletions pkg/machine/vm/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,28 @@ func TestSend(t *testing.T) {
test(t, tc)
}

func TestSend0Amount(t *testing.T) {
tc := NewTestCase()
tc.compile(t, `send [EUR/2 0] (
source=@alice
destination=@bob
)`)
tc.setBalance("alice", "EUR/2", 100)
tc.expected = CaseResult{
Printed: []core.Value{},
Postings: []Posting{
{
Asset: "EUR/2",
Amount: core.NewMonetaryInt(0),
Source: "alice",
Destination: "bob",
},
},
ExitCode: EXIT_OK,
}
test(t, tc)
}

func TestVariables(t *testing.T) {
tc := NewTestCase()
tc.compile(t, `vars {
Expand Down Expand Up @@ -555,16 +577,23 @@ func TestNoEmptyPostings(t *testing.T) {
test(t, tc)
}

func TestNoEmptyPostings2(t *testing.T) {
func TestEmptyPostings(t *testing.T) {
tc := NewTestCase()
tc.compile(t, `send [GEM *] (
source = @foo
destination = @bar
)`)
tc.setBalance("foo", "GEM", 0)
tc.expected = CaseResult{
Printed: []core.Value{},
Postings: []Posting{},
Printed: []core.Value{},
Postings: []Posting{
{
Source: "foo",
Destination: "bar",
Amount: core.NewMonetaryInt(0),
Asset: "GEM",
},
},
ExitCode: EXIT_OK,
}
test(t, tc)
Expand Down

0 comments on commit 25e147c

Please sign in to comment.