forked from Salvionied/apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.go
63 lines (52 loc) · 1.6 KB
/
Utils.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
package apollo
import (
"encoding/hex"
"sort"
"github.com/Salvionied/apollo/serialization/UTxO"
)
/*
*
SortUtxos sorts a slice of UTxO objects in descending order
based on their amounts.
Params:
utxos ([]UTxO.UTxO): A slice of UTxO objects to be sorted.
Returns:
[]UTxO.UTxO: A new slice of UTxO objects sorted by descending amounts.
*/
func SortUtxos(utxos []UTxO.UTxO) []UTxO.UTxO {
res := make([]UTxO.UTxO, len(utxos))
copy(res, utxos)
// Sort UTXOs first by large ADA-only UTXOs, then by assets
sort.Slice(res, func(i, j int) bool {
if !res[i].Output.GetValue().HasAssets && !res[j].Output.GetValue().HasAssets {
return res[i].Output.Lovelace() > res[j].Output.Lovelace()
} else if res[i].Output.GetValue().HasAssets && res[j].Output.GetValue().HasAssets {
return res[i].Output.GetAmount().Greater(res[j].Output.GetAmount())
} else {
return res[j].Output.GetAmount().HasAssets
}
})
return res
}
/*
*
SortInputs sorts a slice of UTxO objects based on their strings.
Params:
inputs ([]UTxO.UTxO): A slice of UTxO objects to be sorted.
Returns:
[]UTxO.UTxO: A new slice of UTxO objects sorted based on input strings.
*/
func SortInputs(inputs []UTxO.UTxO) []UTxO.UTxO {
sortedInputs := make([]UTxO.UTxO, 0)
sortedInputs = append(sortedInputs, inputs...)
sort.Slice(sortedInputs, func(i, j int) bool {
iTxId := hex.EncodeToString(sortedInputs[i].Input.TransactionId)
jTxId := hex.EncodeToString(sortedInputs[j].Input.TransactionId)
if iTxId != jTxId {
return iTxId < jTxId
} else {
return sortedInputs[i].Input.Index < sortedInputs[j].Input.Index
}
})
return sortedInputs
}