Skip to content

Commit

Permalink
feat: Numscript: return postings with zero amount (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Gelloz authored and flemzord committed May 12, 2023
1 parent 2ca1cbb commit 32988a6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 11 deletions.
8 changes: 8 additions & 0 deletions pkg/machine/internal/funding.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ 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
80 changes: 75 additions & 5 deletions pkg/machine/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,30 @@ var testCases = []testCase{
expectErrorCode: vm.ScriptErrorInsufficientFund,
},
{
name: "send 0$",
name: "send $0",
script: `
send [USD/2 0] (
source = @alice
destination = @user:001
)`,
expectResult: Result{
Postings: []core.Posting{
core.NewPosting("alice", "user:001", "USD/2", big.NewInt(0)),
},
Metadata: map[string]any{},
AccountMetadata: map[string]core.Metadata{},
},
},
{
name: "send $0 world",
script: `
send [USD/2 0] (
source = @world
destination = @user:001
)`,
expectResult: Result{
Postings: []core.Posting{
// TODO: The machine should return a posting with 0 as amount
//core.NewPosting("world", "user:001", "USD/2", core.NewMonetaryInt(0)),
core.NewPosting("world", "user:001", "USD/2", big.NewInt(0)),
},
Metadata: map[string]any{},
AccountMetadata: map[string]core.Metadata{},
Expand All @@ -76,8 +90,7 @@ var testCases = []testCase{
)`,
expectResult: Result{
Postings: []core.Posting{
// TODO: The machine should return a posting with 0 as amount
//core.NewPosting("world", "user:001", "USD/2", core.NewMonetaryInt(0)),
core.NewPosting("alice", "user:001", "USD/2", big.NewInt(0)),
},
Metadata: map[string]any{},
AccountMetadata: map[string]core.Metadata{},
Expand Down Expand Up @@ -314,6 +327,63 @@ var testCases = []testCase{
AccountMetadata: map[string]core.Metadata{},
},
},
{
name: "send amount 0",
setup: func(t *testing.T, store storage.LedgerStore) {
require.NoError(t, store.EnsureAccountExists(context.Background(), "alice"))
},
script: `
send [USD 0] (
source = @alice
destination = @bob
)`,
expectResult: Result{
Postings: []core.Posting{
core.NewPosting("alice", "bob", "USD", big.NewInt(0)),
},
Metadata: core.Metadata{},
AccountMetadata: map[string]core.Metadata{},
},
},
{
name: "send all with balance 0",
setup: func(t *testing.T, store storage.LedgerStore) {
require.NoError(t, store.EnsureAccountExists(context.Background(), "alice"))
},
script: `
send [USD *] (
source = @alice
destination = @bob
)`,
expectResult: Result{
Postings: []core.Posting{
core.NewPosting("alice", "bob", "USD", big.NewInt(0)),
},
Metadata: core.Metadata{},
AccountMetadata: map[string]core.Metadata{},
},
},
{
name: "send account balance of 0",
setup: func(t *testing.T, store storage.LedgerStore) {
require.NoError(t, store.EnsureAccountExists(context.Background(), "alice"))
},
script: `
vars {
monetary $bal = balance(@alice, USD)
}
send $bal (
source = @alice
destination = @bob
)`,
expectResult: Result{
Postings: []core.Posting{
core.NewPosting("alice", "bob", "USD", big.NewInt(0)),
},
Metadata: core.Metadata{},
AccountMetadata: map[string]core.Metadata{},
},
},
}

func TestMachine(t *testing.T) {
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 @@ -420,9 +420,6 @@ func (m *Machine) tick() (bool, byte, error) {
for _, part := range funding.Parts {
src := part.Account
amt := part.Amount
if amt.Eq(internal.NewMonetaryInt(0)) {
continue
}
m.Postings = append(m.Postings, Posting{
Source: string(src),
Destination: string(dest),
Expand Down
13 changes: 10 additions & 3 deletions pkg/machine/vm/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,16 +552,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: []internal.Value{},
Postings: []Posting{},
Printed: []internal.Value{},
Postings: []Posting{
{
Source: "foo",
Destination: "bar",
Amount: internal.NewMonetaryInt(0),
Asset: "GEM",
},
},
ExitCode: EXIT_OK,
}
test(t, tc)
Expand Down

0 comments on commit 32988a6

Please sign in to comment.