Skip to content

Commit

Permalink
chore: use near-abi 0.2.0 (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
itegulov authored Sep 22, 2022
1 parent 3c747b0 commit 0926ae9
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 60 deletions.
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.

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
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: 1 addition & 1 deletion near-sdk/src/private/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 0926ae9

Please sign in to comment.