From 0926ae914b302f4a0f97d7c833d24cccfd20b7bf Mon Sep 17 00:00:00 2001 From: Daniyar Itegulov Date: Thu, 22 Sep 2022 16:47:40 +1000 Subject: [PATCH] chore: use near-abi 0.2.0 (#927) --- examples/adder/Cargo.lock | 16 +++- examples/adder/res/adder_abi.json | 95 ++++++++++--------- examples/fungible-token/Cargo.lock | 4 +- .../src/core_impl/abi/abi_generator.rs | 52 ++++++++-- near-sdk/Cargo.toml | 2 +- near-sdk/src/private/mod.rs | 2 +- 6 files changed, 111 insertions(+), 60 deletions(-) diff --git a/examples/adder/Cargo.lock b/examples/adder/Cargo.lock index 23b7d0438..4848bd641 100644 --- a/examples/adder/Cargo.lock +++ b/examples/adder/Cargo.lock @@ -58,7 +58,7 @@ name = "adder" version = "0.1.0" dependencies = [ "anyhow", - "near-abi", + "near-abi 0.1.0-pre.0", "near-sdk", "schemars", "serde", @@ -1288,6 +1288,18 @@ dependencies = [ "serde", ] +[[package]] +name = "near-abi" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85f9816032a8688b52e2e0e51e1a5513e7e05310eb2eebe1d913fcf82be1e1d7" +dependencies = [ + "borsh", + "schemars", + "semver", + "serde", +] + [[package]] name = "near-account-id" version = "0.14.0" @@ -1530,7 +1542,7 @@ dependencies = [ "base64 0.13.0", "borsh", "bs58", - "near-abi", + "near-abi 0.2.0", "near-crypto", "near-primitives", "near-primitives-core", diff --git a/examples/adder/res/adder_abi.json b/examples/adder/res/adder_abi.json index 28c34891a..6734a12aa 100644 --- a/examples/adder/res/adder_abi.json +++ b/examples/adder/res/adder_abi.json @@ -1,11 +1,16 @@ { - "schema_version": "0.1.0", + "schema_version": "0.2.0", "metadata": { "name": "adder", "version": "0.1.0", "authors": [ "Near Inc " - ] + ], + "build": { + "compiler": "rustc 1.61.0", + "builder": "cargo-near 0.2.0" + }, + "wasm_hash": "B4XgA4rGVyaCWyDv2h1XAN5QMbtdJN1frRwoxELUMvDe" }, "body": { "functions": [ @@ -13,22 +18,23 @@ "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": { @@ -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": { diff --git a/examples/fungible-token/Cargo.lock b/examples/fungible-token/Cargo.lock index 2bfd22d3c..721521bdb 100644 --- a/examples/fungible-token/Cargo.lock +++ b/examples/fungible-token/Cargo.lock @@ -1310,9 +1310,9 @@ dependencies = [ [[package]] name = "near-abi" -version = "0.1.0-pre.0" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d5281adc3a63d798b0e35683dee66095cbb94e431960ec859d5a8397b093f39" +checksum = "85f9816032a8688b52e2e0e51e1a5513e7e05310eb2eebe1d913fcf82be1e1d7" dependencies = [ "borsh", "schemars", diff --git a/near-sdk-macros/src/core_impl/abi/abi_generator.rs b/near-sdk-macros/src/core_impl/abi/abi_generator.rs index 9786c65c9..4210089d5 100644 --- a/near-sdk-macros/src/core_impl/abi/abi_generator.rs +++ b/near-sdk-macros/src/core_impl/abi/abi_generator.rs @@ -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)); @@ -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 { @@ -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 @@ -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, } }, } diff --git a/near-sdk/Cargo.toml b/near-sdk/Cargo.toml index 1d35be4d0..2ef9bc857 100644 --- a/near-sdk/Cargo.toml +++ b/near-sdk/Cargo.toml @@ -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) diff --git a/near-sdk/src/private/mod.rs b/near-sdk/src/private/mod.rs index 35780ca18..805c9c0b4 100644 --- a/near-sdk/src/private/mod.rs +++ b/near-sdk/src/private/mod.rs @@ -1,7 +1,7 @@ #[cfg(feature = "abi")] pub use near_abi::__private::ChunkedAbiEntry; #[cfg(feature = "abi")] -pub use near_abi::{AbiFunction, AbiParameter, AbiType}; +pub use near_abi::{AbiBorshParameter, AbiFunction, AbiJsonParameter, AbiParameters, AbiType}; #[cfg(feature = "abi")] pub use schemars;