You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/develop/contracts/workspaces.md
+72-59Lines changed: 72 additions & 59 deletions
Original file line number
Diff line number
Diff line change
@@ -28,15 +28,19 @@ The Workspaces interface is supported by the following libraries:
28
28
29
29
- TypeScript/JavaScript
30
30
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
+
:::
34
36
35
37
- Rust
36
38
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
+
:::
40
44
41
45
42
46
## Quick Start
@@ -49,7 +53,9 @@ To get started with NEAR Workspaces you need to do two things:
49
53
- See the JavaScript and Rust examples below.
50
54
51
55
:::tip
56
+
52
57
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).
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.
260
267
261
268
:::note
269
+
262
270
`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
+
263
272
:::
264
273
265
274
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.
332
341
333
342
1. When creating Worker set network to `testnet` and pass your master account:
334
343
335
-
<Tabs>
336
-
<TabItem value="js" label="JavaScript" default>
344
+
<Tabs>
345
+
<TabItem value="js" label="JavaScript" default>
337
346
338
-
```ts
339
-
constworker=awaitWorker.init({
340
-
network: 'testnet',
341
-
testnetMasterAccountId: '<yourAccountName>',
342
-
})
343
-
```
347
+
```ts
348
+
constworker=awaitWorker.init({
349
+
network: 'testnet',
350
+
testnetMasterAccountId: '<yourAccountName>',
351
+
})
352
+
```
344
353
345
-
</TabItem>
346
-
<TabItem value="rust" label="Rust">
354
+
</TabItem>
355
+
<TabItem value="rust" label="Rust">
347
356
348
-
```rust
349
-
#[tokio::main] // or whatever runtime we want
350
-
asyncfnmain() ->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
-
letworker=workspaces::testnet().await?;
356
-
}
357
-
```
357
+
```rust
358
+
#[tokio::main] // or whatever runtime we want
359
+
asyncfnmain() ->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
+
letworker=workspaces::testnet().await?;
365
+
}
366
+
```
358
367
359
-
</TabItem>
360
-
</Tabs>
368
+
</TabItem>
369
+
</Tabs>
361
370
362
371
363
372
2. Set the `NEAR_WORKSPACES_NETWORK` and `TESTNET_MASTER_ACCOUNT_ID` environment variables when running your tests:
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.
373
382
374
-
</TabItem>
375
-
</Tabs>
383
+
</TabItem>
384
+
</Tabs>
376
385
377
386
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.
378
387
379
-
<Tabs>
380
-
<TabItem value="js" label="JavaScript" default>
388
+
<Tabs>
389
+
<TabItem value="js" label="JavaScript" default>
381
390
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:
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.
395
404
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:
In Sandbox-mode, you can add or modify any contract state, contract code, account or access key with `patchState`.
411
420
412
421
:::tip
422
+
413
423
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
+
414
425
:::
415
426
416
427
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
513
524
<TabItem value="js" label="JavaScript">
514
525
515
526
:::note
527
+
516
528
Time Traveling in `workspaces-js` is currently unavailable.
0 commit comments