Skip to content

Commit

Permalink
refactor!: renamed crate to near-workspaces to avoid confusion with C…
Browse files Browse the repository at this point in the history
…argo workspaces; imports should now use `near_workspaces` instead of just `workspaces`
  • Loading branch information
frol committed Oct 4, 2023
1 parent 3feebef commit 20480d1
Show file tree
Hide file tree
Showing 34 changed files with 151 additions and 108 deletions.
72 changes: 54 additions & 18 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
name: Workspace Checks & Tests
name: Test & Release

permissions:
pull-requests: write
contents: write

on:
push:
Expand All @@ -7,33 +11,46 @@ on:

env:
CARGO_TERM_COLOR: always
RUSTFLAGS: '-D warnings'
CARGO_INCREMENTAL: 0
RUST_BACKTRACE: short

jobs:
check:
name: Clippy and fmt
runs-on: ubuntu-latest
clippy:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v1
- name: Check Formatting
run: cargo fmt -- --check
- name: Check Clippy
run: cargo clippy --tests -- -Dclippy::all
- uses: actions/checkout@v4
- name: Run clippy
run: cargo clippy --benches -- -D clippy::all

cargo-fmt:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v4
- name: Run cargo fmt
run: cargo fmt --check

# there're sometimes warnings, which signal, that the generated doc
# won't look as expected, when rendered, and sometimes errors, which will prevent doc from being
# generated at release time altogether.
cargo-doc:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v4
- name: run cargo doc
run: RUSTDOCFLAGS="-D warnings" cargo doc

test:
needs: check
needs: cargo-fmt
strategy:
matrix:
platform: [ubuntu-latest, macos-latest]
toolchain: [stable]

runs-on: ${{ matrix.platform }}

steps:
- uses: actions/checkout@v2
- name: "${{ matrix.toolchain }}"
Expand All @@ -49,3 +66,22 @@ jobs:
run: cargo check --verbose
- name: Run tests with unstable features
run: NEAR_RPC_TIMEOUT_SECS=100 cargo test --verbose --features unstable

release-plz:
runs-on: ubuntu-latest
needs: [clippy, cargo-fmt, cargo-doc, test]
if: github.ref == 'refs/heads/main'
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.MY_GITHUB_TOKEN }}
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Run release-plz
uses: MarcoIeni/release-plz-action@v0.5
env:
# https://release-plz.ieni.dev/docs/github/trigger
GITHUB_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }}
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
members = [
"workspaces",
"examples",
]
]
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</p>

<p>
<a href="https://crates.io/crates/workspaces"><img src="https://img.shields.io/crates/v/workspaces.svg?style=flat-square" alt="Crates.io version" /></a>
<a href="https://crates.io/crates/workspaces"><img src="https://img.shields.io/crates/d/workspaces.svg?style=flat-square" alt="Download" /></a>
<a href="https://docs.rs/workspaces"><img src="https://docs.rs/workspaces/badge.svg" alt="Reference Documentation" /></a>
<a href="https://crates.io/crates/near-workspaces"><img src="https://img.shields.io/crates/v/near-workspaces.svg?style=flat-square" alt="Crates.io version" /></a>
<a href="https://crates.io/crates/near-workspaces"><img src="https://img.shields.io/crates/d/near-workspaces.svg?style=flat-square" alt="Download" /></a>
<a href="https://docs.rs/near-workspaces"><img src="https://docs.rs/near-workspaces/badge.svg" alt="Reference Documentation" /></a>
</p>
</div>

Expand All @@ -24,11 +24,11 @@

### WASM compilation not supported

`workspaces-rs`, the library itself, does not currently compile to WASM. Best to put this dependency in `[dev-dependencies]` section of `Cargo.toml` if we were trying to run this library alongside something that already does compile to WASM, such as `near-sdk-rs`.
`near-workspaces-rs`, the library itself, does not currently compile to WASM. Best to put this dependency in `[dev-dependencies]` section of `Cargo.toml` if we were trying to run this library alongside something that already does compile to WASM, such as `near-sdk-rs`.

## Simple Testing Case

