2

我的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"

我不确定dependenciessection 和dependencies.primitivessection 之间的区别,但包substrate-primitives包含在原语部分中。

已经看到有我需要使用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));
4

1 回答 1

1

[dependencies]table under和[dependencies.primitives]table的区别在于primitives依赖的表没有内联。您也可以只内联primitives依赖项并将其放在下面[dependencies]

primitives = { git = "https://github.com/paritytech/substrate", rev = "3bf9540e72df5ecb3955845764dfee7dcdbb26b5", package = "substrate-primitives" }

toml 文档可以为您提供有关表和内联表的更多详细信息。

关于你的问题。你不能像那样导入箱子,因为它被重命名为primitives. 该package字段指定依赖项的真实名称,表名定义用于将其导入项目中的新名称。详情请查看货物文件

因此,您的导入应如下所示:use primitives::sr25519;

于 2020-03-01T09:49:19.213 回答