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

Commit ad05c9f

Browse files
authored
Merge pull request #2064 from OpenBazaar/brian.fixoldcontracts
Handle legacy contracts
2 parents 86ec0be + d37d25e commit ad05c9f

File tree

4 files changed

+85
-31
lines changed

4 files changed

+85
-31
lines changed

api/jsonapi.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,14 +1621,22 @@ func (i *jsonAPIHandler) POSTOrderConfirmation(w http.ResponseWriter, r *http.Re
16211621
ErrorResponse(w, http.StatusBadRequest, err.Error())
16221622
return
16231623
}
1624+
1625+
order, err := i.node.GetOrder(conf.OrderID)
1626+
if err != nil {
1627+
ErrorResponse(w, http.StatusInternalServerError, err.Error())
1628+
return
1629+
}
1630+
v5contract := order.Contract
1631+
16241632
contract, state, funded, records, _, _, err := i.node.Datastore.Sales().GetByOrderId(conf.OrderID)
16251633
if err != nil {
16261634
ErrorResponse(w, http.StatusNotFound, err.Error())
16271635
return
16281636
}
16291637

16301638
// TODO: Remove once broken contracts are migrated
1631-
lookupCoin := contract.BuyerOrder.Payment.AmountCurrency.Code
1639+
lookupCoin := v5contract.BuyerOrder.Payment.AmountCurrency.Code
16321640
_, err = i.node.LookupCurrency(lookupCoin)
16331641
if err != nil {
16341642
log.Warningf("invalid BuyerOrder.Payment.Coin (%s) on order (%s)", lookupCoin, conf.OrderID)
@@ -1776,7 +1784,7 @@ func (i *jsonAPIHandler) POSTRefund(w http.ResponseWriter, r *http.Request) {
17761784
ErrorResponse(w, http.StatusBadRequest, err.Error())
17771785
return
17781786
}
1779-
contract, state, _, records, _, _, err := i.node.Datastore.Sales().GetByOrderId(can.OrderID)
1787+
_, state, _, records, _, _, err := i.node.Datastore.Sales().GetByOrderId(can.OrderID)
17801788
if err != nil {
17811789
ErrorResponse(w, http.StatusNotFound, "order not found")
17821790
return
@@ -1787,14 +1795,21 @@ func (i *jsonAPIHandler) POSTRefund(w http.ResponseWriter, r *http.Request) {
17871795
}
17881796

17891797
// TODO: Remove once broken contracts are migrated
1790-
lookupCoin := contract.BuyerOrder.Payment.AmountCurrency.Code
1798+
order, err := i.node.GetOrder(can.OrderID)
1799+
if err != nil {
1800+
ErrorResponse(w, http.StatusInternalServerError, err.Error())
1801+
return
1802+
}
1803+
v5contract := order.Contract
1804+
1805+
lookupCoin := v5contract.BuyerOrder.Payment.AmountCurrency.Code
17911806
_, err = i.node.LookupCurrency(lookupCoin)
17921807
if err != nil {
17931808
log.Warningf("invalid BuyerOrder.Payment.Coin (%s) on order (%s)", lookupCoin, can.OrderID)
17941809
//contract.BuyerOrder.Payment.Coin = paymentCoin.String()
17951810
}
17961811

1797-
err = i.node.RefundOrder(contract, records)
1812+
err = i.node.RefundOrder(v5contract, records)
17981813
if err != nil {
17991814
ErrorResponse(w, http.StatusInternalServerError, err.Error())
18001815
return
@@ -1980,14 +1995,21 @@ func (i *jsonAPIHandler) POSTOrderFulfill(w http.ResponseWriter, r *http.Request
19801995
ErrorResponse(w, http.StatusBadRequest, err.Error())
19811996
return
19821997
}
1983-
contract, state, _, records, _, _, err := i.node.Datastore.Sales().GetByOrderId(fulfill.OrderId)
1998+
_, state, _, records, _, _, err := i.node.Datastore.Sales().GetByOrderId(fulfill.OrderId)
19841999
if err != nil {
19852000
ErrorResponse(w, http.StatusNotFound, "order not found")
19862001
return
19872002
}
19882003

19892004
// TODO: Remove once broken contracts are migrated
1990-
lookupCoin := contract.BuyerOrder.Payment.AmountCurrency.Code
2005+
order, err := i.node.GetOrder(fulfill.OrderId)
2006+
if err != nil {
2007+
ErrorResponse(w, http.StatusNotFound, "order not found")
2008+
return
2009+
}
2010+
v5contract := order.Contract
2011+
2012+
lookupCoin := v5contract.BuyerOrder.Payment.AmountCurrency.Code
19912013
_, err = i.node.LookupCurrency(lookupCoin)
19922014
if err != nil {
19932015
log.Warningf("invalid BuyerOrder.Payment.Coin (%s) on order (%s)", lookupCoin, fulfill.OrderId)
@@ -1998,7 +2020,7 @@ func (i *jsonAPIHandler) POSTOrderFulfill(w http.ResponseWriter, r *http.Request
19982020
ErrorResponse(w, http.StatusBadRequest, "order must be in state AWAITING_FULFILLMENT or PARTIALLY_FULFILLED to fulfill")
19992021
return
20002022
}
2001-
err = i.node.FulfillOrder(&fulfill, contract, records)
2023+
err = i.node.FulfillOrder(&fulfill, v5contract, records)
20022024
if err != nil {
20032025
ErrorResponse(w, http.StatusInternalServerError, err.Error())
20042026
return
@@ -2211,6 +2233,16 @@ func (i *jsonAPIHandler) GETCase(w http.ResponseWriter, r *http.Request) {
22112233
ErrorResponse(w, http.StatusInternalServerError, err.Error())
22122234
return
22132235
}
2236+
2237+
if buyerContract.BuyerOrder.Payment.BigAmount == "" {
2238+
v5order, err := repo.ToV5Order(buyerContract.BuyerOrder, nil)
2239+
if err != nil {
2240+
ErrorResponse(w, http.StatusInternalServerError, err.Error())
2241+
return
2242+
}
2243+
buyerContract.BuyerOrder = v5order
2244+
}
2245+
22142246
resp.BuyerContract = buyerContract
22152247
resp.VendorContract = vendorContract
22162248
resp.BuyerOpened = buyerOpened
@@ -2326,7 +2358,13 @@ func (i *jsonAPIHandler) POSTReleaseEscrow(w http.ResponseWriter, r *http.Reques
23262358
}
23272359

23282360
// TODO: Remove once broken contracts are migrated
2329-
lookupCoin := contract.BuyerOrder.Payment.AmountCurrency.Code
2361+
order, err := i.node.GetOrder(rel.OrderID)
2362+
if err != nil {
2363+
ErrorResponse(w, http.StatusBadRequest, "Could not retrieve the order")
2364+
return
2365+
}
2366+
2367+
lookupCoin := order.Contract.BuyerOrder.Payment.AmountCurrency.Code
23302368
_, err = i.node.LookupCurrency(lookupCoin)
23312369
if err != nil {
23322370
log.Warningf("invalid BuyerOrder.Payment.Coin (%s) on order (%s)", lookupCoin, rel.OrderID)

core/confirmation.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
"encoding/hex"
66
"errors"
77
"fmt"
8-
"github.com/OpenBazaar/openbazaar-go/repo"
98
"math/big"
109
"strings"
1110
"time"
1211

12+
"github.com/OpenBazaar/openbazaar-go/repo"
13+
1314
crypto "gx/ipfs/QmTW4SdgBWq9GjsBsHeUx8WuGxzhgzAf88UMH2w62PC8yK/go-libp2p-crypto"
1415

1516
"github.com/OpenBazaar/wallet-interface"
@@ -103,7 +104,13 @@ func (n *OpenBazaarNode) ConfirmOfflineOrder(oldState pb.OrderState, contract *p
103104
if err != nil {
104105
return err
105106
}
106-
wal, err := n.Multiwallet.WalletForCurrencyCode(confirmedContract.BuyerOrder.Payment.AmountCurrency.Code)
107+
108+
order, err := repo.ToV5Order(contract.BuyerOrder, n.LookupCurrency)
109+
if err != nil {
110+
return err
111+
}
112+
113+
wal, err := n.Multiwallet.WalletForCurrencyCode(order.Payment.AmountCurrency.Code)
107114
if err != nil {
108115
return err
109116
}

core/order.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,36 @@ func (n *OpenBazaarNode) GetOrder(orderID string) (*pb.OrderRespApi, error) {
6262
isSale = true
6363
}
6464

65-
for i, l := range contract.VendorListings {
66-
repoListing, err := repo.NewListingFromProtobuf(l)
67-
if err != nil {
68-
log.Errorf("failed getting contract listing: %s", err.Error())
69-
return nil, err
70-
}
71-
normalizedListing, err := repoListing.Normalize()
72-
if err != nil {
73-
log.Errorf("failed converting contract listing to v5 schema: %s", err.Error())
74-
return nil, err
75-
}
76-
contract.VendorListings[i] = normalizedListing.GetProtobuf()
77-
}
78-
7965
resp := new(pb.OrderRespApi)
8066
resp.Contract = contract
8167
resp.Funded = funded
8268
resp.Read = read
8369
resp.State = state
8470

85-
v5Order, err := repo.ToV5Order(contract.BuyerOrder, n.LookupCurrency)
86-
if err != nil {
87-
log.Errorf("failed converting contract buyer order to v5 schema: %s", err.Error())
88-
return nil, err
71+
if contract.BuyerOrder.Payment.AmountCurrency != nil {
72+
resp.Contract.BuyerOrder = contract.BuyerOrder
73+
} else {
74+
for i, l := range contract.VendorListings {
75+
repoListing, err := repo.NewListingFromProtobuf(l)
76+
if err != nil {
77+
log.Errorf("failed getting contract listing: %s", err.Error())
78+
return nil, err
79+
}
80+
normalizedListing, err := repoListing.Normalize()
81+
if err != nil {
82+
log.Errorf("failed converting contract listing to v5 schema: %s", err.Error())
83+
return nil, err
84+
}
85+
contract.VendorListings[i] = normalizedListing.GetProtobuf()
86+
}
87+
88+
v5Order, err := repo.ToV5Order(contract.BuyerOrder, n.LookupCurrency)
89+
if err != nil {
90+
log.Errorf("failed converting contract buyer order to v5 schema: %s", err.Error())
91+
return nil, err
92+
}
93+
resp.Contract.BuyerOrder = v5Order
8994
}
90-
resp.Contract.BuyerOrder = v5Order
9195

9296
paymentTxs, refundTx, err := n.BuildTransactionRecords(contract, records, state)
9397
if err != nil {

repo/db/cases.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,13 @@ func (c *CasesDB) GetAll(stateFilter []pb.OrderState, searchTerm string, sortByA
282282
buyerHandle = contract.BuyerOrder.BuyerID.Handle
283283
}
284284
if contract.BuyerOrder.Payment != nil {
285-
total0, _ := new(big.Int).SetString(contract.BuyerOrder.Payment.BigAmount, 10)
286-
total = total0
285+
if contract.BuyerOrder.Payment.BigAmount != "" {
286+
total0, _ := new(big.Int).SetString(contract.BuyerOrder.Payment.BigAmount, 10)
287+
total = total0
288+
} else {
289+
total1 := new(big.Int).SetUint64(contract.BuyerOrder.Payment.Amount)
290+
total = total1
291+
}
287292
}
288293
}
289294

0 commit comments

Comments
 (0)