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

Commit 99dd7a5

Browse files
committed
[#1736] Express repo.Listing.CryptoDivisibility as exponential value
1 parent 15b9c4e commit 99dd7a5

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

repo/listing.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,51 @@ func ExtractIDFromListing(data []byte) (*pb.ID, error) {
577577
return vendorPlay, nil
578578
}
579579

580+
// GetCryptoDivisibility returns the listing crypto divisibility
581+
func (l *Listing) GetCryptoDivisibility() uint32 {
582+
ct, err := l.GetContractType()
583+
if err == nil && ct != pb.Listing_Metadata_CRYPTOCURRENCY.String() {
584+
return 0
585+
}
586+
div := parseProtoCryptoDivisibility(l.ListingBytes)
587+
switch l.ListingVersion {
588+
case 5:
589+
return div
590+
default: // version <4
591+
if div != 0 {
592+
return uint32(math.Log10(float64(div)))
593+
}
594+
}
595+
return 0
596+
}
597+
598+
func parseProtoCryptoDivisibility(listing []byte) uint32 {
599+
var listingT struct {
600+
Metadata struct {
601+
CryptoDivisibility uint32 `json:"coinDivisibility"`
602+
} `json:"metadata"`
603+
}
604+
err := json.Unmarshal(listing, &listingT)
605+
if err != nil {
606+
return 0
607+
}
608+
return listingT.Metadata.CryptoDivisibility
609+
}
610+
611+
// GetCryptoCurrencyCode returns the listing crypto currency code
612+
func (l *Listing) GetCryptoCurrencyCode() string {
613+
var listingT struct {
614+
Metadata struct {
615+
CryptoCurrencyCode string `json:"coinType"`
616+
} `json:"metadata"`
617+
}
618+
err := json.Unmarshal(l.ListingBytes, &listingT)
619+
if err != nil {
620+
return ""
621+
}
622+
return listingT.Metadata.CryptoCurrencyCode
623+
}
624+
580625
// GetTitle - return listing title
581626
func (l *Listing) GetTitle() (string, error) {
582627
type title struct {
@@ -1301,6 +1346,8 @@ func (l *Listing) GetMetadata() (*pb.Listing_Metadata, error) {
13011346
Language: lang,
13021347
EscrowTimeoutHours: l.GetEscrowTimeout(),
13031348
PriceModifier: priceMod,
1349+
CryptoDivisibility: parseProtoCryptoDivisibility(l.ListingBytes),
1350+
CryptoCurrencyCode: l.GetCryptoCurrencyCode(),
13041351
}
13051352
return &m, nil
13061353
}

repo/listing_test.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func TestListingAttributes(t *testing.T) {
3838
expectedSlug string
3939
expectedPrice *repo.CurrencyValue
4040
expectedAcceptedCurrencies []string
41+
expectedCryptoDivisibility uint32
42+
expectedCryptoCurrencyCode string
4143
}{
4244
{
4345
fixtureName: "v3-physical-good",
@@ -53,6 +55,8 @@ func TestListingAttributes(t *testing.T) {
5355
},
5456
},
5557
expectedAcceptedCurrencies: []string{"BCH"},
58+
expectedCryptoDivisibility: 0,
59+
expectedCryptoCurrencyCode: "",
5660
},
5761
{
5862
fixtureName: "v4-physical-good",
@@ -68,6 +72,8 @@ func TestListingAttributes(t *testing.T) {
6872
},
6973
},
7074
expectedAcceptedCurrencies: []string{"ZEC", "LTC", "BTC", "BCH"},
75+
expectedCryptoDivisibility: 0,
76+
expectedCryptoCurrencyCode: "",
7177
},
7278
{
7379
fixtureName: "v4-digital-good",
@@ -83,6 +89,8 @@ func TestListingAttributes(t *testing.T) {
8389
},
8490
},
8591
expectedAcceptedCurrencies: []string{"ZEC"},
92+
expectedCryptoDivisibility: 0,
93+
expectedCryptoCurrencyCode: "",
8694
},
8795
{
8896
fixtureName: "v4-service",
@@ -98,6 +106,8 @@ func TestListingAttributes(t *testing.T) {
98106
},
99107
},
100108
expectedAcceptedCurrencies: []string{"ZEC", "LTC", "BCH", "BTC"},
109+
expectedCryptoDivisibility: 0,
110+
expectedCryptoCurrencyCode: "",
101111
},
102112
{
103113
fixtureName: "v4-cryptocurrency",
@@ -106,6 +116,8 @@ func TestListingAttributes(t *testing.T) {
106116
expectedSlug: "ltc-xmr",
107117
expectedPrice: nil,
108118
expectedAcceptedCurrencies: []string{"LTC"},
119+
expectedCryptoDivisibility: 8,
120+
expectedCryptoCurrencyCode: "XMR",
109121
},
110122
{
111123
fixtureName: "v5-physical-good",
@@ -121,10 +133,13 @@ func TestListingAttributes(t *testing.T) {
121133
},
122134
},
123135
expectedAcceptedCurrencies: []string{"BTC", "BCH", "ZEC", "LTC", "ETH"},
136+
expectedCryptoDivisibility: 0,
137+
expectedCryptoCurrencyCode: "",
124138
},
125139
}
126140

