paritytech / substrate

Substrate: The platform for blockchain innovators
Apache License 2.0
8.39k stars 2.65k forks source link

rpc: the arguments for api method are incorrect #14728

Open Snikko opened 1 year ago

Snikko commented 1 year ago

Is there an existing issue?

Experiencing problems? Have you tried our Stack Exchange first?

Description of bug

I want to add rpc function in my substrate blockchain to supply restful apis for others.But some mistake occur when the project is built.The errors show like this:

45 api.get_value(&at,v).map_err(runtime_error_into_rpc_err) --------- ^^^ expected associated type, found &BlockId<_>
arguments to this method are incorrect

1691463385285

Steps to reproduce

1、add rpc configuration to the prject correctly 2、build the project 3、errors like this:

45 api.get_value(&at,v).map_err(runtime_error_into_rpc_err) --------- ^^^ expected associated type, found &BlockId<_>
arguments to this method are incorrect

ggwpez commented 1 year ago

Did you follow some tutorial or how did you add it to the template?
Where did you get the code from?

Snikko commented 1 year ago

Did you follow some tutorial or how did you add it to the template? Where did you get the code from?

yeah,I followed some tutorials to add it to my template,sunch as https://substrate.stackexchange.com/questions/5554/how-to-add-custom-rpcs and https://github.com/AlexD10S/susbtrate-node-template/blob/rpc-custom-methods/pallets/template/rpc/src/lib.rs#L9 and others. I compared the codes among them and found that they did the same thing.But ,when I added the codes in my project, errors occured like that when it is built.So,maybe,I think wether the newest codes causing the problem,I am not sure.

ggwpez commented 1 year ago

@AlexD10S do you know if the tutorial maybe needs an update?

Snikko commented 1 year ago

@AlexD10S do you know if the tutorial maybe needs an update? yeah,I know.I download the newest version of substrate-node-template and add the rpc configuration to it following the tutorial,changing the version to the newest in the file Cargo.toml of rpc.The version is v1.0.0.(branch = "polkadot-v1.0.0")

error[E0308]: mismatched types --> pallets/template/src/rpc/src/lib.rs:45:23 45 api.get_value(&at,v).map_err(runtime_error_into_rpc_err) --------- ^^^ expected associated type, found &BlockId<_>
arguments to this method are incorrect
 = note: expected associated type `<Block as BlockT>::Hash`
                  found reference `&BlockId<_>`
help: a method is available that returns <Block as BlockT>::Hash --> /root/.cargo/git/checkouts/substrate-7e08433d4c370a21/948fbd2/primitives/runtime/src/traits.rs:1274:2 1274 fn hash(&self) -> Self::Hash { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider calling sp_api::BlockT::hash note: method defined here --> /home/nikko/substrate-node-template/pallets/template/src/rpc/runtime-api/src/lib.rs:7:12
7 fn get_value(v: u32) -> bool;
^^^^^^^^^

For more information about this error, try rustc --explain E0308.


1691546791033

Snikko commented 1 year ago

@AlexD10S do you know if the tutorial maybe needs an update?

I have found the answer.Thanks.

AlexD10S commented 1 year ago

I am going to take a look, probably it needs an update. The tutorial was done with polkadot-v0.9.28.

@Snikko If you have found an answer, can you share your solution here or in the StackExchange question with a new answer or editing mine. The community will benefit if you share your solution there.

Snikko commented 1 year ago

I am going to take a look, probably it needs an update. The tutorial was done with polkadot-v0.9.28.

@Snikko If you have found an answer, can you share your solution here or in the StackExchange question with a new answer or editing mine. The community will benefit if you share your solution there.

In recent versions,we have to change some codes in the file pallets/template/src/rpc/src/lib.rs,as follows:

impl<C, Block> TemplateApiServer<::Hash> for TemplatePallet<C, Block> where Block: BlockT, C: Send + Sync + 'static + ProvideRuntimeApi + HeaderBackend, C::Api: TemplateRuntimeApi, { fn get_value(&self, at: Option<::Hash>) -> RpcResult

{
    let api = self.client.runtime_api();
    let at = at.unwrap_or_else(||self.client.info().best_hash);

    api.get_value(at).map_err(runtime_error_into_rpc_err)
}

}