github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/runtime_version.go (about)

     1  // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
     2  //
     3  // Copyright 2020 Stafi Protocol
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package types
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  
    23  	"github.com/stafiprotocol/go-substrate-rpc-client/pkg/scale"
    24  )
    25  
    26  type RuntimeVersion struct {
    27  	APIs               []RuntimeVersionAPI `json:"apis"`
    28  	AuthoringVersion   U32                 `json:"authoringVersion"`
    29  	ImplName           string              `json:"implName"`
    30  	ImplVersion        U32                 `json:"implVersion"`
    31  	SpecName           string              `json:"specName"`
    32  	SpecVersion        U32                 `json:"specVersion"`
    33  	TransactionVersion U32                 `json:"transactionVersion"`
    34  }
    35  
    36  func NewRuntimeVersion() *RuntimeVersion {
    37  	return &RuntimeVersion{APIs: make([]RuntimeVersionAPI, 0)}
    38  }
    39  
    40  func (r *RuntimeVersion) Decode(decoder scale.Decoder) error {
    41  	err := decoder.Decode(&r.APIs)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	err = decoder.Decode(&r.AuthoringVersion)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	err = decoder.Decode(&r.ImplName)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	err = decoder.Decode(&r.ImplVersion)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	err = decoder.Decode(&r.SpecName)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	err = decoder.Decode(&r.SpecVersion)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func (r RuntimeVersion) Encode(encoder scale.Encoder) error {
    75  	err := encoder.Encode(r.APIs)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	err = encoder.Encode(r.AuthoringVersion)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	err = encoder.Encode(r.ImplName)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	err = encoder.Encode(r.ImplVersion)
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	err = encoder.Encode(r.SpecName)
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	err = encoder.Encode(r.SpecVersion)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	return nil
   106  }
   107  
   108  type RuntimeVersionAPI struct {
   109  	APIID   string
   110  	Version U32
   111  }
   112  
   113  func (r *RuntimeVersionAPI) UnmarshalJSON(b []byte) error {
   114  	tmp := []interface{}{&r.APIID, &r.Version}
   115  	wantLen := len(tmp)
   116  	if err := json.Unmarshal(b, &tmp); err != nil {
   117  		return err
   118  	}
   119  	if g, e := len(tmp), wantLen; g != e {
   120  		return fmt.Errorf("wrong number of fields in Notification: %d != %d", g, e)
   121  	}
   122  	return nil
   123  }
   124  
   125  func (r RuntimeVersionAPI) MarshalJSON() ([]byte, error) {
   126  	tmp := []interface{}{r.APIID, r.Version}
   127  	return json.Marshal(tmp)
   128  }
   129  
   130  func (r *RuntimeVersionAPI) Decode(decoder scale.Decoder) error {
   131  	err := decoder.Decode(&r.APIID)
   132  	if err != nil {
   133  		return err
   134  	}
   135  
   136  	err = decoder.Decode(&r.Version)
   137  	if err != nil {
   138  		return err
   139  	}
   140  
   141  	return nil
   142  }
   143  
   144  func (r RuntimeVersionAPI) Encode(encoder scale.Encoder) error {
   145  	err := encoder.Encode(r.APIID)
   146  	if err != nil {
   147  		return err
   148  	}
   149  
   150  	err = encoder.Encode(r.Version)
   151  	if err != nil {
   152  		return err
   153  	}
   154  
   155  	return nil
   156  }