-
Notifications
You must be signed in to change notification settings - Fork 48
/
utxo_info.go
134 lines (113 loc) · 3.91 KB
/
utxo_info.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
package reader
import (
"math/big"
lpb "github.com/xuperchain/xupercore/bcs/ledger/xledger/xldgpb"
xctx "github.com/xuperchain/xupercore/kernel/common/xcontext"
"github.com/xuperchain/xupercore/kernel/engines/xuperos/common"
"github.com/xuperchain/xupercore/lib/logs"
"github.com/xuperchain/xupercore/lib/utils"
)
type UtxoReader interface {
// 获取账户余额
GetBalance(account string) (string, error)
// 获取账户冻结余额
GetFrozenBalance(account string) (string, error)
// 获取账户余额详情
GetBalanceDetail(account string) ([]*lpb.BalanceDetailInfo, error)
// 拉取固定数目的utxo
QueryUtxoRecord(account string, count int64) (*lpb.UtxoRecordDetail, error)
// 选择合适金额的utxo
SelectUTXO(account string, need *big.Int, isLock, isExclude bool) (*lpb.UtxoOutput, error)
// 按最大交易大小选择utxo
SelectUTXOBySize(account string, isLock, isExclude bool) (*lpb.UtxoOutput, error)
}
type utxoReader struct {
chainCtx *common.ChainCtx
baseCtx xctx.XContext
log logs.Logger
}
func NewUtxoReader(chainCtx *common.ChainCtx, baseCtx xctx.XContext) UtxoReader {
reader := &utxoReader{
chainCtx: chainCtx,
baseCtx: baseCtx,
log: baseCtx.GetLog(),
}
return reader
}
func (t *utxoReader) GetBalance(address string) (string, error) {
balance, err := t.chainCtx.State.GetBalance(address)
if err != nil {
t.log.Warn("get balance error", "err", err)
return "", common.CastError(err)
}
return balance.String(), nil
}
func (t *utxoReader) GetFrozenBalance(account string) (string, error) {
balance, err := t.chainCtx.State.GetFrozenBalance(account)
if err != nil {
t.log.Warn("get frozen balance error", "err", err)
return "", common.CastError(err)
}
return balance.String(), nil
}
func (t *utxoReader) GetBalanceDetail(account string) ([]*lpb.BalanceDetailInfo, error) {
tokenDetails, err := t.chainCtx.State.GetBalanceDetail(account)
if err != nil {
t.log.Warn("get balance detail error", "err", err)
return nil, common.CastError(err)
}
return tokenDetails, nil
}
func (t *utxoReader) QueryUtxoRecord(account string, count int64) (*lpb.UtxoRecordDetail, error) {
utxoRecord, err := t.chainCtx.State.QueryUtxoRecord(account, count)
if err != nil {
t.log.Warn("get utxo record error", "err", err)
return nil, common.CastError(err)
}
return utxoRecord, nil
}
func (t *utxoReader) SelectUTXO(account string,
need *big.Int, isLock, isExclude bool) (*lpb.UtxoOutput, error) {
utxos, _, totalSelected, err := t.chainCtx.State.SelectUtxos(account, need, isLock, isExclude)
if err != nil {
t.log.Warn("failed to select utxo", "err", err)
return nil, common.CastError(err)
}
utxoList := make([]*lpb.Utxo, 0, len(utxos))
for _, v := range utxos {
utxo := &lpb.Utxo{}
utxo.RefTxid = v.RefTxid
utxo.Amount = v.Amount
utxo.RefOffset = v.RefOffset
utxo.ToAddr = v.FromAddr
utxoList = append(utxoList, utxo)
t.log.Trace("Select utxo list", "refTxid", utils.F(v.RefTxid), "refOffset", v.RefOffset, "amount", new(big.Int).SetBytes(v.Amount).String())
}
out := &lpb.UtxoOutput{
UtxoList: utxoList,
TotalSelected: totalSelected.String(),
}
return out, nil
}
func (t *utxoReader) SelectUTXOBySize(account string, isLock, isExclude bool) (*lpb.UtxoOutput, error) {
utxos, _, totalSelected, err := t.chainCtx.State.SelectUtxosBySize(account, isLock, isExclude)
if err != nil {
t.log.Warn("failed to select utxo", "err", err)
return nil, common.CastError(err)
}
utxoList := make([]*lpb.Utxo, 0, len(utxos))
for _, v := range utxos {
utxo := &lpb.Utxo{}
utxo.RefTxid = v.RefTxid
utxo.Amount = v.Amount
utxo.RefOffset = v.RefOffset
utxo.ToAddr = v.FromAddr
utxoList = append(utxoList, utxo)
t.log.Trace("Select utxo list", "refTxid", utils.F(v.RefTxid), "refOffset", v.RefOffset, "amount", new(big.Int).SetBytes(v.Amount).String())
}
out := &lpb.UtxoOutput{
UtxoList: utxoList,
TotalSelected: totalSelected.String(),
}
return out, nil
}