/
tx_decode.go
295 lines (234 loc) · 8.32 KB
/
tx_decode.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
292
293
294
295
package beam
import (
"fmt"
"github.com/blocktree/openwallet/common"
"github.com/blocktree/openwallet/openwallet"
"github.com/shopspring/decimal"
"math/big"
"time"
)
type TransactionDecoder struct {
openwallet.TransactionDecoderBase
wm *WalletManager //钱包管理者
}
//NewTransactionDecoder 交易单解析器
func NewTransactionDecoder(wm *WalletManager) *TransactionDecoder {
decoder := TransactionDecoder{}
decoder.wm = wm
return &decoder
}
//CreateRawTransaction 创建交易单
func (decoder *TransactionDecoder) CreateRawTransaction(wrapper openwallet.WalletDAI, rawTx *openwallet.RawTransaction) error {
var (
to string
amount string
txFrom []string
txTo []string
fixFees *big.Int
)
for k, v := range rawTx.To {
to = k
amount = v
}
amountDec, _ := decimal.NewFromString(amount)
amountDec = amountDec.Shift(decoder.wm.Decimal())
//取一个地址作为发送
addresses, err := decoder.wm.walletClient.GetAddressList()
if err != nil {
return err
}
if addresses == nil || len(addresses) == 0 {
return openwallet.Errorf(openwallet.ErrAccountNotAddress, "wallet address is not created")
}
from := addresses[0]
if len(rawTx.FeeRate) > 0 {
fixFees = common.StringNumToBigIntWithExp(rawTx.FeeRate, decoder.wm.Decimal())
rawTx.Fees = rawTx.FeeRate
} else {
fixFees = common.StringNumToBigIntWithExp(decoder.wm.Config.fixfees, decoder.wm.Decimal())
rawTx.FeeRate = decoder.wm.Config.fixfees
rawTx.Fees = decoder.wm.Config.fixfees
}
if fixFees.Cmp(big.NewInt(0)) <= 0 {
return openwallet.Errorf(openwallet.ErrUnknownException, "fee is lower than 0")
}
walletStatus, err := decoder.wm.walletClient.GetWalletStatus()
if err != nil {
return err
}
sendAmount := uint64(amountDec.IntPart())
//判断钱包余额是否足够
if walletStatus.Available < sendAmount+fixFees.Uint64() {
return openwallet.Errorf(openwallet.ErrInsufficientBalanceOfAccount, "wallet available balance is not enough")
}
txFrom = []string{fmt.Sprintf("%s:%s", from, amount)}
txTo = []string{fmt.Sprintf("%s:%s", to, amount)}
rawTx.FeeRate = decoder.wm.Config.fixfees
rawTx.Fees = decoder.wm.Config.fixfees
rawTx.IsBuilt = true
rawTx.TxFrom = txFrom
rawTx.TxTo = txTo
return nil
}
//SignRawTransaction 签名交易单
func (decoder *TransactionDecoder) SignRawTransaction(wrapper openwallet.WalletDAI, rawTx *openwallet.RawTransaction) error {
return nil
}
//
//VerifyRawTransaction 验证交易单,验证交易单并返回加入签名后的交易单
func (decoder *TransactionDecoder) VerifyRawTransaction(wrapper openwallet.WalletDAI, rawTx *openwallet.RawTransaction) error {
rawTx.IsCompleted = true
return nil
}
//SendRawTransaction 广播交易单
func (decoder *TransactionDecoder) SubmitRawTransaction(wrapper openwallet.WalletDAI, rawTx *openwallet.RawTransaction) (*openwallet.Transaction, error) {
var (
to string
amount string
fixFees *big.Int
)
for k, v := range rawTx.To {
to = k
amount = v
}
amountDec, _ := decimal.NewFromString(amount)
amountDec = amountDec.Shift(decoder.wm.Decimal())
//取一个地址作为发送
addresses, err := decoder.wm.walletClient.GetAddressList()
if err != nil {
return nil, err
}
if addresses == nil || len(addresses) == 0 {
return nil, fmt.Errorf("wallet address is not created")
}
from := addresses[0]
if len(rawTx.FeeRate) > 0 {
fixFees = common.StringNumToBigIntWithExp(rawTx.FeeRate, decoder.wm.Decimal())
rawTx.Fees = rawTx.FeeRate
} else {
fixFees = common.StringNumToBigIntWithExp(decoder.wm.Config.fixfees, decoder.wm.Decimal())
rawTx.FeeRate = decoder.wm.Config.fixfees
rawTx.Fees = decoder.wm.Config.fixfees
}
if fixFees.Cmp(big.NewInt(0)) <= 0 {
return nil, openwallet.Errorf(openwallet.ErrUnknownException, "fee is lower than 0")
}
walletStatus, err := decoder.wm.walletClient.GetWalletStatus()
if err != nil {
return nil, err
}
sendAmount := uint64(amountDec.IntPart())
//判断钱包余额是否足够
if walletStatus.Available < sendAmount+fixFees.Uint64() {
return nil, openwallet.Errorf(openwallet.ErrInsufficientBalanceOfAccount, "wallet available balance is not enough")
}
txid, err := decoder.wm.walletClient.SendTransaction(from, to, sendAmount, fixFees.Uint64(), "")
if err != nil {
return nil, err
}
decoder.wm.Log.Infof("Transaction [%s] submitted to the network successfully.", txid)
rawTx.TxID = txid
rawTx.IsSubmit = true
txFrom := []string{fmt.Sprintf("%s:%s", from, amount)}
txTo := []string{fmt.Sprintf("%s:%s", to, amount)}
decimals := decoder.wm.Decimal()
//记录一个交易单
tx := &openwallet.Transaction{
From: txFrom,
To: txTo,
Amount: amount,
Coin: rawTx.Coin,
TxID: rawTx.TxID,
Decimal: decimals,
Fees: rawTx.Fees,
SubmitTime: time.Now().Unix(),
}
tx.WxID = openwallet.GenTransactionWxID(tx)
return tx, nil
}
//GetRawTransactionFeeRate 获取交易单的费率
func (decoder *TransactionDecoder) GetRawTransactionFeeRate() (feeRate string, unit string, err error) {
return decoder.wm.Config.fixfees, "TX", nil
}
//CreateSummaryRawTransaction 创建汇总交易
func (decoder *TransactionDecoder) CreateSummaryRawTransaction(wrapper openwallet.WalletDAI, sumRawTx *openwallet.SummaryRawTransaction) ([]*openwallet.RawTransaction, error) {
var (
rawTxWithErrArray []*openwallet.RawTransactionWithError
rawTxArray = make([]*openwallet.RawTransaction, 0)
err error
)
rawTxWithErrArray, err = decoder.CreateSummaryRawTransactionWithError(wrapper, sumRawTx)
if err != nil {
return nil, err
}
for _, rawTxWithErr := range rawTxWithErrArray {
if rawTxWithErr.Error != nil {
continue
}
rawTxArray = append(rawTxArray, rawTxWithErr.RawTx)
}
return rawTxArray, nil
}
//CreateSummaryRawTransactionWithError 创建汇总交易,返回能原始交易单数组(包含带错误的原始交易单)
func (decoder *TransactionDecoder) CreateSummaryRawTransactionWithError(wrapper openwallet.WalletDAI, sumRawTx *openwallet.SummaryRawTransaction) ([]*openwallet.RawTransactionWithError, error) {
var (
decimals = decoder.wm.Decimal()
rawTxArray = make([]*openwallet.RawTransactionWithError, 0)
minTransfer = common.StringNumToBigIntWithExp(sumRawTx.MinTransfer, decimals)
retainedBalance = common.StringNumToBigIntWithExp(sumRawTx.RetainedBalance, decimals)
fixFees = big.NewInt(0)
)
if minTransfer.Cmp(retainedBalance) < 0 {
return nil, fmt.Errorf("mini transfer amount must be greater than address retained balance")
}
if len(sumRawTx.FeeRate) > 0 {
fixFees = common.StringNumToBigIntWithExp(sumRawTx.FeeRate, decoder.wm.Decimal())
} else {
fixFees = common.StringNumToBigIntWithExp(decoder.wm.Config.fixfees, decoder.wm.Decimal())
sumRawTx.FeeRate = decoder.wm.Config.fixfees
}
if fixFees.Cmp(big.NewInt(0)) <= 0 {
return nil, openwallet.Errorf(openwallet.ErrUnknownException, "fee is lower than 0")
}
walletStatus, err := decoder.wm.walletClient.GetWalletStatus()
if err != nil {
return nil, err
}
//检查余额是否超过最低转账
addrBalance_BI := new(big.Int)
addrBalance_BI.SetUint64(walletStatus.Available)
addrBalance := common.IntToDecimals(int64(walletStatus.Available), decoder.wm.Decimal())
if addrBalance_BI.Cmp(minTransfer) < 0 || addrBalance_BI.Cmp(big.NewInt(0)) <= 0 {
return rawTxArray, nil
}
//计算汇总数量 = 余额 - 保留余额
sumAmount_BI := new(big.Int)
sumAmount_BI.Sub(addrBalance_BI, retainedBalance)
//减去手续费
sumAmount_BI.Sub(sumAmount_BI, fixFees)
if sumAmount_BI.Cmp(big.NewInt(0)) <= 0 {
return rawTxArray, nil
}
sumAmount := common.BigIntToDecimals(sumAmount_BI, decimals)
feesAmount := common.BigIntToDecimals(fixFees, decimals)
decoder.wm.Log.Debugf("balance: %v", addrBalance.String())
decoder.wm.Log.Debugf("fees: %v", feesAmount)
decoder.wm.Log.Debugf("sumAmount: %v", sumAmount)
//创建一笔交易单
rawTx := &openwallet.RawTransaction{
Coin: sumRawTx.Coin,
Account: sumRawTx.Account,
To: map[string]string{
sumRawTx.SummaryAddress: sumAmount.StringFixed(decoder.wm.Decimal()),
},
Required: 1,
}
createTxErr := decoder.CreateRawTransaction(wrapper, rawTx)
rawTxWithErr := &openwallet.RawTransactionWithError{
RawTx: rawTx,
Error: openwallet.ConvertError(createTxErr),
}
//创建成功,添加到队列
rawTxArray = append(rawTxArray, rawTxWithErr)
return rawTxArray, nil
}