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

Expose the SCALE encoded call data of an extrinsic #573

Merged
merged 2 commits into from
Jun 22, 2022
Merged
Changes from all 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
23 changes: 13 additions & 10 deletions subxt/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,18 @@ where
.await
}

/// Return the SCALE encoded bytes representing the call data of the transaction.
pub fn call_data(&self) -> Result<Vec<u8>, BasicError> {
let mut bytes = Vec::new();
let locked_metadata = self.client.metadata();
let metadata = locked_metadata.read();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is very common to use the read only locked metadata, I wonder whether it is worth adding a method for that.

Copy link
Collaborator Author

@jsdw jsdw Jun 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm yeah, quite possibly; it might also be worth just cloning it out of the lock immediately too since it's behind an Arc iirc; no need to actually hold the lock for any appreciable amount of time either I think!

Copy link
Contributor

@ascjones ascjones Jun 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cloning the metadata seems a bit wasteful in most cases since as you say it is usually short lived

Sorry I misunderstood what you meant

let pallet = metadata.pallet(C::PALLET)?;
bytes.push(pallet.index());
bytes.push(pallet.call_index::<C>()?);
self.call.encode_to(&mut bytes);
Ok(bytes)
}

/// Creates a returns a raw signed extrinsic, without submitting it.
pub async fn create_signed(
&self,
Expand All @@ -371,16 +383,7 @@ where
};

// 2. SCALE encode call data to bytes (pallet u8, call u8, call params).
let call_data = {
let mut bytes = Vec::new();
let locked_metadata = self.client.metadata();
let metadata = locked_metadata.read();
let pallet = metadata.pallet(C::PALLET)?;
bytes.push(pallet.index());
bytes.push(pallet.call_index::<C>()?);
self.call.encode_to(&mut bytes);
Encoded(bytes)
};
let call_data = Encoded(self.call_data()?);

// 3. Construct our custom additional/extra params.
let additional_and_extra_params = {
Expand Down