forked from kaspanet/kaspad
-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_unsigned_transaction.go
168 lines (141 loc) · 4.96 KB
/
create_unsigned_transaction.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
package server
import (
"context"
"fmt"
"github.com/Kash-Protocol/kashd/domain/consensus/model/externalapi"
"time"
"github.com/Kash-Protocol/kashd/cmd/kashwallet/daemon/pb"
"github.com/Kash-Protocol/kashd/cmd/kashwallet/libkashwallet"
"github.com/Kash-Protocol/kashd/domain/consensus/utils/constants"
"github.com/Kash-Protocol/kashd/util"
"github.com/pkg/errors"
"golang.org/x/exp/slices"
)
// TODO: Implement a better fee estimation mechanism
const feePerInput = 10000
func (s *server) CreateUnsignedTransactions(_ context.Context, request *pb.CreateUnsignedTransactionsRequest) (
*pb.CreateUnsignedTransactionsResponse, error,
) {
s.lock.Lock()
defer s.lock.Unlock()
unsignedTransactions, err := s.createUnsignedTransactions(request.Address,
externalapi.AssetTypeFromUint32(request.AssetType), request.Amount, request.IsSendAll,
request.From, request.UseExistingChangeAddress)
if err != nil {
return nil, err
}
return &pb.CreateUnsignedTransactionsResponse{UnsignedTransactions: unsignedTransactions}, nil
}
func (s *server) createUnsignedTransactions(address string, assetType externalapi.AssetType,
amount uint64, isSendAll bool, fromAddressesString []string, useExistingChangeAddress bool) ([][]byte, error) {
if !s.isSynced() {
return nil, errors.Errorf("wallet daemon is not synced yet, %s", s.formatSyncStateReport())
}
// make sure address string is correct before proceeding to a
// potentially long UTXO refreshment operation
toAddress, err := util.DecodeAddress(address, s.params.Prefix)
if err != nil {
return nil, err
}
err = s.refreshUTXOs()
if err != nil {
return nil, err
}
var fromAddresses []*walletAddress
for _, from := range fromAddressesString {
fromAddress, exists := s.addressSet[from]
if !exists {
return nil, fmt.Errorf("Specified from address %s does not exists", from)
}
fromAddresses = append(fromAddresses, fromAddress)
}
selectedUTXOs, spendValue, changeSompi, err := s.selectUTXOs(amount, isSendAll, feePerInput, fromAddresses, assetType)
if err != nil {
return nil, err
}
if len(selectedUTXOs) == 0 {
return nil, errors.Errorf("couldn't find funds to spend")
}
changeAddress, changeWalletAddress, err := s.changeAddress(useExistingChangeAddress, fromAddresses)
if err != nil {
return nil, err
}
payments := []*libkashwallet.Payment{{
Address: toAddress,
AssetType: assetType,
Amount: spendValue,
}}
if changeSompi > 0 {
payments = append(payments, &libkashwallet.Payment{
Address: changeAddress,
AssetType: assetType,
Amount: changeSompi,
})
}
// TODO: Add support for MintKUSD, StakeKSH, RedeemKSH.
TxTypeMapping := map[externalapi.AssetType]externalapi.DomainTransactionType{
externalapi.KSH: externalapi.TransferKSH,
externalapi.KUSD: externalapi.TransferKUSD,
externalapi.KRV: externalapi.TransferKRV,
}
unsignedTransaction, err := libkashwallet.CreateUnsignedTransaction(s.keysFile.ExtendedPublicKeys,
s.keysFile.MinimumSignatures,
payments, selectedUTXOs, TxTypeMapping[assetType])
if err != nil {
return nil, err
}
unsignedTransactions, err := s.maybeAutoCompoundTransaction(unsignedTransaction, toAddress,
changeAddress, changeWalletAddress, assetType)
if err != nil {
return nil, err
}
return unsignedTransactions, nil
}
func (s *server) selectUTXOs(spendAmount uint64, isSendAll bool, feePerInput uint64, fromAddresses []*walletAddress, assetType externalapi.AssetType) (
selectedUTXOs []*libkashwallet.UTXO, totalReceived uint64, changeSompi uint64, err error) {
selectedUTXOs = []*libkashwallet.UTXO{}
totalValue := uint64(0)
dagInfo, err := s.rpcClient.GetBlockDAGInfo()
if err != nil {
return nil, 0, 0, err
}
for _, utxo := range s.utxosSortedByAmount {
if utxo.UTXOEntry.AssetType() != assetType ||
(fromAddresses != nil && !slices.Contains(fromAddresses, utxo.address)) ||
!isUTXOSpendable(utxo, dagInfo.VirtualDAAScore, s.params.BlockCoinbaseMaturity) {
continue
}
if broadcastTime, ok := s.usedOutpoints[*utxo.Outpoint]; ok {
if time.Since(broadcastTime) > time.Minute {
delete(s.usedOutpoints, *utxo.Outpoint)
} else {
continue
}
}
selectedUTXOs = append(selectedUTXOs, &libkashwallet.UTXO{
Outpoint: utxo.Outpoint,
UTXOEntry: utxo.UTXOEntry,
DerivationPath: s.walletAddressPath(utxo.address),
})
totalValue += utxo.UTXOEntry.Amount()
fee := feePerInput * uint64(len(selectedUTXOs))
totalSpend := spendAmount + fee
if !isSendAll && totalValue >= totalSpend {
break
}
}
fee := feePerInput * uint64(len(selectedUTXOs))
var totalSpend uint64
if isSendAll {
totalSpend = totalValue
totalReceived = totalValue - fee
} else {
totalSpend = spendAmount + fee
totalReceived = spendAmount
}
if totalValue < totalSpend {
return nil, 0, 0, errors.Errorf("Insufficient funds for send: %f required, while only %f available",
float64(totalSpend)/constants.SompiPerKash, float64(totalValue)/constants.SompiPerKash)
}
return selectedUTXOs, totalReceived, totalValue - totalSpend, nil
}