Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 9fdc036

Browse files
authored
Merge pull request #1990 from OpenBazaar/refactor-price-conversion
(*repo.CurrencyValue).ConvertTo accepts a *repo.CurrencyConverter
2 parents f5536bb + 8700a76 commit 9fdc036

File tree

10 files changed

+646
-398
lines changed

10 files changed

+646
-398
lines changed

cmd/start.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,12 @@ func (x *Start) Execute(args []string) error {
586586
}
587587
core.Node.PublishLock.Lock()
588588

589+
// assert reserve wallet is available on startup for later usage
590+
_, err = core.Node.ReserveCurrencyConverter()
591+
if err != nil {
592+
return fmt.Errorf("verifying reserve currency converter: %s", err.Error())
593+
}
594+
589595
// Offline messaging storage
590596
var storage sto.OfflineMessagingStorage
591597
if x.Storage == "self-hosted" || x.Storage == "" {

core/moderation.go

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -161,72 +161,56 @@ func (n *OpenBazaarNode) GetModeratorFee(transactionTotal *big.Int, txCurrencyCo
161161
if err != nil {
162162
return big.NewInt(0), fmt.Errorf("lookup dispute transaction currency (%s): %s", txCurrencyCode, err)
163163
}
164-
t := new(big.Float).SetInt(transactionTotal)
165164
switch profile.ModeratorInfo.Fee.FeeType {
166165
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+
172170
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)
174172
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)
180174
}
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())
190179
}
191-
fee, err := n.getPriceInSatoshi(txCurrency.CurrencyCode().String(), profile.ModeratorInfo.Fee.FixedFee.AmountCurrency.Code, amt)
180+
181+
convertedModFee, _, err := modFeeValue.ConvertTo(txCurrency, cc)
192182
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 {
195186
return big.NewInt(0), errors.New("Fixed moderator fee exceeds transaction amount")
196187
}
197-
return fee, err
188+
return convertedModFee.AmountBigInt(), nil
198189

199190
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)
203192
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)
205194
}
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())
221199
}
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 {
227210
return big.NewInt(0), errors.New("Fixed moderator fee exceeds transaction amount")
228211
}
229-
return feeTotal, nil
212+
return feeTotal.AmountBigInt(), nil
213+
230214
default:
231215
return big.NewInt(0), errors.New("Unrecognized fee type")
232216
}

0 commit comments

Comments
 (0)