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: Add async builders for view_* functions #218

Merged
merged 26 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ad7def3
Initial into_future working
ChaoticTempest Oct 6, 2022
96a7d5a
Moved over to generic impl
ChaoticTempest Oct 7, 2022
84d9105
Cleanup and fixing ViewFunction
ChaoticTempest Oct 7, 2022
a91c877
Added ViewFunction impls and FunctionOwned/Args type
ChaoticTempest Oct 7, 2022
4cb54d1
Cleanup usage of futures::TryFutureExt
ChaoticTempest Oct 7, 2022
1ce4b43
Some more cleanup
ChaoticTempest Oct 7, 2022
38ef6d0
Updated view_code and view_state to use new async builder
ChaoticTempest Oct 7, 2022
f39a58b
Added view async builder to API surface
ChaoticTempest Oct 7, 2022
1902dd4
Removed view_latest_block for view_block instead
ChaoticTempest Oct 7, 2022
ef4fe5a
view_account async builder now apart of the API surface
ChaoticTempest Oct 7, 2022
3b5b414
Got rid of unused client.view_{code, state}
ChaoticTempest Oct 7, 2022
50bcf1f
Sort imports
ChaoticTempest Oct 7, 2022
5f2a6d2
Added Finality type
ChaoticTempest Oct 8, 2022
6e431c8
Added doc
ChaoticTempest Oct 10, 2022
eb99079
Expose access keys
ChaoticTempest Oct 10, 2022
99b12a4
Expose access keys list
ChaoticTempest Oct 10, 2022
807e937
Rename QueryMethod => Method
ChaoticTempest Oct 11, 2022
b22ac35
Added gas price
ChaoticTempest Oct 11, 2022
ae020ef
Rename Queryable methods
ChaoticTempest Oct 11, 2022
27b6899
Rename Queryable to ProcessQuery
ChaoticTempest Oct 11, 2022
897ab47
Merge branch 'main' of https://github.com/near/runner-rs into feat/vi…
ChaoticTempest Oct 11, 2022
be7d910
Addressed comments
ChaoticTempest Oct 25, 2022
758767c
Addressed comments
ChaoticTempest Oct 26, 2022
4fa351b
Addressed comments (TM)
ChaoticTempest Oct 27, 2022
e9bc8fa
AccessKey info should be public
ChaoticTempest Oct 27, 2022
5266978
Addressed comments
ChaoticTempest Oct 31, 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
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

## [Unreleased]

### Added

