Skip to content

Commit

Permalink
Code refactoring and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
smiasojed committed Nov 14, 2023
1 parent 88aba33 commit 7fbf8c8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 52 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bump `subxt` to `0.32.0` - [#1352](https://github.com/paritytech/cargo-contract/pull/1352)
- Remove check for compatible `scale` and `scale-info` versions - [#1370](https://github.com/paritytech/cargo-contract/pull/1370)
- Add workspace support -[#1358](https://github.com/paritytech/cargo-contract/pull/1358)
- Add `Storage Total Deposit` to `info` command output - [#1347](https://github.com/paritytech/cargo-contract/pull/1347)
- Add dynamic types support - [#1399](https://github.com/paritytech/cargo-contract/pull/1399)

## [4.0.0-alpha]

Expand Down
73 changes: 39 additions & 34 deletions crates/extrinsics/src/contract_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ use subxt::{
utils::AccountId32,
};

#[derive(DecodeAsType, Debug)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
struct AccountData {
pub free: Balance,
pub reserved: Balance,
}

#[derive(DecodeAsType, Debug)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
pub struct BoundedVec<T>(pub ::std::vec::Vec<T>);

/// Return the account data for an account ID.
async fn get_account_balance(
account: &AccountId32,
Expand All @@ -68,24 +57,12 @@ async fn get_account_balance(
.await?
.ok_or_else(|| anyhow::anyhow!("Failed to fetch account data"))?;

#[derive(DecodeAsType, Debug)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
struct AccountInfo {
data: AccountData,
}

let data = account.as_type::<AccountInfo>()?.data;
Ok(data)
}

/// Decode the deposit account from the contract info
fn get_deposit_account_id(contract_info: &DecodedValueThunk) -> Result<AccountId32> {
#[derive(DecodeAsType)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
struct DepositAccount {
deposit_account: AccountId32,
}

let account = contract_info.as_type::<DepositAccount>()?;
Ok(account.deposit_account)
}
Expand Down Expand Up @@ -115,15 +92,6 @@ pub async fn fetch_contract_info(
)
})?;

#[derive(DecodeAsType, Debug)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
struct ContractInfoOf {
trie_id: BoundedVec<u8>,
code_hash: CodeHash,
storage_items: u32,
storage_item_deposit: Balance,
}

// Pallet-contracts [>=10, <15] store the contract's deposit as a free balance
// in a secondary account (deposit account). Other versions store it as
// reserved balance on the main contract's account. If the
Expand Down Expand Up @@ -226,7 +194,7 @@ fn parse_contract_account_address(
.map_err(|err| anyhow!("AccountId deserialization error: {}", err))
}

/// Fetch all contract addresses from the storage using the provided client
/// Fetch all contract addresses from the storage using the provided client.
pub async fn fetch_all_contracts(
client: &Client,
rpc: &LegacyRpcMethods<DefaultConfig>,
Expand Down Expand Up @@ -254,6 +222,43 @@ pub async fn fetch_all_contracts(
Ok(contract_accounts)
}

/// A struct used in the storage reads to access account info.
#[derive(DecodeAsType, Debug)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
struct AccountInfo {
data: AccountData,
}

/// A struct used in the storage reads to access account data.
#[derive(Debug, DecodeAsType)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
struct AccountData {
pub free: Balance,
pub reserved: Balance,
}

/// A struct representing `Vec`` used in the storage reads.
#[derive(Debug, DecodeAsType)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
struct BoundedVec<T>(pub ::std::vec::Vec<T>);

/// A struct used in the storage reads to access contract info.
#[derive(Debug, DecodeAsType)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
struct ContractInfoOf {
trie_id: BoundedVec<u8>,
code_hash: CodeHash,
storage_items: u32,
storage_item_deposit: Balance,
}

/// A struct used in storage reads to access the deposit account from contract info.
#[derive(Debug, DecodeAsType)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
struct DepositAccount {
deposit_account: AccountId32,
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -267,7 +272,7 @@ mod tests {
DecodeWithMetadata,
};

/// Find the type index in the metadata.
// Find the type index in the metadata.
fn get_metadata_type_index(
ident: &'static str,
module_path: &'static str,
Expand Down
6 changes: 4 additions & 2 deletions crates/extrinsics/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use subxt::{
Config,
};

/// A custom event emitted by the contract.
#[derive(
scale::Decode,
scale::Encode,
Expand All @@ -57,7 +58,6 @@ use subxt::{
)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
/// A custom event emitted by the contract.
pub struct ContractEmitted {
pub contract: <DefaultConfig as Config>::AccountId,
pub data: Vec<u8>,
Expand Down Expand Up @@ -90,6 +90,7 @@ impl StaticEvent for ContractInstantiated {
const EVENT: &'static str = "Instantiated";
}

/// An event triggered by either the `instantiate_with_code` or the `upload_code` call.
#[derive(
Debug,
scale::Decode,
Expand All @@ -109,6 +110,7 @@ impl StaticEvent for CodeStored {
const EVENT: &'static str = "CodeStored";
}

/// An event triggered by the `remove_code` call.
#[derive(
Debug,
scale::Decode,
Expand Down Expand Up @@ -151,7 +153,7 @@ impl Field {
}
}

/// An event produced from from invoking a contract extrinsic.
/// An event produced from invoking a contract extrinsic.
#[derive(serde::Serialize)]
pub struct Event {
/// name of a pallet
Expand Down
1 change: 0 additions & 1 deletion crates/extrinsics/src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ impl InstantiateExec {
code: Vec<u8>,
gas_limit: Weight,
) -> Result<InstantiateExecResult, ErrorVariant> {
// TODO: check if should be used unvalidated
let call = subxt::tx::Payload::new(
"Contracts",
"instantiate_with_code",
Expand Down
13 changes: 0 additions & 13 deletions crates/extrinsics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ use contract_build::{
CrateMetadata,
DEFAULT_KEY_COL_WIDTH,
};
use pallet_contracts_primitives::{
ContractResult,
InstantiateReturnValue,
};
use scale::{
Decode,
Encode,
Expand All @@ -57,8 +53,6 @@ use subxt::{
};
use subxt_signer::sr25519::Keypair;

use sp_runtime::DispatchError;

pub use balance::{
BalanceVariant,
TokenMetadata,
Expand Down Expand Up @@ -113,13 +107,6 @@ pub type Client = OnlineClient<DefaultConfig>;
pub type Balance = u128;
pub type CodeHash = <DefaultConfig as Config>::Hash;

/// Result type of a `bare_instantiate` call as well as `ContractsApi::instantiate`.
pub type ContractInstantiateResult<AccountId, Balance, EventRecord> = ContractResult<
Result<InstantiateReturnValue<AccountId>, DispatchError>,
Balance,
EventRecord,
>;

/// The Wasm code of a contract.
#[derive(Debug)]
pub struct WasmCode(Vec<u8>);
Expand Down
2 changes: 1 addition & 1 deletion crates/extrinsics/src/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl RemoveCommandBuilder<state::ExtrinsicOptions> {
}
#[derive(scale_encode::EncodeAsType)]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
struct RemoveCode {
pub struct RemoveCode {
code_hash: CodeHash,
}

Expand Down

0 comments on commit 7fbf8c8

Please sign in to comment.