-
Notifications
You must be signed in to change notification settings - Fork 19
/
genesis_state.go
127 lines (107 loc) · 3.12 KB
/
genesis_state.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package assets
import (
"encoding/json"
"errors"
types "code.vegaprotocol.io/vega/protos/vega"
)
type AssetDetails struct {
Name string `json:"name"`
Symbol string `json:"symbol"`
Decimals uint64 `json:"decimals"`
Quantum string `json:"quantum"`
Source *Source `json:"source"`
}
type Source struct {
BuiltinAsset *BuiltinAsset `json:"builtin_asset,omitempty"`
Erc20 *Erc20 `json:"erc20,omitempty"`
}
type BuiltinAsset struct {
MaxFaucetAmountMint string `json:"max_faucet_amount_mint"`
}
type Erc20 struct {
ContractAddress string `json:"contract_address"`
LifetimeLimit string `json:"lifetime_limit"`
WithdrawThreshold string `json:"withdraw_threshold"`
}
func (a *AssetDetails) IntoProto() (*types.AssetDetails, error) {
if a.Source == nil || (a.Source.BuiltinAsset == nil && a.Source.Erc20 == nil) {
return nil, errors.New("missing asset source")
}
if a.Source.BuiltinAsset != nil && a.Source.Erc20 != nil {
return nil, errors.New("multiple asset sources specified")
}
details := types.AssetDetails{
Name: a.Name,
Symbol: a.Symbol,
Decimals: a.Decimals,
Quantum: a.Quantum,
}
if a.Source.BuiltinAsset != nil {
details.Source = &types.AssetDetails_BuiltinAsset{
BuiltinAsset: &types.BuiltinAsset{
MaxFaucetAmountMint: a.Source.BuiltinAsset.MaxFaucetAmountMint,
},
}
}
if a.Source.Erc20 != nil {
details.Source = &types.AssetDetails_Erc20{
Erc20: &types.ERC20{
ContractAddress: a.Source.Erc20.ContractAddress,
WithdrawThreshold: a.Source.Erc20.WithdrawThreshold,
LifetimeLimit: a.Source.Erc20.LifetimeLimit,
},
}
}
return &details, nil
}
type GenesisState map[string]AssetDetails
var governanceAsset = AssetDetails{
Name: "VOTE",
Symbol: "VOTE",
Decimals: 5,
Quantum: "1",
Source: &Source{
BuiltinAsset: &BuiltinAsset{
MaxFaucetAmountMint: "10000",
},
},
}
func DefaultGenesisState() GenesisState {
assets := map[string]AssetDetails{
"VOTE": governanceAsset,
}
return assets
}
func LoadGenesisState(bytes []byte) (map[string]*types.AssetDetails, error) {
state := struct {
Assets GenesisState `json:"assets"`
}{}
err := json.Unmarshal(bytes, &state)
if err != nil {
return nil, err
}
// now convert them all into proto
out := map[string]*types.AssetDetails{}
for k, v := range state.Assets {
details, err := v.IntoProto()
if err != nil {
return nil, err
}
out[k] = details
}
return out, nil
}