Skip to content

Commit

Permalink
feat: fix balance resolution using variable
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Aug 15, 2023
1 parent 25a49e1 commit d55966b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/machine/vm/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,13 @@ func (m *Machine) ResolveBalances() (chan BalanceRequest, error) {
}
return
}

if account, ok := (*account).(core.AccountAddress); ok {
m.Balances[account] = make(map[core.Asset]*core.MonetaryInt)

if _, ok := m.Balances[account]; !ok {
m.Balances[account] = make(map[core.Asset]*core.MonetaryInt)
}

// for every asset, send request
for addr := range neededAssets {
mon, ok := m.getResource(addr)
Expand Down
34 changes: 34 additions & 0 deletions pkg/machine/vm/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1929,3 +1929,37 @@ func TestSendWithArithmetic(t *testing.T) {
test(t, tc)
})
}

func TestUseDifferentAssetsWithSameSourceAccount(t *testing.T) {
tc := NewTestCase()
tc.compile(t, `vars {
account $a_account
}
send [A 100] (
source = $a_account allowing unbounded overdraft
destination = @account1
)
send [B 100] (
source = @world
destination = @account2
)`)
tc.setBalance("account1", "A", 100)
tc.setBalance("account2", "B", 100)
tc.setVarsFromJSON(t, `{"a_account": "world"}`)
tc.expected = CaseResult{
Printed: []core.Value{},
Postings: []Posting{{
Source: "world",
Destination: "account1",
Amount: core.NewMonetaryInt(100),
Asset: "A",
}, {
Source: "world",
Destination: "account2",
Amount: core.NewMonetaryInt(100),
Asset: "B",
}},
ExitCode: EXIT_OK,
}
test(t, tc)
}

0 comments on commit d55966b

Please sign in to comment.