Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix balance resolution using variable #454

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}