code.vegaprotocol.io/vega@v0.79.0/core/assets/genesis_state.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package assets 17 18 import ( 19 "encoding/json" 20 "errors" 21 22 types "code.vegaprotocol.io/vega/protos/vega" 23 ) 24 25 type AssetDetails struct { 26 Name string `json:"name"` 27 Symbol string `json:"symbol"` 28 Decimals uint64 `json:"decimals"` 29 Quantum string `json:"quantum"` 30 Source *Source `json:"source"` 31 } 32 33 type Source struct { 34 BuiltinAsset *BuiltinAsset `json:"builtin_asset,omitempty"` 35 Erc20 *Erc20 `json:"erc20,omitempty"` 36 } 37 38 type BuiltinAsset struct { 39 MaxFaucetAmountMint string `json:"max_faucet_amount_mint"` 40 } 41 42 type Erc20 struct { 43 ContractAddress string `json:"contract_address"` 44 LifetimeLimit string `json:"lifetime_limit"` 45 WithdrawThreshold string `json:"withdraw_threshold"` 46 ChainID string `json:"chain_id"` 47 } 48 49 func (a *AssetDetails) IntoProto() (*types.AssetDetails, error) { 50 if a.Source == nil || (a.Source.BuiltinAsset == nil && a.Source.Erc20 == nil) { 51 return nil, errors.New("missing asset source") 52 } 53 54 if a.Source.BuiltinAsset != nil && a.Source.Erc20 != nil { 55 return nil, errors.New("multiple asset sources specified") 56 } 57 58 details := types.AssetDetails{ 59 Name: a.Name, 60 Symbol: a.Symbol, 61 Decimals: a.Decimals, 62 Quantum: a.Quantum, 63 } 64 65 if a.Source.BuiltinAsset != nil { 66 details.Source = &types.AssetDetails_BuiltinAsset{ 67 BuiltinAsset: &types.BuiltinAsset{ 68 MaxFaucetAmountMint: a.Source.BuiltinAsset.MaxFaucetAmountMint, 69 }, 70 } 71 } 72 73 if a.Source.Erc20 != nil { 74 details.Source = &types.AssetDetails_Erc20{ 75 Erc20: &types.ERC20{ 76 ContractAddress: a.Source.Erc20.ContractAddress, 77 WithdrawThreshold: a.Source.Erc20.WithdrawThreshold, 78 LifetimeLimit: a.Source.Erc20.LifetimeLimit, 79 ChainId: a.Source.Erc20.ChainID, 80 }, 81 } 82 } 83 84 return &details, nil 85 } 86 87 type GenesisState map[string]AssetDetails 88 89 var governanceAsset = AssetDetails{ 90 Name: "VOTE", 91 Symbol: "VOTE", 92 Decimals: 5, 93 Quantum: "1", 94 Source: &Source{ 95 BuiltinAsset: &BuiltinAsset{ 96 MaxFaucetAmountMint: "10000", 97 }, 98 }, 99 } 100 101 func DefaultGenesisState() GenesisState { 102 assets := map[string]AssetDetails{ 103 "VOTE": governanceAsset, 104 } 105 106 return assets 107 } 108 109 func LoadGenesisState(bytes []byte) (map[string]*types.AssetDetails, error) { 110 state := struct { 111 Assets GenesisState `json:"assets"` 112 }{} 113 err := json.Unmarshal(bytes, &state) 114 if err != nil { 115 return nil, err 116 } 117 118 // now convert them all into proto 119 out := map[string]*types.AssetDetails{} 120 for k, v := range state.Assets { 121 details, err := v.IntoProto() 122 if err != nil { 123 return nil, err 124 } 125 out[k] = details 126 } 127 128 return out, nil 129 }