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: expose experimental apis #285

Merged
merged 13 commits into from Sep 11, 2023
37 changes: 36 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ anyhow = "1.0"
borsh = "0.10"
maplit = "1.0"
near-units = "0.2.0"
near-jsonrpc-primitives = "0.17"
near-primitives = "0.17"
serde = "1.0"
serde_with = "1"
# arbitrary_precision enabled for u128 types that workspaces requires for Balance types
serde_json = { version = "1.0", features = ["arbitrary_precision"] }
tokio = { version = "1.10.0", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3.5", features = ["env-filter"] }
workspaces = { path = "../workspaces" }
workspaces = { path = "../workspaces", features = ["experimental"] }


[[example]]
name = "async_transaction"
Expand Down Expand Up @@ -49,3 +52,35 @@ path = "src/croncat.rs"
[[example]]
name = "various_queries"
path = "src/various_queries.rs"

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

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

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

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

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

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

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

[[example]]
name = "tx_status"
path = "src/tx_status.rs"
36 changes: 36 additions & 0 deletions examples/src/changes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use near_primitives::{types::BlockReference, views::StateChangesRequestView};
use serde_json::json;

const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";

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

let outcome = contract
.call("set_status")
.args_json(json!({
"message": "hello_world",
}))
.transact()
.await?;

let block_ref = {
let hash = near_primitives::hash::CryptoHash(outcome.outcome().block_hash.0);
BlockReference::BlockId(near_primitives::types::BlockId::Hash(hash))
};

let state_changes = {
StateChangesRequestView::ContractCodeChanges {
account_ids: vec![contract.id().clone()],
}
};

// NOTE: this API is under the "experimental" flag and no guarantees are given.
let res = worker.changes(block_ref, state_changes).await?;
Comment on lines +20 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with this one and consistency with Query struct. We only need to supply state_changes while further methods can supply block_ref if it's needed with Finality::Final being the default when not supplied


println!("StateChangesInBlock {res:?}");
frol marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
}
30 changes: 30 additions & 0 deletions examples/src/changes_in_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use near_primitives::types::BlockReference;
use serde_json::json;

const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";

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

let outcome = contract
.call("set_status")
.args_json(json!({
"message": "hello_world",
}))
.transact()
.await?;

let block_ref = {
let hash = near_primitives::hash::CryptoHash(outcome.outcome().block_hash.0);
BlockReference::BlockId(near_primitives::types::BlockId::Hash(hash))
};

// NOTE: this API is under the "experimental" flag and no guarantees are given.
let res = worker.changes_in_block(block_ref).await?;

println!("StateChangesInBlockByType {res:?}");
Ok(())
}
33 changes: 33 additions & 0 deletions examples/src/check_tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use serde_json::json;

const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";

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

let (fn_name, fn_args) = ("set_status", json!({ "message": "hello_world"}));

let _outcome = contract
.call(fn_name)
.args_json(fn_args.clone())
.transact()
.await?;

let signed_tx = worker
.signed_transaction(
contract.id(),
contract.signer(),
fn_name.to_string(),
fn_args,
)
.await?;

// NOTE: this API is under the "experimental" flag and no guarantees are given.
let resp = worker.check_tx(signed_tx).await?;

println!("RpcBroadcastTxSyncResponse {resp:?}");
Ok(())
}
11 changes: 11 additions & 0 deletions examples/src/genesis_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let worker = workspaces::sandbox().await?;

// NOTE: this API is under the "experimental" flag.
let genesis_config = worker.genesis_config().await?;

// dump the genesis config info to stdout
println!("Genesis Config {:?}", genesis_config);
Ok(())
}
29 changes: 29 additions & 0 deletions examples/src/protocol_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";

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

let outcome = contract
.call("set_status")
.args_json(serde_json::json!({
"message": "hello_world",
}))
.transact()
.await?;

let block_reference = {
let hash = outcome.outcome().block_hash;
near_primitives::types::BlockReference::BlockId(near_primitives::types::BlockId::Hash(
near_primitives::hash::CryptoHash(hash.0),
))
};

// NOTE: this API is under the "experimental" flag.
let protocol_config = worker.protocol_config(block_reference).await?;

println!("Protocol Config {protocol_config:?}");
Ok(())
}
40 changes: 40 additions & 0 deletions examples/src/receipt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use near_jsonrpc_primitives::types::receipts::ReceiptReference;

const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";

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

let outcome = contract
.call("set_status")
.args_json(serde_json::json!({
"message": "hello_world",
}))
.transact()
.await?;

let receipt_ref = {
let mut ids = outcome.outcome().receipt_ids.clone();
if ids.is_empty() {
println!("no receipt ids present");
return Ok(());
}

println!("receipts found: {ids:?}");

ReceiptReference {
receipt_id: near_primitives::hash::CryptoHash(
ids.pop().expect("expected at least one receipt id").0,
),
}
};

// NOTE: this API is under the "experimental" flag and no guarantees are given.
let resp = worker.receipt(receipt_ref).await?;
Comment on lines +19 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to expose the fact that we're using ReceiptReference since all it's taking is a CryptoHash. So, we can just have the signature fn receipt(CryptoHash) instead.


println!("ReceiptView: {resp:?}");
Ok(())
}
35 changes: 35 additions & 0 deletions examples/src/tx_status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use near_jsonrpc_primitives::types::transactions::TransactionInfo;
use serde_json::json;

const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";

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

let outcome = contract
.call("set_status")
.args_json(json!({
"message": "hello_world",
}))
.transact()
.await?;

println!("set_status: {:?}", outcome);

let tx_info = {
let outcome = outcome.outcome();
TransactionInfo::TransactionId {
hash: near_primitives::hash::CryptoHash(outcome.block_hash.0),
frol marked this conversation as resolved.
Show resolved Hide resolved
account_id: outcome.executor_id.clone(),
}
};

// NOTE: this API is under the "experimental" flag and no guarantees are given.
let resp = worker.tx_status(tx_info).await?;

println!("FinalExecutionOutcomeWithReceiptView {resp:?}");
Ok(())
}
10 changes: 10 additions & 0 deletions examples/src/validators_ordered.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let worker = workspaces::sandbox().await?;

// NOTE: this API is under the "experimental" flag.
let validators = worker.validators_ordered(None).await?;

println!("Validators {validators:?}");
Ok(())
}
2 changes: 2 additions & 0 deletions workspaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ near-primitives = "0.17"
near-jsonrpc-primitives = "0.17"
near-jsonrpc-client = { version = "0.6", features = ["sandbox"] }
near-sandbox-utils = "0.6.2"
near-chain-configs = { version = "0.17.0", optional = true }

[build-dependencies]
near-sandbox-utils = "0.6.2"
Expand All @@ -60,6 +61,7 @@ default = ["install", "interop_sdk"]
install = [] # Install the sandbox binary during compile time
interop_sdk = ["near-sdk"]
unstable = ["cargo_metadata"]
experimental = ["near-chain-configs"]

[package.metadata.docs.rs]
features = ["unstable"]
Loading
Loading