github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/extrinsic_status.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  	"strings"
    23  
    24  	"github.com/stafiprotocol/go-substrate-rpc-client/pkg/scale"
    25  )
    26  
    27  // ExtrinsicStatus is an enum containing the result of an extrinsic submission
    28  type ExtrinsicStatus struct {
    29  	IsFuture          bool // 00:: Future
    30  	IsReady           bool // 1:: Ready
    31  	IsBroadcast       bool // 2:: Broadcast(Vec<Text>)
    32  	AsBroadcast       []Text
    33  	IsInBlock         bool // 3:: InBlock(BlockHash)
    34  	AsInBlock         Hash
    35  	IsRetracted       bool // 4:: Retracted(BlockHash)
    36  	AsRetracted       Hash
    37  	IsFinalityTimeout bool // 5:: FinalityTimeout(BlockHash)
    38  	AsFinalityTimeout Hash
    39  	IsFinalized       bool // 6:: Finalized(BlockHash)
    40  	AsFinalized       Hash
    41  	IsUsurped         bool // 7:: Usurped(Hash)
    42  	AsUsurped         Hash
    43  	IsDropped         bool // 8:: Dropped
    44  	IsInvalid         bool // 9:: Invalid
    45  }
    46  
    47  func (e *ExtrinsicStatus) Decode(decoder scale.Decoder) error {
    48  	b, err := decoder.ReadOneByte()
    49  
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	switch b {
    55  	case 0:
    56  		e.IsFuture = true
    57  	case 1:
    58  		e.IsReady = true
    59  	case 2:
    60  		e.IsBroadcast = true
    61  		err = decoder.Decode(&e.AsBroadcast)
    62  	case 3:
    63  		e.IsInBlock = true
    64  		err = decoder.Decode(&e.AsInBlock)
    65  	case 4:
    66  		e.IsRetracted = true
    67  		err = decoder.Decode(&e.AsRetracted)
    68  	case 5:
    69  		e.IsFinalityTimeout = true
    70  		err = decoder.Decode(&e.AsFinalityTimeout)
    71  	case 6:
    72  		e.IsFinalized = true
    73  		err = decoder.Decode(&e.AsFinalized)
    74  	case 7:
    75  		e.IsUsurped = true
    76  		err = decoder.Decode(&e.AsUsurped)
    77  	case 8:
    78  		e.IsDropped = true
    79  	case 9:
    80  		e.IsInvalid = true
    81  	}
    82  
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	return nil
    88  }
    89  
    90  func (e ExtrinsicStatus) Encode(encoder scale.Encoder) error {
    91  	var err1, err2 error
    92  	switch {
    93  	case e.IsFuture:
    94  		err1 = encoder.PushByte(0)
    95  	case e.IsReady:
    96  		err1 = encoder.PushByte(1)
    97  	case e.IsBroadcast:
    98  		err1 = encoder.PushByte(2)
    99  		err2 = encoder.Encode(e.AsBroadcast)
   100  	case e.IsInBlock:
   101  		err1 = encoder.PushByte(3)
   102  		err2 = encoder.Encode(e.AsInBlock)
   103  	case e.IsRetracted:
   104  		err1 = encoder.PushByte(4)
   105  		err2 = encoder.Encode(e.AsRetracted)
   106  	case e.IsFinalityTimeout:
   107  		err1 = encoder.PushByte(5)
   108  		err2 = encoder.Encode(e.AsFinalityTimeout)
   109  	case e.IsFinalized:
   110  		err1 = encoder.PushByte(6)
   111  		err2 = encoder.Encode(e.AsFinalized)
   112  	case e.IsUsurped:
   113  		err1 = encoder.PushByte(7)
   114  		err2 = encoder.Encode(e.AsUsurped)
   115  	case e.IsDropped:
   116  		err1 = encoder.PushByte(8)
   117  	case e.IsInvalid:
   118  		err1 = encoder.PushByte(9)
   119  	}
   120  
   121  	if err1 != nil {
   122  		return err1
   123  	}
   124  	if err2 != nil {
   125  		return err2
   126  	}
   127  
   128  	return nil
   129  }
   130  
   131  func (e *ExtrinsicStatus) UnmarshalJSON(b []byte) error {
   132  	input := strings.TrimSpace(string(b))
   133  	if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
   134  		input = input[1 : len(input)-1]
   135  	}
   136  
   137  	switch {
   138  	case input == "future":
   139  		e.IsFuture = true
   140  		return nil
   141  	case input == "ready":
   142  		e.IsReady = true
   143  		return nil
   144  	case input == "dropped":
   145  		e.IsDropped = true
   146  		return nil
   147  	case input == "invalid":
   148  		e.IsInvalid = true
   149  		return nil
   150  	}
   151  
   152  	// no simple case, decode into helper
   153  	var tmp struct {
   154  		AsBroadcast       []Text `json:"broadcast"`
   155  		AsInBlock         Hash   `json:"inBlock"`
   156  		AsRetracted       Hash   `json:"retracted"`
   157  		AsFinalityTimeout Hash   `json:"finalityTimeout"`
   158  		AsFinalized       Hash   `json:"finalized"`
   159  		AsUsurped         Hash   `json:"usurped"`
   160  	}
   161  	if err := json.Unmarshal(b, &tmp); err != nil {
   162  		return err
   163  	}
   164  
   165  	switch {
   166  	case strings.HasPrefix(input, "{\"broadcast\""):
   167  		e.IsBroadcast = true
   168  		e.AsBroadcast = tmp.AsBroadcast
   169  		return nil
   170  	case strings.HasPrefix(input, "{\"inBlock\""):
   171  		e.IsInBlock = true
   172  		e.AsInBlock = tmp.AsInBlock
   173  		return nil
   174  	case strings.HasPrefix(input, "{\"retracted\""):
   175  		e.IsRetracted = true
   176  		e.AsRetracted = tmp.AsRetracted
   177  		return nil
   178  	case strings.HasPrefix(input, "{\"finalityTimeout\""):
   179  		e.IsFinalityTimeout = true
   180  		e.AsFinalityTimeout = tmp.AsFinalityTimeout
   181  		return nil
   182  	case strings.HasPrefix(input, "{\"finalized\""):
   183  		e.IsFinalized = true
   184  		e.AsFinalized = tmp.AsFinalized
   185  		return nil
   186  	case strings.HasPrefix(input, "{\"usurped\""):
   187  		e.IsUsurped = true
   188  		e.AsUsurped = tmp.AsUsurped
   189  		return nil
   190  	}
   191  
   192  	return fmt.Errorf("unexpected JSON for ExtrinsicStatus, got %v", string(b))
   193  }
   194  
   195  func (e ExtrinsicStatus) MarshalJSON() ([]byte, error) {
   196  	switch {
   197  	case e.IsFuture:
   198  		return []byte("\"future\""), nil
   199  	case e.IsReady:
   200  		return []byte("\"ready\""), nil
   201  	case e.IsDropped:
   202  		return []byte("\"dropped\""), nil
   203  	case e.IsInvalid:
   204  		return []byte("\"invalid\""), nil
   205  	case e.IsBroadcast:
   206  		var tmp struct {
   207  			AsBroadcast []Text `json:"broadcast"`
   208  		}
   209  		tmp.AsBroadcast = e.AsBroadcast
   210  		return json.Marshal(tmp)
   211  	case e.IsInBlock:
   212  		var tmp struct {
   213  			AsInBlock Hash `json:"inBlock"`
   214  		}
   215  		tmp.AsInBlock = e.AsInBlock
   216  		return json.Marshal(tmp)
   217  	case e.IsRetracted:
   218  		var tmp struct {
   219  			AsRetracted Hash `json:"retracted"`
   220  		}
   221  		tmp.AsRetracted = e.AsRetracted
   222  		return json.Marshal(tmp)
   223  	case e.IsFinalityTimeout:
   224  		var tmp struct {
   225  			AsFinalityTimeout Hash `json:"finalityTimeout"`
   226  		}
   227  		tmp.AsFinalityTimeout = e.AsFinalityTimeout
   228  		return json.Marshal(tmp)
   229  	case e.IsFinalized:
   230  		var tmp struct {
   231  			AsFinalized Hash `json:"finalized"`
   232  		}
   233  		tmp.AsFinalized = e.AsFinalized
   234  		return json.Marshal(tmp)
   235  	case e.IsUsurped:
   236  		var tmp struct {
   237  			AsUsurped Hash `json:"usurped"`
   238  		}
   239  		tmp.AsUsurped = e.AsUsurped
   240  		return json.Marshal(tmp)
   241  	}
   242  	return nil, fmt.Errorf("cannot marshal ExtrinsicStatus, got %#v", e)
   243  }