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

chore: include near-abi-client examples #303

Merged
merged 14 commits into from Feb 3, 2024
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ target
.vscode

# generated files
.near-credentials
.near-credentials
8 changes: 8 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3.5", features = ["env-filter"] }
near-workspaces = { path = "../workspaces", features = ["experimental", "unstable"] }

[build-dependencies]
anyhow = "1.0"
near-workspaces = { path = "../workspaces" }

[[example]]
name = "async_transaction"
path = "src/async_transaction.rs"
Expand Down Expand Up @@ -52,6 +56,10 @@ path = "src/croncat.rs"
name = "various_queries"
path = "src/various_queries.rs"

[[example]]
name = "view_abi"
path = "src/view_abi.rs"

[[example]]
name = "genesis_config"
path = "src/genesis_config.rs"
Expand Down
6 changes: 6 additions & 0 deletions examples/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() -> anyhow::Result<()> {
near_workspaces::near_abi_client::Generator::new("src/gen".into())
.file("res/adder.json")
.generate()?;
Ok(())
}
66 changes: 66 additions & 0 deletions examples/res/adder.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"schema_version": "0.3.0",
"metadata": {
"name": "abi",
"version": "0.1.0",
"authors": [
"Near Inc <hello@nearprotocol.com>"
]
},
"body": {
"functions": [
{
"name": "add",
"doc": " Adds two pairs point-wise.",
"kind": "view",
"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": {
"$ref": "#/definitions/Pair"
}
}
}
],
"root_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "String",
"type": "string",
"definitions": {
"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/res/adder.wasm
Binary file not shown.
10 changes: 10 additions & 0 deletions examples/src/gen/adder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub type Pair = Vec<i64>;
pub struct AbiClient {
pub contract: near_workspaces::Contract,
}
impl AbiClient {
pub async fn add(&self, a: Pair, b: Pair) -> anyhow::Result<Pair> {
let result = self.contract.call("add").args_json([a, b]).view().await?;
Ok(result.json::<Pair>()?)
}
}
44 changes: 44 additions & 0 deletions examples/src/view_abi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::convert::TryInto;

const ADDER_WASM_FILEPATH: &str = "./examples/res/adder.wasm";

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let wasm = std::fs::read(ADDER_WASM_FILEPATH)?;
let contract = worker.dev_deploy(&wasm).await?;

macro_run(contract.clone()).await?;
generation_run(contract).await?;

Ok(())
}

/// This part of the example uses the Macro API to get a client and use it.
mod macro_adder {
near_workspaces::near_abi_client::generate!(Client for "../examples/res/adder.json");
}

pub async fn macro_run(contract: near_workspaces::Contract) -> anyhow::Result<()> {
let contract = macro_adder::Client { contract };
let res = contract.add(vec![1, 2], vec![3, 4]).await?;

let res = (res[0].try_into().unwrap(), res[1].try_into().unwrap());
assert_eq!(res, (4, 6));

Ok(())
}

/// This part of the example uses the Generation API to generate a client and use it.
#[path = "gen/adder.rs"]
mod generation_adder;

pub async fn generation_run(contract: near_workspaces::Contract) -> anyhow::Result<()> {
let contract = generation_adder::AbiClient { contract };
let res = contract.add(vec![1, 2], vec![3, 4]).await?;

let res = (res[0].try_into().unwrap(), res[1].try_into().unwrap());
assert_eq!(res, (4, 6));

Ok(())
}
1 change: 1 addition & 0 deletions workspaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ tokio-retry = "0.3"
tracing = "0.1"
url = { version = "2.2.2", features = ["serde"] }

near-abi-client = { git = "/~https://github.com/shariffdev/near-abi-client-rs", branch = "chore/bump-workspaces" }
near-gas = { version = "0.2.3", features = ["serde", "borsh", "schemars"] }
near-token = { version = "0.2.0", features = ["serde"] }
near-sdk = { version = "4.1", optional = true }
Expand Down
4 changes: 4 additions & 0 deletions workspaces/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub mod result;
pub mod rpc;
pub mod types;

/// The near_abi_client implementation is currently in flux and we offer a re-export
/// of it and example code. No public near_abi APIs are baked into workspace-rs yet.
pub use near_abi_client;

pub use network::pick_unused_port;
pub use network::variants::{DevNetwork, Network};
pub use result::Result;
Expand Down