code.vegaprotocol.io/vega@v0.79.0/datanode/gateway/graphql/proposal_resolver.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 gql
    17  
    18  import (
    19  	"context"
    20  	"strconv"
    21  
    22  	"code.vegaprotocol.io/vega/libs/num"
    23  	types "code.vegaprotocol.io/vega/protos/vega"
    24  	vega "code.vegaprotocol.io/vega/protos/vega"
    25  )
    26  
    27  type proposalResolver VegaResolverRoot
    28  
    29  func (r *proposalResolver) RejectionReason(_ context.Context, data *types.GovernanceData) (*vega.ProposalError, error) {
    30  	return data.Proposal.Reason, nil
    31  }
    32  
    33  func (r *proposalResolver) ID(_ context.Context, data *types.GovernanceData) (*string, error) {
    34  	if data == nil || data.Proposal == nil {
    35  		return nil, ErrInvalidProposal
    36  	}
    37  	return &data.Proposal.Id, nil
    38  }
    39  
    40  func (r *proposalResolver) Reference(_ context.Context, data *types.GovernanceData) (string, error) {
    41  	if data == nil || data.Proposal == nil {
    42  		return "", ErrInvalidProposal
    43  	}
    44  	return data.Proposal.Reference, nil
    45  }
    46  
    47  func (r *proposalResolver) Party(ctx context.Context, data *types.GovernanceData) (*types.Party, error) {
    48  	if data == nil || data.Proposal == nil {
    49  		return nil, ErrInvalidProposal
    50  	}
    51  	p, err := getParty(ctx, r.log, r.tradingDataClientV2, data.Proposal.PartyId)
    52  	if p == nil && err == nil {
    53  		// the api could return an nil party in some cases
    54  		// e.g: when a party does not exists in the stores
    55  		// this is not an error, but here we are not checking
    56  		// if a party exists or not, but what party did propose
    57  		p = &types.Party{Id: data.Proposal.PartyId}
    58  	}
    59  	return p, err
    60  }
    61  
    62  func (r *proposalResolver) State(_ context.Context, data *types.GovernanceData) (vega.Proposal_State, error) {
    63  	if data == nil || data.Proposal == nil {
    64  		return vega.Proposal_STATE_UNSPECIFIED, ErrInvalidProposal
    65  	}
    66  	return data.Proposal.State, nil
    67  }
    68  
    69  func (r *proposalResolver) Datetime(_ context.Context, data *types.GovernanceData) (int64, error) {
    70  	if data == nil || data.Proposal == nil {
    71  		return 0, ErrInvalidProposal
    72  	}
    73  	return data.Proposal.Timestamp, nil
    74  }
    75  
    76  func (r *proposalResolver) Rationale(_ context.Context, data *types.GovernanceData) (*types.ProposalRationale, error) {
    77  	if data == nil || data.Proposal == nil {
    78  		return nil, ErrInvalidProposal
    79  	}
    80  	return data.Proposal.Rationale, nil
    81  }
    82  
    83  func (r *proposalResolver) Terms(_ context.Context, data *types.GovernanceData) (*types.ProposalTerms, error) {
    84  	if data == nil || data.Proposal == nil {
    85  		return nil, ErrInvalidProposal
    86  	}
    87  	return data.Proposal.Terms, nil
    88  }
    89  
    90  func (r *proposalResolver) Votes(_ context.Context, obj *types.GovernanceData) (*ProposalVotes, error) {
    91  	if obj == nil {
    92  		return nil, ErrInvalidProposal
    93  	}
    94  
    95  	var yesWeight float64
    96  	yesToken := num.UintZero()
    97  	var yesLPWeight num.Decimal
    98  	for _, yes := range obj.Yes {
    99  		weight, err := strconv.ParseFloat(yes.TotalGovernanceTokenWeight, 64)
   100  		if err != nil {
   101  			return nil, err
   102  		}
   103  		yesWeight += weight
   104  		yesUint, notOk := num.UintFromString(yes.TotalGovernanceTokenBalance, 10)
   105  		if notOk {
   106  			continue
   107  		}
   108  		yesToken.Add(yesToken, yesUint)
   109  		weightLP, err := num.DecimalFromString(yes.TotalEquityLikeShareWeight)
   110  		if err != nil {
   111  			return nil, err
   112  		}
   113  		yesLPWeight = yesLPWeight.Add(weightLP)
   114  	}
   115  	var noWeight float64
   116  	noToken := num.UintZero()
   117  	var noLPWeight num.Decimal
   118  	for _, no := range obj.No {
   119  		weight, err := strconv.ParseFloat(no.TotalGovernanceTokenWeight, 64)
   120  		if err != nil {
   121  			return nil, err
   122  		}
   123  		noWeight += weight
   124  		noUint, notOk := num.UintFromString(no.TotalGovernanceTokenBalance, 10)
   125  		if notOk {
   126  			continue
   127  		}
   128  		noToken.Add(noToken, noUint)
   129  		weightLP, err := num.DecimalFromString(no.TotalEquityLikeShareWeight)
   130  		if err != nil {
   131  			return nil, err
   132  		}
   133  		noLPWeight = noLPWeight.Add(weightLP)
   134  	}
   135  
   136  	votes := &ProposalVotes{
   137  		Yes: &ProposalVoteSide{
   138  			Votes:                      obj.Yes,
   139  			TotalNumber:                strconv.Itoa(len(obj.Yes)),
   140  			TotalWeight:                strconv.FormatFloat(yesWeight, 'f', -1, 64),
   141  			TotalTokens:                yesToken.String(),
   142  			TotalEquityLikeShareWeight: yesLPWeight.String(),
   143  		},
   144  		No: &ProposalVoteSide{
   145  			Votes:                      obj.No,
   146  			TotalNumber:                strconv.Itoa(len(obj.No)),
   147  			TotalWeight:                strconv.FormatFloat(noWeight, 'f', -1, 64),
   148  			TotalTokens:                noToken.String(),
   149  			TotalEquityLikeShareWeight: noLPWeight.String(),
   150  		},
   151  	}
   152  
   153  	return votes, nil
   154  }
   155  
   156  func (r *proposalResolver) ErrorDetails(_ context.Context, data *types.GovernanceData) (*string, error) {
   157  	return data.Proposal.ErrorDetails, nil
   158  }
   159  
   160  func (r *proposalResolver) RequiredMajority(_ context.Context, data *types.GovernanceData) (string, error) {
   161  	return data.Proposal.RequiredMajority, nil
   162  }
   163  
   164  func (r *proposalResolver) RequiredParticipation(_ context.Context, data *types.GovernanceData) (string, error) {
   165  	return data.Proposal.RequiredParticipation, nil
   166  }
   167  
   168  func (r *proposalResolver) RequiredLpMajority(_ context.Context, data *types.GovernanceData) (*string, error) {
   169  	return data.Proposal.RequiredLiquidityProviderMajority, nil
   170  }
   171  
   172  func (r *proposalResolver) RequiredLpParticipation(_ context.Context, data *types.GovernanceData) (*string, error) {
   173  	return data.Proposal.RequiredLiquidityProviderParticipation, nil
   174  }