Skip to content

Commit

Permalink
Adde generics to upload and remove commands
Browse files Browse the repository at this point in the history
  • Loading branch information
smiasojed committed Jan 23, 2024
1 parent e48a522 commit 4bc30da
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 91 deletions.
18 changes: 11 additions & 7 deletions crates/cargo-contract/src/cmd/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ use contract_extrinsics::{
DefaultConfig,
ExtrinsicOptsBuilder,
RemoveCommandBuilder,
RemoveExec,
RemoveResult,
};
use ink_env::DefaultEnvironment;
use subxt::Config;

#[derive(Debug, clap::Args)]
Expand Down Expand Up @@ -55,12 +58,13 @@ impl RemoveCommand {
.suri(self.extrinsic_cli_opts.suri.clone())
.storage_deposit_limit(self.extrinsic_cli_opts.storage_deposit_limit.clone())
.done();
let remove_exec = RemoveCommandBuilder::default()
.code_hash(self.code_hash)
.extrinsic_opts(extrinsic_opts)
.done()
.await?;
let remove_result = remove_exec.remove_code().await?;
let remove_exec: RemoveExec<DefaultConfig, DefaultEnvironment> =
RemoveCommandBuilder::default()
.code_hash(self.code_hash)
.extrinsic_opts(extrinsic_opts)
.done()
.await?;
let remove_result: RemoveResult<_, _> = remove_exec.remove_code().await?;
let display_events = remove_result.display_events;
let output_events = if self.output_json() {
display_events.to_json()?
Expand All @@ -71,7 +75,7 @@ impl RemoveCommand {
)?
};
if let Some(code_removed) = remove_result.code_removed {
let remove_result = code_removed.code_hash;
let remove_result: <DefaultConfig as Config>::Hash = code_removed.code_hash;

if self.output_json() {
// Create a JSON object with the events and the removed code hash.
Expand Down
12 changes: 6 additions & 6 deletions crates/extrinsics/src/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ use rust_decimal::{
Decimal,
};
use serde_json::json;
use subxt::backend::legacy::LegacyRpcMethods;

use super::{
Balance,
DefaultConfig,
use subxt::{
backend::legacy::LegacyRpcMethods,
Config,
};

use super::Balance;

use anyhow::{
anyhow,
Context,
Expand Down Expand Up @@ -75,7 +75,7 @@ pub enum UnitPrefix {

impl TokenMetadata {
/// Query [TokenMetadata] through the node's RPC
pub async fn query(client: &LegacyRpcMethods<DefaultConfig>) -> Result<Self> {
pub async fn query<C: Config>(client: &LegacyRpcMethods<C>) -> Result<Self> {
let sys_props = client.system_properties().await?;

let default_decimals = json!(12);
Expand Down
2 changes: 1 addition & 1 deletion crates/extrinsics/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl CallExec {
let result =
submit_extrinsic(&self.client, &self.rpc, &call, &self.signer).await?;

let display_events = DisplayEvents::from_events(
let display_events = DisplayEvents::from_events::<DefaultConfig>(
&result,
Some(&self.transcoder),
&self.client.metadata(),
Expand Down
67 changes: 42 additions & 25 deletions crates/extrinsics/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
// along with cargo-contract. If not, see <http://www.gnu.org/licenses/>.

use super::{
Balance,
BalanceVariant,
CodeHash,
DefaultConfig,
TokenMetadata,
};
use crate::DEFAULT_KEY_COL_WIDTH;
Expand All @@ -42,7 +39,10 @@ use subxt::{
blocks::ExtrinsicEvents,
events::StaticEvent,
ext::{
scale_decode,
scale_decode::{
self,
IntoVisitor,
},
scale_encode,
},
Config,
Expand All @@ -58,12 +58,15 @@ use subxt::{
)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
pub struct ContractEmitted {
pub contract: <DefaultConfig as Config>::AccountId,
pub struct ContractEmitted<AccountId> {
pub contract: AccountId,
pub data: Vec<u8>,
}

impl StaticEvent for ContractEmitted {
impl<AccountId> StaticEvent for ContractEmitted<AccountId>
where
AccountId: IntoVisitor,
{
const PALLET: &'static str = "Contracts";
const EVENT: &'static str = "ContractEmitted";
}
Expand All @@ -78,14 +81,17 @@ impl StaticEvent for ContractEmitted {
)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
pub struct ContractInstantiated {
pub struct ContractInstantiated<AccountId> {
/// Account id of the deployer.
pub deployer: <DefaultConfig as Config>::AccountId,
pub deployer: AccountId,
/// Account id where the contract was instantiated to.
pub contract: <DefaultConfig as Config>::AccountId,
pub contract: AccountId,
}

impl StaticEvent for ContractInstantiated {
impl<AccountId> StaticEvent for ContractInstantiated<AccountId>
where
AccountId: IntoVisitor,
{
const PALLET: &'static str = "Contracts";
const EVENT: &'static str = "Instantiated";
}
Expand All @@ -100,12 +106,15 @@ impl StaticEvent for ContractInstantiated {
)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
pub struct CodeStored {
pub struct CodeStored<Hash> {
/// Hash under which the contract code was stored.
pub code_hash: CodeHash,
pub code_hash: Hash,
}

impl StaticEvent for CodeStored {
impl<Hash> StaticEvent for CodeStored<Hash>
where
Hash: IntoVisitor,
{
const PALLET: &'static str = "Contracts";
const EVENT: &'static str = "CodeStored";
}
Expand All @@ -120,13 +129,18 @@ impl StaticEvent for CodeStored {
)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
pub struct CodeRemoved {
pub code_hash: CodeHash,
pub struct CodeRemoved<Hash, Balance, AccountId> {
pub code_hash: Hash,
pub deposit_released: Balance,
pub remover: <DefaultConfig as Config>::AccountId,
pub remover: AccountId,
}

impl StaticEvent for CodeRemoved {
impl<Hash, Balance, AccountId> StaticEvent for CodeRemoved<Hash, Balance, AccountId>
where
Hash: IntoVisitor,
Balance: IntoVisitor,
AccountId: IntoVisitor,
{
const PALLET: &'static str = "Contracts";
const EVENT: &'static str = "CodeRemoved";
}
Expand Down Expand Up @@ -170,11 +184,14 @@ pub struct DisplayEvents(Vec<Event>);

impl DisplayEvents {
/// Parses events and returns an object which can be serialised
pub fn from_events(
result: &ExtrinsicEvents<DefaultConfig>,
pub fn from_events<C: Config>(
result: &ExtrinsicEvents<C>,
transcoder: Option<&ContractMessageTranscoder>,
subxt_metadata: &subxt::Metadata,
) -> Result<DisplayEvents> {
) -> Result<DisplayEvents>
where
C::AccountId: IntoVisitor,
{
let mut events: Vec<Event> = vec![];

let events_transcoder = TranscoderBuilder::new(subxt_metadata.types())
Expand Down Expand Up @@ -202,13 +219,13 @@ impl DisplayEvents {
let event_sig_topic = event.topics().iter().next();
let mut unnamed_field_name = 0;
for field_metadata in event_fields {
if <ContractEmitted as StaticEvent>::is_event(
if <ContractEmitted<C::AccountId> as StaticEvent>::is_event(
event.pallet_name(),
event.variant_name(),
) && field_metadata.name == Some("data".to_string())
{
tracing::debug!("event data: {:?}", hex::encode(&event_data));
let field = contract_event_data_field(
let field = contract_event_data_field::<C>(
transcoder,
field_metadata,
event_sig_topic,
Expand Down Expand Up @@ -300,10 +317,10 @@ impl DisplayEvents {

/// Construct the contract event data field, attempting to decode the event using the
/// [`ContractMessageTranscoder`] if available.
fn contract_event_data_field(
fn contract_event_data_field<C: Config>(
transcoder: Option<&ContractMessageTranscoder>,
field_metadata: &scale_info::Field<PortableForm>,
event_sig_topic: Option<&sp_core::H256>,
event_sig_topic: Option<&C::Hash>,
event_data: &mut &[u8],
) -> Result<Field> {
let event_value = if let Some(transcoder) = transcoder {
Expand Down
6 changes: 3 additions & 3 deletions crates/extrinsics/src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,11 @@ impl InstantiateExec {
// The CodeStored event is only raised if the contract has not already been
// uploaded.
let code_hash = result
.find_first::<CodeStored>()?
.find_first::<CodeStored<<DefaultConfig as Config>::Hash>>()?
.map(|code_stored| code_stored.code_hash);

let instantiated = result
.find_last::<ContractInstantiated>()?
.find_last::<ContractInstantiated<<DefaultConfig as Config>::AccountId>>()?
.ok_or_else(|| anyhow!("Failed to find Instantiated event"))?;

Ok(InstantiateExecResult {
Expand Down Expand Up @@ -417,7 +417,7 @@ impl InstantiateExec {
submit_extrinsic(&self.client, &self.rpc, &call, &self.signer).await?;

let instantiated = result
.find_first::<ContractInstantiated>()?
.find_first::<ContractInstantiated<<DefaultConfig as Config>::AccountId>>()?
.ok_or_else(|| anyhow!("Failed to find Instantiated event"))?;

Ok(InstantiateExecResult {
Expand Down
7 changes: 5 additions & 2 deletions crates/extrinsics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub use instantiate::{
pub use remove::{
RemoveCommandBuilder,
RemoveExec,
RemoveResult,
};

pub use subxt::PolkadotConfig as DefaultConfig;
Expand Down Expand Up @@ -153,7 +154,9 @@ where
T: Config,
Call: tx::TxPayload,
Signer: tx::Signer<T>,
T::Signature: From<subxt_signer::sr25519::Signature>,
<T::ExtrinsicParams as config::ExtrinsicParams<T>>::OtherParams: Default,
T::AccountId: From<subxt_signer::sr25519::PublicKey>,
{
let account_id = Signer::account_id(signer);
let account_nonce = get_account_nonce(client, rpc, &account_id).await?;
Expand Down Expand Up @@ -220,8 +223,8 @@ where
Ok(account_nonce)
}

async fn state_call<A: Encode, R: Decode>(
rpc: &LegacyRpcMethods<DefaultConfig>,
async fn state_call<C: Config, A: Encode, R: Decode>(
rpc: &LegacyRpcMethods<C>,
func: &str,
args: A,
) -> Result<R> {
Expand Down
Loading

0 comments on commit 4bc30da

Please sign in to comment.