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

Commit 84a572a

Browse files
committed
[#1983] Implement CurrencyValue.ConvertUsingProtobufDef
1 parent d97a689 commit 84a572a

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

core/order.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ func (n *OpenBazaarNode) CalculateOrderTotal(contract *pb.RicardianContract) (*b
10191019
return big.NewInt(0), fmt.Errorf("preparing reserve currency converter: %s", err.Error())
10201020
}
10211021

1022-
finalItemAmount, err := itemOriginAmt.ConvertUsingProtobufDef(v5Order.Payment.AmountCurrency, cc)
1022+
finalItemAmount, _, err := itemOriginAmt.ConvertUsingProtobufDef(v5Order.Payment.AmountCurrency, cc)
10231023
if err != nil {
10241024
return big.NewInt(0), err
10251025
}
@@ -1104,7 +1104,7 @@ func (n *OpenBazaarNode) calculateShippingTotalForListings(contract *pb.Ricardia
11041104
if err != nil {
11051105
return big.NewInt(0), fmt.Errorf("parsing service price (%v): %s", service.Name, err.Error())
11061106
}
1107-
convertedShippingPrice, err := servicePrice.ConvertUsingProtobufDef(v5Order.Payment.AmountCurrency, cc)
1107+
convertedShippingPrice, _, err := servicePrice.ConvertUsingProtobufDef(v5Order.Payment.AmountCurrency, cc)
11081108
if err != nil {
11091109
return big.NewInt(0), fmt.Errorf("converting service price (%s): %s", service.Name, err.Error())
11101110
}
@@ -1115,7 +1115,7 @@ func (n *OpenBazaarNode) calculateShippingTotalForListings(contract *pb.Ricardia
11151115
}
11161116
var convertedAuxPrice = repo.NewCurrencyValueFromBigInt(big.NewInt(0), convertedShippingPrice.Currency)
11171117
if auxServicePrice.IsPositive() {
1118-
finalAux, err := auxServicePrice.ConvertUsingProtobufDef(v5Order.Payment.AmountCurrency, cc)
1118+
finalAux, _, err := auxServicePrice.ConvertUsingProtobufDef(v5Order.Payment.AmountCurrency, cc)
11191119
if err != nil {
11201120
return big.NewInt(0), fmt.Errorf("converting aux service price (%s): %s", service.Name, err.Error())
11211121
}

repo/currency.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,14 @@ func (c *CurrencyValue) ConvertTo(final CurrencyDefinition, reserveConverter *Cu
243243
// target currency. If the divisibility provided by the pb.CurrencyDefinition is different
244244
// than the one provided for the exchange rate, the converted amount will be adjusted to
245245
// match the provided divisibility.
246-
func (c *CurrencyValue) ConvertUsingProtobufDef(convertTo *pb.CurrencyDefinition, reserve *CurrencyConverter) (*CurrencyValue, error) {
247-
return c, nil
246+
func (c *CurrencyValue) ConvertUsingProtobufDef(convertTo *pb.CurrencyDefinition, reserve *CurrencyConverter) (*CurrencyValue, big.Accuracy, error) {
247+
var repoCurrencyDef CurrencyDefinition
248+
if c, err := AllCurrencies().Lookup(convertTo.Code); err != nil {
249+
repoCurrencyDef = c
250+
} else {
251+
repoCurrencyDef = NewUnknownCryptoDefinition(convertTo.Code, uint(convertTo.Divisibility))
252+
}
253+
return c.ConvertTo(repoCurrencyDef, reserve)
248254
}
249255

250256
// Cmp exposes the (*big.Int).Cmp behavior after verifying currency and adjusting

repo/currency_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66
"testing"
77

8+
"github.com/OpenBazaar/openbazaar-go/pb"
89
"github.com/OpenBazaar/openbazaar-go/repo"
910
"github.com/OpenBazaar/openbazaar-go/test/factory"
1011
)
@@ -456,6 +457,43 @@ func TestCurrencyValuesConvertCorrectly(t *testing.T) {
456457
}
457458
}
458459

460+
func TestConvertUsingProtobufDef(t *testing.T) {
461+
subject := factory.MustNewCurrencyValue("10", "USD")
462+
subject.Currency.Divisibility = 2
463+
conv, err := factory.NewCurrencyConverter("BTC", map[string]float64{
464+
"USD": 2,
465+
"BCH": 0.5,
466+
})
467+
if err != nil {
468+
t.Fatal(err)
469+
}
470+
convertTo := &pb.CurrencyDefinition{
471+
Code: "TBCH",
472+
Divisibility: 8,
473+
}
474+
expected := &repo.CurrencyValue{
475+
Amount: big.NewInt(2500000), // 10 * (1/2 BTC/USD) * (1/2 BCH/BTC) * 1000000 (divisibility)
476+
Currency: repo.CurrencyDefinition{
477+
Code: "TBCH",
478+
CurrencyType: repo.Crypto,
479+
Divisibility: 8,
480+
},
481+
}
482+
483+
cv, acc, err := subject.ConvertUsingProtobufDef(convertTo, conv)
484+
if err != nil {
485+
t.Fatal(err)
486+
}
487+
488+
if acc != big.Exact {
489+
t.Errorf("expected result to be exact, but was (%s)", acc.String())
490+
}
491+
492+
if !cv.Equal(expected) {
493+
t.Errorf("expected result amount to be (%v), but was (%v)", expected, cv)
494+
}
495+
}
496+
459497
func TestNewCurrencyValueWithLookup(t *testing.T) {
460498
_, err := repo.NewCurrencyValueWithLookup("0", "")
461499
if err == nil {

0 commit comments

Comments
 (0)