diff --git a/.gitignore b/.gitignore index ed88c2b2..da694856 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ target .vscode # generated files -.near-credentials \ No newline at end of file +.near-credentials diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 6239adc1..7e8f4552 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -22,6 +22,10 @@ near-workspaces = { path = "../workspaces", features = [ "unstable", ] } +[build-dependencies] +anyhow = "1.0" +near-workspaces = { path = "../workspaces" } + [[example]] name = "async_transaction" path = "src/async_transaction.rs" @@ -89,3 +93,11 @@ path = "src/noop.rs" [[example]] name = "custom_network" path = "src/custom_network.rs" + +[[example]] +name = "build_gen_abi" +path = "src/build_gen_abi.rs" + +[[example]] +name = "macro_gen_abi" +path = "src/macro_gen_abi.rs" diff --git a/examples/build.rs b/examples/build.rs new file mode 100644 index 00000000..2ba2e91d --- /dev/null +++ b/examples/build.rs @@ -0,0 +1,6 @@ +fn main() -> anyhow::Result<()> { + near_workspaces::near_abi_client::Generator::new("src/gen".into()) + .file("res/adder.json") + .generate()?; + Ok(()) +} diff --git a/examples/res/adder.json b/examples/res/adder.json new file mode 100644 index 00000000..e825f295 --- /dev/null +++ b/examples/res/adder.json @@ -0,0 +1,66 @@ +{ + "schema_version": "0.4.0", + "metadata": { + "name": "abi", + "version": "0.1.0", + "authors": [ + "Near Inc " + ] + }, + "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 + } + } + } + } +} \ No newline at end of file diff --git a/examples/res/adder.wasm b/examples/res/adder.wasm new file mode 100755 index 00000000..936a36dc Binary files /dev/null and b/examples/res/adder.wasm differ diff --git a/examples/src/build_gen_abi.rs b/examples/src/build_gen_abi.rs new file mode 100644 index 00000000..75e8734f --- /dev/null +++ b/examples/src/build_gen_abi.rs @@ -0,0 +1,28 @@ +// This example shows how to use the `near_abi_client` Generation Based API. +// We are generating client code using the schema for the ABI and and `workspaces-rs` to call into the contract. +// More information about usage can be found here: +// +// A good scenario for usage might be when you are interacting with a contract or multiple contracts at an automated level +// and you want to have a type-safe way of interacting with them. + +/// The generated api requires setup in the `build.rs` file to generate the client code. +#[path = "gen/adder.rs"] +mod generation_adder; + +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?; + + // The client is initialized with the contract. + let abi_client = generation_adder::AbiClient { contract }; + + // Here we can call the method, now typed with arguments and return types. + let res = abi_client.add(vec![1, 2], vec![3, 4]).await?; + + assert_eq!(res, [4, 6]); + Ok(()) +} diff --git a/examples/src/gen/adder.rs b/examples/src/gen/adder.rs new file mode 100644 index 00000000..175ed801 --- /dev/null +++ b/examples/src/gen/adder.rs @@ -0,0 +1 @@ +// No content here, it's to be generated on build. Here to allow cargofmt to work. diff --git a/examples/src/macro_gen_abi.rs b/examples/src/macro_gen_abi.rs new file mode 100644 index 00000000..fc5dd0ef --- /dev/null +++ b/examples/src/macro_gen_abi.rs @@ -0,0 +1,26 @@ +// This example shows how to use the `near_abi_client` Macro Based API. +// We are generating client code using the schema for the ABI and and `workspaces-rs` to call into the contract. +// More information about usage can be found here: +// +// A good scenario for usage might be when you are interacting with a contract or multiple contracts at an automated level +// and you want to have a type-safe way of interacting with them. + +const ADDER_WASM_FILEPATH: &str = "./examples/res/adder.wasm"; + +near_workspaces::near_abi_client::generate!(AbiClient for "res/adder.json"); + +#[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?; + + // The client is initialized with the contract. + let abi_client = AbiClient { contract }; + + // Here we can call the method, now typed with arguments and return types. + let res = abi_client.add(vec![1, 2], vec![3, 4]).await?; + + assert_eq!(res, [4, 6]); + Ok(()) +} diff --git a/workspaces/Cargo.toml b/workspaces/Cargo.toml index ae56b816..daf6ac24 100644 --- a/workspaces/Cargo.toml +++ b/workspaces/Cargo.toml @@ -30,6 +30,7 @@ tokio-retry = "0.3" tracing = "0.1" url = { version = "2.2.2", features = ["serde"] } +near-abi-client = "0.1.1" near-gas = { version = "0.2.5", features = ["serde", "borsh", "schemars"] } near-token = { version = "0.2.0", features = ["serde"] } near-sdk = { version = "5.0.0-alpha.2", optional = true } diff --git a/workspaces/src/lib.rs b/workspaces/src/lib.rs index e985ed49..12bb2c56 100644 --- a/workspaces/src/lib.rs +++ b/workspaces/src/lib.rs @@ -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;