-
Notifications
You must be signed in to change notification settings - Fork 199
/
factory.go
86 lines (71 loc) · 2.25 KB
/
factory.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
package builtInFunctions
import (
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/mitchellh/mapstructure"
)
// ArgsCreateBuiltInFunctionContainer -
type ArgsCreateBuiltInFunctionContainer struct {
GasMap map[string]map[string]uint64
MapDNSAddresses map[string]struct{}
EnableUserNameChange bool
}
// CreateBuiltInFunctionContainer will create the list of built-in functions
func CreateBuiltInFunctionContainer(args ArgsCreateBuiltInFunctionContainer) (process.BuiltInFunctionContainer, error) {
gasConfig, err := createGasConfig(args.GasMap)
if err != nil {
return nil, err
}
container := NewBuiltInFunctionContainer()
var newFunc process.BuiltinFunction
newFunc = NewClaimDeveloperRewardsFunc(gasConfig.BuiltInCost.ClaimDeveloperRewards)
err = container.Add(core.BuiltInFunctionClaimDeveloperRewards, newFunc)
if err != nil {
return nil, err
}
newFunc = NewChangeOwnerAddressFunc(gasConfig.BuiltInCost.ChangeOwnerAddress)
err = container.Add(core.BuiltInFunctionChangeOwnerAddress, newFunc)
if err != nil {
return nil, err
}
newFunc, err = NewSaveUserNameFunc(gasConfig.BuiltInCost.SaveUserName, args.MapDNSAddresses, args.EnableUserNameChange)
if err != nil {
return nil, err
}
err = container.Add(core.BuiltInFunctionSetUserName, newFunc)
if err != nil {
return nil, err
}
newFunc = NewSaveKeyValueStorageFunc(gasConfig.BaseOperationCost, gasConfig.BuiltInCost.SaveKeyValue)
err = container.Add(core.BuiltInFunctionSaveKeyValue, newFunc)
if err != nil {
return nil, err
}
return container, nil
}
func createGasConfig(gasMap map[string]map[string]uint64) (*GasCost, error) {
baseOps := &BaseOperationCost{}
err := mapstructure.Decode(gasMap[core.BaseOperationCost], baseOps)
if err != nil {
return nil, err
}
err = check.ForZeroUintFields(*baseOps)
if err != nil {
return nil, err
}
builtInOps := &BuiltInCost{}
err = mapstructure.Decode(gasMap[core.BuiltInCost], builtInOps)
if err != nil {
return nil, err
}
err = check.ForZeroUintFields(*builtInOps)
if err != nil {
return nil, err
}
gasCost := GasCost{
BaseOperationCost: *baseOps,
BuiltInCost: *builtInOps,
}
return &gasCost, nil
}