Skip to content

Commit

Permalink
Merge branch 'master' into phuong/update-workspaces-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoticTempest authored Sep 22, 2022
2 parents 567e712 + 0926ae9 commit 7d31a5b
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 132 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## [Unreleased]

### Removed
- Deleted `metadata` macro. Use https://github.com/near/abi instead. [PR 920](https://github.com/near/near-sdk-rs/pull/920)

### Fixes
- Updated the associated error type for `Base58CryptoHash` parsing through `TryFrom` to concrete type. [PR 919](https://github.com/near/near-sdk-rs/pull/919)

## [4.1.0-pre.3] - 2022-08-30

### Added
Expand Down
16 changes: 14 additions & 2 deletions examples/adder/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 51 additions & 44 deletions examples/adder/res/adder_abi.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
{
"schema_version": "0.1.0",
"schema_version": "0.2.0",
"metadata": {
"name": "adder",
"version": "0.1.0",
"authors": [
"Near Inc <[email protected]>"
]
],
"build": {
"compiler": "rustc 1.61.0",
"builder": "cargo-near 0.2.0"
},
"wasm_hash": "B4XgA4rGVyaCWyDv2h1XAN5QMbtdJN1frRwoxELUMvDe"
},
"body": {
"functions": [
{
"name": "add",
"doc": " Adds two pairs point-wise.",
"is_view": true,
"params": [
{
"name": "a",
"serialization_type": "json",
"type_schema": {
"$ref": "#/definitions/Pair"
}
},
{
"name": "b",
"serialization_type": "json",
"type_schema": {
"$ref": "#/definitions/Pair"
"params": {
"serialization_type": "json",
"args": [
{
"name": "a",
"type_schema": {
"$ref": "#/definitions/Pair"
}
},
{
"name": "b",
"type_schema": {
"$ref": "#/definitions/Pair"
}
}
}
],
]
},
"result": {
"serialization_type": "json",
"type_schema": {
Expand All @@ -39,38 +45,39 @@
{
"name": "add_borsh",
"is_view": true,
"params": [
{
"name": "a",
"serialization_type": "borsh",
"type_schema": {
"declaration": "Pair",
"definitions": {
"Pair": {
"Struct": [
"u32",
"u32"
]
"params": {
"serialization_type": "borsh",
"args": [
{
"name": "a",
"type_schema": {
"declaration": "Pair",
"definitions": {
"Pair": {
"Struct": [
"u32",
"u32"
]
}
}
}
}
},
{
"name": "b",
"serialization_type": "borsh",
"type_schema": {
"declaration": "Pair",
"definitions": {
"Pair": {
"Struct": [
"u32",
"u32"
]
},
{
"name": "b",
"type_schema": {
"declaration": "Pair",
"definitions": {
"Pair": {
"Struct": [
"u32",
"u32"
]
}
}
}
}
}
],
]
},
"result": {
"serialization_type": "borsh",
"type_schema": {
Expand Down
4 changes: 2 additions & 2 deletions examples/fungible-token/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions examples/status-message/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{env, log, metadata, near_bindgen, AccountId};

use near_sdk::{env, log, near_bindgen, AccountId};
use std::collections::HashMap;

metadata! {
#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct StatusMessage {
Expand All @@ -19,12 +17,11 @@ impl StatusMessage {
self.records.insert(account_id, message);
}

pub fn get_status(&self, account_id: AccountId) -> Option::<String> {
pub fn get_status(&self, account_id: AccountId) -> Option<String> {
log!("get_status for account_id {}", account_id);
self.records.get(&account_id).cloned()
}
}
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
Expand Down
52 changes: 42 additions & 10 deletions near-sdk-macros/src/core_impl/abi/abi_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,21 @@ impl ImplItemMethodInfo {
let arg_name = arg.ident.to_string();
match arg.bindgen_ty {
BindgenArgType::Regular => {
let abi_type = generate_abi_type(typ, &arg.serializer_ty);
params.push(quote! {
near_sdk::__private::AbiParameter {
name: #arg_name.to_string(),
typ: #abi_type
}
});
let schema = generate_schema(typ, &arg.serializer_ty);
match arg.serializer_ty {
SerializerType::JSON => params.push(quote! {
near_sdk::__private::AbiJsonParameter {
name: #arg_name.to_string(),
type_schema: #schema,
}
}),
SerializerType::Borsh => params.push(quote! {
near_sdk::__private::AbiBorshParameter {
name: #arg_name.to_string(),
type_schema: #schema,
}
}),
};
}
BindgenArgType::CallbackArg => {
callbacks.push(generate_abi_type(typ, &arg.serializer_ty));
Expand Down Expand Up @@ -146,6 +154,18 @@ impl ImplItemMethodInfo {
}
};
}
let params = match self.attr_signature_info.input_serializer {
SerializerType::JSON => quote! {
near_sdk::__private::AbiParameters::Json {
args: vec![#(#params),*]
}
},
SerializerType::Borsh => quote! {
near_sdk::__private::AbiParameters::Borsh {
args: vec![#(#params),*]
}
},
};
let callback_vec = callback_vec.unwrap_or(quote! { None });

let result = match self.attr_signature_info.method_type {
Expand Down Expand Up @@ -198,7 +218,7 @@ impl ImplItemMethodInfo {
is_init: #is_init,
is_payable: #is_payable,
is_private: #is_private,
params: vec![#(#params),*],
params: #params,
callbacks: vec![#(#callbacks),*],
callbacks_vec: #callback_vec,
result: #result
Expand All @@ -207,16 +227,28 @@ impl ImplItemMethodInfo {
}
}

fn generate_schema(ty: &Type, serializer_type: &SerializerType) -> TokenStream2 {
match serializer_type {
SerializerType::JSON => quote! {
gen.subschema_for::<#ty>()
},
SerializerType::Borsh => quote! {
<#ty>::schema_container()
},
}
}

fn generate_abi_type(ty: &Type, serializer_type: &SerializerType) -> TokenStream2 {
let schema = generate_schema(ty, serializer_type);
match serializer_type {
SerializerType::JSON => quote! {
near_sdk::__private::AbiType::Json {
type_schema: gen.subschema_for::<#ty>(),
type_schema: #schema,
}
},
SerializerType::Borsh => quote! {
near_sdk::__private::AbiType::Borsh {
type_schema: <#ty>::schema_container(),
type_schema: #schema,
}
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl ImplItemMethodInfo {
/// }
/// ```
/// If args are serialized with Borsh it will not include `#[derive(borsh::BorshSchema)]`.
pub fn metadata_struct(&self) -> TokenStream2 {
pub(crate) fn metadata_struct(&self) -> TokenStream2 {
let method_name_str = self.attr_signature_info.ident.to_string();
let is_view = matches!(&self.attr_signature_info.method_type, &MethodType::View);
let is_init = matches!(
Expand Down
2 changes: 1 addition & 1 deletion near-sdk-macros/src/core_impl/metadata/metadata_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use syn::{Error, ItemImpl};

/// Information relevant to metadata extracted from the `impl` section decorated with `#[near_bindgen]`.
#[derive(Default)]
pub struct MetadataVisitor {
pub(crate) struct MetadataVisitor {
impl_item_infos: Vec<ItemImplInfo>,
/// Errors that occured while extracting the data.
errors: Vec<Error>,
Expand Down
4 changes: 2 additions & 2 deletions near-sdk-macros/src/core_impl/metadata/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod metadata_generator;
pub mod metadata_visitor;
pub(crate) mod metadata_generator;
pub(crate) mod metadata_visitor;
6 changes: 3 additions & 3 deletions near-sdk-macros/src/core_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ mod code_generator;
mod info_extractor;
mod metadata;
mod utils;
pub use code_generator::*;
pub use info_extractor::*;
pub use metadata::metadata_visitor::MetadataVisitor;
pub(crate) use code_generator::*;
pub(crate) use info_extractor::*;
pub(crate) use metadata::metadata_visitor::MetadataVisitor;
4 changes: 4 additions & 0 deletions near-sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ pub fn init(_attr: TokenStream, item: TokenStream) -> TokenStream {
/// `metadata` generates the metadata method and should be placed at the very end of the `lib.rs` file.
// TODO: Once Rust allows inner attributes and custom procedural macros for modules we should switch this
// to be `#![metadata]` attribute at the top of the contract file instead. https://github.com/rust-lang/rust/issues/54727
#[deprecated(
since = "4.1.0",
note = "metadata macro is no longer used. Use https://github.com/near/abi to generate a contract schema"
)]
#[proc_macro]
pub fn metadata(item: TokenStream) -> TokenStream {
if let Ok(input) = syn::parse::<File>(item) {
Expand Down
2 changes: 1 addition & 1 deletion near-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ wee_alloc = { version = "0.4.5", default-features = false, optional = true }
# Used for caching, might be worth porting only functionality needed.
once_cell = { version = "1.8", default-features = false }

near-abi = { version = "0.1.0-pre.0", features = ["__chunked-entries"], optional = true }
near-abi = { version = "0.2.0", features = ["__chunked-entries"], optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# alt_bn128 feature will need to be removed on the next version update (now stabilized)
Expand Down
2 changes: 0 additions & 2 deletions near-sdk/compilation_tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ fn compilation_tests() {
t.pass("compilation_tests/regular.rs");
t.pass("compilation_tests/private.rs");
t.pass("compilation_tests/trait_impl.rs");
t.pass("compilation_tests/metadata.rs");
t.compile_fail("compilation_tests/metadata_invalid_rust.rs");
t.compile_fail("compilation_tests/bad_argument.rs");
t.pass("compilation_tests/complex.rs");
t.compile_fail("compilation_tests/impl_generic.rs");
Expand Down
18 changes: 0 additions & 18 deletions near-sdk/compilation_tests/metadata.rs

This file was deleted.

Loading

0 comments on commit 7d31a5b

Please sign in to comment.