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
33 changes: 32 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ 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"
serde_json = { version = "1.0" }
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,6 +52,34 @@ path = "src/croncat.rs"
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 = "receipt"
path = "src/receipt.rs"

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

[[example]]
name = "noop"
path = "src/noop.rs"
42 changes: 42 additions & 0 deletions examples/src/changes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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


// Example output:
//
// StateChangesInBlock RpcStateChangesInBlockResponse {
// block_hash: 5SnL82tfQX1NtsSuqU5334ThZxM1B5KkUWUbeeMvVNRH,
// changes: [],
// }
println!("StateChangesInBlock {res:#?}");
Ok(())
}
52 changes: 52 additions & 0 deletions examples/src/changes_in_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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?;

// Example output:
//
// StateChangesInBlockByType RpcStateChangesInBlockByTypeResponse {
// block_hash: 7ifRdyBsJMXVyp8zw8uGdBMaRShiXuD6yghrp66jqrst,
// changes: [
// AccountTouched {
// account_id: AccountId(
// "dev-20230822100117-44171728969098",
// ),
// },
// AccessKeyTouched {
// account_id: AccountId(
// "dev-20230822100117-44171728969098",
// ),
// },
// DataTouched {
// account_id: AccountId(
// "dev-20230822100117-44171728969098",
// ),
// },
// ],
// }
println!("StateChangesInBlockByType {res:#?}");
Ok(())
}
87 changes: 87 additions & 0 deletions examples/src/genesis_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let worker = workspaces::sandbox().await?;

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

// Example output:
//
// GenesisConfig GenesisConfig {
// protocol_version: 60,
// genesis_time: 2023-08-22T10:05:32.129104Z,
// chain_id: "test-chain-5oKXo",
// genesis_height: 0,
// num_block_producer_seats: 50,
// num_block_producer_seats_per_shard: [
// 50,
// ],
// avg_hidden_validator_seats_per_shard: [
// 0,
// ],
// dynamic_resharding: false,
// protocol_upgrade_stake_threshold: Ratio {
// numer: 4,
// denom: 5,
// },
// epoch_length: 500,
// gas_limit: 1000000000000000,
// min_gas_price: 100000000,
// max_gas_price: 10000000000000000000000,
// block_producer_kickout_threshold: 90,
// chunk_producer_kickout_threshold: 90,
// online_min_threshold: Ratio {
// numer: 9,
// denom: 10,
// },
// online_max_threshold: Ratio {
// numer: 99,
// denom: 100,
// },
// gas_price_adjustment_rate: Ratio {
// numer: 1,
// denom: 100,
// },
// validators: [
// AccountInfo {
// account_id: AccountId(
// "test.near",
// ),
// public_key: ed25519:4Q4fpCWcsVFj3WT7xkCt45qwW84hskFB4SRMHAQfuCne,
// amount: 50000000000000000000000000000000,
// },
// ],
// transaction_validity_period: 100,
// protocol_reward_rate: Ratio {
// numer: 1,
// denom: 10,
// },
// max_inflation_rate: Ratio {
// numer: 1,
// denom: 20,
// },
// total_supply: 2050000000000000000000000000000000,
// num_blocks_per_year: 31536000,
// protocol_treasury_account: AccountId(
// "test.near",
// ),
// fishermen_threshold: 10000000000000000000000000,
// minimum_stake_divisor: 10,
// shard_layout: V0(
// ShardLayoutV0 {
// num_shards: 1,
// version: 0,
// },
// ),
// num_chunk_only_producer_seats: 300,
// minimum_validators_per_shard: 1,
// max_kickout_stake_perc: 100,
// minimum_stake_ratio: Ratio {
// numer: 1,
// denom: 6250,
// },
// use_production_config: false,
// }
println!("GenesisConfig {:#?}", genesis_config);
Ok(())
}
Loading
Loading