Skip to content

Commit f771d0c

Browse files
authored
Update workspaces.md (#1067)
1 parent 177342d commit f771d0c

File tree

1 file changed

+72
-59
lines changed

1 file changed

+72
-59
lines changed

docs/develop/contracts/workspaces.md

Lines changed: 72 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ The Workspaces interface is supported by the following libraries:
2828

2929
- TypeScript/JavaScript
3030

31-
:::note
32-
The current version of `workspaces-js` does not support the "Time Traveling" feature provided by the `fast_forward` method. This will be addressed in a future release.
33-
:::
31+
:::note
32+
33+
The current version of `workspaces-js` does not support the "Time Traveling" feature provided by the `fast_forward` method. This will be addressed in a future release.
34+
35+
:::
3436

3537
- Rust
3638

37-
:::note
38-
The current version of `workspaces-rs` does not support macOS on M1 chip devices due to internal upgrades with wasmer. M1 users should use `workspaces-rs` version `0.1.1` until this issue is resolved.
39-
:::
39+
:::note
40+
41+
The current version of `workspaces-rs` does not support macOS on M1 chip devices due to internal upgrades with wasmer. M1 users should use `workspaces-rs` version `0.1.1` until this issue is resolved.
42+
43+
:::
4044

4145

4246
## Quick Start
@@ -49,7 +53,9 @@ To get started with NEAR Workspaces you need to do two things:
4953
- See the JavaScript and Rust examples below.
5054

5155
:::tip
56+
5257
If you want to use NEAR Workspaces on Windows, please install the [Windows Subsystem for Linux](https://docs.microsoft.com/en-us/windows/wsl/install) (WSL).
58+
5359
:::
5460

5561
### Initializing a Worker
@@ -112,14 +118,15 @@ const NFT_WASM_FILEPATH: &str = "./examples/res/non_fungible_token.wasm";
112118
This includes launching the sandbox, loading your wasm file and deploying it to the sandbox environment.
113119

114120
```rust
115-
116121
#[tokio::test]
117122
async fn test_nft_contract() -> anyhow::Result<()> {
118123
let worker = workspaces::sandbox().await?;
119124
let wasm = std::fs::read(NFT_WASM_FILEPATH)?;
120125
let contract = worker.dev_deploy(&wasm).await?;
121126
```
127+
122128
Where
129+
123130
* `anyhow` - A crate that deals with error handling, making it more robust for developers.
124131
* `worker` - Our gateway towards interacting with our sandbox environment.
125132
* `contract`- The deployed contract on sandbox the developer interacts with.
@@ -259,7 +266,9 @@ const refFinance = await root.importContract({
259266
This would copy the Wasm bytes and contract state from [v2.ref-finance.near](https://explorer.near.org/accounts/v2.ref-finance.near) to your local blockchain as it existed at block `50_000_000`. This makes use of Sandbox's special [patch state](#patch-state-on-the-fly) feature to keep the contract name the same, even though the top level account might not exist locally (note that this means it only works in Sandbox testing mode). You can then interact with the contract in a deterministic way the same way you interact with all other accounts created with near-workspaces.
260267
261268
:::note
269+
262270
`withData` will only work out-of-the-box if the contract's data is 50kB or less. This is due to the default configuration of RPC servers; see [the "Heads Up" note here](https://docs.near.org/docs/api/rpc/contracts#view-contract-state).
271+
263272
:::
264273
265274
See a [TypeScript example of spooning](https://github.com/near/workspaces-js/blob/main/__tests__/05.spoon-contract-to-sandbox.ava.ts) contracts.
@@ -332,85 +341,87 @@ You can switch to testnet mode in three ways.
332341
333342
1. When creating Worker set network to `testnet` and pass your master account:
334343
335-
<Tabs>
336-
<TabItem value="js" label="JavaScript" default>
344+
<Tabs>
345+
<TabItem value="js" label="JavaScript" default>
337346
338-
```ts
339-
const worker = await Worker.init({
340-
network: 'testnet',
341-
testnetMasterAccountId: '<yourAccountName>',
342-
})
343-
```
347+
```ts
348+
const worker = await Worker.init({
349+
network: 'testnet',
350+
testnetMasterAccountId: '<yourAccountName>',
351+
})
352+
```
344353
345-
</TabItem>
346-
<TabItem value="rust" label="Rust">
354+
</TabItem>
355+
<TabItem value="rust" label="Rust">
347356
348-
```rust
349-
#[tokio::main] // or whatever runtime we want
350-
async fn main() -> anyhow::Result<()> {
351-
// Create a sandboxed environment.
352-
// NOTE: Each call will create a new sandboxed environment
353-
let worker = workspaces::sandbox().await?;
354-
// or for testnet:
355-
let worker = workspaces::testnet().await?;
356-
}
357-
```
357+
```rust
358+
#[tokio::main] // or whatever runtime we want
359+
async fn main() -> anyhow::Result<()> {
360+
// Create a sandboxed environment.
361+
// NOTE: Each call will create a new sandboxed environment
362+
let worker = workspaces::sandbox().await?;
363+
// or for testnet:
364+
let worker = workspaces::testnet().await?;
365+
}
366+
```
358367
359-
</TabItem>
360-
</Tabs>
368+
</TabItem>
369+
</Tabs>
361370
362371
363372
2. Set the `NEAR_WORKSPACES_NETWORK` and `TESTNET_MASTER_ACCOUNT_ID` environment variables when running your tests:
364373
365-
<Tabs>
366-
<TabItem value="js" label="JavaScript" default>
374+
<Tabs>
375+
<TabItem value="js" label="JavaScript" default>
367376
368-
```bash
369-
NEAR_WORKSPACES_NETWORK=testnet TESTNET_MASTER_ACCOUNT_ID=<your master account Id> node test.js
370-
```
377+
```bash
378+
NEAR_WORKSPACES_NETWORK=testnet TESTNET_MASTER_ACCOUNT_ID=<your master account Id> node test.js
379+
```
371380
372-
If you set this environment variables and pass `{network: 'testnet', testnetMasterAccountId: <masterAccountId>}` to `Worker.init`, the config object takes precedence.
381+
If you set this environment variables and pass `{network: 'testnet', testnetMasterAccountId: <masterAccountId>}` to `Worker.init`, the config object takes precedence.
373382
374-
</TabItem>
375-
</Tabs>
383+
</TabItem>
384+
</Tabs>
376385
377386
3. If using `near-workspaces` with AVA, you can use a custom config file. Other test runners allow similar config files; adjust the following instructions for your situation.
378387
379-
<Tabs>
380-
<TabItem value="js" label="JavaScript" default>
388+
<Tabs>
389+
<TabItem value="js" label="JavaScript" default>
381390
382-
Create a file in the same directory as your `package.json` called `ava.testnet.config.cjs` with the following contents:
391+
Create a file in the same directory as your `package.json` called `ava.testnet.config.cjs` with the following contents:
383392
384-
```js
385-
module.exports = {
386-
...require('near-workspaces/ava.testnet.config.cjs'),
387-
...require('./ava.config.cjs'),
388-
};
389-
module.exports.environmentVariables = {
390-
TESTNET_MASTER_ACCOUNT_ID: '<masterAccountId>',
391-
};
392-
```
393+
```js
394+
module.exports = {
395+
...require('near-workspaces/ava.testnet.config.cjs'),
396+
...require('./ava.config.cjs'),
397+
};
398+
module.exports.environmentVariables = {
399+
TESTNET_MASTER_ACCOUNT_ID: '<masterAccountId>',
400+
};
401+
```
393402
394-
The [near-workspaces/ava.testnet.config.cjs](https://github.com/near/workspaces-js/blob/main/ava.testnet.config.cjs) import sets the `NEAR_WORKSPACES_NETWORK` environment variable for you. A benefit of this approach is that you can then easily ignore files that should only run in Sandbox mode.
403+
The [near-workspaces/ava.testnet.config.cjs](https://github.com/near/workspaces-js/blob/main/ava.testnet.config.cjs) import sets the `NEAR_WORKSPACES_NETWORK` environment variable for you. A benefit of this approach is that you can then easily ignore files that should only run in Sandbox mode.
395404
396-
Now you'll also want to add a `test:testnet` script to your `package.json`'s `scripts` section:
405+
Now you'll also want to add a `test:testnet` script to your `package.json`'s `scripts` section:
397406
398-
```diff
399-
"scripts": {
400-
"test": "ava",
401-
+ "test:testnet": "ava --config ./ava.testnet.config.cjs"
402-
}
403-
```
407+
```diff
408+
"scripts": {
409+
"test": "ava",
410+
+ "test:testnet": "ava --config ./ava.testnet.config.cjs"
411+
}
412+
```
404413
405-
</TabItem>
406-
</Tabs>
414+
</TabItem>
415+
</Tabs>
407416
408417
## Patch State on the Fly
409418
410419
In Sandbox-mode, you can add or modify any contract state, contract code, account or access key with `patchState`.
411420
412421
:::tip
422+
413423
You can alter contract code, accounts, and access keys using normal transactions via the `DeployContract`, `CreateAccount`, and `AddKey` [actions](https://nomicon.io/RuntimeSpec/Actions#addkeyaction). But this limits you to altering your own account or sub-account. `patchState` allows you to perform these operations on any account.
424+
414425
:::
415426
416427
Keep in mind that you cannot perform arbitrary mutation on contract state with transactions since transactions can only include contract calls that mutate state in a contract-programmed way. For example, with an NFT contract, you can perform some operation with NFTs you have ownership of, but you cannot manipulate NFTs that are owned by other accounts since the smart contract is coded with checks to reject that. This is the expected behavior of the NFT contract. However, you may want to change another person's NFT for a test setup. This is called "arbitrary mutation on contract state" and can be done with `patchState`:
@@ -513,7 +524,9 @@ This approach is more complex to do and also cannot be performed without restart
513524
<TabItem value="js" label="JavaScript">
514525
515526
:::note
527+
516528
Time Traveling in `workspaces-js` is currently unavailable.
529+
517530
:::
518531
519532
</TabItem>

0 commit comments

Comments
 (0)