如何导入 substrate_primitives 以使用 sr25519?
How to import substrate_primitives in order to use sr25519?
我的 Cargo.toml
文件中有以下依赖项:
[package]
name = "api-client-tutorial"
version = "0.1.0"
authors = ["Supercomputing Systems AG <info@scs.ch>"]
edition = "2018"
[dependencies]
substrate-api-client = { git = "https://github.com/scs/substrate-api-client.git" }
codec = { package = "parity-scale-codec", features = ["derive"], version = "1.0.0", default-features = false }
[dependencies.primitives]
git = "https://github.com/paritytech/substrate"
rev = "3bf9540e72df5ecb3955845764dfee7dcdbb26b5"
package = "substrate-primitives"
[dependencies.keyring]
git = "https://github.com/paritytech/substrate"
rev = "3bf9540e72df5ecb3955845764dfee7dcdbb26b5"
package = "substrate-keyring"
我不确定 dependencies
部分和 dependencies.primitives
部分之间的区别,但是包 substrate-primitives
包含在基元部分中。
我 have seen substrate_primitives
有我需要使用的模块 sr25519
,但是当我尝试在我的代码中导入它时:
use substrate_api_client::{Api, node_metadata};
use substrate_primitives::sr25519;
fn main() {
// instantiate an Api that connects to the given address
let url = "127.0.0.1:9944";
// if no signer is set in the whole program, we need to give to Api a specific type instead of an associated type
// as during compilation the type needs to be defined.
let api = Api::<sr25519::Pair>::new(format!("ws://{}", url));
let meta = api.get_metadata();
println!("Metadata:\n {}", node_metadata::pretty_format(&meta).unwrap());
}
我收到以下错误:
unresolved import `substrate_primitives`
use of undeclared type or module `substrate_primitives`rustc(E0432)
main.rs(2, 5): use of undeclared type or module `substrate_primitives`
如何导入 sr25519 以便我可以在我的代码中使用以下行?
let api = Api::<sr25519::Pair>::new(format!("ws://{}", url));
[dependencies]
下的table和[dependencies.primitives]
table的区别在于primitives
依赖的table不是内联。您也可以只内联 primitives
依赖项并将其放在 [dependencies]
下:
primitives = { git = "https://github.com/paritytech/substrate", rev = "3bf9540e72df5ecb3955845764dfee7dcdbb26b5", package = "substrate-primitives" }
toml documentation 可以为您提供有关 table 和内联 table 的更多详细信息。
关于您的问题。您不能像那样导入 crate,因为它已重命名为 primitives
。 package
字段指定依赖项的真实名称,table 名称定义用于将其导入到项目中的新名称。有关详细信息,请查看 cargo documentation.
因此您的导入应该如下所示:use primitives::sr25519;
我的 Cargo.toml
文件中有以下依赖项:
[package]
name = "api-client-tutorial"
version = "0.1.0"
authors = ["Supercomputing Systems AG <info@scs.ch>"]
edition = "2018"
[dependencies]
substrate-api-client = { git = "https://github.com/scs/substrate-api-client.git" }
codec = { package = "parity-scale-codec", features = ["derive"], version = "1.0.0", default-features = false }
[dependencies.primitives]
git = "https://github.com/paritytech/substrate"
rev = "3bf9540e72df5ecb3955845764dfee7dcdbb26b5"
package = "substrate-primitives"
[dependencies.keyring]
git = "https://github.com/paritytech/substrate"
rev = "3bf9540e72df5ecb3955845764dfee7dcdbb26b5"
package = "substrate-keyring"
我不确定 dependencies
部分和 dependencies.primitives
部分之间的区别,但是包 substrate-primitives
包含在基元部分中。
我 have seen substrate_primitives
有我需要使用的模块 sr25519
,但是当我尝试在我的代码中导入它时:
use substrate_api_client::{Api, node_metadata};
use substrate_primitives::sr25519;
fn main() {
// instantiate an Api that connects to the given address
let url = "127.0.0.1:9944";
// if no signer is set in the whole program, we need to give to Api a specific type instead of an associated type
// as during compilation the type needs to be defined.
let api = Api::<sr25519::Pair>::new(format!("ws://{}", url));
let meta = api.get_metadata();
println!("Metadata:\n {}", node_metadata::pretty_format(&meta).unwrap());
}
我收到以下错误:
unresolved import `substrate_primitives`
use of undeclared type or module `substrate_primitives`rustc(E0432)
main.rs(2, 5): use of undeclared type or module `substrate_primitives`
如何导入 sr25519 以便我可以在我的代码中使用以下行?
let api = Api::<sr25519::Pair>::new(format!("ws://{}", url));
[dependencies]
下的table和[dependencies.primitives]
table的区别在于primitives
依赖的table不是内联。您也可以只内联 primitives
依赖项并将其放在 [dependencies]
下:
primitives = { git = "https://github.com/paritytech/substrate", rev = "3bf9540e72df5ecb3955845764dfee7dcdbb26b5", package = "substrate-primitives" }
toml documentation 可以为您提供有关 table 和内联 table 的更多详细信息。
关于您的问题。您不能像那样导入 crate,因为它已重命名为 primitives
。 package
字段指定依赖项的真实名称,table 名称定义用于将其导入到项目中的新名称。有关详细信息,请查看 cargo documentation.
因此您的导入应该如下所示:use primitives::sr25519;