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

Commit 0b0f8dc

Browse files
authored
Merge pull request #1758 from OpenBazaar/1757-big-sku-quantity
Update to use protobuf Listing.Item.Sku.BigQuantity
2 parents 9a7fe5d + 13ebe38 commit 0b0f8dc

File tree

13 files changed

+376
-331
lines changed

13 files changed

+376
-331
lines changed

api/jsonapi_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,12 @@ func TestListingsQuantity(t *testing.T) {
377377
"POST", "/ob/listing", jsonFor(t, listing), 200, `{"slug": "crypto"}`,
378378
})
379379

380-
listing.Item.Skus[0].Quantity = 0
380+
listing.Item.Skus[0].BigQuantity = "0"
381381
runAPITest(t, apiTest{
382382
"POST", "/ob/listing", jsonFor(t, listing), 200, anyResponseJSON,
383383
})
384384

385-
listing.Item.Skus[0].Quantity = -1
385+
listing.Item.Skus[0].BigQuantity = "-1"
386386
runAPITest(t, apiTest{
387387
"POST", "/ob/listing", jsonFor(t, listing), 200, anyResponseJSON,
388388
})
@@ -394,12 +394,12 @@ func TestCryptoListingsQuantity(t *testing.T) {
394394
"POST", "/ob/listing", jsonFor(t, listing), 200, `{"slug": "crypto"}`,
395395
})
396396

397-
listing.Item.Skus[0].Quantity = 0
397+
listing.Item.Skus[0].BigQuantity = "0"
398398
runAPITest(t, apiTest{
399399
"POST", "/ob/listing", jsonFor(t, listing), 500, errorResponseJSON(repo.ErrCryptocurrencySkuQuantityInvalid),
400400
})
401401

402-
listing.Item.Skus[0].Quantity = -1
402+
listing.Item.Skus[0].BigQuantity = "-1"
403403
runAPITest(t, apiTest{
404404
"POST", "/ob/listing", jsonFor(t, listing), 500, errorResponseJSON(repo.ErrCryptocurrencySkuQuantityInvalid),
405405
})
@@ -479,7 +479,7 @@ func TestMarketRatePrice(t *testing.T) {
479479
listing.Item.PriceCurrency = &pb.CurrencyDefinition{Code: "BTC", Divisibility: 8}
480480

481481
runAPITests(t, apiTests{
482-
{"POST", "/ob/listing", jsonFor(t, listing), 500, errorResponseJSON(repo.ErrMarketPriceListingIllegalField("item.price"))},
482+
{"POST", "/ob/listing", jsonFor(t, listing), 500, errorResponseJSON(repo.ErrMarketPriceListingIllegalField("item.bigPrice"))},
483483
})
484484
}
485485

