github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/rpc/state/get_storage.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 state
    18  
    19  import (
    20  	"github.com/stafiprotocol/go-substrate-rpc-client/pkg/client"
    21  	"github.com/stafiprotocol/go-substrate-rpc-client/types"
    22  )
    23  
    24  // GetStorage retreives the stored data and decodes them into the provided interface. Ok is true if the value is not
    25  // empty.
    26  func (s *State) GetStorage(key types.StorageKey, target interface{}, blockHash types.Hash) (ok bool, err error) {
    27  	raw, err := s.getStorageRaw(key, &blockHash)
    28  	if err != nil {
    29  		return false, err
    30  	}
    31  	if len(*raw) == 0 {
    32  		return false, nil
    33  	}
    34  	return true, types.DecodeFromBytes(*raw, target)
    35  }
    36  
    37  // GetStorageLatest retreives the stored data for the latest block height and decodes them into the provided interface.
    38  // Ok is true if the value is not empty.
    39  func (s *State) GetStorageLatest(key types.StorageKey, target interface{}) (ok bool, err error) {
    40  	raw, err := s.getStorageRaw(key, nil)
    41  	if err != nil {
    42  		return false, err
    43  	}
    44  	if len(*raw) == 0 {
    45  		return false, nil
    46  	}
    47  	return true, types.DecodeFromBytes(*raw, target)
    48  }
    49  
    50  // GetStorageRaw retreives the stored data as raw bytes, without decoding them
    51  func (s *State) GetStorageRaw(key types.StorageKey, blockHash types.Hash) (*types.StorageDataRaw, error) {
    52  	return s.getStorageRaw(key, &blockHash)
    53  }
    54  
    55  // GetStorageRawLatest retreives the stored data for the latest block height as raw bytes, without decoding them
    56  func (s *State) GetStorageRawLatest(key types.StorageKey) (*types.StorageDataRaw, error) {
    57  	return s.getStorageRaw(key, nil)
    58  }
    59  
    60  func (s *State) getStorageRaw(key types.StorageKey, blockHash *types.Hash) (*types.StorageDataRaw, error) {
    61  	var res string
    62  	err := client.CallWithBlockHash(s.client, &res, "state_getStorage", blockHash, key.Hex())
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	bz, err := types.HexDecodeString(res)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	data := types.NewStorageDataRaw(bz)
    73  	return &data, nil
    74  }