Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal/funds/scaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,19 @@ func BuildScaledAsset(baseAsset string, scale int64) string {
// GetAssets collects, per scale, the (uncolored) amount an account holds of
// baseAsset. Scaling converts only uncolored balances and emits uncolored
// postings, so colored balances are excluded.
//
// Postings name the asset with BuildScaledAsset, so a balance whose asset is not
// in that form (e.g. "EUR/0", "EUR/02") is excluded too: it can't be withdrawn
// under the name the conversion would post, and it would collide with the
// canonical asset of the same scale.
func GetAssets(accountBalances []AccountBalance, baseAsset string) map[int64]*big.Int {
result := make(map[int64]*big.Int)
for _, accBalance := range accountBalances {
if accBalance.Color != "" {
continue
}
accBalanceAsset, scale := GetBaseAndScale(accBalance.Asset)
if accBalanceAsset == baseAsset {
if accBalanceAsset == baseAsset && BuildScaledAsset(baseAsset, scale) == accBalance.Asset {
result[scale] = new(big.Int).Set(accBalance.Amount)
}
}
Expand Down
15 changes: 15 additions & 0 deletions internal/funds/scaling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ func TestGetAssetsExcludesColoredBalances(t *testing.T) {
}, assets)
}

func TestGetAssetsExcludesNonCanonicalAssets(t *testing.T) {
assets := funds.GetAssets([]funds.AccountBalance{
{Asset: "EUR", Amount: big.NewInt(5)},
{Asset: "EUR/0", Amount: big.NewInt(0)},
{Asset: "EUR/02", Amount: big.NewInt(3)},
{Asset: "EUR/x", Amount: big.NewInt(4)},
{Asset: "EUR/2", Amount: big.NewInt(100)},
}, "EUR")

require.Equal(t, map[int64]*big.Int{
0: big.NewInt(5),
2: big.NewInt(100),
}, assets)
}

func TestScalingAvoidSwappingAlreadyHaveAsset(t *testing.T) {
// Need [USD/2 200]
// Got: {USD/2 100, USD 2}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,41 @@
}
]
},
{
"it": "a non-canonical asset of the same scale does not mask the canonical one",
"balances": [
{
"account": "src",
"asset": "EUR",
"amount": 4
},
{
"account": "src",
"asset": "EUR/0",
"amount": 0
}
],
"expect.postings": [
{
"source": "src",
"destination": "swap",
"asset": "EUR",
"amount": 4
},
{
"source": "swap",
"destination": "src",
"asset": "EUR/2",
"amount": 400
},
{
"source": "src",
"destination": "dest",
"asset": "EUR/2",
"amount": 400
}
]
},
{
"it": "fails when there aren't enough funds",
"balances": [
Expand Down
Loading