@@ -161,72 +161,56 @@ func (n *OpenBazaarNode) GetModeratorFee(transactionTotal *big.Int, txCurrencyCo
161
161
if err != nil {
162
162
return big .NewInt (0 ), fmt .Errorf ("lookup dispute transaction currency (%s): %s" , txCurrencyCode , err )
163
163
}
164
- t := new (big.Float ).SetInt (transactionTotal )
165
164
switch profile .ModeratorInfo .Fee .FeeType {
166
165
case pb .Moderator_Fee_PERCENTAGE :
167
- f := big .NewFloat (float64 (profile .ModeratorInfo .Fee .Percentage ))
168
- f .Mul (f , big .NewFloat (0.01 ))
169
- t .Mul (t , f )
170
- total , _ := t .Int (nil )
171
- return total , nil
166
+ feePercent := new (big.Float ).Mul (big .NewFloat (float64 (profile .ModeratorInfo .Fee .Percentage )), big .NewFloat (0.01 ))
167
+ feePercentAmt , _ := repo .NewCurrencyValueFromBigInt (transactionTotal , txCurrency ).MulBigFloat (feePercent )
168
+ return feePercentAmt .AmountBigInt (), nil
169
+
172
170
case pb .Moderator_Fee_FIXED :
173
- modFeeCurrency , err := n . LookupCurrency (profile .ModeratorInfo .Fee .FixedFee .AmountCurrency . Code )
171
+ modFeeValue , err := repo . NewCurrencyValueFromProtobuf (profile .ModeratorInfo .Fee .FixedFee .BigAmount , profile . ModeratorInfo . Fee . FixedFee . AmountCurrency )
174
172
if err != nil {
175
- return big .NewInt (0 ), fmt .Errorf ("lookup moderator fee currency (%s): %s" , profile .ModeratorInfo .Fee .FixedFee .AmountCurrency .Code , err )
176
- }
177
- fixedFee , ok := new (big.Int ).SetString (profile .ModeratorInfo .Fee .FixedFee .BigAmount , 10 )
178
- if ! ok {
179
- return big .NewInt (0 ), errors .New ("invalid fixed fee amount" )
173
+ return big .NewInt (0 ), fmt .Errorf ("parse moderator fee currency: %s" , err )
180
174
}
181
- if modFeeCurrency .Equal (txCurrency ) {
182
- if fixedFee .Cmp (transactionTotal ) > 0 {
183
- return big .NewInt (0 ), errors .New ("fixed moderator fee exceeds transaction amount" )
184
- }
185
- return fixedFee , nil
186
- }
187
- amt , ok := new (big.Int ).SetString (profile .ModeratorInfo .Fee .FixedFee .BigAmount , 10 )
188
- if ! ok {
189
- return big .NewInt (0 ), errors .New ("invalid fixed fee amount" )
175
+
176
+ cc , err := n .ReserveCurrencyConverter ()
177
+ if err != nil {
178
+ return big .NewInt (0 ), fmt .Errorf ("preparing reserve currency converter: %s" , err .Error ())
190
179
}
191
- fee , err := n .getPriceInSatoshi (txCurrency .CurrencyCode ().String (), profile .ModeratorInfo .Fee .FixedFee .AmountCurrency .Code , amt )
180
+
181
+ convertedModFee , _ , err := modFeeValue .ConvertTo (txCurrency , cc )
192
182
if err != nil {
193
- return big .NewInt (0 ), err
194
- } else if fee .Cmp (transactionTotal ) > 0 {
183
+ return big .NewInt (0 ), fmt .Errorf ("convert moderator fee into transaction currency (%s): %s" , txCurrency .String (), err )
184
+ }
185
+ if convertedModFee .AmountBigInt ().Cmp (transactionTotal ) > 0 {
195
186
return big .NewInt (0 ), errors .New ("Fixed moderator fee exceeds transaction amount" )
196
187
}
197
- return fee , err
188
+ return convertedModFee . AmountBigInt (), nil
198
189
199
190
case pb .Moderator_Fee_FIXED_PLUS_PERCENTAGE :
200
- var fixed * big.Int
201
- var ok bool
202
- modFeeCurrency , err := n .LookupCurrency (profile .ModeratorInfo .Fee .FixedFee .AmountCurrency .Code )
191
+ modFeeValue , err := repo .NewCurrencyValueFromProtobuf (profile .ModeratorInfo .Fee .FixedFee .BigAmount , profile .ModeratorInfo .Fee .FixedFee .AmountCurrency )
203
192
if err != nil {
204
- return big .NewInt (0 ), fmt .Errorf ("lookup moderator fee currency (%s) : %s" , profile . ModeratorInfo . Fee . FixedFee . AmountCurrency . Code , err )
193
+ return big .NewInt (0 ), fmt .Errorf ("parse moderator fee currency: %s" , err )
205
194
}
206
- if modFeeCurrency .Equal (txCurrency ) {
207
- fixed , ok = new (big.Int ).SetString (profile .ModeratorInfo .Fee .FixedFee .BigAmount , 10 )
208
- if ! ok {
209
- return big .NewInt (0 ), errors .New ("invalid fixed fee amount" )
210
- }
211
- } else {
212
- f , ok := new (big.Int ).SetString (profile .ModeratorInfo .Fee .FixedFee .BigAmount , 10 )
213
- if ! ok {
214
- return big .NewInt (0 ), errors .New ("invalid fixed fee amount" )
215
- }
216
- f0 , err := n .getPriceInSatoshi (txCurrency .CurrencyCode ().String (), profile .ModeratorInfo .Fee .FixedFee .AmountCurrency .Code , f )
217
- if err != nil {
218
- return big .NewInt (0 ), err
219
- }
220
- fixed = f0
195
+
196
+ cc , err := n .ReserveCurrencyConverter ()
197
+ if err != nil {
198
+ return big .NewInt (0 ), fmt .Errorf ("preparing reserve currency converter: %s" , err .Error ())
221
199
}
222
- f := big .NewFloat (float64 (profile .ModeratorInfo .Fee .Percentage ))
223
- f .Mul (f , big .NewFloat (0.01 ))
224
- percentAmt , _ := new (big.Float ).Mul (t , f ).Int (nil )
225
- feeTotal := new (big.Int ).Add (fixed , percentAmt )
226
- if feeTotal .Cmp (transactionTotal ) > 0 {
200
+
201
+ convertedModFee , _ , err := modFeeValue .ConvertTo (txCurrency , cc )
202
+ if err != nil {
203
+ return big .NewInt (0 ), fmt .Errorf ("convert moderator fee into transaction currency (%s): %s" , txCurrency .String (), err )
204
+ }
205
+
206
+ feePercent := new (big.Float ).Mul (big .NewFloat (float64 (profile .ModeratorInfo .Fee .Percentage )), big .NewFloat (0.01 ))
207
+ feePercentAmt , _ := repo .NewCurrencyValueFromBigInt (transactionTotal , txCurrency ).MulBigFloat (feePercent )
208
+ feeTotal := feePercentAmt .AddBigInt (convertedModFee .AmountBigInt ())
209
+ if feeTotal .AmountBigInt ().Cmp (transactionTotal ) > 0 {
227
210
return big .NewInt (0 ), errors .New ("Fixed moderator fee exceeds transaction amount" )
228
211
}
229
- return feeTotal , nil
212
+ return feeTotal .AmountBigInt (), nil
213
+
230
214
default :
231
215
return big .NewInt (0 ), errors .New ("Unrecognized fee type" )
232
216
}
0 commit comments