forked from qtumproject/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtum_getUTXOs.go
142 lines (119 loc) · 3.63 KB
/
qtum_getUTXOs.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
package transformer
import (
"context"
"fmt"
"math/big"
"github.com/labstack/echo"
"github.com/qtumproject/janus/pkg/eth"
"github.com/qtumproject/janus/pkg/qtum"
"github.com/qtumproject/janus/pkg/utils"
"github.com/shopspring/decimal"
)
type ProxyQTUMGetUTXOs struct {
*qtum.Qtum
}
var _ ETHProxy = (*ProxyQTUMGetUTXOs)(nil)
func (p *ProxyQTUMGetUTXOs) Method() string {
return "qtum_getUTXOs"
}
func (p *ProxyQTUMGetUTXOs) Request(req *eth.JSONRPCRequest, c echo.Context) (interface{}, eth.JSONRPCError) {
var params eth.GetUTXOsRequest
if err := unmarshalRequest(req.Params, ¶ms); err != nil {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError("couldn't unmarshal request parameters")
}
err := params.CheckHasValidValues()
if err != nil {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError("couldn't validate parameters value")
}
return p.request(c.Request().Context(), params)
}
func (p *ProxyQTUMGetUTXOs) request(ctx context.Context, params eth.GetUTXOsRequest) (*eth.GetUTXOsResponse, eth.JSONRPCError) {
address, err := convertETHAddress(utils.RemoveHexPrefix(params.Address), p.Chain())
if err != nil {
return nil, eth.NewInvalidParamsError("couldn't convert Ethereum address to Qtum address")
}
req := qtum.GetAddressUTXOsRequest{
Addresses: []string{address},
}
resp, err := p.Qtum.GetAddressUTXOs(ctx, &req)
if err != nil {
return nil, eth.NewCallbackError(err.Error())
}
blockCount, err := p.Qtum.GetBlockCount(ctx)
if err != nil {
return nil, eth.NewCallbackError(err.Error())
}
matureBlockHeight := big.NewInt(int64(p.Qtum.GetMatureBlockHeight()))
//Convert minSumAmount to Satoshis
minimumSum := convertFromQtumToSatoshis(params.MinSumAmount)
queryingAll := minimumSum.Equal(decimal.Zero)
allUtxoTypes := false
if len(params.Types) > 0 {
if params.Types[0] == eth.ALL_UTXO_TYPES {
allUtxoTypes = true
}
} else {
allUtxoTypes = true
}
utxoTypes := map[eth.UTXOScriptType]bool{}
for _, typ := range params.Types {
utxoTypes[typ] = true
}
var utxos []eth.QtumUTXO
var minUTXOsSum decimal.Decimal
for _, utxo := range *resp {
ethUTXO := toEthResponseType(utxo)
ethUTXO.Height = uint64(utxo.Height.Int64())
ethUTXO.ScriptPubKey = utxo.Script
utxoType := ethUTXO.GetType()
ethUTXO.Type = utxoType.String()
ethUTXO.Safe = true
if !allUtxoTypes {
if _, ok := utxoTypes[utxoType]; !ok {
continue
}
}
// TODO: This doesn't work on regtest coinbase
if utxo.IsStake {
matureAt := big.NewInt(utxo.Height.Int64()).Add(
big.NewInt(utxo.Height.Int64()),
matureBlockHeight,
)
if blockCount.Int.Cmp(matureAt) <= 0 {
// immature
ethUTXO.Safe = false
if !allUtxoTypes {
if _, ok := utxoTypes[eth.IMMATURE]; !ok {
continue
}
}
}
}
ethUTXO.Confirmations = blockCount.Int64() - utxo.Height.Int64()
if ethUTXO.Confirmations < 0 {
panic(fmt.Sprintf("Computed negative confirmations: %d - %d = %d\n", blockCount.Int64(), utxo.Height.Int64(), ethUTXO.Confirmations))
}
ethUTXO.Spendable = true
if ethUTXO.Safe {
minUTXOsSum = minUTXOsSum.Add(utxo.Satoshis)
}
utxos = append(utxos, ethUTXO)
if !queryingAll && minUTXOsSum.GreaterThanOrEqual(minimumSum) {
return (*eth.GetUTXOsResponse)(&utxos), nil
}
}
if queryingAll {
return (*eth.GetUTXOsResponse)(&utxos), nil
}
return nil, eth.NewCallbackError("required minimum amount is greater than total amount of UTXOs")
}
func toEthResponseType(utxo qtum.UTXO) eth.QtumUTXO {
return eth.QtumUTXO{
Address: utxo.Address,
TXID: utxo.TXID,
Vout: utxo.OutputIndex,
Amount: convertFromSatoshisToQtum(utxo.Satoshis).String(),
}
}