Skip to content

Commit

Permalink
use near-abi-rs (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
miraclx committed Aug 26, 2022
1 parent 79b07d0 commit 885437e
Show file tree
Hide file tree
Showing 21 changed files with 136 additions and 139 deletions.
4 changes: 2 additions & 2 deletions cargo-near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ cargo_metadata = "0.14"
clap = { version = "3.2", features = ["derive", "env"] }
colored = "2.0"
env_logger = "0.9"
near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
log = "0.4"
prettyplease = "0.1"
toml = "0.5"
serde_json = "1.0"
symbolic-debuginfo = "8.8"
syn = "1.0"
quote = "1.0"
schemars = "0.8"
near-abi = { version = "0.1.0-pre.0", features = ["__chunked-entries"] }

[dev-dependencies]
function_name = "0.3"
proc-macro2 = "1.0"
schemars = "0.8"
7 changes: 3 additions & 4 deletions cargo-near/src/abi/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub(crate) fn generate_main_rs(dylib_path: &Path) -> anyhow::Result<String> {
let near_abi_function_defs = near_abi_symbols.iter().map(|s| {
let name = format_ident!("{}", s);
quote! {
fn #name() -> near_sdk::__private::AbiRoot;
fn #name() -> near_sdk::__private::ChunkedAbiEntry;
}
});
let near_abi_function_invocations = near_abi_symbols.iter().map(|s| {
Expand All @@ -141,9 +141,8 @@ pub(crate) fn generate_main_rs(dylib_path: &Path) -> anyhow::Result<String> {
}

fn main() -> Result<(), std::io::Error> {
let root_abis = vec![#(#near_abi_function_invocations),*];
let combined_root_abi = near_sdk::__private::AbiRoot::combine(root_abis);
let contents = serde_json::to_string_pretty(&combined_root_abi)?;
let abi_entries = vec![#(#near_abi_function_invocations),*];
let contents = serde_json::to_string_pretty(&abi_entries)?;
print!("{}", contents);
Ok(())
}
Expand Down
25 changes: 12 additions & 13 deletions cargo-near/src/abi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::cargo::{manifest::CargoManifestPath, metadata::CrateMetadata};
use crate::util;
use near_sdk::__private::schemars::schema::{Schema, SchemaObject};
use near_sdk::__private::{AbiMetadata, AbiRoot};
use std::collections::HashMap;
use std::{fs, path::PathBuf};

Expand Down Expand Up @@ -50,35 +48,36 @@ pub(crate) fn execute(
)],
)?;

let mut near_abi: AbiRoot = serde_json::from_slice(&stdout)?;
let metadata = extract_metadata(&crate_metadata);
near_abi.metadata = metadata;
let mut contract_abi = near_abi::__private::ChunkedAbiEntry::combine(
serde_json::from_slice::<Vec<_>>(&stdout)?.into_iter(),
)?
.into_abi_root(extract_metadata(&crate_metadata));
if !generate_docs {
strip_docs(&mut near_abi);
strip_docs(&mut contract_abi);
}
let near_abi_json = serde_json::to_string_pretty(&near_abi)?;
let near_abi_json = serde_json::to_string_pretty(&contract_abi)?;
let out_path_abi = crate_metadata.target_directory.join(ABI_FILE);
fs::write(&out_path_abi, near_abi_json)?;

Ok(AbiResult { path: out_path_abi })
}

fn extract_metadata(crate_metadata: &CrateMetadata) -> AbiMetadata {
fn extract_metadata(crate_metadata: &CrateMetadata) -> near_abi::AbiMetadata {
let package = &crate_metadata.root_package;
AbiMetadata {
near_abi::AbiMetadata {
name: Some(package.name.clone()),
version: Some(package.version.to_string()),
authors: package.authors.clone(),
other: HashMap::new(),
}
}

fn strip_docs(abi_root: &mut AbiRoot) {
for function in &mut abi_root.abi.functions {
fn strip_docs(abi_root: &mut near_abi::AbiRoot) {
for function in &mut abi_root.body.functions {
function.doc = None;
}
for schema in &mut abi_root.abi.root_schema.definitions.values_mut() {
if let Schema::Object(SchemaObject {
for schema in &mut abi_root.body.root_schema.definitions.values_mut() {
if let schemars::schema::Schema::Object(schemars::schema::SchemaObject {
metadata: Some(metadata),
..
}) = schema
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ publish = false

[dev-dependencies]
anyhow = "1.0"
borsh = "0.9"
cargo-near = { path = "../cargo-near" }
function_name = "0.3"
git2 = "0.14"
near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
prettyplease = "0.1"
proc-macro2 = "1.0"
schemars = "0.8"
serde_json = "1.0"
syn = "1.0"
tempfile = "3.3"
quote = "1.0"
near-abi = "0.1.0-pre.0"
2 changes: 1 addition & 1 deletion integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ macro_rules! generate_abi {
doc: false,
}))?;

let abi_root: near_sdk::__private::AbiRoot =
let abi_root: near_abi::AbiRoot =
serde_json::from_slice(&fs::read(workspace_dir.join(function_name!()).join("target").join("near").join("abi.json"))?)?;
abi_root
}};
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/templates/_Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
near-sdk = { version = "4.1.0-pre.2", features = ["abi"] }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/templates/_Cargo_no_abi_feature.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { version = "4.1.0-pre.1" }
near-sdk = { version = "4.1.0-pre.2" }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ serde = { version = "1", features = ["derive"] }
schemars = "0.8"

[dependencies.near-sdk]
version = "4.1.0-pre.1"
version = "4.1.0-pre.2"
features = ["abi"]

[workspace]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "::path::", version = "4.1.0-pre.1", features = ["abi"] }
near-sdk = { path = "::path::", version = "4.1.0-pre.2", features = ["abi"] }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { version = "4.1.0-pre.1", features = ["abi", "unstable"] }
near-sdk = { version = "4.1.0-pre.2", features = ["abi", "unstable"] }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { version = "4.1.0-pre.1", default-features = false, features = ["abi"] }
near-sdk = { version = "4.1.0-pre.2", default-features = false, features = ["abi"] }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

Expand Down
4 changes: 2 additions & 2 deletions integration-tests/templates/sdk-dependency/_Cargo_patch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
near-sdk = { version = "4.1.0-pre.2", features = ["abi"] }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

[patch.crates-io]
near-sdk = { git = "https://github.com/near/near-sdk-rs.git", rev = "aa151597033872d44d5639933540ce6f1eca505a" }
near-sdk = { git = "https://github.com/near/near-sdk-rs.git", rev = "83cb1c40403e4156bd6d1d258237bbdf03326afc" }

[workspace]
members = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ serde = { version = "1", features = ["derive"] }
schemars = "0.8"

[target.'cfg(windows)'.dependencies]
near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
near-sdk = { version = "4.1.0-pre.2", features = ["abi"] }

[target.'cfg(unix)'.dependencies]
near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
near-sdk = { version = "4.1.0-pre.2", features = ["abi"] }

[workspace]
members = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near = { version = "4.1.0-pre.1", package = "near-sdk", features = ["abi"] }
near = { version = "4.1.0-pre.2", package = "near-sdk", features = ["abi"] }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

Expand Down
54 changes: 27 additions & 27 deletions integration-tests/tests/cargo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn clone_git_repo(version: &str) -> anyhow::Result<TempDir> {
#[test]
#[named]
fn test_dependency_local_path() -> anyhow::Result<()> {
let near_sdk_dir = clone_git_repo("aa151597033872d44d5639933540ce6f1eca505a")?;
let near_sdk_dir = clone_git_repo("83cb1c40403e4156bd6d1d258237bbdf03326afc")?;
let near_sdk_dep_path = near_sdk_dir.path().join("near-sdk");

// near-sdk = { path = "::path::", features = ["abi"] }
Expand All @@ -30,8 +30,8 @@ fn test_dependency_local_path() -> anyhow::Result<()> {
pub fn foo(&self, a: bool, b: u32) {}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand All @@ -40,19 +40,19 @@ fn test_dependency_local_path() -> anyhow::Result<()> {
#[test]
#[named]
fn test_dependency_local_path_with_version() -> anyhow::Result<()> {
let near_sdk_dir = clone_git_repo("aa151597033872d44d5639933540ce6f1eca505a")?;
let near_sdk_dir = clone_git_repo("83cb1c40403e4156bd6d1d258237bbdf03326afc")?;
let near_sdk_dep_path = near_sdk_dir.path().join("near-sdk");

// near-sdk = { path = "::path::", version = "4.1.0-pre.1", features = ["abi"] }
// near-sdk = { path = "::path::", version = "4.1.0-pre.2", features = ["abi"] }
let abi_root = generate_abi_fn! {
with Cargo "/templates/sdk-dependency/_Cargo_local_path_with_version.toml",
and vars HashMap::from([("path", near_sdk_dep_path.to_str().unwrap())]);

pub fn foo(&self, a: bool, b: u32) {}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand All @@ -62,16 +62,16 @@ fn test_dependency_local_path_with_version() -> anyhow::Result<()> {
#[named]
fn test_dependency_explicit() -> anyhow::Result<()> {
// [dependencies.near-sdk]
// version = "4.1.0-pre.1"
// version = "4.1.0-pre.2"
// features = ["abi"]
let abi_root = generate_abi_fn! {
with Cargo "/templates/sdk-dependency/_Cargo_explicit.toml";

pub fn foo(&self, a: bool, b: u32) {}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand All @@ -80,15 +80,15 @@ fn test_dependency_explicit() -> anyhow::Result<()> {
#[test]
#[named]
fn test_dependency_no_default_features() -> anyhow::Result<()> {
// near-sdk = { version = "4.1.0-pre.1", default-features = false, features = ["abi"] }
// near-sdk = { version = "4.1.0-pre.2", default-features = false, features = ["abi"] }
let abi_root = generate_abi_fn! {
with Cargo "/templates/sdk-dependency/_Cargo_no_default_features.toml";

pub fn foo(&self, a: bool, b: u32) {}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand All @@ -97,15 +97,15 @@ fn test_dependency_no_default_features() -> anyhow::Result<()> {
#[test]
#[named]
fn test_dependency_multiple_features() -> anyhow::Result<()> {
// near-sdk = { version = "4.1.0-pre.1", features = ["abi", "unstable"] }
// near-sdk = { version = "4.1.0-pre.2", features = ["abi", "unstable"] }
let abi_root = generate_abi_fn! {
with Cargo "/templates/sdk-dependency/_Cargo_multiple_features.toml";

pub fn foo(&self, a: bool, b: u32) {}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand All @@ -117,18 +117,18 @@ fn test_dependency_multiple_features() -> anyhow::Result<()> {
#[named]
fn test_dependency_platform_specific() -> anyhow::Result<()> {
// [target.'cfg(windows)'.dependencies]
// near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
// near-sdk = { version = "4.1.0-pre.2", features = ["abi"] }
//
// [target.'cfg(unix)'.dependencies]
// near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
// near-sdk = { version = "4.1.0-pre.2", features = ["abi"] }
let abi_root = generate_abi_fn! {
with Cargo "/templates/sdk-dependency/_Cargo_platform_specific.toml";

pub fn foo(&self, a: bool, b: u32) {}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand All @@ -139,7 +139,7 @@ fn test_dependency_platform_specific() -> anyhow::Result<()> {
#[test]
#[named]
fn test_dependency_renamed() -> anyhow::Result<()> {
// near = { version = "4.1.0-pre.1", package = "near-sdk", features = ["abi"] }
// near = { version = "4.1.0-pre.2", package = "near-sdk", features = ["abi"] }
let abi_root = generate_abi! {
with Cargo "/templates/sdk-dependency/_Cargo_renamed.toml";

Expand All @@ -156,8 +156,8 @@ fn test_dependency_renamed() -> anyhow::Result<()> {
}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand All @@ -169,18 +169,18 @@ fn test_dependency_renamed() -> anyhow::Result<()> {
#[named]
fn test_dependency_patch() -> anyhow::Result<()> {
// [dependencies]
// near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
// near-sdk = { version = "4.1.0-pre.2", features = ["abi"] }
//
// [patch.crates-io]
// near-sdk = { git = "https://github.com/near/near-sdk-rs.git", rev = "aa151597033872d44d5639933540ce6f1eca505a" }
// near-sdk = { git = "https://github.com/near/near-sdk-rs.git", rev = "83cb1c40403e4156bd6d1d258237bbdf03326afc" }
let abi_root = generate_abi_fn! {
with Cargo "/templates/sdk-dependency/_Cargo_patch.toml";

pub fn foo(&self, a: bool, b: u32) {}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand Down
Loading

0 comments on commit 885437e

Please sign in to comment.