code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/referral/staking_tiers_configs.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 referral 17 18 import ( 19 "embed" 20 "fmt" 21 "io" 22 23 "code.vegaprotocol.io/vega/core/integration/steps/helpers" 24 "code.vegaprotocol.io/vega/core/types" 25 vegapb "code.vegaprotocol.io/vega/protos/vega" 26 27 "github.com/golang/protobuf/jsonpb" 28 "github.com/jinzhu/copier" 29 ) 30 31 var ( 32 //go:embed defaults/staking-tiers/*.json 33 defaultStakingTiersConfigs embed.FS 34 defaultStakingTiersNames = []string{ 35 "defaults/staking-tiers/default.json", 36 } 37 ) 38 39 type stakingTiersConfig struct { 40 config map[string][]*types.StakingTier 41 } 42 43 func newStakingTiersConfigs() *stakingTiersConfig { 44 config := &stakingTiersConfig{ 45 config: map[string][]*types.StakingTier{}, 46 } 47 48 contentReaders := helpers.ReadAll(defaultStakingTiersConfigs, defaultStakingTiersNames) 49 for name, contentReader := range contentReaders { 50 stakingTiersConfig, err := unmarshalStakingTiers(contentReader) 51 if err != nil { 52 panic(fmt.Errorf("couldn't unmarshal default staking tiers config %s: %v", name, err)) 53 } 54 config.Add(name, stakingTiersConfig) 55 } 56 57 return config 58 } 59 60 func (f *stakingTiersConfig) Add(name string, stakingTiers []*types.StakingTier) { 61 f.config[name] = stakingTiers 62 } 63 64 func (f *stakingTiersConfig) Get(name string) ([]*types.StakingTier, error) { 65 stakingTiers, ok := f.config[name] 66 if !ok { 67 return nil, fmt.Errorf("no staking tiers configuration registered for name %q", name) 68 } 69 70 // Copy to avoid modification between tests. 71 copyConfig := []*types.StakingTier{} 72 if err := copier.Copy(©Config, &stakingTiers); err != nil { 73 return nil, fmt.Errorf("failed to deep copy staking tiers configuration: %v", err) 74 } 75 return copyConfig, nil 76 } 77 78 func unmarshalStakingTiers(r io.Reader) ([]*types.StakingTier, error) { 79 proto := &vegapb.ReferralProgram{} 80 unmarshaler := jsonpb.Unmarshaler{} 81 err := unmarshaler.Unmarshal(r, proto) 82 if err != nil { 83 return nil, err 84 } 85 referralProgram := types.NewReferralProgramFromProto(proto) 86 return referralProgram.StakingTiers, nil 87 }