code.vegaprotocol.io/vega@v0.79.0/cmd/vega/commands/verify/asset.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 verify
    17  
    18  import (
    19  	"time"
    20  
    21  	types "code.vegaprotocol.io/vega/protos/vega"
    22  )
    23  
    24  type AssetCmd struct{}
    25  
    26  func (opts *AssetCmd) Execute(params []string) error {
    27  	return verifier(params, verifyAsset)
    28  }
    29  
    30  func verifyAsset(r *reporter, bs []byte) string {
    31  	prop := &types.Proposal{}
    32  	if !unmarshal(r, bs, prop) {
    33  		return ""
    34  	}
    35  
    36  	if len(prop.Reference) <= 0 {
    37  		r.Warn("no proposal.reference specified")
    38  	}
    39  
    40  	if len(prop.PartyId) <= 0 {
    41  		r.Err("proposal.partyID is missing")
    42  	} else {
    43  		if !isValidParty(prop.PartyId) {
    44  			r.Warn("proposal.partyID does not seems to be a valid party ID")
    45  		}
    46  	}
    47  
    48  	if prop.Terms == nil {
    49  		r.Err("missing proposal.Terms")
    50  	} else {
    51  		verifyAssetTerms(r, prop)
    52  	}
    53  
    54  	return marshal(prop)
    55  }
    56  
    57  func verifyAssetTerms(r *reporter, prop *types.Proposal) {
    58  	if prop.Terms.ClosingTimestamp == 0 {
    59  		r.Err("prop.terms.closingTimestamp is missing or 0")
    60  	} else if time.Unix(prop.Terms.ClosingTimestamp, 0).Before(time.Now()) {
    61  		r.Warn("prop.terms.closingTimestamp may be in the past")
    62  	}
    63  	if prop.Terms.ValidationTimestamp == 0 {
    64  		r.Err("prop.terms.validationTimestamp is missing or 0")
    65  	} else if time.Unix(prop.Terms.ValidationTimestamp, 0).Before(time.Now()) {
    66  		r.Warn("prop.terms.validationTimestamp may be in the past")
    67  	}
    68  
    69  	if prop.Terms.EnactmentTimestamp == 0 {
    70  		r.Err("prop.terms.enactmentTimestamp is missing or 0")
    71  	} else if time.Unix(prop.Terms.EnactmentTimestamp, 0).Before(time.Now()) {
    72  		r.Warn("prop.terms.enactmentTimestamp may be in the past")
    73  	}
    74  
    75  	newAsset := prop.Terms.GetNewAsset()
    76  	if newAsset == nil {
    77  		r.Err("prop.terms.newAsset is missing or null")
    78  		return
    79  	}
    80  	if newAsset.Changes == nil {
    81  		r.Err("prop.terms.newAsset.changes is missing or null")
    82  		return
    83  	}
    84  
    85  	if len(newAsset.Changes.Name) <= 0 {
    86  		r.Err("prop.terms.newAsset.changes.name is missing or empty")
    87  	}
    88  	if len(newAsset.Changes.Symbol) <= 0 {
    89  		r.Err("prop.terms.newAsset.changes.symbol is missing or empty")
    90  	}
    91  	if newAsset.Changes.Decimals == 0 {
    92  		r.Err("prop.terms.newAsset.changes.decimals is missing or empty")
    93  	}
    94  	if len(newAsset.Changes.Quantum) <= 0 {
    95  		r.Err("prop.terms.newAsset.changes.quantum is missing or empty")
    96  	}
    97  
    98  	switch source := newAsset.Changes.Source.(type) {
    99  	case *types.AssetDetails_Erc20:
   100  		contractAddress := source.Erc20.GetContractAddress()
   101  		if len(contractAddress) <= 0 {
   102  			r.Err("prop.terms.newAsset.changes.erc20.contractAddress is missing")
   103  		} else if !isValidEthereumAddress(contractAddress) {
   104  			r.Warn("prop.terms.newAsset.changes.erc20.contractAddress may not be a valid ethereum address")
   105  		}
   106  	default:
   107  		r.Err("unsupported prop.terms.newAsset.changes")
   108  	}
   109  }