Skip to content

Commit

Permalink
Implement multi-layer auto-compound (#2115)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Sutton <mikisiton2@gmail.com>
  • Loading branch information
svarogg and michaelsutton authored Jul 15, 2022
1 parent 1c9bb54 commit b26b9f6
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions cmd/kaspawallet/daemon/server/split_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,10 @@ func (s *server) maybeAutoCompoundTransaction(transactionBytes []byte, toAddress
return nil, err
}

splitTransactions, err := s.maybeSplitTransaction(transaction, changeAddress)
splitTransactions, err := s.maybeSplitAndMergeTransaction(transaction, toAddress, changeAddress, changeWalletAddress)
if err != nil {
return nil, err
}
if len(splitTransactions) > 1 {
mergeTransaction, err := s.mergeTransaction(splitTransactions, transaction, toAddress, changeAddress, changeWalletAddress)
if err != nil {
return nil, err
}
splitTransactions = append(splitTransactions, mergeTransaction)
}

splitTransactionsBytes := make([][]byte, len(splitTransactions))
for i, splitTransaction := range splitTransactions {
splitTransactionsBytes[i], err = serialization.SerializePartiallySignedTransaction(splitTransaction)
Expand Down Expand Up @@ -113,8 +105,8 @@ func (s *server) mergeTransaction(
return serialization.DeserializePartiallySignedTransaction(mergeTransactionBytes)
}

func (s *server) maybeSplitTransaction(transaction *serialization.PartiallySignedTransaction,
changeAddress util.Address) ([]*serialization.PartiallySignedTransaction, error) {
func (s *server) maybeSplitAndMergeTransaction(transaction *serialization.PartiallySignedTransaction, toAddress util.Address,
changeAddress util.Address, changeWalletAddress *walletAddress) ([]*serialization.PartiallySignedTransaction, error) {

transactionMass, err := s.estimateMassAfterSignatures(transaction)
if err != nil {
Expand All @@ -141,6 +133,20 @@ func (s *server) maybeSplitTransaction(transaction *serialization.PartiallySigne
}
}

if len(splitTransactions) > 1 {
mergeTransaction, err := s.mergeTransaction(splitTransactions, transaction, toAddress, changeAddress, changeWalletAddress)
if err != nil {
return nil, err
}
// Recursion will be 2-3 iterations deep even in the rarest` cases, so considered safe..
splitMergeTransaction, err := s.maybeSplitAndMergeTransaction(mergeTransaction, toAddress, changeAddress, changeWalletAddress)
if err != nil {
return nil, err
}
splitTransactions = append(splitTransactions, splitMergeTransaction...)

}

return splitTransactions, nil
}

Expand Down

0 comments on commit b26b9f6

Please sign in to comment.