Skip to content

Commit

Permalink
Function renamings in state api interface + improved documentation (#…
Browse files Browse the repository at this point in the history
…448)

* rename key_hash to storage_key

* rename parameters storage_prefix -> pallet, storage_key_name -> key

* cargo fmt

* renamings in state

* rename key -> storage_item

* rename get_storage_map_key_prefix -> get_storage_key_prefix

* more namings in state.rs

* added comments

* added missing renames in metadata.rs

* renamings and comments after code review
  • Loading branch information
masapr authored Feb 10, 2023
1 parent 9b084fc commit b140271
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 121 deletions.
4 changes: 2 additions & 2 deletions examples/examples/get_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async fn main() {
let mut api = Api::<_, _, PlainTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();

// get some plain storage value
let result: u128 = api.get_storage_value("Balances", "TotalIssuance", None).unwrap().unwrap();
let result: u128 = api.get_storage("Balances", "TotalIssuance", None).unwrap().unwrap();
println!("[+] TotalIssuance is {}", result);

let proof = api.get_storage_value_proof("Balances", "TotalIssuance", None).unwrap();
Expand Down Expand Up @@ -71,7 +71,7 @@ async fn main() {
println!("Retrieving value for key {:?}", storage_key);
// We're expecting account info as return value because we fetch added a prefix of "System" + "Account".
let storage_data: AccountInfo =
api.get_storage_by_key_hash(storage_key.clone(), None).unwrap().unwrap();
api.get_storage_by_key(storage_key.clone(), None).unwrap().unwrap();
println!("Retrieved data {:?}", storage_data);
}
}
5 changes: 2 additions & 3 deletions examples/examples/staking_batch_payout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ async fn main() {
let validator_stash =
AccountId32::from_ss58check("5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY").unwrap();

let active_era: ActiveEraInfo =
api.get_storage_value("Staking", "ActiveEra", None).unwrap().unwrap();
let active_era: ActiveEraInfo = api.get_storage("Staking", "ActiveEra", None).unwrap().unwrap();
println!("{:?}", active_era);
let current_era_index = active_era.index;

Expand Down Expand Up @@ -150,7 +149,7 @@ pub fn get_last_reward_received_for(
let ledger_storage_key = api.metadata().storage_map_key("Staking", "Ledger", account).unwrap();

let claimed_rewards: Vec<u32> =
match api.get_storage_by_key_hash::<StakingLedger>(ledger_storage_key, None) {
match api.get_storage_by_key::<StakingLedger>(ledger_storage_key, None) {
Ok(Some(ledger)) => ledger.claimed_rewards,
_ => Vec::new(),
};
Expand Down
44 changes: 17 additions & 27 deletions node-api/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,50 +455,40 @@ impl TryFrom<RuntimeMetadataPrefixed> for Metadata {
impl Metadata {
pub fn storage_value_key(
&self,
storage_prefix: &'static str,
storage_key_name: &'static str,
pallet: &'static str,
storage_item: &'static str,
) -> Result<StorageKey, MetadataError> {
Ok(self
.pallet(storage_prefix)?
.storage(storage_key_name)?
.get_value(storage_prefix)?
.key())
Ok(self.pallet(pallet)?.storage(storage_item)?.get_value(pallet)?.key())
}

pub fn storage_map_key<K: Encode>(
&self,
storage_prefix: &'static str,
storage_key_name: &'static str,
pallet: &'static str,
storage_item: &'static str,
map_key: K,
) -> Result<StorageKey, MetadataError> {
Ok(self
.pallet(storage_prefix)?
.storage(storage_key_name)?
.get_map::<K>(storage_prefix)?
.key(map_key))
Ok(self.pallet(pallet)?.storage(storage_item)?.get_map::<K>(pallet)?.key(map_key))
}

pub fn storage_map_key_prefix(
&self,
storage_prefix: &'static str,
storage_key_name: &'static str,
pallet: &'static str,
storage_item: &'static str,
) -> Result<StorageKey, MetadataError> {
self.pallet(storage_prefix)?
.storage(storage_key_name)?
.get_map_prefix(storage_prefix)
self.pallet(pallet)?.storage(storage_item)?.get_map_prefix(pallet)
}

pub fn storage_double_map_key<K: Encode, Q: Encode>(
&self,
storage_prefix: &'static str,
storage_key_name: &'static str,
first: K,
second: Q,
pallet: &'static str,
storage_item: &'static str,
first_double_map_key: K,
second_double_map_key: Q,
) -> Result<StorageKey, MetadataError> {
Ok(self
.pallet(storage_prefix)?
.storage(storage_key_name)?
.get_double_map::<K, Q>(storage_prefix)?
.key(first, second))
.pallet(pallet)?
.storage(storage_item)?
.get_double_map::<K, Q>(pallet)?
.key(first_double_map_key, second_double_map_key))
}
}
2 changes: 1 addition & 1 deletion src/api/rpc_api/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ where
fn fetch_events_from_block(&self, block_hash: Runtime::Hash) -> Result<Events<Runtime::Hash>> {
let key = crate::storage_key("System", "Events");
let event_bytes = self
.get_opaque_storage_by_key_hash(key, Some(block_hash))?
.get_opaque_storage_by_key(key, Some(block_hash))?
.ok_or(Error::BlockNotFound)?;
let events =
Events::<Runtime::Hash>::new(self.metadata().clone(), Default::default(), event_bytes);
Expand Down
2 changes: 1 addition & 1 deletion src/api/rpc_api/frame_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ where
)?;

info!("storage key is: 0x{}", hex::encode(&storagekey));
self.get_storage_by_key_hash(storagekey, None)
self.get_storage_by_key(storagekey, None)
}

fn get_account_data(
Expand Down
Loading

0 comments on commit b140271

Please sign in to comment.