Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generics for info and storage commands #1465

Merged
merged 6 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cargo-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ semver = "1.0"
jsonschema = "0.17"
schemars = "0.8"
ink_metadata = "5.0.0-rc"
ink_env = "5.0.0-rc"
crossterm = "0.27.0"

# dependencies for extrinsics (deploying and calling a contract)
Expand Down
39 changes: 26 additions & 13 deletions crates/cargo-contract/src/cmd/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ use contract_extrinsics::{
fetch_contract_info,
fetch_wasm_code,
url_to_string,
Balance,
CodeHash,
ContractInfo,
ErrorVariant,
TrieId,
};
use ink_env::{
DefaultEnvironment,
Environment,
};
use std::{
fmt::Debug,
io::Write,
Expand Down Expand Up @@ -103,7 +105,10 @@ impl InfoCommand {
.as_ref()
.expect("Contract argument was not provided");

let info_to_json = fetch_contract_info(contract, &rpc, &client).await?;
let info_to_json = fetch_contract_info::<DefaultConfig, DefaultEnvironment>(
contract, &rpc, &client,
)
.await?;

let wasm_code =
fetch_wasm_code(&client, &rpc, info_to_json.code_hash()).await?;
Expand All @@ -122,15 +127,19 @@ impl InfoCommand {
} else if self.output_json {
println!(
"{}",
serde_json::to_string_pretty(&ExtendedContractInfo::new(
info_to_json,
&wasm_code
serde_json::to_string_pretty(&ExtendedContractInfo::<
<DefaultConfig as Config>::Hash,
<DefaultEnvironment as Environment>::Balance,
>::new(
info_to_json, &wasm_code
))?
)
} else {
basic_display_format_extended_contract_info(&ExtendedContractInfo::new(
info_to_json,
&wasm_code,
basic_display_format_extended_contract_info(&ExtendedContractInfo::<
<DefaultConfig as Config>::Hash,
<DefaultEnvironment as Environment>::Balance,
>::new(
info_to_json, &wasm_code
))
}
Ok(())
Expand All @@ -139,17 +148,21 @@ impl InfoCommand {
}

#[derive(serde::Serialize)]
pub struct ExtendedContractInfo {
pub struct ExtendedContractInfo<Hash, Balance> {
pub trie_id: TrieId,
pub code_hash: CodeHash,
pub code_hash: Hash,
pub storage_items: u32,
pub storage_items_deposit: Balance,
pub storage_total_deposit: Balance,
pub source_language: String,
}

impl ExtendedContractInfo {
pub fn new(contract_info: ContractInfo, code: &[u8]) -> Self {
impl<Hash, Balance> ExtendedContractInfo<Hash, Balance>
where
Hash: serde::Serialize + Copy,
Balance: serde::Serialize + Copy,
{
pub fn new(contract_info: ContractInfo<Hash, Balance>, code: &[u8]) -> Self {
let language = match determine_language(code).ok() {
Some(lang) => lang.to_string(),
None => "Unknown".to_string(),
Expand Down
7 changes: 6 additions & 1 deletion crates/cargo-contract/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ use contract_extrinsics::{
Balance,
BalanceVariant,
};
use core::fmt;
use pallet_contracts_primitives::ContractResult;
use std::io::{
self,
Expand Down Expand Up @@ -224,7 +225,11 @@ pub fn print_gas_required_success(gas: Weight) {
}

/// Display contract information in a formatted way
pub fn basic_display_format_extended_contract_info(info: &ExtendedContractInfo) {
pub fn basic_display_format_extended_contract_info<Hash>(
info: &ExtendedContractInfo<Hash, Balance>,
) where
Hash: fmt::Debug,
{
name_value_println!("TrieId", info.trie_id, MAX_KEY_COL_WIDTH);
name_value_println!(
"Code Hash",
Expand Down
4 changes: 3 additions & 1 deletion crates/cargo-contract/src/cmd/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use contract_extrinsics::{
ErrorVariant,
};
use crossterm::terminal;
use ink_env::DefaultEnvironment;
use std::{
cmp,
path::PathBuf,
Expand Down Expand Up @@ -66,7 +67,8 @@ pub struct StorageCommand {
impl StorageCommand {
pub async fn run(&self) -> Result<(), ErrorVariant> {
let rpc = ContractStorageRpc::<DefaultConfig>::new(&self.url).await?;
let storage_layout = ContractStorage::<DefaultConfig>::new(rpc);
let storage_layout =
ContractStorage::<DefaultConfig, DefaultEnvironment>::new(rpc);

if self.raw {
let storage_data = storage_layout
Expand Down
1 change: 1 addition & 0 deletions crates/extrinsics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ subxt = "0.33.0"
subxt-signer = { version = "0.33.0", features = ["subxt", "sr25519"] }
hex = "0.4.3"
ink_metadata = "5.0.0-rc"
ink_env = "5.0.0-rc"

[dev-dependencies]
ink = "5.0.0-rc"
Expand Down
Loading
Loading