github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/storage_change_set.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 24 // StorageChangeSet contains changes from storage subscriptions 25 type StorageChangeSet struct { 26 Block Hash `json:"block"` 27 Changes []KeyValueOption `json:"changes"` 28 } 29 30 type KeyValueOption struct { 31 StorageKey StorageKey 32 HasStorageData bool 33 StorageData StorageDataRaw 34 } 35 36 func (r *KeyValueOption) UnmarshalJSON(b []byte) error { 37 var tmp []string 38 if err := json.Unmarshal(b, &tmp); err != nil { 39 return err 40 } 41 switch len(tmp) { 42 case 0: 43 return fmt.Errorf("expected at least one entry for KeyValueOption") 44 case 2: 45 r.HasStorageData = true 46 data, err := HexDecodeString(tmp[1]) 47 if err != nil { 48 return err 49 } 50 r.StorageData = data 51 fallthrough 52 case 1: 53 key, err := HexDecodeString(tmp[0]) 54 if err != nil { 55 return err 56 } 57 r.StorageKey = key 58 default: 59 return fmt.Errorf("expected 1 or 2 entries for KeyValueOption, got %v", len(tmp)) 60 } 61 return nil 62 } 63 64 func (r KeyValueOption) MarshalJSON() ([]byte, error) { 65 var tmp []string 66 if r.HasStorageData { 67 tmp = []string{r.StorageKey.Hex(), r.StorageData.Hex()} 68 } else { 69 tmp = []string{r.StorageKey.Hex()} 70 } 71 return json.Marshal(tmp) 72 }