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

use near-abi-rs #23

Merged
merged 17 commits into from
Aug 26, 2022
8 changes: 6 additions & 2 deletions cargo-near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@ 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"

[dependencies.near-abi]
git = "https://github.com/near/near-abi-rs"
rev = "4844ef4ff3e5cf3c3a8aa6565c3195a2339a345f"
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
21 changes: 10 additions & 11 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) {
fn strip_docs(abi_root: &mut near_abi::AbiRoot) {
for function in &mut abi_root.abi.functions {
function.doc = None;
}
for schema in &mut abi_root.abi.root_schema.definitions.values_mut() {
if let Schema::Object(SchemaObject {
if let schemars::schema::Schema::Object(schemars::schema::SchemaObject {
metadata: Some(metadata),
..
}) = schema
Expand Down
6 changes: 5 additions & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ 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"

[dependencies.near-abi]
git = "https://github.com/near/near-abi-rs"
rev = "4844ef4ff3e5cf3c3a8aa6565c3195a2339a345f"
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
6 changes: 5 additions & 1 deletion integration-tests/templates/_Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ edition = "2021"
crate-type = ["cdylib"]

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

[dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
rev = "0d4a4ac3f7bf759f10430b6ecf0864d87114798d"
features = ["abi"]

[workspace]
members = []

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

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

[dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
rev = "0d4a4ac3f7bf759f10430b6ecf0864d87114798d"

[workspace]
members = []

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

[dependencies.near-sdk]
version = "4.1.0-pre.1"
git = "https://github.com/near/near-sdk-rs.git"
rev = "0d4a4ac3f7bf759f10430b6ecf0864d87114798d"
features = ["abi"]

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

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

[dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
rev = "0d4a4ac3f7bf759f10430b6ecf0864d87114798d"
features = ["abi", "unstable"]

[workspace]
members = []

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

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

[dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
rev = "0d4a4ac3f7bf759f10430b6ecf0864d87114798d"
default-features = false
features = ["abi"]

[workspace]
members = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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 = "0d4a4ac3f7bf759f10430b6ecf0864d87114798d" }

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

[target.'cfg(windows)'.dependencies]
near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
[target.'cfg(windows)'.dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
rev = "0d4a4ac3f7bf759f10430b6ecf0864d87114798d"
features = ["abi"]

[target.'cfg(unix)'.dependencies]
near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
[target.'cfg(unix)'.dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
rev = "0d4a4ac3f7bf759f10430b6ecf0864d87114798d"
features = ["abi"]

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

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

[dependencies.near]
package = "near-sdk"
git = "https://github.com/near/near-sdk-rs.git"
rev = "0d4a4ac3f7bf759f10430b6ecf0864d87114798d"
features = ["abi"]

[workspace]
members = []

Expand Down
6 changes: 3 additions & 3 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("0d4a4ac3f7bf759f10430b6ecf0864d87114798d")?;
let near_sdk_dep_path = near_sdk_dir.path().join("near-sdk");

// near-sdk = { path = "::path::", features = ["abi"] }
Expand All @@ -40,7 +40,7 @@ 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("0d4a4ac3f7bf759f10430b6ecf0864d87114798d")?;
let near_sdk_dep_path = near_sdk_dir.path().join("near-sdk");

// near-sdk = { path = "::path::", version = "4.1.0-pre.1", features = ["abi"] }
Expand Down Expand Up @@ -172,7 +172,7 @@ fn test_dependency_patch() -> anyhow::Result<()> {
// near-sdk = { version = "4.1.0-pre.1", 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 = "0d4a4ac3f7bf759f10430b6ecf0864d87114798d" }
let abi_root = generate_abi_fn! {
with Cargo "/templates/sdk-dependency/_Cargo_patch.toml";

Expand Down
6 changes: 2 additions & 4 deletions integration-tests/tests/tests/borsh_schema.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use borsh::schema::{BorshSchemaContainer, Definition, Fields};
use cargo_near_integration_tests::{generate_abi, generate_abi_fn};
use function_name::named;
use near_sdk::{
__private::{AbiParameter, AbiType},
borsh::schema::{BorshSchemaContainer, Definition, Fields},
};
use near_abi::{AbiParameter, AbiType};
use std::{collections::HashMap, fs};

trait AsBorshSchema {
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/tests/callbacks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cargo_near_integration_tests::generate_abi_fn;
use function_name::named;
use near_sdk::__private::{AbiParameter, AbiType};
use near_abi::{AbiParameter, AbiType};
use schemars::gen::SchemaGenerator;
use std::fs;

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/tests/e2e.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cargo_near_integration_tests::generate_abi_fn;
use function_name::named;
use near_sdk::__private::{AbiFunction, AbiParameter, AbiType};
use near_abi::{AbiFunction, AbiParameter, AbiType};
use schemars::gen::SchemaGenerator;
use std::fs;

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/tests/json_schema.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cargo_near_integration_tests::{generate_abi, generate_abi_fn};
use function_name::named;
use near_sdk::__private::{AbiParameter, AbiType};
use near_abi::{AbiParameter, AbiType};
use schemars::schema::Schema;
use std::fs;

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/tests/result.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cargo_near_integration_tests::generate_abi_fn;
use function_name::named;
use near_sdk::__private::AbiType;
use near_abi::AbiType;
use schemars::gen::SchemaGenerator;
use std::fs;

Expand Down