This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 284
/
spend.go
153 lines (134 loc) · 4.1 KB
/
spend.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package core
import (
"errors"
"fmt"
"strings"
"time"
"github.com/OpenBazaar/openbazaar-go/pb"
"github.com/OpenBazaar/openbazaar-go/repo"
"github.com/OpenBazaar/wallet-interface"
"github.com/btcsuite/btcutil"
)
// DefaultCurrencyDivisibility is the Divisibility of the Currency if not
// defined otherwise
const DefaultCurrencyDivisibility uint32 = 1e8
type SpendRequest struct {
decodedAddress btcutil.Address
Address string `json:"address"`
Amount int64 `json:"amount"`
FeeLevel string `json:"feeLevel"`
Memo string `json:"memo"`
OrderID string `json:"orderId"`
RequireAssociatedOrder bool `json:"requireOrder"`
Wallet string `json:"wallet"`
SpendAll bool `json:"spendAll"`
}
type SpendResponse struct {
Amount int64 `json:"amount"`
ConfirmedBalance int64 `json:"confirmedBalance"`
Memo string `json:"memo"`
OrderID string `json:"orderId"`
Timestamp time.Time `json:"timestamp"`
Txid string `json:"txid"`
UnconfirmedBalance int64 `json:"unconfirmedBalance"`
PeerID string `json:"-"`
}
// Spend will attempt to move funds from the node to the destination address described in the
// SpendRequest for the amount indicated.
func (n *OpenBazaarNode) Spend(args *SpendRequest) (*SpendResponse, error) {
var feeLevel wallet.FeeLevel
wal, err := n.Multiwallet.WalletForCurrencyCode(args.Wallet)
if err != nil {
return nil, ErrUnknownWallet
}
addr, err := wal.DecodeAddress(args.Address)
if err != nil {
return nil, ErrInvalidSpendAddress
}
args.decodedAddress = addr
contract, err := n.getOrderContractBySpendRequest(args)
if err != nil && args.RequireAssociatedOrder {
return nil, ErrOrderNotFound
}
switch strings.ToUpper(args.FeeLevel) {
case "PRIORITY":
feeLevel = wallet.PRIOIRTY
case "NORMAL":
feeLevel = wallet.NORMAL
case "ECONOMIC":
feeLevel = wallet.ECONOMIC
default:
feeLevel = wallet.NORMAL
}
txid, err := wal.Spend(args.Amount, addr, feeLevel, args.OrderID, args.SpendAll)
if err != nil {
switch {
case err == wallet.ErrorInsuffientFunds:
return nil, ErrInsufficientFunds
case err == wallet.ErrorDustAmount:
return nil, ErrSpendAmountIsDust
default:
return nil, err
}
}
var (
thumbnail string
title string
memo = args.Memo
)
if contract != nil {
if contract.VendorListings[0].Item != nil && len(contract.VendorListings[0].Item.Images) > 0 {
thumbnail = contract.VendorListings[0].Item.Images[0].Tiny
title = contract.VendorListings[0].Item.Title
}
}
if memo == "" && title != "" {
memo = title
}
if err := n.Datastore.TxMetadata().Put(repo.Metadata{
Txid: txid.String(),
Address: args.Address,
Memo: memo,
OrderId: args.OrderID,
Thumbnail: thumbnail,
CanBumpFee: false,
}); err != nil {
return nil, fmt.Errorf("failed persisting transaction metadata: %s", err)
}
confirmed, unconfirmed := wal.Balance()
txn, err := wal.GetTransaction(*txid)
if err != nil {
return nil, fmt.Errorf("failed retrieving new wallet balance: %s", err)
}
return &SpendResponse{
Txid: txid.String(),
ConfirmedBalance: confirmed,
UnconfirmedBalance: unconfirmed,
Amount: -(txn.Value),
Timestamp: txn.Timestamp,
Memo: memo,
OrderID: args.OrderID,
}, nil
}
func (n *OpenBazaarNode) getOrderContractBySpendRequest(args *SpendRequest) (*pb.RicardianContract, error) {
var errorStr = "unable to find order from order id or spend address"
if args.OrderID != "" {
contract, _, _, _, _, _, err := n.Datastore.Purchases().GetByOrderId(args.OrderID)
if err != nil {
return nil, fmt.Errorf("%s: %s", errorStr, err)
}
if contract != nil {
return contract, nil
}
}
if args.decodedAddress != nil {
contract, _, _, _, err := n.Datastore.Purchases().GetByPaymentAddress(args.decodedAddress)
if err != nil {
return nil, fmt.Errorf("%s: %s", errorStr, err)
}
if contract != nil {
return contract, nil
}
}
return nil, errors.New(errorStr)
}