core/listings.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,12 @@ func (n *OpenBazaarNode) extractListingData(listing *pb.SignedListing) (repo.Lis
281281
shipsTo = append(shipsTo, region.String())
282282
}
283283
for _, service := range shippingOption.Services {
284+
if service.BigPrice == "" {
285+
return repo.ListingIndexData{}, errors.New("expected shipping service price")
286+
}
284287
servicePrice, ok := new(big.Int).SetString(service.BigPrice, 10)
285288
if !ok {
286-
return repo.ListingIndexData{}, errors.New("invalid price amount")
289+
return repo.ListingIndexData{}, errors.New("invalid shipping service price amount")
287290
}
288291
if servicePrice.Cmp(big.NewInt(0)) == 0 && !contains(freeShipping, region.String()) {
289292
freeShipping = append(freeShipping, region.String())
@@ -293,14 +296,14 @@ func (n *OpenBazaarNode) extractListingData(listing *pb.SignedListing) (repo.Lis
293296
}
294297

295298
var priceValue *repo.CurrencyValue
296-
if listing.Listing.Item.PriceCurrency != nil {
299+
if listing.Listing.Item.PriceCurrency != nil && listing.Listing.Item.BigPrice != "" {
297300
defn, err := n.LookupCurrency(listing.Listing.Item.PriceCurrency.Code)
298301
if err != nil {
299302
return repo.ListingIndexData{}, errors.New("invalid pricing currency")
300303
}
301304
amt, ok := new(big.Int).SetString(listing.Listing.Item.BigPrice, 10)
302305
if !ok {
303-
return repo.ListingIndexData{}, errors.New("invalid price amount")
306+
return repo.ListingIndexData{}, errors.New("invalid item price amount")
304307
}
305308
priceValue = &repo.CurrencyValue{Currency: defn, Amount: amt}
306309
}
@@ -675,7 +678,7 @@ func (n *OpenBazaarNode) GetListingFromSlug(slug string) (*pb.SignedListing, err
675678
for variant, count := range inventory {
676679
for i, s := range sl.Listing.Item.Skus {
677680
if variant == i {
678-
s.Quantity = count
681+
s.BigQuantity = fmt.Sprintf("%d", count)
679682
break
680683
}
681684
}

core/signed_listings.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core
22

33
import (
4+
"fmt"
45
"io/ioutil"
56

67
"github.com/OpenBazaar/jsonpb"
@@ -42,7 +43,7 @@ func AssignMatchingQuantities(inventory map[int]int64, sl *pb.SignedListing) err
4243
for variant, count := range inventory {
4344
for i, s := range sl.Listing.Item.Skus {
4445
if variant == i {
45-
s.Quantity = count
46+
s.BigQuantity = fmt.Sprintf("%d", count)
4647
break
4748
}
4849
}

core/signed_listings_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestOpenBazaarSignedListings_AssignMatchingQuantities(t *testing.T) {
9090
t.Error(err)
9191
}
9292

93-
if listing.Listing.Item.Skus[0].Quantity != 1000 {
93+
if listing.Listing.Item.Skus[0].BigQuantity != "1000" {
9494
t.Error("Inventory was not set properly")
9595
}
9696
}

pb/contracts.pb.go

Lines changed: 294 additions & 285 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pb/protos/contracts.proto

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ message Listing {
9696
repeated uint32 variantCombo = 1;
9797
string productID = 2;
9898
int64 surcharge = 3 [deprecated = true];
99-
int64 quantity = 4; // Not saved with listing
99+
int64 quantity = 4 [deprecated = true]; // prefer bigQuantity
100100
string bigSurcharge = 5; // added schema v5
101+
string bigQuantity = 6; // added schema v5
101102
}
102103

103104
message Image {

qa/manage_crypto_listings.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ def run_test(self):
3030
with open('testdata/listing_crypto.json') as listing_file:
3131
listing_json = json.load(listing_file, object_pairs_hook=OrderedDict)
3232

33-
listing_json["item"]["priceCurrency"]["code"] = "t" + self.cointype
34-
listing_json["metadata"]["acceptedCurrencies"] = ["t" + self.cointype]
33+
listing_json["coinType"] = "TETH"
3534
api_url = vendor["gateway_url"] + "ob/listing"
3635
r = requests.post(api_url, data=json.dumps(listing_json, indent=4))
3736
if r.status_code != 200:
@@ -47,8 +46,8 @@ def run_test(self):
4746
for listing in resp:
4847
if listing['contractType'] == 'CRYPTOCURRENCY':
4948
print(listing)
50-
if listing["coinType"] != "t" + self.cointype:
51-
raise TestFailure("ManageCryptoListingsTest - FAIL: coinType incorrect: %s", listing["item"]["priceCurrency"]["code"])
49+
if listing["cryptoCurrencyCode"] != "TETH":
50+
raise TestFailure("ManageCryptoListingsTest - FAIL: cryptoCurrencyCode incorrect: %s", listing["cryptoCurrencyCode"])
5251

5352
# delete listing
5453
api_url = vendor["gateway_url"] + "ob/listing/"+slug

qa/runtests.sh

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
#!/bin/bash
2+
# $1 is the openbazaar binary path
3+
# $2 is the bitcoind path
4+
# $3 will filter to match against script name
25
for SCRIPT in `ls | grep -v "eth_"`
36
do
47
b=$(basename $SCRIPT)
58
extension="${b##*.}"
69
p="py"
7-
if [ $extension = $p ]
8-
then
9-
python3 $SCRIPT -b $1 -d $2 $3
10-
### echo $SCRIPT
10+
if [[ $extension = $p ]]; then
11+
if [[ -z $3 ]]; then
12+
echo "python3 $SCRIPT -b $1 -d $2"
13+
python3 $SCRIPT -b $1 -d $2
14+
else
15+
# filter only the scripts of interest
16+
if [[ $SCRIPT == *"$3"* ]]; then
17+
echo "python3 $SCRIPT -b $1 -d $2"
18+
python3 $SCRIPT -b $1 -d $2
19+
fi
20+
fi
1121
fi
1222
done

qa/testdata/eth_listing_crypto.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"contractType": "CRYPTOCURRENCY",
66
"format": "MARKET_PRICE",
77
"expiry": "2030-08-17T04:52:19.000Z",
8-
"acceptedCurrencies": [],
8+
"acceptedCurrencies": ["TBTC"],
99
"escrowTimeoutHours": 1080,
1010
"coinType": "TETH",
1111
"coinDivisibility": 18
@@ -25,10 +25,7 @@
2525
}],
2626
"grams": 0,
2727
"options": [],
28-
"skus": [{
29-
"bigSurcharge": "0",
30-
"quantity": 350000000
31-
}]
28+
"skus": [{"bigQuantity":"350000000"}]
3229
},
3330
"moderators": [],
3431
"termsAndConditions": "NA",

qa/testdata/listing_crypto.json

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,16 @@
55
"contractType": "CRYPTOCURRENCY",
66
"format": "MARKET_PRICE",
77
"expiry": "2030-08-17T04:52:19.000Z",
8-
"acceptedCurrencies": [],
8+
"acceptedCurrencies": ["TBTC"],
99
"escrowTimeoutHours": 1080,
10-
"coinType": "ETH",
10+
"coinType": "TETH",
1111
"coinDivisibility": 18
1212
},
1313
"item": {
1414
"title": "Ether",
1515
"description": "",
1616
"processingTime": "1 to 2 hours",
1717
"price": "0",
18-
"priceCurrency": {
19-
"code": "TBTC",
20-
"divisibility": 8,
21-
"name": "A",
22-
"currencyType": "A"
23-
},
2418
"images": [{
2519
"tiny": "QmUAuYuiafnJRZxDDX7MuruMNsicYNuyub5vUeAcupUBNs",
2620
"small": "QmXSEqXLCzpCByJU4wqbJ37TcBEj77FKMUWUP1qLh56847",
@@ -32,8 +26,7 @@
3226
"grams": 0,
3327
"options": [],
3428
"skus": [{
35-
"bigSurcharge": "0",
36-
"quantity": 350000000
29+
"bigQuantity": "350000000"
3730
}]
3831
},
3932
"moderators": [],

0 commit comments

Comments
 (0)