- [`view_*` asynchronous builders have been added which provides being able to query from a specific block hash or block height](https://github.com/near/workspaces-rs/pull/218)

### Changed

- [Apart of the changes from adding `view_*` async builders, we can have a couple breaking changes to the `view_*` functions](https://github.com/near/workspaces-rs/pull/218):
- `{Account, Contract, Worker}::view_state` moved `prefix` parameter into builder. i.e.
```
worker.view_state("account_id", Some(prefix)).await?;
// is now
worker.view_state("account_id")
.prefix(prefix)
.await?;
// if prefix was `None`, then simply delete the None argument.
```
- `view` function changed to be a builder, and no longer take in `args` as a parameter. It instead has been moved to the builder side.
- Changed `Worker::view_latest_block` to `Worker::view_block` as the default behavior is equivalent.

## [0.6.0]

### Added
Expand Down
4 changes: 2 additions & 2 deletions examples/src/fast_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn main() -> anyhow::Result<()> {
contract.call("current_env_data").view().await?.json()?;
println!("timestamp = {}, epoch_height = {}", timestamp, epoch_height);

let block_info = worker.view_latest_block().await?;
let block_info = worker.view_block().await?;
println!("BlockInfo pre-fast_forward {:?}", block_info);

// Call into fast_forward. This will take a bit of time to invoke, but is
Expand All @@ -29,7 +29,7 @@ async fn main() -> anyhow::Result<()> {
contract.call("current_env_data").view().await?.json()?;
println!("timestamp = {}, epoch_height = {}", timestamp, epoch_height);

let block_info = worker.view_latest_block().await?;
let block_info = worker.view_block().await?;
println!("BlockInfo post-fast_forward {:?}", block_info);

Ok(())
Expand Down
3 changes: 1 addition & 2 deletions examples/src/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ async fn main() -> anyhow::Result<()> {
println!("nft_mint outcome: {:#?}", outcome);

let result: serde_json::Value = worker
.view(contract.id(), "nft_metadata", Vec::new())
.view(contract.id(), "nft_metadata")
.await?
.json()?;

println!("--------------\n{}", result);

println!("Dev Account ID: {}", contract.id());

Ok(())
Expand Down
78 changes: 27 additions & 51 deletions examples/src/ref_finance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,32 +258,22 @@ async fn main() -> anyhow::Result<()> {
///////////////////////////////////////////////////////////////////////////

let ft_deposit: String = worker
.view(
ref_finance.id(),
"get_deposit",
serde_json::json!({
"account_id": owner.id(),
"token_id": ft.id(),
})
.to_string()
.into_bytes(),
)
.view(ref_finance.id(), "get_deposit")
.args_json(serde_json::json!({
"account_id": owner.id(),
"token_id": ft.id(),
}))
.await?
.json()?;
println!("Current FT deposit: {}", ft_deposit);
assert_eq!(ft_deposit, parse_near!("100 N").to_string());

let wnear_deposit: String = worker
.view(
ref_finance.id(),
"get_deposit",
serde_json::json!({
"account_id": owner.id(),
"token_id": wnear.id(),
})
.to_string()
.into_bytes(),
)
.view(ref_finance.id(), "get_deposit")
.args_json(serde_json::json!({
"account_id": owner.id(),
"token_id": wnear.id(),
}))
.await?
.json()?;

Expand All @@ -295,18 +285,13 @@ async fn main() -> anyhow::Result<()> {
///////////////////////////////////////////////////////////////////////////

let expected_return: String = worker
.view(
ref_finance.id(),
"get_return",
serde_json::json!({
"pool_id": pool_id,
"token_in": ft.id(),
"token_out": wnear.id(),
"amount_in": parse_near!("1 N").to_string(),
})
.to_string()
.into_bytes(),
)
.view(ref_finance.id(), "get_return")
.args_json(serde_json::json!({
"pool_id": pool_id,
"token_in": ft.id(),
"token_out": wnear.id(),
"amount_in": parse_near!("1 N").to_string(),
}))
.await?
.json()?;

Expand Down Expand Up @@ -345,31 +330,22 @@ async fn main() -> anyhow::Result<()> {
///////////////////////////////////////////////////////////////////////////

let ft_deposit: String = worker
.view(
ref_finance.id(),
"get_deposit",
serde_json::json!({
"account_id": owner.id(),
"token_id": ft.id(),
})
.to_string()
.into_bytes(),
)
.view(ref_finance.id(), "get_deposit")
.args_json(serde_json::json!({
"account_id": owner.id(),
"token_id": ft.id(),
}))
.await?
.json()?;
println!("New FT deposit after swap: {}", ft_deposit);
assert_eq!(ft_deposit, parse_near!("99 N").to_string());

let wnear_deposit: String = ref_finance
.view(
"get_deposit",
serde_json::json!({
"account_id": owner.id(),
"token_id": wnear.id(),
})
.to_string()
.into_bytes(),
)
.view("get_deposit")
.args_json(serde_json::json!({
"account_id": owner.id(),
"token_id": wnear.id(),
}))
.await?
.json()?;
println!("New WNear deposit after swap: {}", wnear_deposit);
Expand Down
26 changes: 9 additions & 17 deletions examples/src/spooning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async fn main() -> anyhow::Result<()> {
.parse()
.map_err(anyhow::Error::msg)?;

let mut state_items = worker.view_state(&contract_id, None).await?;
let mut state_items = worker.view_state(&contract_id).await?;

let state = state_items.remove(b"STATE".as_slice()).unwrap();
let status_msg = StatusMessage::try_from_slice(&state)?;
Expand All @@ -106,14 +106,10 @@ async fn main() -> anyhow::Result<()> {

// Now grab the state to see that it has indeed been patched:
let status: String = sandbox_contract
.view(
"get_status",
serde_json::json!({
"account_id": testnet_contract_id,
})
.to_string()
.into_bytes(),
)
.view("get_status")
.args_json(serde_json::json!({
"account_id": testnet_contract_id,
}))
.await?
.json()?;

Expand All @@ -122,14 +118,10 @@ async fn main() -> anyhow::Result<()> {

// See that sandbox state was overriden. Grabbing get_status(sandbox_contract_id) should yield Null
let result: Option<String> = sandbox_contract
.view(
"get_status",
serde_json::json!({
"account_id": sandbox_contract.id(),
})
.to_string()
.into_bytes(),
)
.view("get_status")
.args_json(serde_json::json!({
"account_id": sandbox_contract.id(),
}))
.await?
.json()?;
assert_eq!(result, None);
Expand Down
12 changes: 4 additions & 8 deletions examples/src/status_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ async fn main() -> anyhow::Result<()> {
println!("set_status: {:?}", outcome);

let result: String = contract
.view(
"get_status",
json!({
"account_id": contract.id(),
})
.to_string()
.into_bytes(),
)
.view("get_status")
.args_json(json!({
ChaoticTempest marked this conversation as resolved.
Show resolved Hide resolved
"account_id": contract.id(),
}))
.await?
.json()?;

Expand Down
1 change: 1 addition & 0 deletions workspaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bs58 = "0.4"
cargo_metadata = { version = "0.14.2", optional = true }
chrono = "0.4.19"
dirs = "3.0.2"
futures = "0.3"
ChaoticTempest marked this conversation as resolved.
Show resolved Hide resolved
hex = "0.4.2"
portpicker = "0.1.1"
rand = "0.8.4"
Expand Down
26 changes: 13 additions & 13 deletions workspaces/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ const MAX_GAS: Gas = 300_000_000_000_000;
/// A set of arguments we can provide to a transaction, containing
/// the function name, arguments, the amount of gas to use and deposit.
#[derive(Debug)]
pub struct Function<'a> {
name: &'a str,
args: Result<Vec<u8>>,
deposit: Balance,
gas: Gas,
pub struct FunctionArgs<T> {
pub(crate) name: T,
pub(crate) args: Result<Vec<u8>>,
pub(crate) deposit: Balance,
pub(crate) gas: Gas,
}

impl<'a> Function<'a> {
pub type Function<'a> = FunctionArgs<&'a str>;
/// Owned version of [`Function`], whereby a lifetime is not attached.
pub type FunctionOwned = FunctionArgs<String>;
ChaoticTempest marked this conversation as resolved.
Show resolved Hide resolved

impl<T> FunctionArgs<T> {
/// Initialize a new instance of [`Function`], tied to a specific function on a
/// contract that lives directly on a contract we've specified in [`Transaction`].
pub fn new(name: &'a str) -> Self {
pub fn new(name: T) -> Self {
Self {
name,
args: Ok(vec![]),
Expand Down Expand Up @@ -321,12 +325,8 @@ impl<'a, 'b> CallTransaction<'a, 'b> {
/// Instead of transacting the transaction, call into the specified view function.
pub async fn view(self) -> Result<ViewResultDetails> {
self.worker
.client()
.view(
self.contract_id,
self.function.name.to_string(),
self.function.args?,
)
.view(&self.contract_id, self.function.name)
.args(self.function.args?)
.await
}
}
Expand Down
Loading