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

feat: near_abi! macro #831

Merged
merged 36 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
30fd61c
add abi models and schemars dependency
itegulov Jun 3, 2022
4f017dd
implement near_abi macro
itegulov Jun 3, 2022
c2fe70b
add abi example
itegulov Jun 3, 2022
05af560
clippy
itegulov Jun 3, 2022
f3b6b35
use `cargo near abi` instead
itegulov Jun 6, 2022
9abc6e5
add serde default for all optional fields
itegulov Jun 6, 2022
eb48446
ignore empty params as well
itegulov Jun 6, 2022
ac4cfbc
update example
itegulov Jun 6, 2022
c1c97d0
add parameter names to ABI
itegulov Jun 6, 2022
28cb425
rename ABI models and move name inside AbiParameter
itegulov Jun 6, 2022
8c6f2ea
hide macro and abi models behind unstable
itegulov Jun 8, 2022
e5f5f66
re-generate wasm files
itegulov Jun 8, 2022
88e9234
make near_abi an attribute macro
itegulov Jun 8, 2022
b0580c9
clippy
itegulov Jun 8, 2022
6425c76
add unstable feature for near-sdk-macros
itegulov Jun 13, 2022
8a1fea1
rename metainfo to metadata
itegulov Jun 13, 2022
428b1d5
empty
itegulov Jun 14, 2022
ccfba2e
handle result callbacks for ABI
itegulov Jun 15, 2022
a2f7b1d
improve abi example
itegulov Jun 15, 2022
ddc6824
make schemars an optional dependency
itegulov Jun 15, 2022
43d991e
implement JsonSchema for a few more types
itegulov Jun 20, 2022
4c037db
handle init methods in a special way
itegulov Jun 20, 2022
2be9534
fix optional dependencies
itegulov Jun 20, 2022
0570f78
derive JsonSchema for storage types
itegulov Jun 21, 2022
24350f7
generate multiple symbols
itegulov Jun 21, 2022
9014d6d
get rid of type registry
itegulov Jun 21, 2022
6779174
refactor function generation & support for result callbacks
itegulov Jun 21, 2022
7a9bae0
rebuild examples
itegulov Jun 22, 2022
71713ac
add is_private & is_payable, make those optional
itegulov Jun 22, 2022
51aed1f
decouple abi feature from unstable
itegulov Jun 22, 2022
49565dd
fix test-contract-defi
itegulov Jun 22, 2022
efce965
Merge remote-tracking branch 'origin/master' into daniyar/abi-macro
itegulov Jun 22, 2022
3206c70
address comments
itegulov Jun 22, 2022
4203424
use schemars from near sdk
itegulov Jun 23, 2022
e6b31bb
hide schemars behind __private
itegulov Jun 23, 2022
37aaa23
rebuild examples
itegulov Jun 23, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,243 changes: 1,243 additions & 0 deletions examples/abi/Cargo.lock

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions examples/abi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "abi"
version = "0.1.0"
authors = ["Near Inc <[email protected]>"]
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../near-sdk", features = ["unstable"] }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"
itegulov marked this conversation as resolved.
Show resolved Hide resolved

[profile.release]
codegen-units = 1
# Tell `rustc` to optimize for small code size.
opt-level = "z"
lto = true
debug = false
panic = "abort"

[workspace]
members = []
12 changes: 12 additions & 0 deletions examples/abi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# ABI

Showcases how to generate ABI from a NEAR contract.

## Generating ABI
To generate the ABI file run:

```bash
cargo near abi
```

This will generate a file located at `target/near/abi.json`.
8 changes: 8 additions & 0 deletions examples/abi/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
TARGET="${CARGO_TARGET_DIR:-target}"
set -e
cd "$(dirname $0)"

cargo build --target wasm32-unknown-unknown --release
cp $TARGET/wasm32-unknown-unknown/release/abi.wasm ./res/
#wasm-opt -Oz --output ./res/status_message.wasm ./res/status_message.wasm
120 changes: 120 additions & 0 deletions examples/abi/res/abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"abi_schema_version": "0.1.0",
"metainfo": {
"name": "abi",
"version": "0.1.0",
"authors": [
"Near Inc <[email protected]>"
]
},
"abi": {
"functions": [
{
"name": "add",
"is_view": true,
"is_init": false,
"params": [
{
"name": "a",
"type_id": 0,
"serialization_type": "json"
},
{
"name": "b",
"type_id": 0,
"serialization_type": "json"
}
],
"result": {
"type_id": 0,
"serialization_type": "json"
}
},
{
"name": "add_callback",
"is_view": true,
"is_init": false,
"callbacks": [
{
"type_id": 1,
"serialization_type": "json"
},
{
"type_id": 1,
"serialization_type": "json"
}
],
"callbacks_vec": {
"type_id": 2,
"serialization_type": "json"
},
"result": {
"type_id": 1,
"serialization_type": "json"
}
}
],
"types": [
{
"id": 1,
"schema": {
"$ref": "#/definitions/DoublePair"
}
},
{
"id": 0,
"schema": {
"$ref": "#/definitions/Pair"
}
},
{
"id": 2,
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/DoublePair"
}
}
}
],
itegulov marked this conversation as resolved.
Show resolved Hide resolved
"root_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "String",
"type": "string",
"definitions": {
"DoublePair": {
"type": "object",
"required": [
"first",
"second"
],
"properties": {
"first": {
"$ref": "#/definitions/Pair"
},
"second": {
"$ref": "#/definitions/Pair"
}
}
},
"Pair": {
"type": "array",
"items": [
{
"type": "integer",
"format": "uint32",
"minimum": 0.0
},
{
"type": "integer",
"format": "uint32",
"minimum": 0.0
}
],
"maxItems": 2,
"minItems": 2
}
}
}
}
}
Binary file added examples/abi/res/abi.wasm
Binary file not shown.
45 changes: 45 additions & 0 deletions examples/abi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use near_sdk::near_abi;

#[near_abi]
mod abi {
itegulov marked this conversation as resolved.
Show resolved Hide resolved
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::near_bindgen;
use near_sdk::schemars::JsonSchema;
use near_sdk::serde::{Deserialize, Serialize};

#[derive(JsonSchema, Serialize, Deserialize)]
pub struct Pair(u32, u32);

#[derive(JsonSchema, Serialize, Deserialize)]
pub struct DoublePair {
first: Pair,
second: Pair,
}

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct Adder {}

#[near_bindgen]
impl Adder {
pub fn add(&self, a: Pair, b: Pair) -> Pair {
sum_pair(&a, &b)
}

pub fn add_callback(
&self,
#[callback_unwrap] a: DoublePair,
#[callback_unwrap] b: DoublePair,
#[callback_vec] others: Vec<DoublePair>,
) -> DoublePair {
vec![b].iter().chain(others.iter()).fold(a, |acc, el| DoublePair {
itegulov marked this conversation as resolved.
Show resolved Hide resolved
first: sum_pair(&acc.first, &el.first),
second: sum_pair(&acc.second, &el.second),
})
}
}

fn sum_pair(a: &Pair, b: &Pair) -> Pair {
Pair(a.0 + b.0, a.1 + b.1)
}
}
42 changes: 42 additions & 0 deletions examples/callback-results/Cargo.lock

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

Binary file modified examples/callback-results/res/callback_results.wasm
Binary file not shown.
42 changes: 42 additions & 0 deletions examples/cross-contract-calls/Cargo.lock

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

Binary file modified examples/cross-contract-calls/res/cross_contract_high_level.wasm
Binary file not shown.
Binary file modified examples/cross-contract-calls/res/cross_contract_low_level.wasm
Binary file not shown.
Loading