This repository was archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathspend.go
More file actions
201 lines (176 loc) · 5.83 KB
/
Copy pathspend.go
File metadata and controls
201 lines (176 loc) · 5.83 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package core
import (
"errors"
"fmt"
"math/big"
"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 uint = 8
type SpendRequest struct {
decodedAddress btcutil.Address
Amount string `json:"amount"`
Currency *repo.CurrencyDefinition `json:"currency"`
CurrencyCode string `json:"currencyCode"`
Address string `json:"address"`
FeeLevel string `json:"feeLevel"`
Memo string `json:"memo"`
OrderID string `json:"orderId"`
RequireAssociatedOrder bool `json:"requireOrder"`
SpendAll bool `json:"spendAll"`
}
type SpendResponse struct {
Amount string `json:"amount"`
ConfirmedBalance string `json:"confirmedBalance"`
UnconfirmedBalance string `json:"unconfirmedBalance"`
Currency *repo.CurrencyDefinition `json:"currency"`
Memo string `json:"memo"`
OrderID string `json:"orderId"`
Timestamp time.Time `json:"timestamp"`
Txid string `json:"txid"`
PeerID string `json:"-"`
ConsumedInput bool `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
peerID string
amt = new(big.Int)
lookupCode = args.CurrencyCode
)
if lookupCode == "" && args.Currency != nil {
lookupCode = args.Currency.Code.String()
}
var currencyDef, err = n.LookupCurrency(lookupCode)
if err != nil {
return nil, repo.ErrCurrencyDefinitionUndefined
}
if args.Currency != nil && currencyDef.Divisibility != args.Currency.Divisibility {
currencyDef.Divisibility = args.Currency.Divisibility
if err := currencyDef.Valid(); err != nil {
return nil, err
}
}
amt, ok := amt.SetString(args.Amount, 10)
if !ok {
return nil, ErrInvalidAmount
}
wal, err := n.Multiwallet.WalletForCurrencyCode(lookupCode)
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
}
log.Info("before wallet spend : amt - ", amt.String(), " addr : ", addr.String())
log.Info("fee level : ", feeLevel, " orderID ", args.OrderID, " spend all ", args.SpendAll)
txid, err := wal.Spend(*amt, addr, feeLevel, args.OrderID, args.SpendAll)
if err != nil {
switch {
case err == wallet.ErrInsufficientFunds:
return nil, ErrInsufficientFunds
case err == wallet.ErrorDustAmount:
return nil, ErrSpendAmountIsDust
default:
return nil, err
}
}
txn, err := wal.GetTransaction(*txid)
if err != nil {
log.Errorf("get txn failed : %v", err.Error())
return nil, fmt.Errorf("failed retrieving new wallet balance: %s", err)
}
var (
thumbnail string
title string
memo = args.Memo
toAddress = args.Address
)
if txn.ToAddress != "" {
toAddress = txn.ToAddress
}
if contract != nil && contract.VendorListings[0] != 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 contract.VendorListings[0].VendorID != nil {
peerID = contract.VendorListings[0].VendorID.PeerID
}
}
if memo == "" && title != "" {
memo = title
}
if err := n.Datastore.TxMetadata().Put(repo.Metadata{
Txid: txid.String(),
Address: toAddress,
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()
defn, err := n.LookupCurrency(wal.CurrencyCode())
if err != nil {
return nil, fmt.Errorf("wallet currency not found in dictionary")
}
return &SpendResponse{
Txid: txid.String(),
ConfirmedBalance: confirmed.Value.String(),
UnconfirmedBalance: unconfirmed.Value.String(),
Currency: &defn,
Amount: strings.TrimPrefix(txn.Value, "-"),
Timestamp: txn.Timestamp,
Memo: memo,
OrderID: args.OrderID,
PeerID: peerID,
}, 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)
}