127141
for _, e := range examples {
142+
t.Logf("example listing (%s)", e.fixtureName)
128143
var (
129144
fixtureBytes = factory.MustLoadListingFixture(e.fixtureName)
130145
l, err = repo.UnmarshalJSONListing(fixtureBytes)
@@ -134,19 +149,25 @@ func TestListingAttributes(t *testing.T) {
134149
continue
135150
}
136151
if l.Metadata.Version != e.expectedResponse {
137-
t.Errorf("expected example (%s) to have version response (%+v), but instead was (%+v)", e.fixtureName, e.expectedResponse, l.Metadata.Version)
152+
t.Errorf("expected to have version response (%+v), but instead was (%+v)", e.expectedResponse, l.Metadata.Version)
138153
}
139154
if title, _ := l.GetTitle(); title != e.expectedTitle {
140-
t.Errorf("expected example (%s) to have title response (%+v), but instead was (%+v)", e.fixtureName, e.expectedTitle, title)
155+
t.Errorf("expected to have title response (%+v), but instead was (%+v)", e.expectedTitle, title)
141156
}
142157
if slug, _ := l.GetSlug(); slug != e.expectedSlug {
143-
t.Errorf("expected example (%s) to have slug response (%+v), but instead was (%+v)", e.fixtureName, e.expectedSlug, slug)
158+
t.Errorf("expected to have slug response (%+v), but instead was (%+v)", e.expectedSlug, slug)
144159
}
145160
if price, _ := l.GetPrice(); !price.Equal(e.expectedPrice) {
146-
t.Errorf("expected example (%s) to have price response (%+v), but instead was (%+v)", e.fixtureName, e.expectedPrice, price)
161+
t.Errorf("expected to have price response (%+v), but instead was (%+v)", e.expectedPrice, price)
147162
}
148163
if acceptedCurrencies, _ := l.GetAcceptedCurrencies(); len(acceptedCurrencies) != len(e.expectedAcceptedCurrencies) {
149-
t.Errorf("expected example (%s) to have acceptedCurrencies response (%+v), but instead was (%+v)", e.fixtureName, e.expectedAcceptedCurrencies, acceptedCurrencies)
164+
t.Errorf("expected to have acceptedCurrencies response (%+v), but instead was (%+v)", e.expectedAcceptedCurrencies, acceptedCurrencies)
165+
}
166+
if actual := l.GetCryptoDivisibility(); actual != e.expectedCryptoDivisibility {
167+
t.Errorf("expected to have divisibility (%d), but was (%d)", e.expectedCryptoDivisibility, actual)
168+
}
169+
if actual := l.GetCryptoCurrencyCode(); actual != e.expectedCryptoCurrencyCode {
170+
t.Errorf("expected to have currency code (%s), but was (%s)", e.expectedCryptoCurrencyCode, actual)
150171
}
151172
}
152173
}

0 commit comments

Comments
 (0)