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 all 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
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@

## [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 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.
- `operations::Function` type no longer takes a lifetime parameter.
ChaoticTempest marked this conversation as resolved.
Show resolved Hide resolved
- `operations::CallTransaction` type takes one less lifetime parameter.

## [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
6 changes: 1 addition & 5 deletions examples/src/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,9 @@ async fn main() -> anyhow::Result<()> {

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

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

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

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

Ok(())
Expand Down
105 changes: 41 additions & 64 deletions examples/src/ref_finance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::convert::TryInto;

use near_units::{parse_gas, parse_near};
use serde_json::json;
use workspaces::network::Sandbox;
use workspaces::{Account, AccountId, Contract, Worker};
use workspaces::{BlockHeight, DevNetwork};
Expand Down Expand Up @@ -36,7 +37,7 @@ async fn create_ref(owner: &Account, worker: &Worker<Sandbox>) -> anyhow::Result

owner
.call(ref_finance.id(), "new")
.args_json(serde_json::json!({
.args_json(json!({
"owner_id": ref_finance.id(),
"exchange_fee": 4,
"referral_fee": 1,
Expand All @@ -47,7 +48,7 @@ async fn create_ref(owner: &Account, worker: &Worker<Sandbox>) -> anyhow::Result

owner
.call(ref_finance.id(), "storage_deposit")
.args_json(serde_json::json!({}))
.args_json(json!({}))
.deposit(parse_near!("30 mN"))
.transact()
.await?
Expand All @@ -68,7 +69,7 @@ async fn create_wnear(owner: &Account, worker: &Worker<Sandbox>) -> anyhow::Resu

owner
.call(wnear.id(), "new")
.args_json(serde_json::json!({
.args_json(json!({
"owner_id": owner.id(),
"total_supply": parse_near!("1,000,000,000 N"),
}))
Expand All @@ -78,7 +79,7 @@ async fn create_wnear(owner: &Account, worker: &Worker<Sandbox>) -> anyhow::Resu

owner
.call(wnear.id(), "storage_deposit")
.args_json(serde_json::json!({}))
.args_json(json!({}))
.deposit(parse_near!("0.008 N"))
.transact()
.await?
Expand Down Expand Up @@ -109,14 +110,14 @@ async fn create_pool_with_liquidity(

ref_finance
.call("extend_whitelisted_tokens")
.args_json(serde_json::json!({ "tokens": token_ids }))
.args_json(json!({ "tokens": token_ids }))
.transact()
.await?
.into_result()?;

let pool_id: u64 = ref_finance
.call("add_simple_pool")
.args_json(serde_json::json!({
.args_json(json!({
"tokens": token_ids,
"fee": 25
}))
Expand All @@ -127,7 +128,7 @@ async fn create_pool_with_liquidity(

owner
.call(ref_finance.id(), "register_tokens")
.args_json(serde_json::json!({
.args_json(json!({
"token_ids": token_ids,
}))
.deposit(1)
Expand All @@ -139,7 +140,7 @@ async fn create_pool_with_liquidity(

owner
.call(ref_finance.id(), "add_liquidity")
.args_json(serde_json::json!({
.args_json(json!({
"pool_id": pool_id,
"amounts": token_amounts,
}))
Expand All @@ -161,7 +162,7 @@ async fn deposit_tokens(
ref_finance
.as_account()
.call(contract_id, "storage_deposit")
.args_json(serde_json::json!({
.args_json(json!({
"registration_only": true,
}))
.deposit(parse_near!("1 N"))
Expand All @@ -171,7 +172,7 @@ async fn deposit_tokens(

owner
.call(contract_id, "ft_transfer_call")
.args_json(serde_json::json!({
.args_json(json!({
"receiver_id": ref_finance.id(),
"amount": amount.to_string(),
"msg": "",
Expand All @@ -198,7 +199,7 @@ async fn create_custom_ft(
// Initialize our FT contract with owner metadata and total supply available
// to be traded and transfered into other contracts such as Ref-Finance
ft.call("new_default_meta")
.args_json(serde_json::json!({
.args_json(json!({
"owner_id": owner.id(),
"total_supply": parse_near!("1,000,000,000 N").to_string(),
}))
Expand Down Expand Up @@ -258,32 +259,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(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(json!({
"account_id": owner.id(),
"token_id": wnear.id(),
}))
.await?
.json()?;

Expand All @@ -295,18 +286,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(json!({
"pool_id": pool_id,
"token_in": ft.id(),
"token_out": wnear.id(),
"amount_in": parse_near!("1 N").to_string(),
}))
.await?
.json()?;

Expand All @@ -318,8 +304,8 @@ async fn main() -> anyhow::Result<()> {

let actual_out = owner
.call(ref_finance.id(), "swap")
.args_json(serde_json::json!({
"actions": vec![serde_json::json!({
.args_json(json!({
"actions": vec![json!({
"pool_id": pool_id,
"token_in": ft.id(),
"token_out": wnear.id(),
Expand All @@ -345,31 +331,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(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(json!({
"account_id": owner.id(),
"token_id": wnear.id(),
}))
.await?
.json()?;
println!("New WNear deposit after swap: {}", wnear_deposit);
Expand Down
32 changes: 13 additions & 19 deletions examples/src/spooning.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use borsh::{self, BorshDeserialize, BorshSerialize};
use std::env;

use borsh::{self, BorshDeserialize, BorshSerialize};
use serde_json::json;
use tracing::info;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::EnvFilter;
Expand Down Expand Up @@ -51,7 +53,7 @@ async fn deploy_status_contract(
// This will `call` into `set_status` with the message we want to set.
contract
.call("set_status")
.args_json(serde_json::json!({
.args_json(json!({
"message": msg,
}))
.transact()
Expand Down Expand Up @@ -79,7 +81,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 +108,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(json!({
"account_id": testnet_contract_id,
}))
.await?
.json()?;

Expand All @@ -122,14 +120,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(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
Loading