-
Notifications
You must be signed in to change notification settings - Fork 7
feat!: expose color as a first-class Posting property #139
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
Changes from 4 commits
1b159d6
378a303
0fa5ddd
f4fae45
6fa2898
cb5e8ce
90dd2ac
36653bc
fbac745
f8b9593
9a568a6
649546a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
|
|
||
| [TestPrettyPrintBalance - 1] | ||
| | [36mAccount[0m | [36mAsset [0m | [36mBalance[0m | | ||
| | alice | EUR/2 | 1 | | ||
| | alice | USD/1234 | 999999 | | ||
| | bob | BTC | 3 | | ||
| | [36mAccount[0m | [36mAsset [0m | [36mColor[0m | [36mBalance[0m | | ||
| | alice | EUR/2 | | 1 | | ||
| | alice | USD/1234 | | 999999 | | ||
| | bob | BTC | | 3 | | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,13 +38,21 @@ func getAssetScale(asset string) (string, int64) { | |
| return asset, 0 | ||
| } | ||
|
|
||
| // getAssets returns, for a given baseAsset, the per-scale uncolored balance. | ||
| // Asset scaling operates on the uncolored bucket only — colored funds are not | ||
| // implicitly converted across scales. | ||
| func getAssets(balance AccountBalance, baseAsset string) map[int64]*big.Int { | ||
| result := make(map[int64]*big.Int) | ||
| for asset, amount := range balance { | ||
| if strings.HasPrefix(asset, baseAsset) { | ||
| _, scale := getAssetScale(asset) | ||
| result[scale] = amount | ||
| for asset, colorMap := range balance { | ||
| if !strings.HasPrefix(asset, baseAsset) { | ||
| continue | ||
| } | ||
| amount, ok := colorMap[""] | ||
| if !ok { | ||
| continue | ||
| } | ||
| _, scale := getAssetScale(asset) | ||
| result[scale] = amount | ||
|
Comment on lines
+41
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't drop the color dimension during scaling lookup.
🤖 Prompt for AI Agents
Comment on lines
+46
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use exact base-asset matching here.
Suggested fix for asset, colorMap := range balance {
- if !strings.HasPrefix(asset, baseAsset) {
+ if asset != baseAsset && !strings.HasPrefix(asset, baseAsset+"/") {
continue
}
amount, ok := colorMap[""]
if !ok {
continue🤖 Prompt for AI Agents |
||
| } | ||
| return result | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,111 +1,186 @@ | ||
| package interpreter | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "math/big" | ||
| "strings" | ||
|
|
||
| "github.com/formancehq/numscript/internal/utils" | ||
| ) | ||
|
|
||
| func (b Balances) DeepClone() Balances { | ||
| cloned := make(Balances) | ||
| for account, accountBalances := range b { | ||
| for asset, amount := range accountBalances { | ||
| utils.NestedMapGetOrPutDefault(cloned, account, asset, func() *big.Int { | ||
| return new(big.Int).Set(amount) | ||
| }) | ||
| // Uncolored builds a ColorBalance holding a single amount under the empty | ||
| // color key (the "no color" bucket). It is meant to keep test setup terse. | ||
| func Uncolored(amount *big.Int) ColorBalance { | ||
| return ColorBalance{"": amount} | ||
| } | ||
|
|
||
| // UnmarshalJSON accepts two JSON shapes for a Balances value: | ||
| // | ||
| // 1. Flat (uncolored shorthand): | ||
| // {"alice": {"USD/2": 100, "EUR/2": -42}} | ||
| // Every asset gets a single entry under the "" (no color) bucket. | ||
| // | ||
| // 2. Colored (full form): | ||
| // {"alice": {"USD/2": {"": 100, "GRANTS": 50}}} | ||
| // | ||
| // Shapes can be mixed across assets within the same document. | ||
| func (b *Balances) UnmarshalJSON(data []byte) error { | ||
| raw := map[string]map[string]json.RawMessage{} | ||
| if err := json.Unmarshal(data, &raw); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| out := Balances{} | ||
| for account, assets := range raw { | ||
| accB := AccountBalance{} | ||
| out[account] = accB | ||
| for asset, rawVal := range assets { | ||
| colorMap, err := decodeColorBalance(rawVal) | ||
| if err != nil { | ||
| return fmt.Errorf("balances[%s][%s]: %w", account, asset, err) | ||
| } | ||
| accB[asset] = colorMap | ||
| } | ||
| } | ||
| return cloned | ||
| *b = out | ||
| return nil | ||
| } | ||
|
|
||
| func coloredAsset(asset string, color *string) string { | ||
| if color == nil || *color == "" { | ||
| return asset | ||
| func decodeColorBalance(data json.RawMessage) (ColorBalance, error) { | ||
| // Try the shorthand: a single number meaning the uncolored bucket. | ||
| var amount json.Number | ||
| if err := json.Unmarshal(data, &amount); err == nil { | ||
| n, ok := new(big.Int).SetString(amount.String(), 10) | ||
| if !ok { | ||
| return nil, fmt.Errorf("invalid integer amount %q", amount.String()) | ||
| } | ||
| return ColorBalance{"": n}, nil | ||
| } | ||
|
|
||
| // note: 1 <= len(parts) <= 2 | ||
| parts := strings.Split(asset, "/") | ||
|
|
||
| coloredAsset := parts[0] + "_" + *color | ||
| if len(parts) > 1 { | ||
| coloredAsset += "/" + parts[1] | ||
| // Otherwise expect a {color: amount} object. | ||
| raw := map[string]json.Number{} | ||
| if err := json.Unmarshal(data, &raw); err != nil { | ||
| return nil, fmt.Errorf("expected integer or {color: amount} object, got %s", string(data)) | ||
| } | ||
| out := ColorBalance{} | ||
| for color, amt := range raw { | ||
| n, ok := new(big.Int).SetString(amt.String(), 10) | ||
| if !ok { | ||
| return nil, fmt.Errorf("color %q: invalid integer amount %q", color, amt.String()) | ||
| } | ||
| out[color] = n | ||
| } | ||
| return coloredAsset | ||
| return out, nil | ||
| } | ||
|
|
||
| // Get the (account, asset) tuple from the Balances | ||
| // if the tuple is not present, it will write a big.NewInt(0) in it and return it | ||
| func (b Balances) fetchBalance(account string, uncoloredAsset string, color string) *big.Int { | ||
| return utils.NestedMapGetOrPutDefault(b, account, coloredAsset(uncoloredAsset, &color), func() *big.Int { | ||
| return new(big.Int) | ||
| }) | ||
| func (b Balances) DeepClone() Balances { | ||
| cloned := make(Balances) | ||
| for account, accountBalances := range b { | ||
| clonedAcc := AccountBalance{} | ||
| cloned[account] = clonedAcc | ||
| for asset, colorMap := range accountBalances { | ||
| clonedColors := ColorBalance{} | ||
| clonedAcc[asset] = clonedColors | ||
| for color, amount := range colorMap { | ||
| clonedColors[color] = new(big.Int).Set(amount) | ||
| } | ||
| } | ||
| } | ||
| return cloned | ||
| } | ||
|
|
||
| func (b Balances) has(account string, asset string) bool { | ||
| accountBalances := utils.MapGetOrPutDefault(b, account, func() AccountBalance { | ||
| // Get the (account, asset, color) balance from Balances. | ||
| // If the entry is not present, it will write a big.NewInt(0) in it and return it. | ||
| func (b Balances) fetchBalance(account string, asset string, color string) *big.Int { | ||
| accBalance := utils.MapGetOrPutDefault(b, account, func() AccountBalance { | ||
| return AccountBalance{} | ||
| }) | ||
| colorMap := utils.MapGetOrPutDefault(accBalance, asset, func() ColorBalance { | ||
| return ColorBalance{} | ||
| }) | ||
| return utils.MapGetOrPutDefault(colorMap, color, func() *big.Int { | ||
| return new(big.Int) | ||
| }) | ||
| } | ||
|
|
||
| _, ok := accountBalances[asset] | ||
| func (b Balances) has(account string, asset string, color string) bool { | ||
| accountBalances, ok := b[account] | ||
| if !ok { | ||
| return false | ||
| } | ||
| colorMap, ok := accountBalances[asset] | ||
| if !ok { | ||
| return false | ||
| } | ||
| _, ok = colorMap[color] | ||
| return ok | ||
| } | ||
|
|
||
| // given a BalanceQuery, return a new query which only contains needed (asset, account) pairs | ||
| // given a BalanceQuery, return a new query which only contains needed (asset, color) pairs | ||
| // (that is, the ones that aren't already cached) | ||
| func (b Balances) filterQuery(q BalanceQuery) BalanceQuery { | ||
| filteredQuery := BalanceQuery{} | ||
| for accountName, queriedCurrencies := range q { | ||
| filteredCurrencies := utils.Filter(queriedCurrencies, func(currency string) bool { | ||
| return !b.has(accountName, currency) | ||
| for accountName, queriedItems := range q { | ||
| filteredItems := utils.Filter(queriedItems, func(item AssetColor) bool { | ||
| return !b.has(accountName, item.Asset, item.Color) | ||
| }) | ||
|
|
||
| if len(filteredCurrencies) > 0 { | ||
| filteredQuery[accountName] = filteredCurrencies | ||
| if len(filteredItems) > 0 { | ||
| filteredQuery[accountName] = filteredItems | ||
| } | ||
|
|
||
| } | ||
| return filteredQuery | ||
| } | ||
|
|
||
| // Merge balances by adding balances in the "update" arg | ||
| func (b Balances) Merge(update Balances) { | ||
| // merge queried balance | ||
| for acc, accBalances := range update { | ||
| cachedAcc := utils.MapGetOrPutDefault(b, acc, func() AccountBalance { | ||
| return AccountBalance{} | ||
| }) | ||
|
|
||
| for curr, amt := range accBalances { | ||
| cachedAcc[curr] = amt | ||
| for asset, colorMap := range accBalances { | ||
| cachedColors := utils.MapGetOrPutDefault(cachedAcc, asset, func() ColorBalance { | ||
| return ColorBalance{} | ||
| }) | ||
| for color, amt := range colorMap { | ||
| cachedColors[color] = amt | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (b Balances) PrettyPrint() string { | ||
| header := []string{"Account", "Asset", "Balance"} | ||
| header := []string{"Account", "Asset", "Color", "Balance"} | ||
|
|
||
| var rows [][]string | ||
| for account, accBalances := range b { | ||
| for asset, balance := range accBalances { | ||
| row := []string{account, asset, balance.String()} | ||
| rows = append(rows, row) | ||
| for asset, colorMap := range accBalances { | ||
| for color, balance := range colorMap { | ||
| rows = append(rows, []string{account, asset, color, balance.String()}) | ||
| } | ||
| } | ||
| } | ||
| return utils.CsvPretty(header, rows, true) | ||
| } | ||
|
|
||
| func CompareBalances(b1 Balances, b2 Balances) bool { | ||
| return utils.Map2Cmp(b1, b2, func(ab1, ab2 *big.Int) bool { | ||
| return ab1.Cmp(ab2) == 0 | ||
| return utils.MapCmp(b1, b2, func(ab1, ab2 AccountBalance) bool { | ||
| return utils.MapCmp(ab1, ab2, func(cm1, cm2 ColorBalance) bool { | ||
| return utils.MapCmp(cm1, cm2, func(a1, a2 *big.Int) bool { | ||
| return a1.Cmp(a2) == 0 | ||
| }) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| // Returns whether the first value is a subset of the second one | ||
| func CompareBalancesIncluding(b1 Balances, b2 Balances) bool { | ||
| return utils.MapIncludes(b2, b1, func(a2 AccountBalance, a1 AccountBalance) bool { | ||
| return utils.MapIncludes(a2, a1, func(a2 *big.Int, a1 *big.Int) bool { | ||
| return a2.Cmp(a1) == 0 | ||
| return utils.MapIncludes(a2, a1, func(cm2, cm1 ColorBalance) bool { | ||
| return utils.MapIncludes(cm2, cm1, func(x2, x1 *big.Int) bool { | ||
| return x2.Cmp(x1) == 0 | ||
| }) | ||
| }) | ||
| }) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This matches any asset starting with
baseAsset, sogetAssets(balance, "USD")also includes assets likeUSDCorUSDX/2in the scaling pool. The match should be exact base asset orbaseAsset + "/"; otherwise unrelated asset balances can be converted during asset scaling.