/
merchant.go
291 lines (233 loc) · 7.91 KB
/
merchant.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright 2018 The openwallet Authors
* This file is part of the openwallet library.
*
* The openwallet library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The openwallet library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package decred
import (
"errors"
"fmt"
"github.com/blocktree/openwallet/v2/log"
"github.com/blocktree/openwallet/v2/openwallet"
"github.com/shopspring/decimal"
"strings"
"time"
)
//CreateMerchantWallet 创建钱包
func (wm *WalletManager) CreateMerchantWallet(wallet *openwallet.Wallet) error {
//先加载是否有配置文件
err := wm.loadConfig()
if err != nil {
return errors.New("The wallet node is not config!")
}
coreWallet, _, err := wm.CreateNewWallet(wallet.Alias, wallet.Password)
if err != nil {
return err
}
wallet = coreWallet
return nil
}
func (wm *WalletManager) CreateMerchantAssetsAccount(wallet *openwallet.Wallet) (*openwallet.AssetsAccount, error) {
return nil, nil
}
//GetMerchantWalletList 获取钱包列表
func (wm *WalletManager) GetMerchantWalletList() ([]*openwallet.Wallet, error) {
return nil, nil
}
func (wm *WalletManager) GetMerchantAssetsAccountList(wallet *openwallet.Wallet) ([]*openwallet.AssetsAccount, error) {
//先加载是否有配置文件
err := wm.loadConfig()
if err != nil {
return nil, errors.New("The wallet node is not config!")
}
balance := wm.GetWalletBalance(wallet.WalletID)
account := wallet.SingleAssetsAccount(Symbol)
account.Balance = balance
return []*openwallet.AssetsAccount{account}, nil
}
//ConfigMerchantWallet 钱包工具配置接口
func (wm *WalletManager) ConfigMerchantWallet(wallet *openwallet.Wallet) error {
return nil
}
//ImportMerchantAddress 导入地址
func (wm *WalletManager) ImportMerchantAddress(wallet *openwallet.Wallet, account *openwallet.AssetsAccount, addresses []*openwallet.Address) error {
//写入到数据库中
db, err := wallet.OpenDB()
if err != nil {
return err
}
defer db.Close()
tx, err := db.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
log.Std.Info("block scanner import [%s] new addresses: %d ", account.Symbol, len(addresses))
createdAt := time.Now()
for _, a := range addresses {
a.WatchOnly = true //观察地址
a.Symbol = strings.ToLower(Symbol)
a.AccountID = account.AccountID
a.CreatedTime = createdAt.Unix()
err = tx.Save(a)
if err != nil {
return err
}
wm.blockscanner.AddAddress(a.Address, wallet.WalletID, wallet)
}
err = tx.Commit()
if err != nil {
return err
}
return nil
}
//CreateMerchantAddress 创建钱包地址
func (wm *WalletManager) CreateMerchantAddress(wallet *openwallet.Wallet, account *openwallet.AssetsAccount, count uint64) ([]*openwallet.Address, error) {
//先加载是否有配置文件
err := wm.loadConfig()
if err != nil {
return nil, errors.New("The wallet node is not config!")
}
log.Std.Info("wallet: %s create %d address...", wallet.WalletID, count)
_, newAddrs, err := wm.CreateBatchAddress(wallet.WalletID, wallet.Password, count)
if err != nil {
return nil, err
}
return newAddrs, nil
}
//GetMerchantAddressList 获取钱包地址
func (wm *WalletManager) GetMerchantAddressList(wallet *openwallet.Wallet, account *openwallet.AssetsAccount, watchOnly bool, offset uint64, limit uint64) ([]*openwallet.Address, error) {
//先加载是否有配置文件
err := wm.loadConfig()
if err != nil {
return nil, errors.New("The wallet node is not config!")
}
return wm.GetAddressesFromLocalDB(wallet.WalletID, watchOnly, int(offset), int(limit))
//return nil, nil
}
//SubmitTransaction 提交转账申请
func (wm *WalletManager) SubmitTransactions(wallet *openwallet.Wallet, account *openwallet.AssetsAccount, withdraws []*openwallet.Withdraw, surplus string) (*openwallet.Transaction, error) {
//先加载是否有配置文件
err := wm.loadConfig()
if err != nil {
return nil, errors.New("The wallet node is not config!")
}
addrs := make([]string, 0)
amounts := make([]decimal.Decimal, 0)
totalSend := decimal.New(0, 0)
for _, with := range withdraws {
if len(with.Address) == 0 {
continue
}
amount, _ := decimal.NewFromString(with.Amount)
addrs = append(addrs, with.Address)
amounts = append(amounts, amount)
totalSend = totalSend.Add(amount)
}
balance := wm.GetWalletBalance(wallet.WalletID)
b, _ := decimal.NewFromString(balance)
sp, _ := decimal.NewFromString(surplus)
//【余额—剩余额】低于提币数量,不允许提
if b.Sub(sp).LessThan(totalSend) {
return nil, fmt.Errorf("the wallet balance: %s is lower than surplus. total send amount: %s ",
b.StringFixed(8), totalSend.StringFixed(8))
//return nil, errors.New("the wallet balance is lower than surplus. ")
}
txID, err := wm.SendBatchTransaction(wallet.WalletID, addrs, amounts, wallet.Password)
if err != nil {
return nil, err
}
t := openwallet.Transaction{TxID: txID}
return &t, nil
}
//AddMerchantObserverForBlockScan 添加区块链观察者,当扫描出新区块时进行通知
func (wm *WalletManager) AddMerchantObserverForBlockScan(obj openwallet.BlockScanNotificationObject, wallet *openwallet.Wallet) error {
//先加载是否有配置文件
err := wm.loadConfig()
if err != nil {
return errors.New("The wallet node is not config! ")
}
wm.blockscanner.AddObserver(obj)
wm.blockscanner.AddWallet(wallet.WalletID, wallet)
wm.blockscanner.Run()
return nil
}
//RemoveMerchantObserverForBlockScan 移除区块链扫描的观测者
func (wm *WalletManager) RemoveMerchantObserverForBlockScan(obj openwallet.BlockScanNotificationObject) {
wm.blockscanner.RemoveObserver(obj)
if len(wm.blockscanner.observers) == 0 {
wm.blockscanner.Stop()
wm.blockscanner.Clear()
}
}
//GetBlockchainInfo 获取区块链信息
func (wm *WalletManager) GetBlockchainInfo() (*openwallet.Blockchain, error) {
//先加载是否有配置文件
err := wm.loadConfig()
if err != nil {
return nil, errors.New("The wallet node is not config! ")
}
height, err := wm.GetBlockHeight()
if err != nil {
return nil, err
}
localHeight, _ := wm.GetLocalNewBlock()
if err != nil {
return nil, err
}
info := openwallet.Blockchain{
Blocks: height,
ScanHeight: localHeight,
}
return &info, nil
}
//GetMerchantAssetsAccount 获取账户资产
func (wm *WalletManager) GetMerchantWalletBalance(walletID string) (string, error) {
//先加载是否有配置文件
err := wm.loadConfig()
if err != nil {
return "0", errors.New("The wallet node is not config! ")
}
return wm.GetWalletBalance(walletID), nil
}
//GetMerchantAssetsAccount 获取地址资产
func (wm *WalletManager) GetMerchantAddressBalance(walletID, address string) (string, error) {
//先加载是否有配置文件
err := wm.loadConfig()
if err != nil {
return "0", errors.New("The wallet node is not config! ")
}
return wm.GetAddressBalance(walletID, address), nil
}
//SetMerchantRescanBlockHeight 商户重置区块链扫描高度
func (wm *WalletManager) SetMerchantRescanBlockHeight(height uint64) error {
//先加载是否有配置文件
err := wm.loadConfig()
if err != nil {
return errors.New("The wallet node is not config! ")
}
return wm.blockscanner.SetRescanBlockHeight(height)
}
//MerchantRescanBlockHeight 商户重置区块链扫描高度范围
func (wm *WalletManager) MerchantRescanBlockHeight(startHeight uint64, endHeight uint64) error {
if startHeight <= endHeight {
for i := startHeight; i <= endHeight; i++ {
err := wm.blockscanner.ScanBlock(i)
if err != nil {
continue
}
}
} else {
return fmt.Errorf("start block height: %d is greater than end block height: %d", startHeight, endHeight)
}
return nil
}