-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
82 lines (71 loc) · 2.21 KB
/
storage.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
package standards
import (
"fmt"
"github.com/unpackdev/standards/shared"
"sort"
)
// storage is a map that holds registered Ethereum standards.
var storage map[shared.Standard]shared.EIP
// RegisterStandard registers a new Ethereum standard to the storage.
// If the standard already exists, it returns an error.
//
// Parameters:
// - s: The Ethereum standard type.
// - cs: The details of the Ethereum standard.
//
// Returns:
// - error: An error if the standard already exists, otherwise nil.
func RegisterStandard(s shared.Standard, cs shared.EIP) error {
if Exists(s) {
return fmt.Errorf("standard %s already exists", s)
}
storage[s] = cs
return nil
}
// GetStandard retrieves the details of a registered Ethereum standard.
//
// Parameters:
// - s: The Ethereum standard type.
//
// Returns:
// - ContractStandard: The details of the Ethereum standard if it exists.
// - bool: A boolean indicating if the standard exists in the storage.
func GetStandard(s shared.Standard) (shared.EIP, bool) {
cs, exists := storage[s]
return cs, exists
}
// Exists checks if a given Ethereum standard is registered in the storage.
//
// Parameters:
// - s: The Ethereum standard type.
//
// Returns:
// - bool: A boolean indicating if the standard exists in the storage.
func Exists(s shared.Standard) bool {
_, exists := storage[s]
return exists
}
// GetRegisteredStandards retrieves all the registered Ethereum standards from the storage.
//
// Returns:
// - map[Standard]ContractStandard: A map of all registered Ethereum standards.
func GetRegisteredStandards() map[shared.Standard]shared.EIP {
return storage
}
// GetSortedRegisteredStandards retrieves all the registered Ethereum standards from the storage in a sorted order.
func GetSortedRegisteredStandards() []shared.EIP {
// Create a slice to hold all EIP values
var eips []shared.EIP
for _, eip := range storage {
eips = append(eips, eip)
}
// Sort the slice based on the Name field of EIP
sort.Slice(eips, func(i, j int) bool {
return eips[i].GetType().String() < eips[j].GetType().String()
})
return eips
}
// StandardsLoaded returns a boolean indicating whether the storage has any registered Ethereum standards.
func StandardsLoaded() bool {
return len(storage) > 1
}