Commit 2499c7d
authored
feat: Update current-account service layer for dimension-agnostic Amount (#1256)
* feat: Update current-account service layer for dimension-agnostic Amount
Replace NewMoney() calls and .Currency() method references in the service
layer with dimension-agnostic Amount constructors and .InstrumentCode():
- Add protoMoneyToAmount() helper in grpc_mappers.go that converts a
google.type.Money proto to domain.Amount using the account's instrument
for precision-aware, dimension-agnostic conversion (supports CURRENCY,
ENERGY, COMPUTE, CARBON, etc.)
- grpc_deposit_endpoints.go: replace NewMoney() calls with protoMoneyToAmount()
- grpc_withdrawal_execute.go: same replacement
- grpc_withdrawal_manage.go: same replacement with minor-unit variable rename
- lien_service.go:
- Move account prefetch before amount parsing to provide instrument context
- Add instrument code validation in legacy lien path before amount conversion
- Replace NewMoney(account.Balance().InstrumentCode(), ...) with
NewAmountFromInstrument(..., account.Balance().Dimension(), ...) at
available-balance calculation sites
- Remove unused protoToMoney() method and commonpb import
* fix: address CodeRabbit review feedback on Amount conversion helpers
- toMoneyAmount: make precision-aware (was hardcoded for 2-decimal currency)
- use scale=10^precision and nanosPerMinor=10^(9-precision) for any instrument
- protoMoneyToAmount: fix signed rounding and add post-rounding overflow guard
- half-up rounding (+ half/nanosPerUnit) incorrectly rounded negative nanos toward zero
- use half-away-from-zero branching on sign so negative amounts round correctly
- add post-rounding overflow guard to catch unitsMinor+nanosMinor overflow
- grpc_deposit_endpoints: add nil guard for req.Amount before account fetch
- lien_service: fall back to account.AvailableBalance() if NewAmountFromInstrument fails
* fix: replace NewMoney with NewAmountFromInstrument in valuation lien path
The valuation path was hardcoding `* 100` (2-decimal assumption) and calling
domain.NewMoney which only handles CURRENCY dimension. Replace with
precision-aware decimal.Shift(precision) scaling and NewAmountFromInstrument
so energy and other non-2-decimal instruments work correctly.
* fix: address second-round CodeRabbit findings on Amount conversion
- grpc_deposit_endpoints: use opStatusInvalidAmount instead of
operationStatusInvalidCurrency when protoMoneyToAmount fails
- grpc_mappers: add explicit precision bounds check [0..9] in
protoMoneyToAmount; add ErrInvalidPrecision sentinel error
---------
Co-authored-by: Ben Coombs <bjcoombs@users.noreply.github.com>1 parent cc33aea commit 2499c7d
5 files changed
Lines changed: 169 additions & 138 deletions
File tree
- services/current-account/service
Lines changed: 13 additions & 31 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
| |||
101 | 100 | | |
102 | 101 | | |
103 | 102 | | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
104 | 109 | | |
105 | 110 | | |
106 | 111 | | |
| |||
120 | 125 | | |
121 | 126 | | |
122 | 127 | | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
144 | 132 | | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
| 133 | + | |
| 134 | + | |
153 | 135 | | |
154 | 136 | | |
155 | 137 | | |
| |||
162 | 144 | | |
163 | 145 | | |
164 | 146 | | |
165 | | - | |
| 147 | + | |
166 | 148 | | |
167 | 149 | | |
168 | 150 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
| |||
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
53 | | - | |
54 | | - | |
55 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
56 | 60 | | |
57 | | - | |
58 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
59 | 65 | | |
60 | 66 | | |
61 | | - | |
62 | | - | |
63 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
64 | 70 | | |
65 | 71 | | |
66 | 72 | | |
| |||
114 | 120 | | |
115 | 121 | | |
116 | 122 | | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
117 | 189 | | |
118 | 190 | | |
119 | 191 | | |
| |||
Lines changed: 6 additions & 30 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
| |||
208 | 207 | | |
209 | 208 | | |
210 | 209 | | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
238 | 214 | | |
239 | 215 | | |
240 | | - | |
| 216 | + | |
241 | 217 | | |
242 | 218 | | |
243 | 219 | | |
| |||
250 | 226 | | |
251 | 227 | | |
252 | 228 | | |
253 | | - | |
| 229 | + | |
254 | 230 | | |
255 | 231 | | |
256 | 232 | | |
| |||
Lines changed: 16 additions & 23 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
| |||
78 | 77 | | |
79 | 78 | | |
80 | 79 | | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
100 | | - | |
| 99 | + | |
| 100 | + | |
101 | 101 | | |
102 | | - | |
| 102 | + | |
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| |||
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | 111 | | |
119 | 112 | | |
120 | 113 | | |
| |||
138 | 131 | | |
139 | 132 | | |
140 | 133 | | |
141 | | - | |
| 134 | + | |
142 | 135 | | |
143 | 136 | | |
144 | 137 | | |
| |||
0 commit comments