This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
/
buyer_order.go
56 lines (46 loc) · 1.59 KB
/
buyer_order.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package repo
import (
"math/big"
"github.com/OpenBazaar/openbazaar-go/pb"
"github.com/golang/protobuf/proto"
)
// ToV5Order scans through the order looking for any deprecated fields and turns them into their v5 counterpart.
func ToV5Order(order *pb.Order, lookupFunc func(currencyCode string) (CurrencyDefinition, error)) (*pb.Order, error) {
if lookupFunc == nil {
lookupFunc = AllCurrencies().Lookup
}
newOrder := proto.Clone(order).(*pb.Order)
if order.RefundFee != 0 && order.BigRefundFee == "" {
newOrder.BigRefundFee = big.NewInt(int64(order.RefundFee)).String()
newOrder.RefundFee = 0
}
for i, item := range order.Items {
if item.Quantity != 0 && item.BigQuantity == "" {
newOrder.Items[i].BigQuantity = big.NewInt(int64(item.Quantity)).String()
newOrder.Items[i].Quantity = 0
}
if item.Quantity64 != 0 && item.BigQuantity == "" {
newOrder.Items[i].BigQuantity = big.NewInt(int64(item.Quantity64)).String()
newOrder.Items[i].Quantity64 = 0
}
}
if order.Payment.Amount != 0 && order.Payment.BigAmount == "" {
newOrder.Payment.BigAmount = big.NewInt(int64(order.Payment.Amount)).String()
newOrder.Payment.Amount = 0
}
if order.Payment.AmountCurrency == nil && order.Payment.Coin == "" {
order.Payment.Coin = "BTC"
}
if order.Payment.Coin != "" && order.Payment.AmountCurrency == nil {
def, err := lookupFunc(order.Payment.Coin)
if err != nil {
return nil, err
}
newOrder.Payment.AmountCurrency = &pb.CurrencyDefinition{
Code: def.Code.String(),
Divisibility: uint32(def.Divisibility),
}
newOrder.Payment.Coin = ""
}
return newOrder, nil
}