A simple test to get us going and familiar with `workspaces` framework. Here, we will be going through the NFT contract and how we can test it with `workspaces-rs`.
A simple test to get us going and familiar with `near-workspaces` framework. Here, we will be going through the NFT contract and how we can test it with `near-workspaces-rs`.

### Setup -- Imports

Expand Down Expand Up @@ -60,7 +60,7 @@ This includes launching our sandbox, loading our wasm file and deploying that wa

#[tokio::test]
async fn test_nft_contract() -> anyhow::Result<()> {
let worker = workspaces::sandbox().await?;
let worker = near_workspaces::sandbox().await?;
let wasm = std::fs::read(NFT_WASM_FILEPATH)?;
let contract = worker.dev_deploy(&wasm).await?;
```
Expand Down Expand Up @@ -129,7 +129,7 @@ Then later on, we can view our minted NFT's metadata via our `view` call into `n

### Updating Contract Afterwards

Note that if our contract code changes, `workspaces-rs` does nothing about it since we are utilizing `deploy`/`dev_deploy` to merely send the contract bytes to the network. So if it does change, we will have to recompile the contract as usual, and point `deploy`/`dev_deploy` again to the right WASM files. However, there is a workspaces feature that will recompile contract changes for us: refer to the experimental/unstable [`compile_project`](#compiling-contracts-during-test-time) function for telling workspaces to compile a _Rust_ project for us.
Note that if our contract code changes, `near-workspaces-rs` does nothing about it since we are utilizing `deploy`/`dev_deploy` to merely send the contract bytes to the network. So if it does change, we will have to recompile the contract as usual, and point `deploy`/`dev_deploy` again to the right WASM files. However, there is a feature that will recompile contract changes for us: refer to the experimental/unstable [`compile_project`](#compiling-contracts-during-test-time) function for telling near-workspaces to compile a _Rust_ project for us.

## Examples

Expand All @@ -150,9 +150,9 @@ cargo run --example nft
async fn main() -> anyhow::Result<()> {
// Create a sandboxed environment.
// NOTE: Each call will create a new sandboxed environment
let worker = workspaces::sandbox().await?;
let worker = near_workspaces::sandbox().await?;
// or for testnet:
let worker = workspaces::testnet().await?;
let worker = near_workspaces::testnet().await?;
}
```

Expand All @@ -161,7 +161,7 @@ async fn main() -> anyhow::Result<()> {
Need to make a helper functions utilizing contracts? Just import it and pass it around:

```rust
use workspaces::Contract;
use near_workspaces::Contract;

// Helper function that calls into a contract we give it
async fn call_my_func(contract: &Contract) -> anyhow::Result<()> {
Expand All @@ -179,7 +179,7 @@ async fn call_my_func(contract: &Contract) -> anyhow::Result<()> {
Or to pass around workers regardless of networks:

```rust
use workspaces::{DevNetwork, Worker};
use near_workspaces::{DevNetwork, Worker};

const CONTRACT_BYTES: &[u8] = include_bytes!("./relative/path/to/file.wasm");

Expand All @@ -198,7 +198,7 @@ We can check the balance of our accounts like so:
#[test(tokio::test)]
async fn test_contract_transfer() -> anyhow::Result<()> {
let transfer_amount = near_units::parse_near!("0.1");
let worker = workspaces::sandbox().await?;
let worker = near_workspaces::sandbox().await?;

let contract = worker
.dev_deploy(include_bytes!("../target/res/your_project_name.wasm"))
Expand Down Expand Up @@ -227,7 +227,7 @@ async fn test_contract_transfer() -> anyhow::Result<()> {
}
```

For viewing other chain related details, look at the docs for [Worker](https://docs.rs/workspaces/0.4.1/workspaces/struct.Worker.html), [Account](https://docs.rs/workspaces/0.4.1/workspaces/struct.Account.html) and [Contract](https://docs.rs/workspaces/0.4.1/workspaces/struct.Contract.html)
For viewing other chain related details, look at the docs for [Worker](https://docs.rs/near-workspaces/latest/near_workspaces/struct.Worker.html), [Account](https://docs.rs/near-workspaces/latest/near_workspaces/struct.Account.html) and [Contract](https://docs.rs/near-workspaces/latest/near_workspaces/struct.Contract.html)

### Spooning - Pulling Existing State and Contracts from Mainnet/Testnet

Expand All @@ -237,8 +237,8 @@ We will first start with the usual imports:

```rust
use near_units::{parse_gas, parse_near};
use workspaces::network::Sandbox;
use workspaces::{Account, AccountId, BlockHeight, Contract, Worker};
use near_workspaces::network::Sandbox;
use near_workspaces::{Account, AccountId, BlockHeight, Contract, Worker};
```

Then specify the contract name from testnet we want to be pulling:
Expand All @@ -257,7 +257,7 @@ Create a function called `pull_contract` which will pull the contract's `.wasm`

```rust
async fn pull_contract(owner: &Account, worker: &Worker<Sandbox>) -> anyhow::Result<Contract> {
let testnet = workspaces::testnet_archival().await?;
let testnet = near_workspaces::testnet_archival().await?;
let contract_id: AccountId = CONTRACT_ACCOUNT.parse()?;
```

Expand Down Expand Up @@ -295,7 +295,7 @@ Note: This is not to be confused with speeding up the current in-flight transact
```rust
#[tokio::test]
async fn test_contract() -> anyhow::Result<()> {
let worker = workspaces::sandbox().await?;
let worker = near_workspaces::sandbox().await?;
let contract = worker.dev_deploy(WASM_BYTES).await?;

let blocks_to_advance = 10000;
Expand All @@ -308,31 +308,31 @@ async fn test_contract() -> anyhow::Result<()> {
}
```

For a full example, take a look at [examples/src/fast_forward.rs](https://github.com/near/workspaces-rs/blob/main/examples/src/fast_forward.rs).
For a full example, take a look at [examples/src/fast_forward.rs](https://github.com/near/near-workspaces-rs/blob/main/examples/src/fast_forward.rs).

### Compiling Contracts During Test Time

Note, this is an unstable feature and will very likely change. To enable it, add the `unstable` feature flag to `workspaces` dependency in `Cargo.toml`:

```toml
[dependencies]
workspaces = { version = "...", features = ["unstable"] }
near-workspaces = { version = "...", features = ["unstable"] }
```

Then, in our tests right before we call into `deploy` or `dev_deploy`, we can compile our projects:

```rust
#[tokio::test]
async fn test_contract() -> anyhow::Result<()> {
let wasm = workspaces::compile_project("path/to/contract-rs-project").await?;
let wasm = near_workspaces::compile_project("path/to/contract-rs-project").await?;

let worker = workspaces::sandbox().await?;
let contract = worker.dev_deploy(&wasm).await?;
...
}
```

For a full example, take a look at [workspaces/tests/deploy_project.rs](https://github.com/near/workspaces-rs/blob/main/workspaces/tests/deploy_project.rs).
For a full example, take a look at [workspaces/tests/deploy_project.rs](https://github.com/near/near-workspaces-rs/blob/main/workspaces/tests/deploy_project.rs).

### Other Features

Expand All @@ -343,7 +343,7 @@ Other features can be directly found in the `examples/` folder, with some docume
These environment variables will be useful if there was ever a snag hit:

- `NEAR_RPC_TIMEOUT_SECS`: The default is 10 seconds, but this is the amount of time before timing out waiting for a RPC service when talking to the sandbox or any other network such as testnet.
- `NEAR_SANDBOX_BIN_PATH`: Set this to our own prebuilt `neard-sandbox` bin path if we want to use a non-default version of the sandbox or configure nearcore with our own custom features that we want to test in workspaces.
- `NEAR_SANDBOX_BIN_PATH`: Set this to our own prebuilt `neard-sandbox` bin path if we want to use a non-default version of the sandbox or configure nearcore with our own custom features that we want to test in near-workspaces.
- `NEAR_SANDBOX_MAX_PAYLOAD_SIZE`: Sets the max payload size for sending transaction commits to sandbox. The default is 1gb and is necessary for patching large states.
- `NEAR_SANDBOX_MAX_FILES`: Set the max amount of files that can be opened at a time in the sandbox. If none is specified, the default size of 4096 will be used. The actual near chain will use over 10,000 in practice, but for testing this should be much lower since we do not have a constantly running blockchain unless our tests take up that much time.
- `NEAR_RPC_API_KEY`: This is the API key necessary for communicating with RPC nodes. This is useful when interacting with services such as Pagoda Console or a service that can access RPC metrics. This is not a **hard** requirement, but it is recommended to running the Pagoda example in the examples folder.
Expand Down
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ serde_json = { version = "1.0" }
tokio = { version = "1", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3.5", features = ["env-filter"] }
workspaces = { path = "../workspaces", features = ["experimental", "unstable"] }
near-workspaces = { path = "../workspaces", features = ["experimental", "unstable"] }

[[example]]
name = "async_transaction"
Expand Down
2 changes: 1 addition & 1 deletion examples/src/async_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";

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

Expand Down
2 changes: 1 addition & 1 deletion examples/src/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";

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

Expand Down
2 changes: 1 addition & 1 deletion examples/src/changes_in_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";

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

Expand Down
7 changes: 3 additions & 4 deletions examples/src/croncat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

use near_gas::NearGas;
use near_units::parse_near;
use near_workspaces::network::Sandbox;
use near_workspaces::{Account, AccountId, Contract, Worker};
use serde::Deserialize;
use serde_json::json;

use workspaces::network::Sandbox;
use workspaces::{Account, AccountId, Contract, Worker};

const MANAGER_CONTRACT: &[u8] = include_bytes!("../res/manager.wasm");
const COUNTER_CONTRACT: &[u8] = include_bytes!("../res/counter.wasm");

Expand Down Expand Up @@ -47,7 +46,7 @@ pub struct Agent {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Spawn sandbox as normal and get us a local blockchain for us to interact and toy with:
let worker = workspaces::sandbox().await?;
let worker = near_workspaces::sandbox().await?;

// Initialize counter contract, which will be pointed to in the manager contract to schedule
// a task later to increment the counter, inside counter contract.
Expand Down
2 changes: 1 addition & 1 deletion examples/src/custom_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ async fn main() -> anyhow::Result<()> {
// `NEAR_RPC_API_KEY="xxx" cargo run --package examples --example custom_network`
if let Ok(val) = std::env::var("NEAR_RPC_API_KEY") {
// Reference to what can be called by this network: https://docs.pagoda.co/endpoints
let worker = workspaces::custom(PAGODA_TESTNET_RPC_URL)
let worker = near_workspaces::custom(PAGODA_TESTNET_RPC_URL)
.api_key(&val)
.await?;
let res = worker.view_block().await?;
Expand Down
2 changes: 1 addition & 1 deletion examples/src/fast_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const SIMPLE_WASM_FILEPATH: &str = "./examples/res/simple_contract.wasm";
/// to be produced, which could take hours with the default genesis configuration.
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let worker = workspaces::sandbox().await?;
let worker = near_workspaces::sandbox().await?;
let contract = worker
.dev_deploy(&std::fs::read(SIMPLE_WASM_FILEPATH)?)
.await?;
Expand Down
2 changes: 1 addition & 1 deletion examples/src/genesis_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let worker = workspaces::sandbox().await?;
let worker = near_workspaces::sandbox().await?;

// NOTE: this API is under the "experimental" flag and no guarantees are given.
let genesis_config = worker.genesis_config().await?;
Expand Down
2 changes: 1 addition & 1 deletion examples/src/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const NFT_WASM_FILEPATH: &str = "./examples/res/non_fungible_token.wasm";

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

Expand Down
4 changes: 2 additions & 2 deletions examples/src/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const NOOP_CONTRACT_WASM_FILEPATH: &str = "./examples/res/noop_contract.wasm";

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

Expand All @@ -12,7 +12,7 @@ async fn main() -> anyhow::Result<()> {
// Ok to error for call with no return value
assert_eq!(
*res.unwrap_err().kind(),
workspaces::error::ErrorKind::DataConversion,
near_workspaces::error::ErrorKind::DataConversion,
"the function call returned an empty value, which cannot be parsed as JSON"
);
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion examples/src/protocol_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";

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

Expand Down
Loading

0 comments on commit 20480d1

Please sign in to comment.